Ignore defaultValue update for select fields (#4193)

* Ignore defaultValue update for select fields

* Fix tests
This commit is contained in:
Charles Bochet 2024-02-26 18:41:29 +01:00 committed by GitHub
parent 7a437751d4
commit 6a1abba9ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 48 additions and 15 deletions

View File

@ -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,
});

View File

@ -8,7 +8,7 @@ type TransformToString<T, Keys extends keyof T> = {
export function transformMetadataForComparison<T, Keys extends keyof T>(
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<T, Keys extends keyof T>(
export function transformMetadataForComparison<T, Keys extends keyof T>(
fieldMetadataCollection: T,
options: {
propertiesToIgnore?: readonly Keys[];
shouldIgnoreProperty?: (property: string, originalMetadata?: T) => boolean;
propertiesToStringify?: readonly Keys[];
},
): TransformToString<T, Keys>;
@ -26,13 +26,11 @@ export function transformMetadataForComparison<T, Keys extends keyof T>(
export function transformMetadataForComparison<T, Keys extends keyof T>(
metadata: T[] | T,
options: {
propertiesToIgnore?: readonly Keys[];
shouldIgnoreProperty?: (property: string, originalMetadata?: T) => boolean;
propertiesToStringify?: readonly Keys[];
keyFactory?: (datum: T) => string;
},
): Record<string, TransformToString<T, Keys>> | TransformToString<T, Keys> {
const propertiesToIgnore = (options.propertiesToIgnore ??
[]) as readonly string[];
const propertiesToStringify = (options.propertiesToStringify ??
[]) as readonly string[];
@ -40,7 +38,10 @@ export function transformMetadataForComparison<T, Keys extends keyof T>(
const transformedField = {} as TransformToString<T, Keys>;
for (const property in datum) {
if (propertiesToIgnore.includes(property)) {
if (
options.shouldIgnoreProperty &&
options.shouldIgnoreProperty(property, datum)
) {
continue;
}
if (

View File

@ -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<PartialFieldMetadata>
> = {};
// 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;

View File

@ -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),
},
);

View File

@ -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}`;
},