diff --git a/app/api/sync/specs/syncWorker.spec.ts b/app/api/sync/specs/syncWorker.spec.ts index e8447cb3ba..147d468604 100644 --- a/app/api/sync/specs/syncWorker.spec.ts +++ b/app/api/sync/specs/syncWorker.spec.ts @@ -179,7 +179,7 @@ describe('syncWorker', () => { await db.disconnect(); }); - it('should sync the configured templates and its defined properties', async () => { + it('should sync the whitelisted templates and properties', async () => { await runAllTenants(); await tenants.run(async () => { const syncedTemplates = await templates.get(); @@ -459,7 +459,7 @@ describe('syncWorker', () => { }, 'target1'); }); - describe('when a template that is configured has been deleted', () => { + describe('when a template that is whitelisted has been deleted', () => { it('should not throw an error', async () => { await tenants.run(async () => { await entitiesModel.delete({ template: template1 }); @@ -467,7 +467,7 @@ describe('syncWorker', () => { await templates.delete({ _id: template1 }); }, 'host1'); - await expect(syncWorker.runAllTenants()).resolves.not.toThrowError(); + await expect(syncWorker.runAllTenants()).resolves.not.toThrow(); }); }); @@ -575,7 +575,7 @@ describe('syncWorker', () => { fixtures.settings[0].sync[0].config.pages = []; await applyFixtures(fixtures, {}); - await expect(runAllTenants).rejects.toThrowError( + await expect(runAllTenants).rejects.toThrow( new Error('Invalid elements found in ordering - pages') ); diff --git a/app/api/sync/syncWorker.ts b/app/api/sync/syncWorker.ts index 540a330cb2..e459303d3f 100644 --- a/app/api/sync/syncWorker.ts +++ b/app/api/sync/syncWorker.ts @@ -26,7 +26,7 @@ class InvalidSyncConfig extends Error { } } -export interface SyncConfig { +interface SyncConfig { url: string; active?: boolean; username: string; @@ -71,39 +71,42 @@ export const syncWorker = { await previousSync; const syncConfig = validateConfig(config); if (syncConfig.active) { - const cookie = await this.login(syncConfig); - await this.syncronizeConfig(syncConfig, cookie); + await this.syncronizeConfig(syncConfig); } }, Promise.resolve()); }, - async syncronizeConfig(config: SyncConfig, cookie: string) { + async syncronizeConfig(config: SyncConfig) { await createSyncIfNotExists(config); const syncConfig = await createSyncConfig(config, config.name, this.UPDATE_LOG_TARGET_COUNT); - await ( - await syncConfig.lastChanges() - ).reduce(async (previousChange, change) => { - await previousChange; - const shouldSync: { skip?: boolean; data?: any } = await syncConfig.shouldSync(change); - if (shouldSync.skip) { - await synchronizer.syncDelete(change, config.url, cookie); - } + const lastChanges = await syncConfig.lastChanges(); - if (shouldSync.data) { - await synchronizer.syncData( - { - url: config.url, - change, - data: shouldSync.data, - cookie, - }, - 'post' - ); - } - await updateSyncs(config.name, change.namespace, change.timestamp); - }, Promise.resolve()); + if (lastChanges.length) { + const cookie = await this.login(config); + + await lastChanges.reduce(async (previousChange, change) => { + await previousChange; + const shouldSync: { skip?: boolean; data?: any } = await syncConfig.shouldSync(change); + if (shouldSync.skip) { + await synchronizer.syncDelete(change, config.url, cookie); + } + + if (shouldSync.data) { + await synchronizer.syncData( + { + url: config.url, + change, + data: shouldSync.data, + cookie, + }, + 'post' + ); + } + await updateSyncs(config.name, change.namespace, change.timestamp); + }, Promise.resolve()); + } }, async login({ url, username, password }: SyncConfig) { @@ -112,3 +115,5 @@ export const syncWorker = { return response.cookie || ''; }, }; + +export type { SyncConfig };