From 6a1abba9eaa7e4443e96340330d08d52cb8f7ca1 Mon Sep 17 00:00:00 2001 From: Charles Bochet Date: Mon, 26 Feb 2024 18:41:29 +0100 Subject: [PATCH] Ignore defaultValue update for select fields (#4193) * Ignore defaultValue update for select fields * Fix tests --- ...sform-metadata-for-comparison.util.spec.ts | 2 +- .../transform-metadata-for-comparison.util.ts | 13 ++++--- .../comparators/workspace-field.comparator.ts | 38 +++++++++++++++++-- .../workspace-object.comparator.ts | 5 ++- .../workspace-relation.comparator.ts | 5 ++- 5 files changed, 48 insertions(+), 15 deletions(-) diff --git a/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/utils/__tests__/transform-metadata-for-comparison.util.spec.ts b/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/utils/__tests__/transform-metadata-for-comparison.util.spec.ts index eac21ff46f..ffa322ebc1 100644 --- a/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/utils/__tests__/transform-metadata-for-comparison.util.spec.ts +++ b/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/utils/__tests__/transform-metadata-for-comparison.util.spec.ts @@ -21,7 +21,7 @@ describe('transformMetadataForComparison', () => { { name: 'Test2', value: { c: 3 }, extra: 'keepMe' }, ]; const result = transformMetadataForComparison(input, { - propertiesToIgnore: ['ignored'], + shouldIgnoreProperty: (property) => ['ignored'].includes(property), propertiesToStringify: ['value'], keyFactory: (datum) => datum.name, }); diff --git a/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/utils/transform-metadata-for-comparison.util.ts b/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/utils/transform-metadata-for-comparison.util.ts index 963b568125..3c55aa1d02 100644 --- a/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/utils/transform-metadata-for-comparison.util.ts +++ b/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/utils/transform-metadata-for-comparison.util.ts @@ -8,7 +8,7 @@ type TransformToString = { export function transformMetadataForComparison( fieldMetadataCollection: T[], options: { - propertiesToIgnore?: readonly Keys[]; + shouldIgnoreProperty?: (property: string, originalMetadata?: T) => boolean; propertiesToStringify?: readonly Keys[]; keyFactory: (datum: T) => string; }, @@ -18,7 +18,7 @@ export function transformMetadataForComparison( export function transformMetadataForComparison( fieldMetadataCollection: T, options: { - propertiesToIgnore?: readonly Keys[]; + shouldIgnoreProperty?: (property: string, originalMetadata?: T) => boolean; propertiesToStringify?: readonly Keys[]; }, ): TransformToString; @@ -26,13 +26,11 @@ export function transformMetadataForComparison( export function transformMetadataForComparison( metadata: T[] | T, options: { - propertiesToIgnore?: readonly Keys[]; + shouldIgnoreProperty?: (property: string, originalMetadata?: T) => boolean; propertiesToStringify?: readonly Keys[]; keyFactory?: (datum: T) => string; }, ): Record> | TransformToString { - const propertiesToIgnore = (options.propertiesToIgnore ?? - []) as readonly string[]; const propertiesToStringify = (options.propertiesToStringify ?? []) as readonly string[]; @@ -40,7 +38,10 @@ export function transformMetadataForComparison( const transformedField = {} as TransformToString; for (const property in datum) { - if (propertiesToIgnore.includes(property)) { + if ( + options.shouldIgnoreProperty && + options.shouldIgnoreProperty(property, datum) + ) { continue; } if ( diff --git a/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/workspace-field.comparator.ts b/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/workspace-field.comparator.ts index 14decf1784..0b85ea648f 100644 --- a/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/workspace-field.comparator.ts +++ b/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/workspace-field.comparator.ts @@ -11,15 +11,16 @@ import { PartialObjectMetadata } from 'src/workspace/workspace-sync-metadata/int import { ObjectMetadataEntity } from 'src/metadata/object-metadata/object-metadata.entity'; import { transformMetadataForComparison } from 'src/workspace/workspace-sync-metadata/comparators/utils/transform-metadata-for-comparison.util'; +import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity'; -const fieldPropertiesToIgnore = [ +const commonFieldPropertiesToIgnore = [ 'id', 'createdAt', 'updatedAt', 'objectMetadataId', 'isActive', 'options', -] as const; +]; const fieldPropertiesToStringify = ['targetColumnMap', 'defaultValue'] as const; @@ -36,13 +37,28 @@ export class WorkspaceFieldComparator { string, Partial > = {}; + // Double security to only compare non-custom fields const filteredOriginalFieldCollection = originalObjectMetadata.fields.filter((field) => !field.isCustom); const originalFieldMetadataMap = transformMetadataForComparison( filteredOriginalFieldCollection, { - propertiesToIgnore: fieldPropertiesToIgnore, + shouldIgnoreProperty: (property, originalMetadata) => { + if (commonFieldPropertiesToIgnore.includes(property)) { + return true; + } + + if ( + originalMetadata && + property === 'defaultValue' && + originalMetadata.type === FieldMetadataType.SELECT + ) { + return true; + } + + return false; + }, propertiesToStringify: fieldPropertiesToStringify, keyFactory(datum) { return datum.name; @@ -52,7 +68,21 @@ export class WorkspaceFieldComparator { const standardFieldMetadataMap = transformMetadataForComparison( standardObjectMetadata.fields, { - propertiesToIgnore: ['options'], + shouldIgnoreProperty: (property, originalMetadata) => { + if (['options'].includes(property)) { + return true; + } + + if ( + originalMetadata && + property === 'defaultValue' && + originalMetadata.type === FieldMetadataType.SELECT + ) { + return true; + } + + return false; + }, propertiesToStringify: fieldPropertiesToStringify, keyFactory(datum) { return datum.name; diff --git a/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/workspace-object.comparator.ts b/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/workspace-object.comparator.ts index bcbd562703..45bd6ce181 100644 --- a/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/workspace-object.comparator.ts +++ b/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/workspace-object.comparator.ts @@ -20,7 +20,7 @@ const objectPropertiesToIgnore = [ 'imageIdentifierFieldMetadataId', 'isActive', 'fields', -] as const; +]; @Injectable() export class WorkspaceObjectComparator { @@ -44,7 +44,8 @@ export class WorkspaceObjectComparator { const partialOriginalObjectMetadata = transformMetadataForComparison( originalObjectMetadata, { - propertiesToIgnore: objectPropertiesToIgnore, + shouldIgnoreProperty: (property) => + objectPropertiesToIgnore.includes(property), }, ); diff --git a/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/workspace-relation.comparator.ts b/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/workspace-relation.comparator.ts index 33f5d7c3ac..9982c27ade 100644 --- a/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/workspace-relation.comparator.ts +++ b/packages/twenty-server/src/workspace/workspace-sync-metadata/comparators/workspace-relation.comparator.ts @@ -10,7 +10,7 @@ import { import { RelationMetadataEntity } from 'src/metadata/relation-metadata/relation-metadata.entity'; import { transformMetadataForComparison } from 'src/workspace/workspace-sync-metadata/comparators/utils/transform-metadata-for-comparison.util'; -const relationPropertiesToIgnore = ['createdAt', 'updatedAt'] as const; +const relationPropertiesToIgnore = ['createdAt', 'updatedAt']; const relationPropertiesToUpdate = ['onDeleteAction']; @Injectable() @@ -38,7 +38,8 @@ export class WorkspaceRelationComparator { const originalRelationMetadataMap = transformMetadataForComparison( originalRelationMetadataCollection, { - propertiesToIgnore: relationPropertiesToIgnore, + shouldIgnoreProperty: (property) => + relationPropertiesToIgnore.includes(property), keyFactory(relationMetadata) { return `${relationMetadata.fromObjectMetadataId}->${relationMetadata.fromFieldMetadataId}`; },