Просмотр исходного кода

fix: correct name and categories extraction in shoprenter-sync database cache #112

Claude 4 месяцев назад
Родитель
Сommit
d05251e5fd
1 измененных файлов с 25 добавлено и 10 удалено
  1. 25 10
      supabase/functions/shoprenter-sync/index.ts

+ 25 - 10
supabase/functions/shoprenter-sync/index.ts

@@ -941,16 +941,31 @@ serve(wrapHandler('shoprenter-sync', async (req) => {
             // Collect all products for Qdrant sync
             allProducts.push(...productsData.items)
 
-            const productsToCache = productsData.items.map((product: any) => ({
-              store_id: storeId,
-              product_id: product.id,
-              inner_id: product.innerId,
-              name: product.name,
-              sku: product.sku,
-              categories: product.productCategory || [],
-              excluded: false, // Will be set if in exclusion list
-              last_synced_at: new Date().toISOString()
-            }))
+            const productsToCache = productsData.items.map((product: any) => {
+              // Extract first language description from productDescriptions array
+              const productDesc = product.productDescriptions?.[0] || {}
+
+              // Extract category IDs from productCategoryRelations
+              const categoryIds: string[] = []
+              const categoryRelations = product.productCategoryRelations || []
+              for (const rel of categoryRelations) {
+                const categoryId = extractCategoryId(rel.category?.href)
+                if (categoryId) {
+                  categoryIds.push(categoryId)
+                }
+              }
+
+              return {
+                store_id: storeId,
+                product_id: product.id,
+                inner_id: product.innerId,
+                name: productDesc.name || product.name || null,
+                sku: product.sku || null,
+                categories: categoryIds,
+                excluded: false, // Will be set if in exclusion list
+                last_synced_at: new Date().toISOString()
+              }
+            })
 
             const { error: upsertError } = await supabaseAdmin
               .from('shoprenter_products_cache')