Spread in parent and requires FlatEntity.__universal (#17753)
# Introduction Requiring the spreaded `__universal` record that aggregates all the universal identifier ( relations fk and aggregators ) of an entity to its root It's blockin for https://github.com/twentyhq/twenty/pull/17687 to be finalized because if we don't we would have to migrated all related entities at once in order for them to always have the universal properties ## `resolveEntityRelationUniversalIdentifiers` Introduced `resolveEntityRelationUniversalIdentifiers` a centralized utility that resolves foreign key IDs to universal identifiers using ALL_METADATA_RELATIONS metadata. It provides strict typing for both input (foreign keys) and output (universal identifiers), with nullability dynamically inferred from entity relation types. Strictly and dynamically typed for both output and input To do so added a new type and const/runtime grain to ALL_METADATA_RELATIONS `isNullable`to many-to-one entries, derived from the entity relation property types. And fixed incorrectly typed typeorm entities ### Usage ```ts const { availabilityObjectMetadataUniversalIdentifier, frontComponentUniversalIdentifier, } = resolveEntityRelationUniversalIdentifiers({ metadataName: 'commandMenuItem', foreignKeyValues: { availabilityObjectMetadataId: createCommandMenuItemInput.availabilityObjectMetadataId, frontComponentId: createCommandMenuItemInput.frontComponentId, }, flatEntityMaps: { flatObjectMetadataMaps, flatFrontComponentMaps }, }); ```
This commit is contained in:
+21
-4
@@ -1,6 +1,9 @@
|
||||
import { trimAndRemoveDuplicatedWhitespacesFromObjectStringProperties } from 'twenty-shared/utils';
|
||||
import { v4 } from 'uuid';
|
||||
|
||||
import { type FlatApplication } from 'src/engine/core-modules/application/types/flat-application.type';
|
||||
import { type AllFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/all-flat-entity-maps.type';
|
||||
import { resolveEntityRelationUniversalIdentifiers } from 'src/engine/metadata-modules/flat-entity/utils/resolve-entity-relation-universal-identifiers.util';
|
||||
import { type FlatPageLayout } from 'src/engine/metadata-modules/flat-page-layout/types/flat-page-layout.type';
|
||||
import { type CreatePageLayoutInput } from 'src/engine/metadata-modules/page-layout/dtos/inputs/create-page-layout.input';
|
||||
import { PageLayoutType } from 'src/engine/metadata-modules/page-layout/enums/page-layout-type.enum';
|
||||
@@ -8,13 +11,14 @@ import { PageLayoutType } from 'src/engine/metadata-modules/page-layout/enums/pa
|
||||
export type FromCreatePageLayoutInputToFlatPageLayoutToCreateArgs = {
|
||||
createPageLayoutInput: CreatePageLayoutInput;
|
||||
workspaceId: string;
|
||||
workspaceCustomApplicationId: string;
|
||||
};
|
||||
flatApplication: FlatApplication;
|
||||
} & Pick<AllFlatEntityMaps, 'flatObjectMetadataMaps'>;
|
||||
|
||||
export const fromCreatePageLayoutInputToFlatPageLayoutToCreate = ({
|
||||
createPageLayoutInput: rawCreatePageLayoutInput,
|
||||
workspaceId,
|
||||
workspaceCustomApplicationId,
|
||||
flatApplication,
|
||||
flatObjectMetadataMaps,
|
||||
}: FromCreatePageLayoutInputToFlatPageLayoutToCreateArgs): FlatPageLayout => {
|
||||
const createPageLayoutInput =
|
||||
trimAndRemoveDuplicatedWhitespacesFromObjectStringProperties(
|
||||
@@ -25,18 +29,31 @@ export const fromCreatePageLayoutInputToFlatPageLayoutToCreate = ({
|
||||
const createdAt = new Date().toISOString();
|
||||
const pageLayoutId = v4();
|
||||
|
||||
const { objectMetadataUniversalIdentifier } =
|
||||
resolveEntityRelationUniversalIdentifiers({
|
||||
metadataName: 'pageLayout',
|
||||
foreignKeyValues: {
|
||||
objectMetadataId: createPageLayoutInput.objectMetadataId,
|
||||
},
|
||||
flatEntityMaps: { flatObjectMetadataMaps },
|
||||
});
|
||||
|
||||
return {
|
||||
id: pageLayoutId,
|
||||
name: createPageLayoutInput.name,
|
||||
type: createPageLayoutInput.type ?? PageLayoutType.RECORD_PAGE,
|
||||
objectMetadataId: createPageLayoutInput.objectMetadataId ?? null,
|
||||
objectMetadataUniversalIdentifier,
|
||||
workspaceId,
|
||||
createdAt,
|
||||
updatedAt: createdAt,
|
||||
deletedAt: null,
|
||||
universalIdentifier: pageLayoutId,
|
||||
applicationId: workspaceCustomApplicationId,
|
||||
applicationId: flatApplication.id,
|
||||
applicationUniversalIdentifier: flatApplication.universalIdentifier,
|
||||
tabIds: [],
|
||||
tabUniversalIdentifiers: [],
|
||||
defaultTabToFocusOnMobileAndSidePanelId: null,
|
||||
defaultTabToFocusOnMobileAndSidePanelUniversalIdentifier: null,
|
||||
};
|
||||
};
|
||||
|
||||
+22
-3
@@ -4,8 +4,10 @@ import {
|
||||
isDefined,
|
||||
} from 'twenty-shared/utils';
|
||||
|
||||
import { FLAT_PAGE_LAYOUT_EDITABLE_PROPERTIES } from 'src/engine/metadata-modules/flat-page-layout/constants/flat-page-layout-editable-properties.constant';
|
||||
import { type AllFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/all-flat-entity-maps.type';
|
||||
import { findFlatEntityByIdInFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/utils/find-flat-entity-by-id-in-flat-entity-maps.util';
|
||||
import { resolveEntityRelationUniversalIdentifiers } from 'src/engine/metadata-modules/flat-entity/utils/resolve-entity-relation-universal-identifiers.util';
|
||||
import { FLAT_PAGE_LAYOUT_EDITABLE_PROPERTIES } from 'src/engine/metadata-modules/flat-page-layout/constants/flat-page-layout-editable-properties.constant';
|
||||
import { type FlatPageLayoutMaps } from 'src/engine/metadata-modules/flat-page-layout/types/flat-page-layout-maps.type';
|
||||
import { type FlatPageLayout } from 'src/engine/metadata-modules/flat-page-layout/types/flat-page-layout.type';
|
||||
import { type UpdatePageLayoutInput } from 'src/engine/metadata-modules/page-layout/dtos/inputs/update-page-layout.input';
|
||||
@@ -23,10 +25,11 @@ export type UpdatePageLayoutInputWithId = {
|
||||
export const fromUpdatePageLayoutInputToFlatPageLayoutToUpdateOrThrow = ({
|
||||
updatePageLayoutInput: rawUpdatePageLayoutInput,
|
||||
flatPageLayoutMaps,
|
||||
flatObjectMetadataMaps,
|
||||
}: {
|
||||
updatePageLayoutInput: UpdatePageLayoutInputWithId;
|
||||
flatPageLayoutMaps: FlatPageLayoutMaps;
|
||||
}): FlatPageLayout => {
|
||||
} & Pick<AllFlatEntityMaps, 'flatObjectMetadataMaps'>): FlatPageLayout => {
|
||||
const { id: pageLayoutToUpdateId } = extractAndSanitizeObjectStringFields(
|
||||
rawUpdatePageLayoutInput,
|
||||
['id'],
|
||||
@@ -49,9 +52,25 @@ export const fromUpdatePageLayoutInputToFlatPageLayoutToUpdateOrThrow = ({
|
||||
FLAT_PAGE_LAYOUT_EDITABLE_PROPERTIES,
|
||||
);
|
||||
|
||||
return mergeUpdateInExistingRecord({
|
||||
const flatPageLayoutToUpdate = mergeUpdateInExistingRecord({
|
||||
existing: existingFlatPageLayoutToUpdate,
|
||||
properties: FLAT_PAGE_LAYOUT_EDITABLE_PROPERTIES,
|
||||
update: updatedEditableFieldProperties,
|
||||
});
|
||||
|
||||
if (updatedEditableFieldProperties.objectMetadataId !== undefined) {
|
||||
const { objectMetadataUniversalIdentifier } =
|
||||
resolveEntityRelationUniversalIdentifiers({
|
||||
metadataName: 'pageLayout',
|
||||
foreignKeyValues: {
|
||||
objectMetadataId: flatPageLayoutToUpdate.objectMetadataId,
|
||||
},
|
||||
flatEntityMaps: { flatObjectMetadataMaps },
|
||||
});
|
||||
|
||||
flatPageLayoutToUpdate.objectMetadataUniversalIdentifier =
|
||||
objectMetadataUniversalIdentifier;
|
||||
}
|
||||
|
||||
return flatPageLayoutToUpdate;
|
||||
};
|
||||
|
||||
+6
-9
@@ -70,14 +70,11 @@ export const transformPageLayoutEntityToFlatPageLayout = ({
|
||||
tabIds: pageLayoutEntity.tabs.map((tab) => tab.id),
|
||||
defaultTabToFocusOnMobileAndSidePanelId:
|
||||
pageLayoutEntity.defaultTabToFocusOnMobileAndSidePanelId,
|
||||
__universal: {
|
||||
universalIdentifier: pageLayoutEntity.universalIdentifier,
|
||||
applicationUniversalIdentifier,
|
||||
objectMetadataUniversalIdentifier,
|
||||
tabUniversalIdentifiers: pageLayoutEntity.tabs.map(
|
||||
(tab) => tab.universalIdentifier,
|
||||
),
|
||||
defaultTabToFocusOnMobileAndSidePanelUniversalIdentifier,
|
||||
},
|
||||
applicationUniversalIdentifier,
|
||||
objectMetadataUniversalIdentifier,
|
||||
tabUniversalIdentifiers: pageLayoutEntity.tabs.map(
|
||||
(tab) => tab.universalIdentifier,
|
||||
),
|
||||
defaultTabToFocusOnMobileAndSidePanelUniversalIdentifier,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user