Enhance resolveProductUrl with smart name search and catalog fallback

This commit is contained in:
Amirul Islam Redwan71
2026-09-01 09:14:19 +06:00
parent 4f028103d7
commit e6429dc37c
+49 -11
View File
@@ -287,39 +287,77 @@ function initPosterLab() {
} }
} }
// Universal Link Resolver // Universal Link & Product Name Resolver
async function resolveProductUrl(input) { async function resolveProductUrl(input) {
if (!input) return null;
if (input.match(/\.(jpeg|jpg|gif|png|webp)($|\?)/i)) { if (input.match(/\.(jpeg|jpg|gif|png|webp)($|\?)/i)) {
return { title: 'Authentic Korean Product', imageUrl: input }; 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}/); 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) { if (uuidMatch) {
const productId = uuidMatch[0]; const productId = uuidMatch[0];
try { try {
const res = await fetch(`https://fmcltrjnuvuooarkvufn.supabase.co/rest/v1/products?id=eq.${productId}&select=*`, { const res = await fetch(`https://fmcltrjnuvuooarkvufn.supabase.co/rest/v1/products?id=eq.${productId}&select=*`, {
headers: { headers: { 'apikey': key, 'Authorization': `Bearer ${key}` }
'apikey': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImZtY2x0cmpudXZ1b29hcmt2dWZuIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzUxMzY3MDQsImV4cCI6MjA5MDcxMjcwNH0.PkSgBAZx41X4sZurfyOdxCVa01hkKTkyBhVkGzx_4y4',
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImZtY2x0cmpudXZ1b29hcmt2dWZuIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzUxMzY3MDQsImV4cCI6MjA5MDcxMjcwNH0.PkSgBAZx41X4sZurfyOdxCVa01hkKTkyBhVkGzx_4y4'
}
}); });
const data = await res.json(); const data = await res.json();
if (data && data.length > 0) { if (data && data.length > 0) {
const p = data[0]; const p = data[0];
let imgUrl = p.image || p.image_url || '';
if (p.images && p.images.length > 0) imgUrl = p.images[0];
return { return {
title: p.name || 'Authentic Korean Skincare', 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) { } 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) { function loadImage(src) {