Files
twenty/packages/twenty-server/src/engine/metadata-modules/flat-view/utils/from-update-view-input-to-flat-view-to-update-or-throw.util.ts
T
Paul Rastoin 476bdf764c Refactor flat entity maps to be universal oriented (#17665)
# Introduction
In preparation of the workspace agnostic builder, we're migrating
`FlatEntityMaps` to be universal identifier oriented and based
As in the builder context there're won't be any ids at all
Please also note that the FlatEntity is a UniversalFlatEntity superset

From
```ts
import { type SyncableFlatEntity } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-from.type';

export type FlatEntityMaps<T extends SyncableFlatEntity> = {
  byId: Partial<Record<string, T>>;
  idByUniversalIdentifier: Partial<Record<string, string>>;
  universalIdentifiersByApplicationId: Partial<Record<string, string[]>>;
};
```

To
```ts
export type FlatEntityMaps<
  T extends SyncableFlatEntity | UniversalSyncableFlatEntity,
> = {
  byUniversalIdentifier: Partial<Record<string, T>>;
  universalIdentifierById: Partial<Record<string, string>>;
  universalIdentifiersByApplicationId: Partial<Record<string, string[]>>; // this might make more sense to be migrated to universalIdentifiersByApplicationUniversalIdentifier but it's the main topic of this PR
};
```

## Low level maps tools
Had to refactor find | create | delete | replace | find-many | get-sub
tools ( through mutations and or throw equivalent )
2026-02-03 16:16:02 +00:00

96 lines
3.7 KiB
TypeScript

import { t } from '@lingui/core/macro';
import {
extractAndSanitizeObjectStringFields,
isDefined,
trimAndRemoveDuplicatedWhitespacesFromObjectStringProperties,
} from 'twenty-shared/utils';
import { type FlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/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 { type FlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/types/flat-field-metadata.type';
import { type FlatViewGroupMaps } from 'src/engine/metadata-modules/flat-view-group/types/flat-view-group-maps.type';
import { type FlatViewGroup } from 'src/engine/metadata-modules/flat-view-group/types/flat-view-group.type';
import { FLAT_VIEW_EDITABLE_PROPERTIES } from 'src/engine/metadata-modules/flat-view/constants/flat-view-editable-properties.constant';
import { type FlatViewMaps } from 'src/engine/metadata-modules/flat-view/types/flat-view-maps.type';
import { type FlatView } from 'src/engine/metadata-modules/flat-view/types/flat-view.type';
import { handleFlatViewUpdateSideEffect } from 'src/engine/metadata-modules/flat-view/utils/handle-flat-view-update-side-effect.util';
import { type UpdateViewInput } from 'src/engine/metadata-modules/view/dtos/inputs/update-view.input';
import {
ViewException,
ViewExceptionCode,
} from 'src/engine/metadata-modules/view/exceptions/view.exception';
import { mergeUpdateInExistingRecord } from 'src/utils/merge-update-in-existing-record.util';
export const fromUpdateViewInputToFlatViewToUpdateOrThrow = ({
updateViewInput: rawUpdateViewInput,
flatViewMaps,
flatViewGroupMaps,
flatFieldMetadataMaps,
userWorkspaceId,
}: {
updateViewInput: UpdateViewInput;
flatViewMaps: FlatViewMaps;
flatViewGroupMaps: FlatViewGroupMaps;
flatFieldMetadataMaps: FlatEntityMaps<FlatFieldMetadata>;
userWorkspaceId?: string;
}): {
flatViewToUpdate: FlatView;
flatViewGroupsToDelete: FlatViewGroup[];
flatViewGroupsToCreate: FlatViewGroup[];
} => {
const { id: viewToUpdateId } =
trimAndRemoveDuplicatedWhitespacesFromObjectStringProperties(
rawUpdateViewInput,
['id'],
);
const existingFlatViewToUpdate = findFlatEntityByIdInFlatEntityMaps({
flatEntityId: viewToUpdateId,
flatEntityMaps: flatViewMaps,
});
if (!isDefined(existingFlatViewToUpdate)) {
throw new ViewException(
t`View to update not found`,
ViewExceptionCode.VIEW_NOT_FOUND,
);
}
const updatedEditableFieldProperties = extractAndSanitizeObjectStringFields(
rawUpdateViewInput,
FLAT_VIEW_EDITABLE_PROPERTIES,
);
const flatViewToUpdate = mergeUpdateInExistingRecord({
existing: existingFlatViewToUpdate,
properties: FLAT_VIEW_EDITABLE_PROPERTIES,
update: updatedEditableFieldProperties,
});
// If changing visibility from WORKSPACE to UNLISTED, ensure createdByUserWorkspaceId is set
// This prevents the view from disappearing for the user making the change
if (
isDefined(rawUpdateViewInput.visibility) &&
rawUpdateViewInput.visibility === 'UNLISTED' &&
existingFlatViewToUpdate.visibility === 'WORKSPACE' &&
isDefined(userWorkspaceId)
) {
// Re-allocate the view to the current user
flatViewToUpdate.createdByUserWorkspaceId = userWorkspaceId;
}
const { flatViewGroupsToDelete, flatViewGroupsToCreate } =
handleFlatViewUpdateSideEffect({
fromFlatView: existingFlatViewToUpdate,
toFlatView: flatViewToUpdate,
flatViewGroupMaps: flatViewGroupMaps,
flatFieldMetadataMaps: flatFieldMetadataMaps,
});
return {
flatViewToUpdate,
flatViewGroupsToDelete,
flatViewGroupsToCreate,
};
};