From e6429dc37cc32f08930f1771173757a32ca87467 Mon Sep 17 00:00:00 2001 From: Amirul Islam Redwan71 Date: Tue, 1 Sep 2026 09:14:19 +0600 Subject: [PATCH] Enhance resolveProductUrl with smart name search and catalog fallback --- app.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/app.js b/app.js index b1e8eb4..9df4fdf 100644 --- a/app.js +++ b/app.js @@ -287,39 +287,77 @@ function initPosterLab() { } } -// Universal Link Resolver +// Universal Link & Product Name Resolver async function resolveProductUrl(input) { + if (!input) return null; if (input.match(/\.(jpeg|jpg|gif|png|webp)($|\?)/i)) { return { title: 'Authentic Korean Product', imageUrl: input }; } - // Glamour's Touch Supabase UUID regex + const key = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImZtY2x0cmpudXZ1b29hcmt2dWZuIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzUxMzY3MDQsImV4cCI6MjA5MDcxMjcwNH0.PkSgBAZx41X4sZurfyOdxCVa01hkKTkyBhVkGzx_4y4'; + + // 1. Glamour's Touch Supabase UUID regex const uuidMatch = input.match(/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/); if (uuidMatch) { const productId = uuidMatch[0]; try { const res = await fetch(`https://fmcltrjnuvuooarkvufn.supabase.co/rest/v1/products?id=eq.${productId}&select=*`, { - headers: { - 'apikey': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImZtY2x0cmpudXZ1b29hcmt2dWZuIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzUxMzY3MDQsImV4cCI6MjA5MDcxMjcwNH0.PkSgBAZx41X4sZurfyOdxCVa01hkKTkyBhVkGzx_4y4', - 'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImZtY2x0cmpudXZ1b29hcmt2dWZuIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzUxMzY3MDQsImV4cCI6MjA5MDcxMjcwNH0.PkSgBAZx41X4sZurfyOdxCVa01hkKTkyBhVkGzx_4y4' - } + headers: { 'apikey': key, 'Authorization': `Bearer ${key}` } }); const data = await res.json(); if (data && data.length > 0) { const p = data[0]; - let imgUrl = p.image || p.image_url || ''; - if (p.images && p.images.length > 0) imgUrl = p.images[0]; return { title: p.name || 'Authentic Korean Skincare', - imageUrl: imgUrl + price: p.price ? '৳ ' + p.price : '', + imageUrl: p.image || p.image_url || (p.images && p.images[0]) || '' }; } } catch (e) { - console.warn('GT Supabase fetch failed:', e); + console.warn('GT Supabase fetch by ID failed:', e); } } - return { title: input, imageUrl: input }; + // 2. Search Supabase by product Name + try { + const cleanQuery = encodeURIComponent(input.replace(/https?:\/\/[^\s]+/g, '').trim().slice(0, 30)); + if (cleanQuery) { + const res = await fetch(`https://fmcltrjnuvuooarkvufn.supabase.co/rest/v1/products?name=ilike.*${cleanQuery}*&select=*&limit=1`, { + headers: { 'apikey': key, 'Authorization': `Bearer ${key}` } + }); + const data = await res.json(); + if (data && data.length > 0) { + const p = data[0]; + return { + title: p.name || input, + price: p.price ? '৳ ' + p.price : '', + imageUrl: p.image || p.image_url || (p.images && p.images[0]) || '' + }; + } + } + } catch (e) { + console.warn('GT Supabase search by name failed:', e); + } + + // 3. Fallback to 1st featured product in catalog + try { + const res = await fetch(`https://fmcltrjnuvuooarkvufn.supabase.co/rest/v1/products?in_stock=eq.true&image=not.is.null&limit=1`, { + headers: { 'apikey': key, 'Authorization': `Bearer ${key}` } + }); + const data = await res.json(); + if (data && data.length > 0) { + const p = data[0]; + return { + title: p.name || input, + price: p.price ? '৳ ' + p.price : '', + imageUrl: p.image || '' + }; + } + } catch (e) { + console.warn('Fallback failed:', e); + } + + return { title: input, imageUrl: 'https://fmcltrjnuvuooarkvufn.supabase.co/storage/v1/object/public/products/product-images/koba-74d48daa-7695-41f9-8f10-9e5fb4f0e2d9.jpg' }; } function loadImage(src) {