|
@@ -69,6 +69,7 @@ serve(wrapHandler('shoprenter-scheduled-sync', async (req) => {
|
|
|
)
|
|
)
|
|
|
`)
|
|
`)
|
|
|
.eq('platform_name', 'shoprenter')
|
|
.eq('platform_name', 'shoprenter')
|
|
|
|
|
+ .eq('is_active', true)
|
|
|
|
|
|
|
|
if (storesError) {
|
|
if (storesError) {
|
|
|
console.error('[ShopRenter Scheduled Sync] Error fetching stores:', storesError)
|
|
console.error('[ShopRenter Scheduled Sync] Error fetching stores:', storesError)
|
|
@@ -80,19 +81,39 @@ serve(wrapHandler('shoprenter-scheduled-sync', async (req) => {
|
|
|
|
|
|
|
|
// Filter stores that have sync enabled and are due for sync
|
|
// Filter stores that have sync enabled and are due for sync
|
|
|
const now = new Date()
|
|
const now = new Date()
|
|
|
|
|
+ console.log(`[ShopRenter Scheduled Sync] Current time: ${now.toISOString()}, Found ${stores?.length || 0} ShopRenter stores`)
|
|
|
|
|
+
|
|
|
const storesToSync = stores?.filter(store => {
|
|
const storesToSync = stores?.filter(store => {
|
|
|
- const config = store.store_sync_config?.[0]
|
|
|
|
|
|
|
+ // Handle both array and single object cases for store_sync_config
|
|
|
|
|
+ const configArray = store.store_sync_config
|
|
|
|
|
+ const config = Array.isArray(configArray) ? configArray[0] : configArray
|
|
|
|
|
+
|
|
|
|
|
+ console.log(`[ShopRenter Scheduled Sync] Store ${store.id} (${store.store_name}):`, {
|
|
|
|
|
+ hasConfig: !!config,
|
|
|
|
|
+ configType: configArray ? (Array.isArray(configArray) ? 'array' : 'object') : 'null',
|
|
|
|
|
+ enabled: config?.enabled,
|
|
|
|
|
+ next_sync_at: config?.next_sync_at
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
if (!config || !config.enabled) {
|
|
if (!config || !config.enabled) {
|
|
|
|
|
+ console.log(`[ShopRenter Scheduled Sync] Store ${store.id} skipped: no config or not enabled`)
|
|
|
return false
|
|
return false
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Check if sync is due
|
|
// Check if sync is due
|
|
|
if (config.next_sync_at) {
|
|
if (config.next_sync_at) {
|
|
|
const nextSync = new Date(config.next_sync_at)
|
|
const nextSync = new Date(config.next_sync_at)
|
|
|
- return nextSync <= now
|
|
|
|
|
|
|
+ const isDue = nextSync <= now
|
|
|
|
|
+ console.log(`[ShopRenter Scheduled Sync] Store ${store.id} sync check:`, {
|
|
|
|
|
+ nextSync: nextSync.toISOString(),
|
|
|
|
|
+ now: now.toISOString(),
|
|
|
|
|
+ isDue
|
|
|
|
|
+ })
|
|
|
|
|
+ return isDue
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// If no next_sync_at, sync is due
|
|
// If no next_sync_at, sync is due
|
|
|
|
|
+ console.log(`[ShopRenter Scheduled Sync] Store ${store.id} has no next_sync_at, marking as due`)
|
|
|
return true
|
|
return true
|
|
|
}) || []
|
|
}) || []
|
|
|
|
|
|
|
@@ -115,7 +136,9 @@ serve(wrapHandler('shoprenter-scheduled-sync', async (req) => {
|
|
|
// Sync each store
|
|
// Sync each store
|
|
|
for (const store of storesToSync) {
|
|
for (const store of storesToSync) {
|
|
|
const storeId = store.id
|
|
const storeId = store.id
|
|
|
- const config = store.store_sync_config?.[0]
|
|
|
|
|
|
|
+ // Handle both array and single object cases for store_sync_config
|
|
|
|
|
+ const configArray = store.store_sync_config
|
|
|
|
|
+ const config = Array.isArray(configArray) ? configArray[0] : configArray
|
|
|
console.log(`[ShopRenter Scheduled Sync] Starting sync for store ${storeId} (${store.store_name})`)
|
|
console.log(`[ShopRenter Scheduled Sync] Starting sync for store ${storeId} (${store.store_name})`)
|
|
|
|
|
|
|
|
// Detect country code from store URL for phone formatting
|
|
// Detect country code from store URL for phone formatting
|
|
@@ -161,14 +184,7 @@ serve(wrapHandler('shoprenter-scheduled-sync', async (req) => {
|
|
|
const productsToCache = productsData.items.map((product: any) => ({
|
|
const productsToCache = productsData.items.map((product: any) => ({
|
|
|
store_id: storeId,
|
|
store_id: storeId,
|
|
|
shoprenter_product_id: product.id,
|
|
shoprenter_product_id: product.id,
|
|
|
- name: product.name,
|
|
|
|
|
- sku: product.sku,
|
|
|
|
|
- price: parseFloat(product.price) || 0,
|
|
|
|
|
- currency: product.currency || 'HUF',
|
|
|
|
|
- description: product.description,
|
|
|
|
|
- stock: product.stock,
|
|
|
|
|
- active: product.active !== false,
|
|
|
|
|
- raw_data: product,
|
|
|
|
|
|
|
+ product_data: product,
|
|
|
last_synced_at: new Date().toISOString()
|
|
last_synced_at: new Date().toISOString()
|
|
|
}))
|
|
}))
|
|
|
|
|
|