Factorize from entity to flat entity utils (#21972)

## What

Factorizes the two responsibilities that were copy‑pasted across every
`from-<entity>-entity-to-flat-<entity>` util into two reusable tools.

### `fromEntityToScalarEntity`
Projects a TypeORM entity into its scalar flat shape using an
**allow‑list** driven by
`ALL_ENTITY_PROPERTIES_CONFIGURATION_BY_METADATA_NAME` (plus the base
columns `id`/`workspaceId`/`applicationId`/`universalIdentifier`). Only
registered scalar columns are forwarded, `Date`s are serialized to ISO
strings, and absent values are normalized to `null`. Replaces the
previous deny‑list (`removePropertiesFromRecord`) approach, so
unregistered/deprecated columns can no longer silently leak into the
flat entity.

### `resolveManyToOneRelationIdsToUniversalIdentifiers`
Resolves an entity's many‑to‑one foreign keys to their universal
identifiers, driven by `ALL_MANY_TO_ONE_METADATA_RELATIONS`. Handles the
always‑present `application`, nullable relations, and throws a
`FlatEntityMapsException` when a referenced id is missing from its
identifier map. Mirrors `resolveUniversalRelationIdentifiersToIds` in
the opposite direction.

Each `from-<entity>` util now reduces to: scalar spread + relation
spread (+ explicit one‑to‑many id/universalIdentifier arrays where
applicable).

### Note
The allow‑list drops `isUIReadOnly` (a `WasRemovedInUpgrade` column not
in the config) from `fieldMetadata`, which is the only
integration‑snapshot change.

<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/twentyhq/twenty/pull/21972?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
This commit is contained in:
Paul Rastoin
2026-06-24 12:20:09 +02:00
committed by GitHub
parent 41d1b478b0
commit d2387430a1
34 changed files with 1106 additions and 1614 deletions
@@ -1,14 +1,11 @@
import { isDefined, removePropertiesFromRecord } from 'twenty-shared/utils';
import { isDefined } from 'twenty-shared/utils';
import {
FlatEntityMapsException,
FlatEntityMapsExceptionCode,
} from 'src/engine/metadata-modules/flat-entity/exceptions/flat-entity-maps.exception';
import { getMetadataEntityRelationProperties } from 'src/engine/metadata-modules/flat-entity/utils/get-metadata-entity-relation-properties.util';
import { fromEntityToScalarEntity } from 'src/engine/metadata-modules/flat-entity/utils/from-entity-to-scalar-entity.util';
import { type FlatPageLayoutWidget } from 'src/engine/metadata-modules/flat-page-layout-widget/types/flat-page-layout-widget.type';
import { fromPageLayoutWidgetConfigurationToUniversalConfiguration } from 'src/engine/metadata-modules/flat-page-layout-widget/utils/from-page-layout-widget-configuration-to-universal-configuration.util';
import { fromPageLayoutWidgetOverridesToUniversalOverrides } from 'src/engine/metadata-modules/flat-page-layout-widget/utils/from-page-layout-widget-overrides-to-universal-overrides.util';
import { type FromEntityToFlatEntityArgs } from 'src/engine/workspace-cache/types/from-entity-to-flat-entity-args.type';
import { resolveManyToOneRelationIdsToUniversalIdentifiers } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/utils/resolve-many-to-one-relation-ids-to-universal-identifiers.util';
type FromPageLayoutWidgetEntityToFlatPageLayoutWidgetArgs =
FromEntityToFlatEntityArgs<'pageLayoutWidget'> & {
@@ -18,64 +15,32 @@ type FromPageLayoutWidgetEntityToFlatPageLayoutWidgetArgs =
viewUniversalIdentifierById?: Partial<Record<string, string>>;
};
export const fromPageLayoutWidgetEntityToFlatPageLayoutWidget = ({
entity: pageLayoutWidgetEntity,
applicationIdToUniversalIdentifierMap,
pageLayoutTabIdToUniversalIdentifierMap,
objectMetadataIdToUniversalIdentifierMap,
fieldMetadataUniversalIdentifierById,
frontComponentUniversalIdentifierById,
viewFieldGroupUniversalIdentifierById,
viewUniversalIdentifierById,
}: FromPageLayoutWidgetEntityToFlatPageLayoutWidgetArgs): FlatPageLayoutWidget => {
const pageLayoutWidgetEntityWithoutRelations = removePropertiesFromRecord(
pageLayoutWidgetEntity,
getMetadataEntityRelationProperties('pageLayoutWidget'),
);
export const fromPageLayoutWidgetEntityToFlatPageLayoutWidget = (
args: FromPageLayoutWidgetEntityToFlatPageLayoutWidgetArgs,
): FlatPageLayoutWidget => {
const {
entity: pageLayoutWidgetEntity,
pageLayoutTabIdToUniversalIdentifierMap,
fieldMetadataUniversalIdentifierById,
frontComponentUniversalIdentifierById,
viewFieldGroupUniversalIdentifierById,
viewUniversalIdentifierById,
} = args;
const applicationUniversalIdentifier =
applicationIdToUniversalIdentifierMap.get(
pageLayoutWidgetEntity.applicationId,
);
const pageLayoutWidgetScalarEntity = fromEntityToScalarEntity({
metadataName: 'pageLayoutWidget',
entity: pageLayoutWidgetEntity,
});
if (!isDefined(applicationUniversalIdentifier)) {
throw new FlatEntityMapsException(
`Application with id ${pageLayoutWidgetEntity.applicationId} not found for pageLayoutWidget ${pageLayoutWidgetEntity.id}`,
FlatEntityMapsExceptionCode.ENTITY_NOT_FOUND,
);
}
const pageLayoutTabUniversalIdentifier =
pageLayoutTabIdToUniversalIdentifierMap.get(
pageLayoutWidgetEntity.pageLayoutTabId,
);
if (!isDefined(pageLayoutTabUniversalIdentifier)) {
throw new FlatEntityMapsException(
`PageLayoutTab with id ${pageLayoutWidgetEntity.pageLayoutTabId} not found for pageLayoutWidget ${pageLayoutWidgetEntity.id}`,
FlatEntityMapsExceptionCode.ENTITY_NOT_FOUND,
);
}
let objectMetadataUniversalIdentifier: string | null = null;
if (isDefined(pageLayoutWidgetEntity.objectMetadataId)) {
objectMetadataUniversalIdentifier =
objectMetadataIdToUniversalIdentifierMap.get(
pageLayoutWidgetEntity.objectMetadataId,
) ?? null;
if (!isDefined(objectMetadataUniversalIdentifier)) {
throw new FlatEntityMapsException(
`ObjectMetadata with id ${pageLayoutWidgetEntity.objectMetadataId} not found for pageLayoutWidget ${pageLayoutWidgetEntity.id}`,
FlatEntityMapsExceptionCode.ENTITY_NOT_FOUND,
);
}
}
const relationUniversalIdentifiers =
resolveManyToOneRelationIdsToUniversalIdentifiers({
metadataName: 'pageLayoutWidget',
...args,
});
const configurationWithUniversalIdentifiers =
fromPageLayoutWidgetConfigurationToUniversalConfiguration({
configuration: pageLayoutWidgetEntityWithoutRelations.configuration,
configuration: pageLayoutWidgetScalarEntity.configuration,
fieldMetadataUniversalIdentifierById,
frontComponentUniversalIdentifierById,
viewFieldGroupUniversalIdentifierById,
@@ -95,16 +60,8 @@ export const fromPageLayoutWidgetEntityToFlatPageLayoutWidget = ({
: null;
return {
...pageLayoutWidgetEntityWithoutRelations,
createdAt: pageLayoutWidgetEntity.createdAt.toISOString(),
updatedAt: pageLayoutWidgetEntity.updatedAt.toISOString(),
deletedAt: pageLayoutWidgetEntity.deletedAt?.toISOString() ?? null,
universalIdentifier:
pageLayoutWidgetEntityWithoutRelations.universalIdentifier,
applicationId: pageLayoutWidgetEntityWithoutRelations.applicationId,
applicationUniversalIdentifier,
pageLayoutTabUniversalIdentifier,
objectMetadataUniversalIdentifier,
...pageLayoutWidgetScalarEntity,
...relationUniversalIdentifiers,
universalConfiguration: configurationWithUniversalIdentifiers,
universalOverrides,
};