mirror of
https://github.com/cpvalente/ontime.git
synced 2026-09-04 13:59:09 +00:00
refactor: improve merge logic
This commit is contained in:
committed by
Carlos Valente
parent
08b8e73393
commit
a5daa76616
@@ -7,24 +7,27 @@ export function safeMerge(existing: DatabaseModel, newData: Partial<DatabaseMode
|
|||||||
const deepExisting = structuredClone(existing);
|
const deepExisting = structuredClone(existing);
|
||||||
const deepNewData = structuredClone(newData);
|
const deepNewData = structuredClone(newData);
|
||||||
|
|
||||||
|
// destructure each property to simplify merging not provided ie: ...{} has no effect
|
||||||
const {
|
const {
|
||||||
rundowns = {},
|
rundowns = {},
|
||||||
project = {},
|
project = {},
|
||||||
settings = {},
|
settings = {},
|
||||||
viewSettings = {},
|
viewSettings = {},
|
||||||
urlPresets = deepExisting.urlPresets,
|
urlPresets = [],
|
||||||
customFields = deepExisting.customFields,
|
customFields = {},
|
||||||
automation = deepExisting.automation,
|
automation,
|
||||||
} = deepNewData;
|
} = deepNewData;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...deepExisting,
|
|
||||||
rundowns: { ...existing.rundowns, ...rundowns },
|
rundowns: { ...existing.rundowns, ...rundowns },
|
||||||
project: { ...deepExisting.project, ...project },
|
project: { ...deepExisting.project, ...project },
|
||||||
settings: { ...deepExisting.settings, ...settings },
|
settings: { ...deepExisting.settings, ...settings },
|
||||||
viewSettings: { ...deepExisting.viewSettings, ...viewSettings },
|
viewSettings: { ...deepExisting.viewSettings, ...viewSettings },
|
||||||
urlPresets: urlPresets ?? deepExisting.urlPresets,
|
// URL presets are independent and we can append them together
|
||||||
customFields: customFields ?? deepExisting.customFields,
|
urlPresets: [...deepExisting.urlPresets, ...urlPresets],
|
||||||
automation: { ...deepExisting.automation, ...automation },
|
// custom fields can be merged
|
||||||
|
customFields: { ...deepExisting.customFields, ...customFields },
|
||||||
|
// trigger and automation are coupled and cannot be changed individually so we replace the whole automation
|
||||||
|
automation: automation ?? deepExisting.automation,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,16 @@
|
|||||||
import { DatabaseModel, Settings, URLPreset } from 'ontime-types';
|
import { AutomationSettings, CustomField, DatabaseModel, Settings, TimerLifeCycle, URLPreset } from 'ontime-types';
|
||||||
|
|
||||||
import { demoDb } from '../../../models/demoProject.js';
|
import { makeNewProject } from '../../../models/dataModel.js';
|
||||||
import { makeOntimeEvent, makeRundown } from '../../../api-data/rundown/__mocks__/rundown.mocks.js';
|
import { makeOntimeEvent, makeRundown } from '../../../api-data/rundown/__mocks__/rundown.mocks.js';
|
||||||
|
|
||||||
import { safeMerge } from '../DataProvider.utils.js';
|
import { safeMerge } from '../DataProvider.utils.js';
|
||||||
|
|
||||||
|
const baseDb = makeNewProject('demo');
|
||||||
|
|
||||||
describe('safeMerge', () => {
|
describe('safeMerge', () => {
|
||||||
it('returns existing data if new data is not provided', () => {
|
it('returns existing data if new data is not provided', () => {
|
||||||
const mergedData = safeMerge(demoDb, {});
|
const mergedData = safeMerge(baseDb, {});
|
||||||
expect(mergedData).toEqual(demoDb);
|
expect(mergedData).toEqual(baseDb);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('overrides a rundown with the same key', () => {
|
it('overrides a rundown with the same key', () => {
|
||||||
@@ -20,7 +22,7 @@ describe('safeMerge', () => {
|
|||||||
},
|
},
|
||||||
order: ['1', '2'],
|
order: ['1', '2'],
|
||||||
});
|
});
|
||||||
const mergedData = safeMerge(demoDb, { rundowns: { demo: newData } });
|
const mergedData = safeMerge(baseDb, { rundowns: { demo: newData } });
|
||||||
expect(mergedData.rundowns.demo).toStrictEqual(newData);
|
expect(mergedData.rundowns.demo).toStrictEqual(newData);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -33,13 +35,13 @@ describe('safeMerge', () => {
|
|||||||
},
|
},
|
||||||
order: ['1', '2'],
|
order: ['1', '2'],
|
||||||
});
|
});
|
||||||
const mergedData = safeMerge(demoDb, { rundowns: { rundown: newData } });
|
const mergedData = safeMerge(baseDb, { rundowns: { rundown: newData } });
|
||||||
expect(mergedData.rundowns.demo).toStrictEqual(demoDb.rundowns.demo);
|
expect(mergedData.rundowns.demo).toStrictEqual(baseDb.rundowns.demo);
|
||||||
expect(mergedData.rundowns.rundown).toStrictEqual(newData);
|
expect(mergedData.rundowns.rundown).toStrictEqual(newData);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('merges the project key', () => {
|
it('merges the project key', () => {
|
||||||
const mergedData = safeMerge(demoDb, {
|
const mergedData = safeMerge(baseDb, {
|
||||||
project: {
|
project: {
|
||||||
title: 'new title',
|
title: 'new title',
|
||||||
info: 'new backstage info',
|
info: 'new backstage info',
|
||||||
@@ -54,10 +56,10 @@ describe('safeMerge', () => {
|
|||||||
|
|
||||||
expect(mergedData.project).toStrictEqual({
|
expect(mergedData.project).toStrictEqual({
|
||||||
title: 'new title',
|
title: 'new title',
|
||||||
description: demoDb.project.description,
|
description: baseDb.project.description,
|
||||||
url: demoDb.project.url,
|
url: baseDb.project.url,
|
||||||
info: 'new backstage info',
|
info: 'new backstage info',
|
||||||
logo: demoDb.project.logo,
|
logo: baseDb.project.logo,
|
||||||
custom: [
|
custom: [
|
||||||
{
|
{
|
||||||
title: 'new custom title',
|
title: 'new custom title',
|
||||||
@@ -68,7 +70,7 @@ describe('safeMerge', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('merges the settings key', () => {
|
it('merges the settings key', () => {
|
||||||
const mergedData = safeMerge(demoDb, {
|
const mergedData = safeMerge(baseDb, {
|
||||||
settings: {
|
settings: {
|
||||||
serverPort: 3000,
|
serverPort: 3000,
|
||||||
language: 'pt',
|
language: 'pt',
|
||||||
@@ -80,7 +82,7 @@ describe('safeMerge', () => {
|
|||||||
serverPort: 3000,
|
serverPort: 3000,
|
||||||
operatorKey: null,
|
operatorKey: null,
|
||||||
editorKey: null,
|
editorKey: null,
|
||||||
timeFormat: '24',
|
timeFormat: baseDb.settings.timeFormat,
|
||||||
language: 'pt',
|
language: 'pt',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -93,16 +95,17 @@ describe('safeMerge', () => {
|
|||||||
] as URLPreset[],
|
] as URLPreset[],
|
||||||
};
|
};
|
||||||
|
|
||||||
const mergedData = safeMerge(demoDb, newData);
|
const mergedData = safeMerge(baseDb, newData);
|
||||||
|
|
||||||
expect(mergedData.urlPresets).toStrictEqual(newData.urlPresets);
|
expect(mergedData.urlPresets).toStrictEqual([...baseDb.urlPresets, ...newData.urlPresets]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('merges customFields into existing object', () => {
|
it('merges customFields into existing object', () => {
|
||||||
const existing = {
|
const existing = {
|
||||||
|
...baseDb,
|
||||||
customFields: {
|
customFields: {
|
||||||
lighting: { type: 'text', label: 'lighting' },
|
lighting: { type: 'text', colour: 'red', label: 'lighting' },
|
||||||
sound: { type: 'text', label: 'sound' },
|
sound: { type: 'text', colour: 'red', label: 'sound' },
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -114,6 +117,8 @@ describe('safeMerge', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const expected = {
|
const expected = {
|
||||||
|
lighting: { type: 'text', colour: 'red', label: 'lighting' },
|
||||||
|
sound: { type: 'text', colour: 'red', label: 'sound' },
|
||||||
switcher: { type: 'text', label: 'switcher' },
|
switcher: { type: 'text', label: 'switcher' },
|
||||||
vfx: { type: 'text', label: 'vfx' },
|
vfx: { type: 'text', label: 'vfx' },
|
||||||
};
|
};
|
||||||
@@ -122,4 +127,79 @@ describe('safeMerge', () => {
|
|||||||
const result = safeMerge(existing, newData);
|
const result = safeMerge(existing, newData);
|
||||||
expect(result.customFields).toEqual(expected);
|
expect(result.customFields).toEqual(expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('overwrites matching customFields keys when merging', () => {
|
||||||
|
const existing = {
|
||||||
|
...baseDb,
|
||||||
|
customFields: {
|
||||||
|
lighting: { type: 'text', colour: 'red', label: 'lighting' } satisfies CustomField,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const newData = {
|
||||||
|
customFields: {
|
||||||
|
lighting: { type: 'text', colour: 'red', label: 'updated lighting' } satisfies CustomField,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = safeMerge(existing, newData);
|
||||||
|
expect(result.customFields).toEqual({
|
||||||
|
lighting: { type: 'text', colour: 'red', label: 'updated lighting' },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('replaces automation settings when provided', () => {
|
||||||
|
const existing = {
|
||||||
|
...baseDb,
|
||||||
|
automation: {
|
||||||
|
enabledAutomations: true,
|
||||||
|
enabledOscIn: true,
|
||||||
|
oscPortIn: 9000,
|
||||||
|
triggers: [
|
||||||
|
{
|
||||||
|
id: 'trigger-1',
|
||||||
|
title: 'Trigger 1',
|
||||||
|
trigger: TimerLifeCycle.onStart,
|
||||||
|
automationId: 'automation-1',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
automations: {
|
||||||
|
'automation-1': {
|
||||||
|
id: 'automation-1',
|
||||||
|
title: 'Automation 1',
|
||||||
|
filterRule: 'all',
|
||||||
|
filters: [],
|
||||||
|
outputs: [],
|
||||||
|
},
|
||||||
|
} satisfies AutomationSettings['automations'],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const newAutomation: AutomationSettings = {
|
||||||
|
enabledAutomations: false,
|
||||||
|
enabledOscIn: false,
|
||||||
|
oscPortIn: 7777,
|
||||||
|
triggers: [
|
||||||
|
{
|
||||||
|
id: 'trigger-2',
|
||||||
|
title: 'Trigger 2',
|
||||||
|
trigger: TimerLifeCycle.onStop,
|
||||||
|
automationId: 'automation-2',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
automations: {
|
||||||
|
'automation-2': {
|
||||||
|
id: 'automation-2',
|
||||||
|
title: 'Automation 2',
|
||||||
|
filterRule: 'any',
|
||||||
|
filters: [],
|
||||||
|
outputs: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = safeMerge(existing, { automation: newAutomation });
|
||||||
|
|
||||||
|
expect(result.automation).toStrictEqual(newAutomation);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user