|
|
@@ -367,6 +367,7 @@ serve(wrapHandler('shoprenter-sync', async (req) => {
|
|
|
|
|
|
// Sync Products
|
|
|
const allProducts: any[] = []
|
|
|
+ let productSyncError: Error | null = null
|
|
|
try {
|
|
|
if (!canSyncProducts) {
|
|
|
console.log('[ShopRenter] Product sync disabled by store permissions')
|
|
|
@@ -427,10 +428,12 @@ serve(wrapHandler('shoprenter-sync', async (req) => {
|
|
|
}
|
|
|
} catch (error) {
|
|
|
console.error('[ShopRenter] Product sync error:', error)
|
|
|
+ productSyncError = error as Error
|
|
|
syncStats.products.errors++
|
|
|
}
|
|
|
|
|
|
// Sync Orders
|
|
|
+ let orderSyncError: Error | null = null
|
|
|
try {
|
|
|
console.log('[ShopRenter] Syncing orders...')
|
|
|
let page = 0 // ShopRenter API uses zero-based pagination
|
|
|
@@ -494,10 +497,12 @@ serve(wrapHandler('shoprenter-sync', async (req) => {
|
|
|
console.log(`[ShopRenter] Orders synced: ${syncStats.orders.synced}`)
|
|
|
} catch (error) {
|
|
|
console.error('[ShopRenter] Order sync error:', error)
|
|
|
+ orderSyncError = error as Error
|
|
|
syncStats.orders.errors++
|
|
|
}
|
|
|
|
|
|
// Sync Customers
|
|
|
+ let customerSyncError: Error | null = null
|
|
|
try {
|
|
|
console.log('[ShopRenter] Syncing customers...')
|
|
|
let page = 0 // ShopRenter API uses zero-based pagination
|
|
|
@@ -557,12 +562,28 @@ serve(wrapHandler('shoprenter-sync', async (req) => {
|
|
|
console.log(`[ShopRenter] Customers synced: ${syncStats.customers.synced}`)
|
|
|
} catch (error) {
|
|
|
console.error('[ShopRenter] Customer sync error:', error)
|
|
|
+ customerSyncError = error as Error
|
|
|
syncStats.customers.errors++
|
|
|
}
|
|
|
|
|
|
// Update BOTH stores table and store_sync_config to maintain consistency
|
|
|
const syncCompletedAt = new Date().toISOString()
|
|
|
|
|
|
+ // Check if any critical errors occurred
|
|
|
+ const hasErrors = productSyncError || orderSyncError || customerSyncError
|
|
|
+ const totalErrors = syncStats.products.errors + syncStats.orders.errors + syncStats.customers.errors
|
|
|
+ const totalSynced = syncStats.products.synced + syncStats.orders.synced + syncStats.customers.synced
|
|
|
+
|
|
|
+ // Build error message if any errors occurred
|
|
|
+ let errorMessage: string | null = null
|
|
|
+ if (hasErrors) {
|
|
|
+ const errorParts: string[] = []
|
|
|
+ if (productSyncError) errorParts.push(`Products: ${productSyncError.message}`)
|
|
|
+ if (orderSyncError) errorParts.push(`Orders: ${orderSyncError.message}`)
|
|
|
+ if (customerSyncError) errorParts.push(`Customers: ${customerSyncError.message}`)
|
|
|
+ errorMessage = errorParts.join('; ')
|
|
|
+ }
|
|
|
+
|
|
|
// Update stores table (for Web UI display)
|
|
|
await supabaseAdmin
|
|
|
.from('stores')
|
|
|
@@ -572,8 +593,8 @@ serve(wrapHandler('shoprenter-sync', async (req) => {
|
|
|
last_sync_stats: syncStats,
|
|
|
last_sync_type: 'manual'
|
|
|
},
|
|
|
- sync_status: 'completed',
|
|
|
- sync_error: null
|
|
|
+ sync_status: hasErrors ? 'error' : 'completed',
|
|
|
+ sync_error: errorMessage
|
|
|
})
|
|
|
.eq('id', storeId)
|
|
|
|
|
|
@@ -597,10 +618,39 @@ serve(wrapHandler('shoprenter-sync', async (req) => {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+ // Return error response if sync failed
|
|
|
+ if (hasErrors && totalSynced === 0) {
|
|
|
+ return new Response(
|
|
|
+ JSON.stringify({
|
|
|
+ success: false,
|
|
|
+ error: 'Sync failed',
|
|
|
+ message: errorMessage,
|
|
|
+ stats: syncStats,
|
|
|
+ timestamp: new Date().toISOString()
|
|
|
+ }),
|
|
|
+ { status: 500, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ // Return partial success if some items synced but there were errors
|
|
|
+ if (hasErrors && totalSynced > 0) {
|
|
|
+ return new Response(
|
|
|
+ JSON.stringify({
|
|
|
+ success: false,
|
|
|
+ error: 'Sync completed with errors',
|
|
|
+ message: errorMessage,
|
|
|
+ stats: syncStats,
|
|
|
+ timestamp: new Date().toISOString()
|
|
|
+ }),
|
|
|
+ { status: 207, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ // Return success if no errors
|
|
|
return new Response(
|
|
|
JSON.stringify({
|
|
|
success: true,
|
|
|
- message: 'Sync completed',
|
|
|
+ message: 'Sync completed successfully',
|
|
|
stats: syncStats,
|
|
|
timestamp: new Date().toISOString()
|
|
|
}),
|