Browse Source

fix: add orders caching to ShopRenter scheduled sync #31

Previously the scheduled sync only counted orders but didn't cache them to the database.
Now it properly maps and upserts orders to shoprenter_orders_cache table, matching the manual sync behavior.
Claude 5 months ago
parent
commit
dfa03c1abe
1 changed files with 31 additions and 2 deletions
  1. 31 2
      supabase/functions/shoprenter-scheduled-sync/index.ts

+ 31 - 2
supabase/functions/shoprenter-scheduled-sync/index.ts

@@ -164,7 +164,36 @@ serve(async (req) => {
             const ordersData = await fetchOrders(storeId, page, limit)
 
             if (ordersData.items && ordersData.items.length > 0) {
-              syncStats.orders.synced += ordersData.items.length
+              const ordersToCache = ordersData.items.map((order: any) => ({
+                store_id: storeId,
+                shoprenter_order_id: order.id,
+                order_number: order.order_number || order.number || order.id,
+                status: order.status,
+                total: parseFloat(order.total) || 0,
+                currency: order.currency || 'HUF',
+                customer_name: order.customer_name || `${order.customer?.firstname || ''} ${order.customer?.lastname || ''}`.trim() || null,
+                customer_email: order.customer_email || order.customer?.email || null,
+                customer_phone: order.customer_phone || order.customer?.phone || order.billing_address?.phone || order.shipping_address?.phone || null,
+                line_items: order.items || order.line_items || [],
+                billing_address: order.billing_address || null,
+                shipping_address: order.shipping_address || null,
+                order_created_at: order.created_at || order.date_created || new Date().toISOString(),
+                raw_data: order,
+                last_synced_at: new Date().toISOString()
+              }))
+
+              const { error: upsertError } = await supabaseAdmin
+                .from('shoprenter_orders_cache')
+                .upsert(ordersToCache, {
+                  onConflict: 'store_id,shoprenter_order_id'
+                })
+
+              if (upsertError) {
+                console.error(`[ShopRenter Scheduled Sync] Error caching orders for store ${storeId}:`, upsertError)
+                syncStats.orders.errors += ordersToCache.length
+              } else {
+                syncStats.orders.synced += ordersToCache.length
+              }
 
               // Check if there are more pages
               if (ordersData.pagination && ordersData.pagination.total) {
@@ -180,7 +209,7 @@ serve(async (req) => {
             }
           }
 
-          console.log(`[ShopRenter Scheduled Sync] Store ${storeId}: Orders synced: ${syncStats.orders.synced}`)
+          console.log(`[ShopRenter Scheduled Sync] Store ${storeId}: Orders synced: ${syncStats.orders.synced}, errors: ${syncStats.orders.errors}`)
         } catch (error) {
           console.error(`[ShopRenter Scheduled Sync] Order sync error for store ${storeId}:`, error)
           syncStats.orders.errors++