|
|
@@ -47,10 +47,25 @@ serve(async (req) => {
|
|
|
const supabaseServiceKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!
|
|
|
const supabaseAdmin = createClient(supabaseUrl, supabaseServiceKey)
|
|
|
|
|
|
- // Get all active ShopRenter stores that need syncing
|
|
|
+ // Get all ShopRenter stores with sync config
|
|
|
const { data: stores, error: storesError } = await supabaseAdmin
|
|
|
.from('stores')
|
|
|
- .select('id, user_id, store_name, store_url, alt_data')
|
|
|
+ .select(`
|
|
|
+ id,
|
|
|
+ user_id,
|
|
|
+ store_name,
|
|
|
+ store_url,
|
|
|
+ alt_data,
|
|
|
+ store_sync_config (
|
|
|
+ enabled,
|
|
|
+ sync_frequency,
|
|
|
+ sync_products,
|
|
|
+ sync_orders,
|
|
|
+ sync_customers,
|
|
|
+ last_sync_at,
|
|
|
+ next_sync_at
|
|
|
+ )
|
|
|
+ `)
|
|
|
.eq('platform_name', 'shoprenter')
|
|
|
|
|
|
if (storesError) {
|
|
|
@@ -61,25 +76,44 @@ serve(async (req) => {
|
|
|
)
|
|
|
}
|
|
|
|
|
|
- if (!stores || stores.length === 0) {
|
|
|
- console.log('[ShopRenter Scheduled Sync] No ShopRenter stores found')
|
|
|
+ // Filter stores that have sync enabled and are due for sync
|
|
|
+ const now = new Date()
|
|
|
+ const storesToSync = stores?.filter(store => {
|
|
|
+ const config = store.store_sync_config?.[0]
|
|
|
+ if (!config || !config.enabled) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ // Check if sync is due
|
|
|
+ if (config.next_sync_at) {
|
|
|
+ const nextSync = new Date(config.next_sync_at)
|
|
|
+ return nextSync <= now
|
|
|
+ }
|
|
|
+
|
|
|
+ // If no next_sync_at, sync is due
|
|
|
+ return true
|
|
|
+ }) || []
|
|
|
+
|
|
|
+ if (storesToSync.length === 0) {
|
|
|
+ console.log('[ShopRenter Scheduled Sync] No ShopRenter stores due for sync')
|
|
|
return new Response(
|
|
|
JSON.stringify({
|
|
|
success: true,
|
|
|
- message: 'No stores to sync',
|
|
|
+ message: 'No stores due for sync',
|
|
|
stores_processed: 0
|
|
|
}),
|
|
|
{ status: 200, headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
|
|
|
)
|
|
|
}
|
|
|
|
|
|
- console.log(`[ShopRenter Scheduled Sync] Found ${stores.length} stores to sync`)
|
|
|
+ console.log(`[ShopRenter Scheduled Sync] Found ${storesToSync.length} stores due for sync`)
|
|
|
|
|
|
const syncResults = []
|
|
|
|
|
|
// Sync each store
|
|
|
- for (const store of stores) {
|
|
|
+ for (const store of storesToSync) {
|
|
|
const storeId = store.id
|
|
|
+ const config = store.store_sync_config?.[0]
|
|
|
console.log(`[ShopRenter Scheduled Sync] Starting sync for store ${storeId} (${store.store_name})`)
|
|
|
|
|
|
// Detect country code from store URL for phone formatting
|
|
|
@@ -307,6 +341,27 @@ serve(async (req) => {
|
|
|
.update({ alt_data: updatedAltData })
|
|
|
.eq('id', storeId)
|
|
|
|
|
|
+ // Update store_sync_config timestamps
|
|
|
+ if (config) {
|
|
|
+ await supabaseAdmin
|
|
|
+ .from('store_sync_config')
|
|
|
+ .update({
|
|
|
+ last_sync_at: new Date().toISOString(),
|
|
|
+ // next_sync_at will be auto-calculated by trigger
|
|
|
+ })
|
|
|
+ .eq('store_id', storeId)
|
|
|
+ } else {
|
|
|
+ // Create sync config if it doesn't exist
|
|
|
+ await supabaseAdmin
|
|
|
+ .from('store_sync_config')
|
|
|
+ .insert({
|
|
|
+ store_id: storeId,
|
|
|
+ enabled: true,
|
|
|
+ sync_frequency: 'hourly',
|
|
|
+ last_sync_at: new Date().toISOString()
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
syncStats.completed_at = new Date().toISOString()
|
|
|
console.log(`[ShopRenter Scheduled Sync] Store ${storeId} sync completed with status: ${syncStats.status}`)
|
|
|
|
|
|
@@ -327,7 +382,7 @@ serve(async (req) => {
|
|
|
.insert({
|
|
|
sync_type: 'scheduled',
|
|
|
platform: 'shoprenter',
|
|
|
- stores_processed: stores.length,
|
|
|
+ stores_processed: storesToSync.length,
|
|
|
results: syncResults,
|
|
|
started_at: syncResults[0]?.started_at || new Date().toISOString(),
|
|
|
completed_at: new Date().toISOString()
|
|
|
@@ -348,7 +403,7 @@ serve(async (req) => {
|
|
|
success: true,
|
|
|
message: 'Scheduled sync completed',
|
|
|
summary: {
|
|
|
- stores_processed: stores.length,
|
|
|
+ stores_processed: storesToSync.length,
|
|
|
success: successCount,
|
|
|
partial: partialCount,
|
|
|
failed: failedCount
|