Browse Source

fix: sync timestamp duplication - update both stores.sync_completed_at and store_sync_config.last_sync_at across all sync functions #75

Claude 5 months ago
parent
commit
318e51a5c9

+ 15 - 6
supabase/functions/shoprenter-scheduled-sync/index.ts

@@ -230,10 +230,13 @@ serve(wrapHandler('shoprenter-scheduled-sync', async (req) => {
           }
           }
         }
         }
 
 
-        // Update store last_sync timestamp
+        // Update BOTH stores table and store_sync_config to maintain consistency
+        const syncCompletedAt = new Date().toISOString()
+
+        // Update store last_sync timestamp and alt_data
         const updatedAltData = {
         const updatedAltData = {
           ...(store.alt_data || {}),
           ...(store.alt_data || {}),
-          last_sync_at: new Date().toISOString(),
+          last_sync_at: syncCompletedAt,
           last_sync_stats: {
           last_sync_stats: {
             products: syncStats.products,
             products: syncStats.products,
             customer_access: syncStats.customer_access,
             customer_access: syncStats.customer_access,
@@ -242,17 +245,23 @@ serve(wrapHandler('shoprenter-scheduled-sync', async (req) => {
           last_sync_type: 'scheduled'
           last_sync_type: 'scheduled'
         }
         }
 
 
+        // Update stores table (for Web UI display)
         await supabaseAdmin
         await supabaseAdmin
           .from('stores')
           .from('stores')
-          .update({ alt_data: updatedAltData })
+          .update({
+            alt_data: updatedAltData,
+            sync_status: syncStats.status === 'success' ? 'completed' : 'error',
+            sync_completed_at: syncCompletedAt,
+            sync_error: syncStats.error_message
+          })
           .eq('id', storeId)
           .eq('id', storeId)
 
 
-        // Update store_sync_config timestamps
+        // Update store_sync_config timestamps (for scheduling logic)
         if (config) {
         if (config) {
           await supabaseAdmin
           await supabaseAdmin
             .from('store_sync_config')
             .from('store_sync_config')
             .update({
             .update({
-              last_sync_at: new Date().toISOString(),
+              last_sync_at: syncCompletedAt,
               // next_sync_at will be auto-calculated by trigger
               // next_sync_at will be auto-calculated by trigger
             })
             })
             .eq('store_id', storeId)
             .eq('store_id', storeId)
@@ -264,7 +273,7 @@ serve(wrapHandler('shoprenter-scheduled-sync', async (req) => {
               store_id: storeId,
               store_id: storeId,
               enabled: true,
               enabled: true,
               sync_frequency: 'hourly',
               sync_frequency: 'hourly',
-              last_sync_at: new Date().toISOString()
+              last_sync_at: syncCompletedAt
             })
             })
         }
         }
 
 

+ 31 - 4
supabase/functions/shoprenter-sync/index.ts

@@ -269,17 +269,44 @@ serve(wrapHandler('shoprenter-sync', async (req) => {
       syncStats.customers.errors++
       syncStats.customers.errors++
     }
     }
 
 
-    // Update store last_sync timestamp
+    // Update BOTH stores table and store_sync_config to maintain consistency
+    const syncCompletedAt = new Date().toISOString()
+
+    // Update stores table (for Web UI display)
     await supabaseAdmin
     await supabaseAdmin
       .from('stores')
       .from('stores')
       .update({
       .update({
         alt_data: {
         alt_data: {
-          last_sync_at: new Date().toISOString(),
-          last_sync_stats: syncStats
-        }
+          last_sync_at: syncCompletedAt,
+          last_sync_stats: syncStats,
+          last_sync_type: 'manual'
+        },
+        sync_status: 'completed',
+        sync_completed_at: syncCompletedAt,
+        sync_error: null
       })
       })
       .eq('id', storeId)
       .eq('id', storeId)
 
 
+    // Update store_sync_config timestamps (for scheduling logic)
+    const { error: configUpdateError } = await supabaseAdmin
+      .from('store_sync_config')
+      .update({
+        last_sync_at: syncCompletedAt
+      })
+      .eq('store_id', storeId)
+
+    // If config doesn't exist, create it
+    if (configUpdateError) {
+      await supabaseAdmin
+        .from('store_sync_config')
+        .insert({
+          store_id: storeId,
+          enabled: true,
+          sync_frequency: 'hourly',
+          last_sync_at: syncCompletedAt
+        })
+    }
+
     return new Response(
     return new Response(
       JSON.stringify({
       JSON.stringify({
         success: true,
         success: true,

+ 48 - 6
supabase/functions/trigger-sync/index.ts

@@ -143,41 +143,83 @@ serve(wrapHandler('trigger-sync', async (req) => {
             .single()
             .single()
 
 
           const altData = currentStore?.alt_data || {}
           const altData = currentStore?.alt_data || {}
+          const syncCompletedAt = new Date().toISOString()
 
 
+          // Update stores table (for Web UI display)
           await supabaseAdmin
           await supabaseAdmin
             .from('stores')
             .from('stores')
             .update({
             .update({
               sync_status: 'completed',
               sync_status: 'completed',
-              sync_completed_at: new Date().toISOString(),
+              sync_completed_at: syncCompletedAt,
               alt_data: {
               alt_data: {
                 ...altData,
                 ...altData,
-                last_sync_at: new Date().toISOString(),
-                last_sync_stats: syncStats
-              }
+                last_sync_at: syncCompletedAt,
+                last_sync_stats: syncStats,
+                last_sync_type: 'manual'
+              },
+              sync_error: null
             })
             })
             .eq('id', store_id)
             .eq('id', store_id)
+
+          // Update store_sync_config timestamps (for scheduling logic)
+          const { error: configUpdateError } = await supabaseAdmin
+            .from('store_sync_config')
+            .update({
+              last_sync_at: syncCompletedAt
+            })
+            .eq('store_id', store_id)
+
+          // If config doesn't exist, create it
+          if (configUpdateError) {
+            await supabaseAdmin
+              .from('store_sync_config')
+              .insert({
+                store_id: store_id,
+                enabled: true,
+                sync_frequency: 'hourly',
+                last_sync_at: syncCompletedAt
+              })
+          }
         } else {
         } else {
           console.error(`[TriggerSync] Sync failed for ${store_id}:`, result)
           console.error(`[TriggerSync] Sync failed for ${store_id}:`, result)
+          const errorTime = new Date().toISOString()
           await supabaseAdmin
           await supabaseAdmin
             .from('stores')
             .from('stores')
             .update({
             .update({
               sync_status: 'error',
               sync_status: 'error',
-              sync_completed_at: new Date().toISOString(),
+              sync_completed_at: errorTime,
               sync_error: result.error || 'Unknown error during sync'
               sync_error: result.error || 'Unknown error during sync'
             })
             })
             .eq('id', store_id)
             .eq('id', store_id)
+
+          // Still update store_sync_config timestamp even on error (for retry logic)
+          await supabaseAdmin
+            .from('store_sync_config')
+            .update({
+              last_sync_at: errorTime
+            })
+            .eq('store_id', store_id)
         }
         }
       })
       })
       .catch(async (error) => {
       .catch(async (error) => {
         console.error(`[TriggerSync] Sync error for ${store_id}:`, error)
         console.error(`[TriggerSync] Sync error for ${store_id}:`, error)
+        const errorTime = new Date().toISOString()
         await supabaseAdmin
         await supabaseAdmin
           .from('stores')
           .from('stores')
           .update({
           .update({
             sync_status: 'error',
             sync_status: 'error',
-            sync_completed_at: new Date().toISOString(),
+            sync_completed_at: errorTime,
             sync_error: error.message || 'Unknown error during sync'
             sync_error: error.message || 'Unknown error during sync'
           })
           })
           .eq('id', store_id)
           .eq('id', store_id)
+
+        // Still update store_sync_config timestamp even on error (for retry logic)
+        await supabaseAdmin
+          .from('store_sync_config')
+          .update({
+            last_sync_at: errorTime
+          })
+          .eq('store_id', store_id)
       })
       })
 
 
     console.log(`[TriggerSync] Sync triggered successfully for ${store_id}`)
     console.log(`[TriggerSync] Sync triggered successfully for ${store_id}`)

+ 16 - 3
supabase/functions/woocommerce-scheduled-sync/index.ts

@@ -228,12 +228,25 @@ serve(wrapHandler('woocommerce-scheduled-sync', async (req) => {
           syncStats.error_message = syncResult.error || 'Unknown error'
           syncStats.error_message = syncResult.error || 'Unknown error'
         }
         }
 
 
-        // Update store_sync_config timestamps
+        // Update BOTH stores table and store_sync_config to maintain consistency
+        const syncCompletedAt = new Date().toISOString()
+
+        // Update stores table (for Web UI display)
+        await supabaseAdmin
+          .from('stores')
+          .update({
+            sync_status: syncStats.status === 'success' ? 'completed' : 'error',
+            sync_completed_at: syncCompletedAt,
+            sync_error: syncStats.error_message
+          })
+          .eq('id', storeId)
+
+        // Update store_sync_config timestamps (for scheduling logic)
         if (config) {
         if (config) {
           await supabaseAdmin
           await supabaseAdmin
             .from('store_sync_config')
             .from('store_sync_config')
             .update({
             .update({
-              last_sync_at: new Date().toISOString(),
+              last_sync_at: syncCompletedAt,
               // next_sync_at will be auto-calculated by trigger
               // next_sync_at will be auto-calculated by trigger
             })
             })
             .eq('store_id', storeId)
             .eq('store_id', storeId)
@@ -245,7 +258,7 @@ serve(wrapHandler('woocommerce-scheduled-sync', async (req) => {
               store_id: storeId,
               store_id: storeId,
               enabled: true,
               enabled: true,
               sync_frequency: 'hourly',
               sync_frequency: 'hourly',
-              last_sync_at: new Date().toISOString()
+              last_sync_at: syncCompletedAt
             })
             })
         }
         }