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:
+8
@@ -12,11 +12,13 @@ import { type FlatViewGroup } from 'src/engine/metadata-modules/flat-view-group/
|
||||
|
||||
type ComputeFlatViewGroupsOnViewCreateArgs = {
|
||||
flatViewToCreateId: string;
|
||||
flatViewToCreateUniversalIdentifier: string;
|
||||
mainGroupByFieldMetadataId: string;
|
||||
} & Pick<AllFlatEntityMaps, 'flatFieldMetadataMaps'>;
|
||||
|
||||
export const computeFlatViewGroupsOnViewCreate = ({
|
||||
flatViewToCreateId,
|
||||
flatViewToCreateUniversalIdentifier,
|
||||
mainGroupByFieldMetadataId,
|
||||
flatFieldMetadataMaps,
|
||||
}: ComputeFlatViewGroupsOnViewCreateArgs): FlatViewGroup[] => {
|
||||
@@ -43,6 +45,7 @@ export const computeFlatViewGroupsOnViewCreate = ({
|
||||
id: viewGroupId,
|
||||
fieldMetadataId: mainGroupByFieldMetadata.id,
|
||||
viewId: flatViewToCreateId,
|
||||
viewUniversalIdentifier: flatViewToCreateUniversalIdentifier,
|
||||
workspaceId: mainGroupByFieldMetadata.workspaceId,
|
||||
createdAt,
|
||||
updatedAt: createdAt,
|
||||
@@ -52,6 +55,8 @@ export const computeFlatViewGroupsOnViewCreate = ({
|
||||
fieldValue: option.value,
|
||||
position: index,
|
||||
applicationId: mainGroupByFieldMetadata.applicationId,
|
||||
applicationUniversalIdentifier:
|
||||
mainGroupByFieldMetadata.applicationUniversalIdentifier,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -64,6 +69,7 @@ export const computeFlatViewGroupsOnViewCreate = ({
|
||||
flatViewGroups.push({
|
||||
id: emptyGroupId,
|
||||
viewId: flatViewToCreateId,
|
||||
viewUniversalIdentifier: flatViewToCreateUniversalIdentifier,
|
||||
workspaceId: mainGroupByFieldMetadata.workspaceId,
|
||||
createdAt,
|
||||
updatedAt: createdAt,
|
||||
@@ -73,6 +79,8 @@ export const computeFlatViewGroupsOnViewCreate = ({
|
||||
fieldValue: '',
|
||||
position: emptyGroupPosition,
|
||||
applicationId: mainGroupByFieldMetadata.applicationId,
|
||||
applicationUniversalIdentifier:
|
||||
mainGroupByFieldMetadata.applicationUniversalIdentifier,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+18
-4
@@ -1,18 +1,22 @@
|
||||
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 FlatViewGroup } from 'src/engine/metadata-modules/flat-view-group/types/flat-view-group.type';
|
||||
import { type CreateViewGroupInput } from 'src/engine/metadata-modules/view-group/dtos/inputs/create-view-group.input';
|
||||
|
||||
export const fromCreateViewGroupInputToFlatViewGroupToCreate = ({
|
||||
createViewGroupInput: rawCreateViewGroupInput,
|
||||
workspaceId,
|
||||
workspaceCustomApplicationId,
|
||||
flatApplication,
|
||||
flatViewMaps,
|
||||
}: {
|
||||
createViewGroupInput: CreateViewGroupInput;
|
||||
workspaceId: string;
|
||||
workspaceCustomApplicationId: string;
|
||||
}): FlatViewGroup => {
|
||||
flatApplication: FlatApplication;
|
||||
} & Pick<AllFlatEntityMaps, 'flatViewMaps'>): FlatViewGroup => {
|
||||
const { viewId, ...createViewGroupInput } =
|
||||
trimAndRemoveDuplicatedWhitespacesFromObjectStringProperties(
|
||||
rawCreateViewGroupInput,
|
||||
@@ -22,9 +26,18 @@ export const fromCreateViewGroupInputToFlatViewGroupToCreate = ({
|
||||
const createdAt = new Date().toISOString();
|
||||
const viewGroupId = createViewGroupInput.id ?? v4();
|
||||
|
||||
const { viewUniversalIdentifier } = resolveEntityRelationUniversalIdentifiers(
|
||||
{
|
||||
metadataName: 'viewGroup',
|
||||
foreignKeyValues: { viewId },
|
||||
flatEntityMaps: { flatViewMaps },
|
||||
},
|
||||
);
|
||||
|
||||
return {
|
||||
id: viewGroupId,
|
||||
viewId,
|
||||
viewUniversalIdentifier,
|
||||
workspaceId,
|
||||
createdAt: createdAt,
|
||||
updatedAt: createdAt,
|
||||
@@ -33,6 +46,7 @@ export const fromCreateViewGroupInputToFlatViewGroupToCreate = ({
|
||||
isVisible: createViewGroupInput.isVisible ?? true,
|
||||
fieldValue: createViewGroupInput.fieldValue,
|
||||
position: createViewGroupInput.position ?? 0,
|
||||
applicationId: workspaceCustomApplicationId,
|
||||
applicationId: flatApplication.id,
|
||||
applicationUniversalIdentifier: flatApplication.universalIdentifier,
|
||||
};
|
||||
};
|
||||
|
||||
+2
-5
@@ -45,10 +45,7 @@ export const fromViewGroupEntityToFlatViewGroup = ({
|
||||
updatedAt: viewGroupEntity.updatedAt.toISOString(),
|
||||
deletedAt: viewGroupEntity.deletedAt?.toISOString() ?? null,
|
||||
universalIdentifier: viewGroupEntityWithoutRelations.universalIdentifier,
|
||||
__universal: {
|
||||
universalIdentifier: viewGroupEntity.universalIdentifier,
|
||||
applicationUniversalIdentifier,
|
||||
viewUniversalIdentifier,
|
||||
},
|
||||
applicationUniversalIdentifier,
|
||||
viewUniversalIdentifier,
|
||||
};
|
||||
};
|
||||
|
||||
-39
@@ -1,39 +0,0 @@
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
|
||||
import { type FlatViewGroup } from 'src/engine/metadata-modules/flat-view-group/types/flat-view-group.type';
|
||||
|
||||
type FlatViewGroupsByViewId = {
|
||||
flatViewGroupRecordByViewId: Record<string, Record<string, FlatViewGroup>>;
|
||||
highestViewGroupPositionByViewId: Record<string, number>;
|
||||
};
|
||||
export const reduceFlatViewGroupsByViewId = ({
|
||||
flatViewGroups,
|
||||
}: {
|
||||
flatViewGroups: FlatViewGroup[];
|
||||
}): FlatViewGroupsByViewId => {
|
||||
const initialAccumulator: FlatViewGroupsByViewId = {
|
||||
flatViewGroupRecordByViewId: {},
|
||||
highestViewGroupPositionByViewId: {},
|
||||
};
|
||||
|
||||
return flatViewGroups.reduce((accumulator, flatViewGroup) => {
|
||||
const accumulatorHighestPosition =
|
||||
accumulator.highestViewGroupPositionByViewId[flatViewGroup.viewId];
|
||||
|
||||
return {
|
||||
flatViewGroupRecordByViewId: {
|
||||
...accumulator.flatViewGroupRecordByViewId,
|
||||
[flatViewGroup.viewId]: {
|
||||
...accumulator.flatViewGroupRecordByViewId[flatViewGroup.viewId],
|
||||
[flatViewGroup.id]: flatViewGroup,
|
||||
},
|
||||
},
|
||||
highestViewGroupPositionByViewId: {
|
||||
...accumulator.highestViewGroupPositionByViewId,
|
||||
[flatViewGroup.viewId]: isDefined(accumulatorHighestPosition)
|
||||
? Math.max(accumulatorHighestPosition, flatViewGroup.position)
|
||||
: flatViewGroup.position,
|
||||
},
|
||||
};
|
||||
}, initialAccumulator);
|
||||
};
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
|
||||
import { type FlatViewGroup } from 'src/engine/metadata-modules/flat-view-group/types/flat-view-group.type';
|
||||
|
||||
type FlatViewGroupsByViewUniversalIdentifier = {
|
||||
flatViewGroupRecordByViewUniversalIdentifier: Record<
|
||||
string,
|
||||
Record<string, FlatViewGroup>
|
||||
>;
|
||||
highestViewGroupPositionByViewUniversalIdentifier: Record<string, number>;
|
||||
};
|
||||
|
||||
export const reduceFlatViewGroupsByViewUniversalIdentifier = ({
|
||||
flatViewGroups,
|
||||
}: {
|
||||
flatViewGroups: FlatViewGroup[];
|
||||
}): FlatViewGroupsByViewUniversalIdentifier => {
|
||||
const initialAccumulator: FlatViewGroupsByViewUniversalIdentifier = {
|
||||
flatViewGroupRecordByViewUniversalIdentifier: {},
|
||||
highestViewGroupPositionByViewUniversalIdentifier: {},
|
||||
};
|
||||
|
||||
return flatViewGroups.reduce((accumulator, flatViewGroup) => {
|
||||
const accumulatorHighestPosition =
|
||||
accumulator.highestViewGroupPositionByViewUniversalIdentifier[
|
||||
flatViewGroup.viewUniversalIdentifier
|
||||
];
|
||||
|
||||
return {
|
||||
flatViewGroupRecordByViewUniversalIdentifier: {
|
||||
...accumulator.flatViewGroupRecordByViewUniversalIdentifier,
|
||||
[flatViewGroup.viewUniversalIdentifier]: {
|
||||
...accumulator.flatViewGroupRecordByViewUniversalIdentifier[
|
||||
flatViewGroup.viewUniversalIdentifier
|
||||
],
|
||||
[flatViewGroup.id]: flatViewGroup,
|
||||
},
|
||||
},
|
||||
highestViewGroupPositionByViewUniversalIdentifier: {
|
||||
...accumulator.highestViewGroupPositionByViewUniversalIdentifier,
|
||||
[flatViewGroup.viewUniversalIdentifier]: isDefined(
|
||||
accumulatorHighestPosition,
|
||||
)
|
||||
? Math.max(accumulatorHighestPosition, flatViewGroup.position)
|
||||
: flatViewGroup.position,
|
||||
},
|
||||
};
|
||||
}, initialAccumulator);
|
||||
};
|
||||
Reference in New Issue
Block a user