fix: customFields was not persisted on import and force Custom Field keys to be lower case (#849)

* refactor: force Custom Field keys to be lower case

* fix: customFields was not persisted on import
This commit is contained in:
Alex Christoffer Rasmussen
2024-03-28 13:51:32 +01:00
committed by GitHub
parent df8b76305e
commit a86496ed27
6 changed files with 42 additions and 40 deletions
@@ -841,35 +841,35 @@ describe('custom fields', () => {
it('creates a field from given parameters', async () => {
const expected = {
lighting: {
label: 'lighting',
label: 'Lighting',
type: 'string',
colour: 'blue',
},
};
const customField = await createCustomField({ label: 'lighting', type: 'string', colour: 'blue' });
const customField = await createCustomField({ label: 'Lighting', type: 'string', colour: 'blue' });
expect(customField).toStrictEqual(expected);
});
});
describe('editCustomField()', () => {
it('edits a field with a given label', async () => {
await createCustomField({ label: 'sound', type: 'string', colour: 'blue' });
await createCustomField({ label: 'Sound', type: 'string', colour: 'blue' });
const expected = {
lighting: {
label: 'lighting',
label: 'Lighting',
type: 'string',
colour: 'blue',
},
sound: {
label: 'sound',
label: 'Sound',
type: 'string',
colour: 'blue',
colour: 'green',
},
};
const customField = await editCustomField('sound', { label: 'sound', type: 'string', colour: 'blue' });
const customField = await editCustomField('sound', { label: 'Sound', type: 'string', colour: 'green' });
expect(customField).toStrictEqual(expected);
});
@@ -879,7 +879,7 @@ describe('custom fields', () => {
it('deletes a field with a given label', async () => {
const expected = {
lighting: {
label: 'lighting',
label: 'Lighting',
type: 'string',
colour: 'blue',
},
@@ -60,6 +60,7 @@ export async function init(initialRundown: Readonly<OntimeRundown>, customFields
persistedCustomFields = structuredClone(customFields);
generate();
await DataProvider.setRundown(persistedRundown);
await DataProvider.setCustomFields(customFields);
}
/**
@@ -428,16 +429,16 @@ function scheduleCustomFieldPersist(persistedCustomFields: CustomFields) {
*/
export const createCustomField = async (field: CustomField) => {
const { label, type, colour } = field;
const key = label.toLowerCase();
// check if label already exists
const alreadyExists = Object.hasOwn(persistedCustomFields, label);
const alreadyExists = Object.hasOwn(persistedCustomFields, key);
if (alreadyExists) {
throw new Error('Label already exists');
}
// update object and persist
persistedCustomFields[label] = { label, type, colour };
persistedCustomFields[key] = { label, type, colour };
scheduleCustomFieldPersist(persistedCustomFields);
@@ -446,29 +447,30 @@ export const createCustomField = async (field: CustomField) => {
/**
* Edits an existing custom field in the database
* @param label
* @param key
* @param newField
* @returns
*/
export const editCustomField = async (label: string, newField: Partial<CustomField>) => {
if (!(label in persistedCustomFields)) {
export const editCustomField = async (key: string, newField: Partial<CustomField>) => {
if (!(key in persistedCustomFields)) {
throw new Error('Could not find label');
}
const existingField = persistedCustomFields[label];
const existingField = persistedCustomFields[key];
if (existingField.type !== newField.type) {
throw new Error('Change of field type is not allowed');
}
persistedCustomFields[newField.label] = { ...existingField, ...newField };
const newKey = newField.label.toLowerCase();
persistedCustomFields[newKey] = { ...existingField, ...newField };
if (existingField.label !== newField.label) {
delete persistedCustomFields[existingField.label];
customFieldChangelog[label] = newField.label;
if (key !== newKey) {
delete persistedCustomFields[key];
customFieldChangelog[key] = newKey;
}
scheduleCustomFieldPersist(persistedCustomFields);
invalidateIfUsed(label);
invalidateIfUsed(key);
return persistedCustomFields;
};