|
|
@@ -1,16 +1,9 @@
|
|
|
import { serve } from 'https://deno.land/std@0.168.0/http/server.ts'
|
|
|
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
|
|
|
import { wrapHandler, logError } from '../_shared/error-handler.ts'
|
|
|
-import { fetchProducts, fetchOrders, fetchCustomers, fetchCategory } from '../_shared/shoprenter-client.ts'
|
|
|
+import { fetchProducts, fetchOrders, fetchCustomers } from '../_shared/shoprenter-client.ts'
|
|
|
import { detectCountryCode } from '../_shared/phone-formatter.ts'
|
|
|
|
|
|
-// Helper function to extract category ID from ShopRenter href
|
|
|
-function extractCategoryId(categoryHref: string): string | null {
|
|
|
- if (!categoryHref) return null
|
|
|
- const parts = categoryHref.split('/')
|
|
|
- return parts[parts.length - 1] || null
|
|
|
-}
|
|
|
-
|
|
|
const corsHeaders = {
|
|
|
'Access-Control-Allow-Origin': '*',
|
|
|
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
|
|
@@ -188,68 +181,16 @@ serve(wrapHandler('shoprenter-scheduled-sync', async (req) => {
|
|
|
const productsData = await fetchProducts(storeId, page, limit)
|
|
|
|
|
|
if (productsData.items && productsData.items.length > 0) {
|
|
|
- // First, collect unique category IDs for this page
|
|
|
- const pageCategoryIds = new Set<string>()
|
|
|
- for (const product of productsData.items) {
|
|
|
- const categoryRelations = product.productCategoryRelations || []
|
|
|
- for (const rel of categoryRelations) {
|
|
|
- const categoryId = extractCategoryId(rel.category?.href)
|
|
|
- if (categoryId) {
|
|
|
- pageCategoryIds.add(categoryId)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Fetch category names for this page (parallel fetching)
|
|
|
- const pageCategoryCache = new Map<string, string>()
|
|
|
- if (pageCategoryIds.size > 0) {
|
|
|
- console.log(`[ShopRenter Scheduled Sync] Fetching ${pageCategoryIds.size} category names for store ${storeId}...`)
|
|
|
- const categoryPromises = Array.from(pageCategoryIds).map(async (categoryId) => {
|
|
|
- try {
|
|
|
- const categoryData = await fetchCategory(storeId, categoryId)
|
|
|
- const categoryDesc = categoryData.categoryDescriptions?.[0]
|
|
|
- if (categoryDesc && categoryDesc.name) {
|
|
|
- pageCategoryCache.set(categoryId, categoryDesc.name)
|
|
|
- }
|
|
|
- } catch (error) {
|
|
|
- console.error(`[ShopRenter Scheduled Sync] Failed to fetch category ${categoryId}:`, error)
|
|
|
- }
|
|
|
- })
|
|
|
- await Promise.all(categoryPromises)
|
|
|
- console.log(`[ShopRenter Scheduled Sync] Fetched ${pageCategoryCache.size} category names successfully`)
|
|
|
- }
|
|
|
-
|
|
|
- const productsToCache = productsData.items.map((product: any) => {
|
|
|
- // Extract first language description from productDescriptions array
|
|
|
- const productDesc = product.productDescriptions?.[0] || {}
|
|
|
-
|
|
|
- // Extract categories with both ID and name
|
|
|
- const categories: Array<{id: string, name: string}> = []
|
|
|
- const categoryRelations = product.productCategoryRelations || []
|
|
|
- for (const rel of categoryRelations) {
|
|
|
- const categoryId = extractCategoryId(rel.category?.href)
|
|
|
- if (categoryId) {
|
|
|
- const categoryName = pageCategoryCache.get(categoryId)
|
|
|
- if (categoryName) {
|
|
|
- categories.push({ id: categoryId, name: categoryName })
|
|
|
- } else {
|
|
|
- // Fallback: store ID only if name fetch failed
|
|
|
- categories.push({ id: categoryId, name: categoryId })
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return {
|
|
|
- store_id: storeId,
|
|
|
- product_id: product.id,
|
|
|
- inner_id: product.innerId,
|
|
|
- name: productDesc.name || product.name || null,
|
|
|
- sku: product.sku || null,
|
|
|
- categories: categories,
|
|
|
- excluded: false, // Will be set if in exclusion list
|
|
|
- last_synced_at: new Date().toISOString()
|
|
|
- }
|
|
|
- })
|
|
|
+ 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 { error: upsertError } = await supabaseAdmin
|
|
|
.from('shoprenter_products_cache')
|