[v2_FIX] Update standard object/field (#15233)

# Introduction
Refactoring the standard overrides dispatcher to only pass over fields
to has to be dispatched in the standardOverrides entry and let the other
side effects resulting from out of standard overrides mutation trigger

Related to https://github.com/twentyhq/core-team-issues/issues/1753

## This allows
- standard field settings, options etc updates and so on

## Remark
- Determine what we should do on object deactivation ( right now in
production we can still access deactivated object relation properties
and so on e.g deactivate opportunities still accessible from a view
field on company ( still have to re-create it as it has been deleted )
=> decided to leave as it is right now, `isActive` could be considered
as uiDeactivated in the end
- We should also add forbidden standard field mutations validation
inside the builder itself ( here we want to early return in the api
input transpiler too as we don't want to spread invalid side effects )
=> or in the end we could just centralize both but it will generate
several errors

## Coverage
```ts
 PASS  test/integration/metadata/suites/object-metadata/successful-update-one-standard-object-metadata.integration-spec.ts
 PASS  test/integration/metadata/suites/field-metadata/successful-update-one-standard-field-metadata.integration-spec.ts
 PASS  test/integration/metadata/suites/object-metadata/failing-update-one-standard-object-metadata.integration-spec.ts
 PASS  test/integration/metadata/suites/field-metadata/failing-update-one-standard-field-metadata.integration-spec.ts

Test Suites: 4 passed, 4 total
Tests:       18 passed, 18 total
Snapshots:   16 passed, 16 total
Time:        8.721 s, estimated 10 s
```

## Update post review
Faced a behavior where updating back the company label to its original
value would result in storing this value in the standard overrides
Refactored both field and object transpilation behavior to rather remove
the standard override value instead and let fallback on original value

Yes it's quite duplicated will factorize once we move this inside the
builder
This commit is contained in:
Paul Rastoin
2025-10-23 11:02:18 +02:00
committed by GitHub
parent 23ae9e5192
commit 2e84c11eae
22 changed files with 1472 additions and 289 deletions
@@ -1,28 +1,24 @@
import {
extractAndSanitizeObjectStringFields,
isDefined,
trimAndRemoveDuplicatedWhitespacesFromObjectStringProperties,
} from 'twenty-shared/utils';
import { ALL_FLAT_ENTITY_PROPERTIES_TO_COMPARE_AND_STRINGIFY } from 'src/engine/metadata-modules/flat-entity/constant/all-flat-entity-properties-to-compare-and-stringify.constant';
import { type AllFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/all-flat-entity-maps.type';
import { type FlatEntityPropertiesToCompare } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-properties-to-compare.type';
import { findFlatEntityByIdInFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/utils/find-flat-entity-by-id-in-flat-entity-maps.util';
import { type FlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/types/flat-field-metadata.type';
import { type FlatIndexMetadata } from 'src/engine/metadata-modules/flat-index-metadata/types/flat-index-metadata.type';
import { FLAT_OBJECT_METADATA_EDITABLE_PROPERTIES } from 'src/engine/metadata-modules/flat-object-metadata/constants/flat-object-metadata-editable-properties.constant';
import { type FlatObjectMetadata } from 'src/engine/metadata-modules/flat-object-metadata/types/flat-object-metadata.type';
import { recomputeIndexAfterFlatObjectMetadataSingularNameUpdate } from 'src/engine/metadata-modules/flat-object-metadata/utils/recompute-index-after-flat-object-metadata-singular-name-update.util';
import { recomputeViewFieldIdentifierAfterFlatObjectIdentifierUpdate } from 'src/engine/metadata-modules/flat-object-metadata/utils/recompute-view-field-identifier-after-flat-object-identifier-update.util';
import { renameRelatedMorphFieldOnObjectNamesUpdate } from 'src/engine/metadata-modules/flat-object-metadata/utils/rename-related-morph-field-on-object-names-update.util';
import { type FlatViewField } from 'src/engine/metadata-modules/flat-view-field/types/flat-view-field.type';
import { OBJECT_METADATA_STANDARD_OVERRIDES_PROPERTIES } from 'src/engine/metadata-modules/object-metadata/constants/object-metadata-standard-overrides-properties.constant';
import {
type FlatObjectMetadataUpdateSideEffects,
handleFlatObjectMetadataUpdateSideEffect,
} from 'src/engine/metadata-modules/flat-object-metadata/utils/handle-flat-object-metadata-update-side-effect.util';
import { sanitizeRawUpdateObjectInput } from 'src/engine/metadata-modules/flat-object-metadata/utils/sanitize-raw-update-object-input';
import { type UpdateOneObjectInput } from 'src/engine/metadata-modules/object-metadata/dtos/update-object.input';
import {
ObjectMetadataException,
ObjectMetadataExceptionCode,
} from 'src/engine/metadata-modules/object-metadata/object-metadata.exception';
import { type ObjectMetadataStandardOverridesProperties } from 'src/engine/metadata-modules/object-metadata/types/object-metadata-standard-overrides-properties.types';
import { isStandardMetadata } from 'src/engine/metadata-modules/utils/is-standard-metadata.util';
import { mergeUpdateInExistingRecord } from 'src/utils/merge-update-in-existing-record.util';
type FromUpdateObjectInputToFlatObjectMetadataArgs = {
updateObjectInput: UpdateOneObjectInput;
@@ -35,24 +31,6 @@ type FromUpdateObjectInputToFlatObjectMetadataArgs = {
| 'flatViewMaps'
>;
const objectMetadataEditableProperties =
ALL_FLAT_ENTITY_PROPERTIES_TO_COMPARE_AND_STRINGIFY.objectMetadata.propertiesToCompare.filter(
(
property,
): property is Exclude<
FlatEntityPropertiesToCompare<'objectMetadata'>,
'standardOverrides'
> => property !== 'standardOverrides',
);
type UpdatedFlatObjectAndRelatedFlatEntities = {
flatObjectMetadata: FlatObjectMetadata;
otherObjectFlatFieldMetadataToUpdate: FlatFieldMetadata[];
flatIndexMetadataToUpdate: FlatIndexMetadata[];
flatViewFieldToUpdate: FlatViewField[];
flatViewFieldToCreate: FlatViewField[];
};
export const fromUpdateObjectInputToFlatObjectMetadataAndRelatedFlatEntities =
({
flatObjectMetadataMaps: existingFlatObjectMetadataMaps,
@@ -61,171 +39,65 @@ export const fromUpdateObjectInputToFlatObjectMetadataAndRelatedFlatEntities =
flatFieldMetadataMaps,
flatViewFieldMaps,
flatViewMaps,
}: FromUpdateObjectInputToFlatObjectMetadataArgs): UpdatedFlatObjectAndRelatedFlatEntities => {
}: FromUpdateObjectInputToFlatObjectMetadataArgs): FlatObjectMetadataUpdateSideEffects & {
flatObjectMetadataToUpdate: FlatObjectMetadata;
} => {
const { id: objectMetadataIdToUpdate } =
trimAndRemoveDuplicatedWhitespacesFromObjectStringProperties(
rawUpdateObjectInput,
['id'],
);
const updatedEditableObjectProperties =
extractAndSanitizeObjectStringFields(
rawUpdateObjectInput.update,
objectMetadataEditableProperties,
);
const flatObjectMetadataToUpdate = findFlatEntityByIdInFlatEntityMaps({
const existingFlatObjectMetadata = findFlatEntityByIdInFlatEntityMaps({
flatEntityMaps: existingFlatObjectMetadataMaps,
flatEntityId: objectMetadataIdToUpdate,
});
if (!isDefined(flatObjectMetadataToUpdate)) {
if (!isDefined(existingFlatObjectMetadata)) {
throw new ObjectMetadataException(
'Object to update not found',
ObjectMetadataExceptionCode.OBJECT_METADATA_NOT_FOUND,
);
}
if (isStandardMetadata(flatObjectMetadataToUpdate)) {
const invalidUpdatedProperties = Object.keys(
updatedEditableObjectProperties,
).filter(
(property) =>
!OBJECT_METADATA_STANDARD_OVERRIDES_PROPERTIES.includes(
property as ObjectMetadataStandardOverridesProperties,
),
);
const isStandardObject = isStandardMetadata(existingFlatObjectMetadata);
const { standardOverrides, updatedEditableObjectProperties } =
sanitizeRawUpdateObjectInput({
existingFlatObjectMetadata,
rawUpdateObjectInput,
});
if (invalidUpdatedProperties.length > 0) {
throw new ObjectMetadataException(
`Cannot edit standard object metadata properties: ${invalidUpdatedProperties.join(', ')}`,
ObjectMetadataExceptionCode.INVALID_OBJECT_INPUT,
);
}
const updatedStandardFlatObjectMetadata =
OBJECT_METADATA_STANDARD_OVERRIDES_PROPERTIES.reduce(
(acc, property) => {
const isPropertyUpdated =
updatedEditableObjectProperties[property] !== undefined;
return {
...acc,
standardOverrides: {
...acc.standardOverrides,
...(isPropertyUpdated
? { [property]: updatedEditableObjectProperties[property] }
: {}),
},
};
},
flatObjectMetadataToUpdate,
);
return {
flatObjectMetadata: updatedStandardFlatObjectMetadata,
otherObjectFlatFieldMetadataToUpdate: [],
flatIndexMetadataToUpdate: [],
flatViewFieldToUpdate: [],
flatViewFieldToCreate: [],
};
}
const initialAccumulator: UpdatedFlatObjectAndRelatedFlatEntities = {
flatObjectMetadata: flatObjectMetadataToUpdate,
otherObjectFlatFieldMetadataToUpdate: [],
flatIndexMetadataToUpdate: [],
flatViewFieldToUpdate: [],
flatViewFieldToCreate: [],
const toFlatObjectMetadata = {
...mergeUpdateInExistingRecord({
existing: existingFlatObjectMetadata,
properties:
FLAT_OBJECT_METADATA_EDITABLE_PROPERTIES[
isStandardObject ? 'standard' : 'custom'
],
update: updatedEditableObjectProperties,
}),
standardOverrides,
};
return objectMetadataEditableProperties.reduce<UpdatedFlatObjectAndRelatedFlatEntities>(
(
{
flatObjectMetadata,
otherObjectFlatFieldMetadataToUpdate,
flatIndexMetadataToUpdate,
flatViewFieldToUpdate,
flatViewFieldToCreate,
},
property,
) => {
const updatedPropertyValue = updatedEditableObjectProperties[property];
const isPropertyUpdated =
updatedPropertyValue !== undefined &&
flatObjectMetadata[property] !== updatedPropertyValue;
const {
flatIndexMetadatasToUpdate,
flatViewFieldsToCreate,
flatViewFieldsToUpdate,
otherObjectFlatFieldMetadatasToUpdate,
} = handleFlatObjectMetadataUpdateSideEffect({
fromFlatObjectMetadata: existingFlatObjectMetadata,
toFlatObjectMetadata,
flatFieldMetadataMaps,
flatIndexMaps,
flatViewFieldMaps,
flatViewMaps,
});
if (!isPropertyUpdated) {
return {
flatObjectMetadata,
otherObjectFlatFieldMetadataToUpdate,
flatIndexMetadataToUpdate,
flatViewFieldToUpdate,
flatViewFieldToCreate,
};
}
const updatedFlatObjectMetadata = {
...flatObjectMetadata,
[property]: updatedPropertyValue,
};
const newUpdatedOtherObjectFlatFieldMetadatas =
property === 'nameSingular' || property === 'namePlural'
? renameRelatedMorphFieldOnObjectNamesUpdate({
flatFieldMetadataMaps,
fromFlatObjectMetadata: updatedFlatObjectMetadata,
toFlatObjectMetadata: updatedFlatObjectMetadata,
})
: [];
const newUpdatedFlatIndexMetadatas =
property === 'nameSingular'
? recomputeIndexAfterFlatObjectMetadataSingularNameUpdate({
flatFieldMetadataMaps,
existingFlatObjectMetadata: flatObjectMetadataToUpdate,
flatIndexMaps,
updatedSingularName: updatedFlatObjectMetadata.nameSingular,
})
: [];
const {
flatViewFieldToCreate: newFlatViewFieldToCreate,
flatViewFieldToUpdate: newFlatViewFieldToUpdate,
} =
property === 'labelIdentifierFieldMetadataId' &&
isDefined(updatedFlatObjectMetadata.labelIdentifierFieldMetadataId)
? recomputeViewFieldIdentifierAfterFlatObjectIdentifierUpdate({
existingFlatObjectMetadata: flatObjectMetadataToUpdate,
flatViewFieldMaps,
flatViewMaps,
updatedLabelIdentifierFieldMetadataId:
updatedFlatObjectMetadata.labelIdentifierFieldMetadataId,
})
: {
flatViewFieldToCreate: [],
flatViewFieldToUpdate: [],
};
return {
flatObjectMetadata: updatedFlatObjectMetadata,
otherObjectFlatFieldMetadataToUpdate: [
...otherObjectFlatFieldMetadataToUpdate,
...newUpdatedOtherObjectFlatFieldMetadatas,
],
flatViewFieldToUpdate: [
...flatViewFieldToUpdate,
...newFlatViewFieldToUpdate,
],
flatViewFieldToCreate: [
...flatViewFieldToCreate,
...newFlatViewFieldToCreate,
],
flatIndexMetadataToUpdate: [
...flatIndexMetadataToUpdate,
...newUpdatedFlatIndexMetadatas,
],
};
},
initialAccumulator,
);
return {
flatIndexMetadatasToUpdate,
flatObjectMetadataToUpdate: toFlatObjectMetadata,
flatViewFieldsToCreate,
flatViewFieldsToUpdate,
otherObjectFlatFieldMetadatasToUpdate,
};
};
@@ -0,0 +1,83 @@
import { type FromTo } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
import { type AllFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/all-flat-entity-maps.type';
import { type FlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/types/flat-field-metadata.type';
import { type FlatIndexMetadata } from 'src/engine/metadata-modules/flat-index-metadata/types/flat-index-metadata.type';
import { type FlatObjectMetadata } from 'src/engine/metadata-modules/flat-object-metadata/types/flat-object-metadata.type';
import { recomputeIndexAfterFlatObjectMetadataSingularNameUpdate } from 'src/engine/metadata-modules/flat-object-metadata/utils/recompute-index-after-flat-object-metadata-singular-name-update.util';
import { recomputeViewFieldIdentifierAfterFlatObjectIdentifierUpdate } from 'src/engine/metadata-modules/flat-object-metadata/utils/recompute-view-field-identifier-after-flat-object-identifier-update.util';
import { renameRelatedMorphFieldOnObjectNamesUpdate } from 'src/engine/metadata-modules/flat-object-metadata/utils/rename-related-morph-field-on-object-names-update.util';
import { type FlatViewField } from 'src/engine/metadata-modules/flat-view-field/types/flat-view-field.type';
export type FlatObjectMetadataUpdateSideEffects = {
otherObjectFlatFieldMetadatasToUpdate: FlatFieldMetadata[];
flatViewFieldsToUpdate: FlatViewField[];
flatViewFieldsToCreate: FlatViewField[];
flatIndexMetadatasToUpdate: FlatIndexMetadata[];
};
type HandleFlatObjectMetadataUpdateSideEffectArgs = FromTo<
FlatObjectMetadata,
'flatObjectMetadata'
> &
Pick<
AllFlatEntityMaps,
| 'flatFieldMetadataMaps'
| 'flatViewFieldMaps'
| 'flatIndexMaps'
| 'flatViewMaps'
>;
export const handleFlatObjectMetadataUpdateSideEffect = ({
flatIndexMaps,
flatFieldMetadataMaps,
flatViewFieldMaps,
flatViewMaps,
fromFlatObjectMetadata,
toFlatObjectMetadata,
}: HandleFlatObjectMetadataUpdateSideEffectArgs): FlatObjectMetadataUpdateSideEffects => {
const otherObjectFlatFieldMetadatasToUpdate =
fromFlatObjectMetadata.nameSingular !== toFlatObjectMetadata.nameSingular ||
fromFlatObjectMetadata.namePlural !== toFlatObjectMetadata.namePlural
? renameRelatedMorphFieldOnObjectNamesUpdate({
flatFieldMetadataMaps,
fromFlatObjectMetadata,
toFlatObjectMetadata,
})
: [];
const flatIndexMetadatasToUpdate =
fromFlatObjectMetadata.nameSingular !== toFlatObjectMetadata.nameSingular
? recomputeIndexAfterFlatObjectMetadataSingularNameUpdate({
flatFieldMetadataMaps,
existingFlatObjectMetadata: fromFlatObjectMetadata,
flatIndexMaps,
updatedSingularName: toFlatObjectMetadata.nameSingular,
})
: [];
const { flatViewFieldsToCreate, flatViewFieldsToUpdate } =
fromFlatObjectMetadata.labelIdentifierFieldMetadataId !==
toFlatObjectMetadata.labelIdentifierFieldMetadataId &&
isDefined(toFlatObjectMetadata.labelIdentifierFieldMetadataId) &&
isDefined(fromFlatObjectMetadata.labelIdentifierFieldMetadataId)
? recomputeViewFieldIdentifierAfterFlatObjectIdentifierUpdate({
existingFlatObjectMetadata: fromFlatObjectMetadata,
flatViewFieldMaps,
flatViewMaps,
updatedLabelIdentifierFieldMetadataId:
toFlatObjectMetadata.labelIdentifierFieldMetadataId,
})
: {
flatViewFieldsToCreate: [],
flatViewFieldsToUpdate: [],
};
return {
flatIndexMetadatasToUpdate,
flatViewFieldsToCreate,
flatViewFieldsToUpdate,
otherObjectFlatFieldMetadatasToUpdate,
};
};
@@ -13,8 +13,8 @@ type RecomputeViewFieldIdentifierAfterFlatObjectIdentifierUpdateArgs = {
} & Pick<AllFlatEntityMaps, 'flatViewFieldMaps' | 'flatViewMaps'>;
type FlatViewFieldToCreateAndUpdate = {
flatViewFieldToCreate: FlatViewField[];
flatViewFieldToUpdate: FlatViewField[];
flatViewFieldsToCreate: FlatViewField[];
flatViewFieldsToUpdate: FlatViewField[];
};
export const recomputeViewFieldIdentifierAfterFlatObjectIdentifierUpdate = ({
existingFlatObjectMetadata,
@@ -28,8 +28,8 @@ export const recomputeViewFieldIdentifierAfterFlatObjectIdentifierUpdate = ({
});
const accumulator: FlatViewFieldToCreateAndUpdate = {
flatViewFieldToCreate: [],
flatViewFieldToUpdate: [],
flatViewFieldsToCreate: [],
flatViewFieldsToUpdate: [],
};
for (const flatView of flatViews) {
@@ -69,7 +69,7 @@ export const recomputeViewFieldIdentifierAfterFlatObjectIdentifierUpdate = ({
applicationId: existingFlatObjectMetadata.applicationId,
};
accumulator.flatViewFieldToCreate.push(flatViewFieldToCreate);
accumulator.flatViewFieldsToCreate.push(flatViewFieldToCreate);
} else if (
labelMetadataIdentifierViewField.position > lowestViewFieldPosition
) {
@@ -78,7 +78,7 @@ export const recomputeViewFieldIdentifierAfterFlatObjectIdentifierUpdate = ({
position: lowestViewFieldPosition - 1,
};
accumulator.flatViewFieldToUpdate.push(updatedFlatViewField);
accumulator.flatViewFieldsToUpdate.push(updatedFlatViewField);
}
}
@@ -0,0 +1,108 @@
import {
extractAndSanitizeObjectStringFields,
isDefined,
} from 'twenty-shared/utils';
import { FLAT_OBJECT_METADATA_EDITABLE_PROPERTIES } from 'src/engine/metadata-modules/flat-object-metadata/constants/flat-object-metadata-editable-properties.constant';
import { type FlatObjectMetadata } from 'src/engine/metadata-modules/flat-object-metadata/types/flat-object-metadata.type';
import { OBJECT_METADATA_STANDARD_OVERRIDES_PROPERTIES } from 'src/engine/metadata-modules/object-metadata/constants/object-metadata-standard-overrides-properties.constant';
import { type UpdateOneObjectInput } from 'src/engine/metadata-modules/object-metadata/dtos/update-object.input';
import {
ObjectMetadataException,
ObjectMetadataExceptionCode,
} from 'src/engine/metadata-modules/object-metadata/object-metadata.exception';
import { type ObjectMetadataStandardOverridesProperties } from 'src/engine/metadata-modules/object-metadata/types/object-metadata-standard-overrides-properties.types';
import { isStandardMetadata } from 'src/engine/metadata-modules/utils/is-standard-metadata.util';
type SanitizeRawUpdateObjectInputArgs = {
rawUpdateObjectInput: UpdateOneObjectInput;
existingFlatObjectMetadata: FlatObjectMetadata;
};
export const sanitizeRawUpdateObjectInput = ({
existingFlatObjectMetadata,
rawUpdateObjectInput,
}: SanitizeRawUpdateObjectInputArgs) => {
const isStandardObject = isStandardMetadata(existingFlatObjectMetadata);
const updatedEditableObjectProperties = extractAndSanitizeObjectStringFields(
rawUpdateObjectInput.update,
[
...new Set([
...FLAT_OBJECT_METADATA_EDITABLE_PROPERTIES.standard,
...FLAT_OBJECT_METADATA_EDITABLE_PROPERTIES.custom,
]),
],
);
if (!isStandardObject) {
return {
updatedEditableObjectProperties,
standardOverrides: null,
};
}
const invalidUpdatedProperties = Object.keys(
updatedEditableObjectProperties,
).filter(
(property) =>
!FLAT_OBJECT_METADATA_EDITABLE_PROPERTIES.standard.includes(
property as ObjectMetadataStandardOverridesProperties,
),
);
if (invalidUpdatedProperties.length > 0) {
throw new ObjectMetadataException(
`Cannot edit standard object metadata properties: ${invalidUpdatedProperties.join(', ')}`,
ObjectMetadataExceptionCode.INVALID_OBJECT_INPUT,
);
}
const standardOverrides =
OBJECT_METADATA_STANDARD_OVERRIDES_PROPERTIES.reduce(
(standardOverrides, property) => {
const propertyValue = updatedEditableObjectProperties[property];
const isPropertyUpdated =
updatedEditableObjectProperties[property] !== undefined;
if (!isPropertyUpdated) {
return standardOverrides;
}
delete updatedEditableObjectProperties[property];
if (propertyValue === existingFlatObjectMetadata[property]) {
if (
isDefined(standardOverrides) &&
Object.prototype.hasOwnProperty.call(standardOverrides, property)
) {
const { [property]: _, ...restOverrides } = standardOverrides;
return restOverrides;
}
return standardOverrides;
}
return {
...standardOverrides,
[property]: propertyValue,
};
},
existingFlatObjectMetadata.standardOverrides,
);
if (
isDefined(standardOverrides) &&
Object.keys(standardOverrides).length === 0
) {
return {
standardOverrides: null,
updatedEditableObjectProperties,
};
}
return {
standardOverrides,
updatedEditableObjectProperties,
};
};