|
@@ -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}`)
|