The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Never infer deletion for rows a manifest cannot express (#25752)
Weiko4d8503f0aabe
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/__tests__/build-all-flat-entity-operation-record-by-metadata-name-from-from-to.util.spec.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/__tests__/build-all-flat-entity-operation-record-by-metadata-name-from-from-to.util.spec.tsindex 45214f49b31..0699bee7140 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/__tests__/build-all-flat-entity-operation-record-by-metadata-name-from-from-to.util.spec.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/__tests__/build-all-flat-entity-operation-record-by-metadata-name-from-from-to.util.spec.ts@@ -1,16 +1,34 @@-import { PageLayoutType } from 'twenty-shared/types';+import {+ CommandMenuItemAvailabilityType,+ NavigationMenuItemType,+ PageLayoutType,+} from 'twenty-shared/types';import { buildAllFlatEntityOperationRecordByMetadataNameFromFromTo } from 'src/engine/core-modules/application/application-manifest/utils/build-all-flat-entity-operation-record-by-metadata-name-from-from-to.util';+import { EngineComponentKey } from 'src/engine/metadata-modules/command-menu-item/enums/engine-component-key.enum';+import { type FlatCommandMenuItem } from 'src/engine/metadata-modules/flat-command-menu-item/types/flat-command-menu-item.type';import { createEmptyAllFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/constant/create-empty-all-flat-entity-maps.constant';+import { type AllFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/all-flat-entity-maps.type';import { addFlatEntityToFlatEntityMapsOrThrow } from 'src/engine/metadata-modules/flat-entity/utils/add-flat-entity-to-flat-entity-maps-or-throw.util';+import { type FlatNavigationMenuItem } from 'src/engine/metadata-modules/flat-navigation-menu-item/types/flat-navigation-menu-item.type';import { type FlatPageLayout } from 'src/engine/metadata-modules/flat-page-layout/types/flat-page-layout.type';+import { type FlatRoleTarget } from 'src/engine/metadata-modules/flat-role-target/types/flat-role-target.type';+import { type FlatWebhook } from 'src/engine/metadata-modules/flat-webhook/types/flat-webhook.type';++const APPLICATION_UNIVERSAL_IDENTIFIER = 'application-universal-identifier';++const SYNCABLE = {+ applicationId: 'application-id',+ applicationUniversalIdentifier: APPLICATION_UNIVERSAL_IDENTIFIER,+ workspaceId: 'workspace-id',+ createdAt: '2026-08-27T00:00:00.000Z',+ updatedAt: '2026-08-27T00:00:00.000Z',+} as const;const PAGE_LAYOUT: FlatPageLayout = {+ ...SYNCABLE,id: 'page-layout-id',universalIdentifier: 'page-layout-universal-identifier',- applicationId: 'application-id',- applicationUniversalIdentifier: 'application-universal-identifier',- workspaceId: 'workspace-id',name: 'App page',type: PageLayoutType.RECORD_PAGE,objectMetadataId: null,@@ -21,15 +39,88 @@ const PAGE_LAYOUT: FlatPageLayout = {tabUniversalIdentifiers: [],isSystemSideEffect: false,isFirstTabPinned: true,- createdAt: '2026-08-27T00:00:00.000Z',- updatedAt: '2026-08-27T00:00:00.000Z',deletedAt: null,};+const ROLE_TARGET: FlatRoleTarget = {+ ...SYNCABLE,+ id: 'role-target-id',+ universalIdentifier: 'role-target-universal-identifier',+ roleId: 'role-id',+ roleUniversalIdentifier: 'role-universal-identifier',+ userWorkspaceId: null,+ agentId: null,+ agentUniversalIdentifier: null,+ apiKeyId: null,+};++const WEBHOOK: FlatWebhook = {+ ...SYNCABLE,+ id: 'webhook-id',+ universalIdentifier: 'webhook-universal-identifier',+ targetUrl: 'https://example.com/hook',+ operations: ['*.*'],+ description: null,+ secret: 'webhook-secret',+ deletedAt: null,+};++const COMMAND_MENU_ITEM: FlatCommandMenuItem = {+ ...SYNCABLE,+ id: 'command-menu-item-id',+ universalIdentifier: 'command-menu-item-universal-identifier',+ workflowVersionId: null,+ frontComponentId: null,+ frontComponentUniversalIdentifier: null,+ engineComponentKey: EngineComponentKey.NAVIGATION,+ label: 'App command',+ icon: null,+ shortLabel: null,+ position: 0,+ isPinned: false,+ isActive: true,+ overrides: null,+ universalOverrides: null,+ availabilityType: CommandMenuItemAvailabilityType.GLOBAL,+ payload: null,+ hotKeys: null,+ conditionalAvailabilityExpression: null,+ conditionalPinnedExpression: null,+ availabilityObjectMetadataId: null,+ availabilityObjectMetadataUniversalIdentifier: null,+ navigationTargetObjectMetadataId: null,+ navigationTargetObjectMetadataUniversalIdentifier: null,+ pageLayoutId: null,+ pageLayoutUniversalIdentifier: null,+ isSystemSideEffect: false,+};++const NAVIGATION_MENU_ITEM: FlatNavigationMenuItem = {+ ...SYNCABLE,+ id: 'navigation-menu-item-id',+ universalIdentifier: 'navigation-menu-item-universal-identifier',+ userWorkspaceId: null,+ targetRecordId: null,+ targetObjectMetadataId: null,+ targetObjectMetadataUniversalIdentifier: null,+ viewId: null,+ viewUniversalIdentifier: null,+ type: NavigationMenuItemType.LINK,+ name: 'App link',+ link: 'https://example.com',+ icon: null,+ color: null,+ folderId: null,+ folderUniversalIdentifier: null,+ pageLayoutId: null,+ pageLayoutUniversalIdentifier: null,+ position: 0,+};+const BUILD_OPTIONS = {isSystemBuild: false,inferDeletionFromMissingEntities: true,- applicationUniversalIdentifier: PAGE_LAYOUT.applicationUniversalIdentifier,+ applicationUniversalIdentifier: APPLICATION_UNIVERSAL_IDENTIFIER,} as const;const buildAllFlatEntityMapsWithPageLayout = (pageLayout: FlatPageLayout) => {@@ -43,7 +134,164 @@ const buildAllFlatEntityMapsWithPageLayout = (pageLayout: FlatPageLayout) => {return allFlatEntityMaps;};+const inferDeletionOf = (+ buildFromAllFlatEntityMaps: (+ allFlatEntityMaps: AllFlatEntityMaps,+ ) => AllFlatEntityMaps,+) =>+ buildAllFlatEntityOperationRecordByMetadataNameFromFromTo({+ fromAllFlatEntityMaps: buildFromAllFlatEntityMaps(+ createEmptyAllFlatEntityMaps(),+ ),+ toAllUniversalFlatEntityMaps: createEmptyAllFlatEntityMaps(),+ buildOptions: BUILD_OPTIONS,+ });++const withRoleTarget =+ (flatRoleTarget: FlatRoleTarget) =>+ (allFlatEntityMaps: AllFlatEntityMaps) => ({+ ...allFlatEntityMaps,+ flatRoleTargetMaps: addFlatEntityToFlatEntityMapsOrThrow({+ flatEntity: flatRoleTarget,+ flatEntityMaps: allFlatEntityMaps.flatRoleTargetMaps,+ }),+ });++const withWebhook =+ (flatWebhook: FlatWebhook) => (allFlatEntityMaps: AllFlatEntityMaps) => ({+ ...allFlatEntityMaps,+ flatWebhookMaps: addFlatEntityToFlatEntityMapsOrThrow({+ flatEntity: flatWebhook,+ flatEntityMaps: allFlatEntityMaps.flatWebhookMaps,+ }),+ });++const withCommandMenuItem =+ (flatCommandMenuItem: FlatCommandMenuItem) =>+ (allFlatEntityMaps: AllFlatEntityMaps) => ({+ ...allFlatEntityMaps,+ flatCommandMenuItemMaps: addFlatEntityToFlatEntityMapsOrThrow({+ flatEntity: flatCommandMenuItem,+ flatEntityMaps: allFlatEntityMaps.flatCommandMenuItemMaps,+ }),+ });++const withNavigationMenuItem =+ (flatNavigationMenuItem: FlatNavigationMenuItem) =>+ (allFlatEntityMaps: AllFlatEntityMaps) => ({+ ...allFlatEntityMaps,+ flatNavigationMenuItemMaps: addFlatEntityToFlatEntityMapsOrThrow({+ flatEntity: flatNavigationMenuItem,+ flatEntityMaps: allFlatEntityMaps.flatNavigationMenuItemMaps,+ }),+ });+describe('buildAllFlatEntityOperationRecordByMetadataNameFromFromTo', () => {+ it('does not delete a member role assignment the manifest cannot express', () => {+ const result = inferDeletionOf(+ withRoleTarget({+ ...ROLE_TARGET,+ userWorkspaceId: 'a-member-user-workspace-id',+ }),+ );++ expect(result.roleTarget?.flatEntityToDelete).toBeUndefined();+ });++ it('does not delete an API key role assignment the manifest cannot express', () => {+ const result = inferDeletionOf(+ withRoleTarget({ ...ROLE_TARGET, apiKeyId: 'an-api-key-id' }),+ );++ expect(result.roleTarget?.flatEntityToDelete).toBeUndefined();+ });++ it('still deletes a role assignment that is not bound to a member or an API key', () => {+ const result = inferDeletionOf(+ withRoleTarget({ ...ROLE_TARGET, agentId: 'an-agent-id' }),+ );++ expect(+ result.roleTarget?.flatEntityToDelete?.[ROLE_TARGET.universalIdentifier],+ ).toBeDefined();+ });++ it('does not delete a webhook the manifest cannot express', () => {+ const result = inferDeletionOf(withWebhook(WEBHOOK));++ expect(result.webhook?.flatEntityToDelete).toBeUndefined();+ });++ it('does not delete a workflow trigger command the manifest cannot express', () => {+ const result = inferDeletionOf(+ withCommandMenuItem({+ ...COMMAND_MENU_ITEM,+ engineComponentKey: EngineComponentKey.TRIGGER_WORKFLOW_VERSION,+ workflowVersionId: 'a-workflow-version-id',+ }),+ );++ expect(result.commandMenuItem?.flatEntityToDelete).toBeUndefined();+ });++ it('does not delete a command without the front component its manifest requires', () => {+ const result = inferDeletionOf(withCommandMenuItem(COMMAND_MENU_ITEM));++ expect(result.commandMenuItem?.flatEntityToDelete).toBeUndefined();+ });++ it('still deletes a command the manifest can express', () => {+ const result = inferDeletionOf(+ withCommandMenuItem({+ ...COMMAND_MENU_ITEM,+ engineComponentKey: EngineComponentKey.FRONT_COMPONENT_RENDERER,+ frontComponentId: 'a-front-component-id',+ frontComponentUniversalIdentifier:+ 'a-front-component-universal-identifier',+ }),+ );++ expect(+ result.commandMenuItem?.flatEntityToDelete?.[+ COMMAND_MENU_ITEM.universalIdentifier+ ],+ ).toBeDefined();+ });++ it('does not delete a personal navigation item the manifest cannot express', () => {+ const result = inferDeletionOf(+ withNavigationMenuItem({+ ...NAVIGATION_MENU_ITEM,+ userWorkspaceId: 'a-member-user-workspace-id',+ }),+ );++ expect(result.navigationMenuItem?.flatEntityToDelete).toBeUndefined();+ });++ it('does not delete a record pinned navigation item the manifest cannot express', () => {+ const result = inferDeletionOf(+ withNavigationMenuItem({+ ...NAVIGATION_MENU_ITEM,+ targetRecordId: 'a-record-id',+ }),+ );++ expect(result.navigationMenuItem?.flatEntityToDelete).toBeUndefined();+ });++ it('still deletes a workspace navigation item', () => {+ const result = inferDeletionOf(+ withNavigationMenuItem(NAVIGATION_MENU_ITEM),+ );++ expect(+ result.navigationMenuItem?.flatEntityToDelete?.[+ NAVIGATION_MENU_ITEM.universalIdentifier+ ],+ ).toBeDefined();+ });+it('does not update an unpinned layout when its manifest is unchanged', () => {const result = buildAllFlatEntityOperationRecordByMetadataNameFromFromTo({fromAllFlatEntityMaps: buildAllFlatEntityMapsWithPageLayout({diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/build-all-flat-entity-operation-record-by-metadata-name-from-from-to.util.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/build-all-flat-entity-operation-record-by-metadata-name-from-from-to.util.tsindex 4590bbd3096..d2ebb8ae0d1 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/build-all-flat-entity-operation-record-by-metadata-name-from-from-to.util.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/build-all-flat-entity-operation-record-by-metadata-name-from-from-to.util.ts@@ -16,6 +16,7 @@ import { isSystemSideEffectFlatEntity } from 'src/engine/metadata-modules/flat-eimport { keepWorkspaceOwnedProperties } from 'src/engine/metadata-modules/flat-entity/utils/keep-workspace-owned-properties.util';import { type MetadataUniversalFlatEntityMaps } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/metadata-universal-flat-entity-maps.type';import { compareTwoFlatEntity } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/utils/compare-two-universal-flat-entity.util';+import { canManifestExpressFlatEntity } from 'src/engine/core-modules/application/application-manifest/utils/can-manifest-express-flat-entity.util';import { shouldInferDeletionFromMissingEntities } from 'src/engine/workspace-manager/workspace-migration/utils/should-infer-deletion-from-missing-entities.util';import { type WorkspaceMigrationBuilderOptions } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-builder/types/workspace-migration-builder-options.type';@@ -68,10 +69,13 @@ const buildFlatEntityOperationRecordForMetadata = <T extends AllMetadataName>({),).filter(- (fromFlatEntity) =>- !isSystemSideEffectFlatEntity(- fromFlatEntity as unknown as MetadataUniversalFlatEntity<AllMetadataName>,- ),+ (fromFlatEntity) => !isSystemSideEffectFlatEntity(fromFlatEntity),+ )+ .filter((fromFlatEntity) =>+ canManifestExpressFlatEntity({+ metadataName,+ flatEntity: fromFlatEntity,+ }),): [];diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/can-manifest-express-flat-entity.util.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/can-manifest-express-flat-entity.util.tsnew file mode 100644index 00000000000..0e977a0bd47--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/can-manifest-express-flat-entity.util.ts@@ -0,0 +1,46 @@+import { type AllMetadataName } from 'twenty-shared/metadata';+import { isDefined } from 'twenty-shared/utils';++const METADATA_NAMES_WITHOUT_MANIFEST_REPRESENTATION: AllMetadataName[] = [+ 'webhook',+];++const WORKSPACE_BOUND_PROPERTIES = [+ 'userWorkspaceId',+ 'apiKeyId',+ 'workflowVersionId',+ 'targetRecordId',+] as const;++type ProbedPropertyName =+ | (typeof WORKSPACE_BOUND_PROPERTIES)[number]+ | 'frontComponentUniversalIdentifier';++type ProbedFlatEntity = {+ universalIdentifier: string;+} & Partial<Record<ProbedPropertyName, string | null>>;++export const canManifestExpressFlatEntity = ({+ metadataName,+ flatEntity,+}: {+ metadataName: AllMetadataName;+ flatEntity: ProbedFlatEntity;+}): boolean => {+ if (METADATA_NAMES_WITHOUT_MANIFEST_REPRESENTATION.includes(metadataName)) {+ return false;+ }++ if (+ WORKSPACE_BOUND_PROPERTIES.some((workspaceBoundProperty) =>+ isDefined(flatEntity[workspaceBoundProperty]),+ )+ ) {+ return false;+ }++ return (+ metadataName !== 'commandMenuItem' ||+ isDefined(flatEntity.frontComponentUniversalIdentifier)+ );+};