Преглед изворни кода

fix: correct shoprenter_products_cache schema and validate product IDs #79

Claude пре 5 месеци
родитељ
комит
b785d9c54c
1 измењених фајлова са 13 додато и 12 уклоњено
  1. 13 12
      supabase/functions/shoprenter-sync/index.ts

+ 13 - 12
supabase/functions/shoprenter-sync/index.ts

@@ -67,7 +67,15 @@ async function syncProductsToQdrant(
   const startTime = new Date()
   const collectionName = getCollectionName(storeName, 'products')
 
-  console.log(`[Qdrant] Syncing ${products.length} products to ${collectionName}`)
+  // Filter out products without valid IDs
+  const validProducts = products.filter(p => p && p.id)
+
+  if (validProducts.length === 0) {
+    console.log('[Qdrant] No valid products to sync (all products missing IDs)')
+    return { synced: 0, errors: 0 }
+  }
+
+  console.log(`[Qdrant] Syncing ${validProducts.length} products to ${collectionName}`)
 
   let synced = 0
   let errors = 0
@@ -93,7 +101,7 @@ async function syncProductsToQdrant(
       existingPoints.points.map((p: any) => p.payload?.product_id).filter(Boolean)
     )
 
-    const currentProductIds = new Set(products.map(p => p.id.toString()))
+    const currentProductIds = new Set(validProducts.map(p => p.id.toString()))
 
     // Find deleted products
     const deletedProductIds = Array.from(existingProductIds).filter(
@@ -112,7 +120,7 @@ async function syncProductsToQdrant(
     }
 
     // Generate text representations for all products
-    const productTexts = products.map((product) =>
+    const productTexts = validProducts.map((product) =>
       createProductText({
         name: product.name,
         description: product.description,
@@ -132,7 +140,7 @@ async function syncProductsToQdrant(
     console.log(`[Qdrant] Embeddings generated successfully`)
 
     // Convert products to Qdrant points with embeddings and comprehensive details
-    const points: QdrantPoint[] = products.map((product, index) => ({
+    const points: QdrantPoint[] = validProducts.map((product, index) => ({
       id: generatePointId('shoprenter', storeId, product.id),
       vector: embeddings[index],
       payload: {
@@ -378,14 +386,7 @@ serve(wrapHandler('shoprenter-sync', async (req) => {
             const productsToCache = productsData.items.map((product: any) => ({
               store_id: storeId,
               shoprenter_product_id: product.id,
-              name: product.name,
-              sku: product.sku,
-              price: parseFloat(product.price) || 0,
-              currency: product.currency || 'HUF',
-              description: product.description,
-              stock: product.stock,
-              active: product.active !== false,
-              raw_data: product,
+              product_data: product,
               last_synced_at: new Date().toISOString()
             }))