Browse Source

feat: improve WooCommerce sync error logging for better diagnostics #24

- Add detailed error messages to sync functions (products, orders, customers)
- Return error messages in sync response and store them in alt_data
- This will help diagnose why WooCommerce cache tables are empty
Claude 5 months ago
parent
commit
5bcb704bd3
1 changed files with 22 additions and 8 deletions
  1. 22 8
      supabase/functions/woocommerce-sync/index.ts

+ 22 - 8
supabase/functions/woocommerce-sync/index.ts

@@ -77,7 +77,7 @@ async function syncProducts(
   storeId: string,
   storeId: string,
   supabaseAdmin: any,
   supabaseAdmin: any,
   rateLimiter: RateLimiter
   rateLimiter: RateLimiter
-): Promise<{ synced: number; errors: number }> {
+): Promise<{ synced: number; errors: number; errorMessage?: string }> {
   console.log('[WooCommerce] Syncing products...')
   console.log('[WooCommerce] Syncing products...')
   let synced = 0
   let synced = 0
   let errors = 0
   let errors = 0
@@ -137,8 +137,10 @@ async function syncProducts(
 
 
     console.log(`[WooCommerce] Products sync complete: ${synced} synced, ${errors} errors`)
     console.log(`[WooCommerce] Products sync complete: ${synced} synced, ${errors} errors`)
   } catch (error) {
   } catch (error) {
-    console.error('[WooCommerce] Product sync error:', error)
+    const errorMessage = error instanceof Error ? error.message : 'Unknown error'
+    console.error('[WooCommerce] Product sync error:', errorMessage, error)
     errors++
     errors++
+    return { synced, errors, errorMessage }
   }
   }
 
 
   return { synced, errors }
   return { synced, errors }
@@ -149,7 +151,7 @@ async function syncOrders(
   storeId: string,
   storeId: string,
   supabaseAdmin: any,
   supabaseAdmin: any,
   rateLimiter: RateLimiter
   rateLimiter: RateLimiter
-): Promise<{ synced: number; errors: number }> {
+): Promise<{ synced: number; errors: number; errorMessage?: string }> {
   console.log('[WooCommerce] Syncing orders...')
   console.log('[WooCommerce] Syncing orders...')
   let synced = 0
   let synced = 0
   let errors = 0
   let errors = 0
@@ -208,8 +210,10 @@ async function syncOrders(
 
 
     console.log(`[WooCommerce] Orders sync complete: ${synced} synced, ${errors} errors`)
     console.log(`[WooCommerce] Orders sync complete: ${synced} synced, ${errors} errors`)
   } catch (error) {
   } catch (error) {
-    console.error('[WooCommerce] Order sync error:', error)
+    const errorMessage = error instanceof Error ? error.message : 'Unknown error'
+    console.error('[WooCommerce] Order sync error:', errorMessage, error)
     errors++
     errors++
+    return { synced, errors, errorMessage }
   }
   }
 
 
   return { synced, errors }
   return { synced, errors }
@@ -220,7 +224,7 @@ async function syncCustomers(
   storeId: string,
   storeId: string,
   supabaseAdmin: any,
   supabaseAdmin: any,
   rateLimiter: RateLimiter
   rateLimiter: RateLimiter
-): Promise<{ synced: number; errors: number }> {
+): Promise<{ synced: number; errors: number; errorMessage?: string }> {
   console.log('[WooCommerce] Syncing customers...')
   console.log('[WooCommerce] Syncing customers...')
   let synced = 0
   let synced = 0
   let errors = 0
   let errors = 0
@@ -277,8 +281,10 @@ async function syncCustomers(
 
 
     console.log(`[WooCommerce] Customers sync complete: ${synced} synced, ${errors} errors`)
     console.log(`[WooCommerce] Customers sync complete: ${synced} synced, ${errors} errors`)
   } catch (error) {
   } catch (error) {
-    console.error('[WooCommerce] Customer sync error:', error)
+    const errorMessage = error instanceof Error ? error.message : 'Unknown error'
+    console.error('[WooCommerce] Customer sync error:', errorMessage, error)
     errors++
     errors++
+    return { synced, errors, errorMessage }
   }
   }
 
 
   return { synced, errors }
   return { synced, errors }
@@ -446,7 +452,7 @@ serve(async (req) => {
 
 
       console.log(`[WooCommerce] Starting ${sync_type} sync for store ${store_id}`)
       console.log(`[WooCommerce] Starting ${sync_type} sync for store ${store_id}`)
 
 
-      const syncStats = {
+      const syncStats: any = {
         products: { synced: 0, errors: 0 },
         products: { synced: 0, errors: 0 },
         orders: { synced: 0, errors: 0 },
         orders: { synced: 0, errors: 0 },
         customers: { synced: 0, errors: 0 }
         customers: { synced: 0, errors: 0 }
@@ -467,6 +473,12 @@ serve(async (req) => {
         syncStats.customers = await syncCustomers(store_id, supabaseAdmin, rateLimiter)
         syncStats.customers = await syncCustomers(store_id, supabaseAdmin, rateLimiter)
       }
       }
 
 
+      // Collect error messages for better diagnostics
+      const errorMessages = []
+      if (syncStats.products.errorMessage) errorMessages.push(`Products: ${syncStats.products.errorMessage}`)
+      if (syncStats.orders.errorMessage) errorMessages.push(`Orders: ${syncStats.orders.errorMessage}`)
+      if (syncStats.customers.errorMessage) errorMessages.push(`Customers: ${syncStats.customers.errorMessage}`)
+
       // Update store alt_data with sync info
       // Update store alt_data with sync info
       const { data: currentStore } = await supabaseAdmin
       const { data: currentStore } = await supabaseAdmin
         .from('stores')
         .from('stores')
@@ -486,7 +498,8 @@ serve(async (req) => {
             productsCount: syncStats.products.synced,
             productsCount: syncStats.products.synced,
             ordersCount: syncStats.orders.synced,
             ordersCount: syncStats.orders.synced,
             customersCount: syncStats.customers.synced,
             customersCount: syncStats.customers.synced,
-            lastSyncStats: syncStats
+            lastSyncStats: syncStats,
+            lastSyncErrors: errorMessages.length > 0 ? errorMessages : undefined
           }
           }
         })
         })
         .eq('id', store_id)
         .eq('id', store_id)
@@ -496,6 +509,7 @@ serve(async (req) => {
           success: true,
           success: true,
           message: `${sync_type} sync completed`,
           message: `${sync_type} sync completed`,
           stats: syncStats,
           stats: syncStats,
+          errorMessages: errorMessages.length > 0 ? errorMessages : undefined,
           timestamp: new Date().toISOString()
           timestamp: new Date().toISOString()
         }),
         }),
         { status: 200, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
         { status: 200, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }