Skip to content

Commit

Permalink
Merge branch 'development' into dependabot/npm_and_yarn/cypress-13.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
txau authored Jun 20, 2024
2 parents 5679e8d + c7b6dae commit 60a97f1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
8 changes: 4 additions & 4 deletions app/api/sync/specs/syncWorker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -459,15 +459,15 @@ 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 });
//@ts-ignore
await templates.delete({ _id: template1 });
}, 'host1');

await expect(syncWorker.runAllTenants()).resolves.not.toThrowError();
await expect(syncWorker.runAllTenants()).resolves.not.toThrow();
});
});

Expand Down Expand Up @@ -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')
);

Expand Down
55 changes: 30 additions & 25 deletions app/api/sync/syncWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class InvalidSyncConfig extends Error {
}
}

export interface SyncConfig {
interface SyncConfig {
url: string;
active?: boolean;
username: string;
Expand Down Expand Up @@ -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) {
Expand All @@ -112,3 +115,5 @@ export const syncWorker = {
return response.cookie || '';
},
};

export type { SyncConfig };

0 comments on commit 60a97f1

Please sign in to comment.