diff --git a/src/backend/services/auto-sync.service.js b/src/backend/services/auto-sync.service.js index f703b71..748aa04 100644 --- a/src/backend/services/auto-sync.service.js +++ b/src/backend/services/auto-sync.service.js @@ -237,6 +237,7 @@ export async function getPendingSyncUsers(service) { const enabledField = service === 'lotw' ? autoSyncSettings.lotwEnabled : autoSyncSettings.dclEnabled; const nextSyncField = service === 'lotw' ? autoSyncSettings.lotwNextSyncAt : autoSyncSettings.dclNextSyncAt; const credentialField = service === 'lotw' ? users.lotwUsername : users.dclApiKey; + const intervalField = service === 'lotw' ? autoSyncSettings.lotwIntervalHours : autoSyncSettings.dclIntervalHours; const now = new Date(); @@ -261,13 +262,45 @@ export async function getPendingSyncUsers(service) { ) ); - // Filter out users without credentials + // Split into users with and without credentials const withCredentials = results.filter(r => r.hasCredentials); + const withoutCredentials = results.filter(r => !r.hasCredentials); + + // For users without credentials, update their next sync time to retry in 24 hours + // This prevents them from being continuously retried every minute + if (withoutCredentials.length > 0) { + const retryDate = new Date(); + retryDate.setHours(retryDate.getHours() + 24); + + for (const user of withoutCredentials) { + const updateData = { + updatedAt: new Date(), + }; + + if (service === 'lotw') { + updateData.lotwNextSyncAt = retryDate; + } else { + updateData.dclNextSyncAt = retryDate; + } + + await db + .update(autoSyncSettings) + .set(updateData) + .where(eq(autoSyncSettings.userId, user.userId)); + } + + logger.warn('Skipped auto-sync for users without credentials, will retry in 24 hours', { + service, + count: withoutCredentials.length, + userIds: withoutCredentials.map(u => u.userId), + }); + } logger.debug('Found pending sync users', { service, total: results.length, withCredentials: withCredentials.length, + withoutCredentials: withoutCredentials.length, }); return withCredentials;