The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(navigation-menu-item): reject PAGE_LAYOUT items that don't reference a STANDALONE_PAGE layout (#22343)
Etiennec4a64467576f
diff --git a/packages/twenty-server/src/engine/metadata-modules/flat-navigation-menu-item/validators/utils/__tests__/validate-navigation-menu-item-page-layout-reference-cross-entity.util.spec.ts b/packages/twenty-server/src/engine/metadata-modules/flat-navigation-menu-item/validators/utils/__tests__/validate-navigation-menu-item-page-layout-reference-cross-entity.util.spec.tsnew file mode 100644index 00000000000..c321765b6e9--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/flat-navigation-menu-item/validators/utils/__tests__/validate-navigation-menu-item-page-layout-reference-cross-entity.util.spec.ts@@ -0,0 +1,230 @@+import { NavigationMenuItemType } from 'twenty-shared/types';++import { createEmptyFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/constant/create-empty-flat-entity-maps.constant';+import { validateNavigationMenuItemPageLayoutReferenceCrossEntity } from 'src/engine/metadata-modules/flat-navigation-menu-item/validators/utils/validate-navigation-menu-item-page-layout-reference-cross-entity.util';+import { NavigationMenuItemExceptionCode } from 'src/engine/metadata-modules/navigation-menu-item/navigation-menu-item.exception';+import { PageLayoutType } from 'src/engine/metadata-modules/page-layout/enums/page-layout-type.enum';++const NAVIGATION_MENU_ITEM_UNIVERSAL_IDENTIFIER =+ '00000000-0000-0000-0000-000000000001';+const PAGE_LAYOUT_UNIVERSAL_IDENTIFIER = '00000000-0000-0000-0000-0000000000aa';+const MISSING_PAGE_LAYOUT_UNIVERSAL_IDENTIFIER =+ '00000000-0000-0000-0000-0000000000bb';++const mapsFrom = (entities: { universalIdentifier: string }[]): any => {+ const maps = createEmptyFlatEntityMaps() as any;++ for (const entity of entities) {+ maps.byUniversalIdentifier[entity.universalIdentifier] = entity;+ }++ return maps;+};++const pageLayout = (+ type: PageLayoutType,+ universalIdentifier = PAGE_LAYOUT_UNIVERSAL_IDENTIFIER,+) => ({+ universalIdentifier,+ type,+});++const navigationMenuItem = (+ pageLayoutUniversalIdentifier: string | null,+ universalIdentifier = NAVIGATION_MENU_ITEM_UNIVERSAL_IDENTIFIER,+) => ({+ universalIdentifier,+ type: NavigationMenuItemType.PAGE_LAYOUT,+ pageLayoutUniversalIdentifier,+});++const emptyActions = () => ({ create: [], update: [], delete: [] });++const reportFrom = ({+ createdNavigationMenuItemUniversalIdentifiers = [],+ updatedNavigationMenuItemUniversalIdentifiers = [],+ createdPageLayoutUniversalIdentifiers = [],+ updatedPageLayoutUniversalIdentifiers = [],+}: {+ createdNavigationMenuItemUniversalIdentifiers?: string[];+ updatedNavigationMenuItemUniversalIdentifiers?: string[];+ createdPageLayoutUniversalIdentifiers?: string[];+ updatedPageLayoutUniversalIdentifiers?: string[];+}): any => ({+ navigationMenuItem: {+ ...emptyActions(),+ create: createdNavigationMenuItemUniversalIdentifiers.map(+ (universalIdentifier) => ({ flatEntity: { universalIdentifier } }),+ ),+ update: updatedNavigationMenuItemUniversalIdentifiers.map(+ (universalIdentifier) => ({ universalIdentifier }),+ ),+ },+ pageLayout: {+ ...emptyActions(),+ create: createdPageLayoutUniversalIdentifiers.map(+ (universalIdentifier) => ({ flatEntity: { universalIdentifier } }),+ ),+ update: updatedPageLayoutUniversalIdentifiers.map(+ (universalIdentifier) => ({+ universalIdentifier,+ }),+ ),+ },+});++const run = ({+ navigationMenuItems,+ pageLayouts,+ report,+}: {+ navigationMenuItems: { universalIdentifier: string }[];+ pageLayouts: { universalIdentifier: string }[];+ report: any;+}) =>+ validateNavigationMenuItemPageLayoutReferenceCrossEntity({+ optimisticUniversalFlatMaps: {+ flatNavigationMenuItemMaps: mapsFrom(navigationMenuItems),+ flatPageLayoutMaps: mapsFrom(pageLayouts),+ },+ orchestratorActionsReport: report,+ });++const errorCodes = (result: ReturnType<typeof run>) =>+ result.navigationMenuItem.flatMap((failed) =>+ failed.errors.map((error) => error.code),+ );++describe('validateNavigationMenuItemPageLayoutReferenceCrossEntity', () => {+ it('accepts a created PAGE_LAYOUT item referencing a same-migration STANDALONE_PAGE layout', () => {+ const result = run({+ navigationMenuItems: [+ navigationMenuItem(PAGE_LAYOUT_UNIVERSAL_IDENTIFIER),+ ],+ pageLayouts: [pageLayout(PageLayoutType.STANDALONE_PAGE)],+ report: reportFrom({+ createdNavigationMenuItemUniversalIdentifiers: [+ NAVIGATION_MENU_ITEM_UNIVERSAL_IDENTIFIER,+ ],+ createdPageLayoutUniversalIdentifiers: [+ PAGE_LAYOUT_UNIVERSAL_IDENTIFIER,+ ],+ }),+ });++ expect(errorCodes(result)).not.toContain(+ NavigationMenuItemExceptionCode.INVALID_NAVIGATION_MENU_ITEM_INPUT,+ );+ });++ it('rejects a created PAGE_LAYOUT item referencing a same-migration DASHBOARD layout', () => {+ const result = run({+ navigationMenuItems: [+ navigationMenuItem(PAGE_LAYOUT_UNIVERSAL_IDENTIFIER),+ ],+ pageLayouts: [pageLayout(PageLayoutType.DASHBOARD)],+ report: reportFrom({+ createdNavigationMenuItemUniversalIdentifiers: [+ NAVIGATION_MENU_ITEM_UNIVERSAL_IDENTIFIER,+ ],+ createdPageLayoutUniversalIdentifiers: [+ PAGE_LAYOUT_UNIVERSAL_IDENTIFIER,+ ],+ }),+ });++ expect(errorCodes(result)).toContain(+ NavigationMenuItemExceptionCode.INVALID_NAVIGATION_MENU_ITEM_INPUT,+ );+ });++ it('rejects a created PAGE_LAYOUT item referencing a same-migration RECORD_PAGE layout', () => {+ const result = run({+ navigationMenuItems: [+ navigationMenuItem(PAGE_LAYOUT_UNIVERSAL_IDENTIFIER),+ ],+ pageLayouts: [pageLayout(PageLayoutType.RECORD_PAGE)],+ report: reportFrom({+ createdNavigationMenuItemUniversalIdentifiers: [+ NAVIGATION_MENU_ITEM_UNIVERSAL_IDENTIFIER,+ ],+ createdPageLayoutUniversalIdentifiers: [+ PAGE_LAYOUT_UNIVERSAL_IDENTIFIER,+ ],+ }),+ });++ expect(errorCodes(result)).toContain(+ NavigationMenuItemExceptionCode.INVALID_NAVIGATION_MENU_ITEM_INPUT,+ );+ });++ it('does not raise a type error when the referenced layout cannot be resolved', () => {+ const result = run({+ navigationMenuItems: [+ navigationMenuItem(MISSING_PAGE_LAYOUT_UNIVERSAL_IDENTIFIER),+ ],+ pageLayouts: [pageLayout(PageLayoutType.STANDALONE_PAGE)],+ report: reportFrom({+ createdNavigationMenuItemUniversalIdentifiers: [+ NAVIGATION_MENU_ITEM_UNIVERSAL_IDENTIFIER,+ ],+ }),+ });++ expect(errorCodes(result)).not.toContain(+ NavigationMenuItemExceptionCode.INVALID_NAVIGATION_MENU_ITEM_INPUT,+ );+ });++ it('rejects an untouched item when its layout type is updated to a non-standalone type in the same migration', () => {+ const result = run({+ navigationMenuItems: [+ navigationMenuItem(PAGE_LAYOUT_UNIVERSAL_IDENTIFIER),+ ],+ pageLayouts: [pageLayout(PageLayoutType.RECORD_PAGE)],+ report: reportFrom({+ updatedPageLayoutUniversalIdentifiers: [+ PAGE_LAYOUT_UNIVERSAL_IDENTIFIER,+ ],+ }),+ });++ expect(errorCodes(result)).toContain(+ NavigationMenuItemExceptionCode.INVALID_NAVIGATION_MENU_ITEM_INPUT,+ );+ });++ it('accepts an updated item whose layout is concurrently updated to STANDALONE_PAGE (no false positive)', () => {+ const result = run({+ navigationMenuItems: [+ navigationMenuItem(PAGE_LAYOUT_UNIVERSAL_IDENTIFIER),+ ],+ pageLayouts: [pageLayout(PageLayoutType.STANDALONE_PAGE)],+ report: reportFrom({+ updatedNavigationMenuItemUniversalIdentifiers: [+ NAVIGATION_MENU_ITEM_UNIVERSAL_IDENTIFIER,+ ],+ updatedPageLayoutUniversalIdentifiers: [+ PAGE_LAYOUT_UNIVERSAL_IDENTIFIER,+ ],+ }),+ });++ expect(errorCodes(result)).not.toContain(+ NavigationMenuItemExceptionCode.INVALID_NAVIGATION_MENU_ITEM_INPUT,+ );+ });++ it('ignores items that are neither touched nor pointing to a touched layout', () => {+ const result = run({+ navigationMenuItems: [+ navigationMenuItem(PAGE_LAYOUT_UNIVERSAL_IDENTIFIER),+ ],+ pageLayouts: [pageLayout(PageLayoutType.DASHBOARD)],+ report: reportFrom({}),+ });++ expect(errorCodes(result)).toHaveLength(0);+ });+});diff --git a/packages/twenty-server/src/engine/metadata-modules/flat-navigation-menu-item/validators/utils/validate-navigation-menu-item-page-layout-reference-cross-entity.util.ts b/packages/twenty-server/src/engine/metadata-modules/flat-navigation-menu-item/validators/utils/validate-navigation-menu-item-page-layout-reference-cross-entity.util.tsnew file mode 100644index 00000000000..2375b7a7645--- /dev/null+++ b/packages/twenty-server/src/engine/metadata-modules/flat-navigation-menu-item/validators/utils/validate-navigation-menu-item-page-layout-reference-cross-entity.util.ts@@ -0,0 +1,128 @@+import { msg, t } from '@lingui/core/macro';+import { NavigationMenuItemType } from 'twenty-shared/types';+import { isDefined } from 'twenty-shared/utils';++import { findFlatEntityByUniversalIdentifier } from 'src/engine/metadata-modules/flat-entity/utils/find-flat-entity-by-universal-identifier.util';+import { NavigationMenuItemExceptionCode } from 'src/engine/metadata-modules/navigation-menu-item/navigation-menu-item.exception';+import { PageLayoutType } from 'src/engine/metadata-modules/page-layout/enums/page-layout-type.enum';+import {+ type OrchestratorActionsReport,+ type OrchestratorFailureReport,+} from 'src/engine/workspace-manager/workspace-migration/types/workspace-migration-orchestrator.type';+import { type AllUniversalFlatEntityMaps } from 'src/engine/workspace-manager/workspace-migration/universal-flat-entity/types/all-universal-flat-entity-maps.type';+import { getEmptyFlatEntityValidationError } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-builder/builders/utils/get-flat-entity-validation-error.util';++export const validateNavigationMenuItemPageLayoutReferenceCrossEntity = ({+ optimisticUniversalFlatMaps,+ orchestratorActionsReport,+}: {+ optimisticUniversalFlatMaps: Pick<+ AllUniversalFlatEntityMaps,+ 'flatNavigationMenuItemMaps' | 'flatPageLayoutMaps'+ >;+ orchestratorActionsReport: Pick<+ OrchestratorActionsReport,+ 'navigationMenuItem' | 'pageLayout'+ >;+}): Pick<OrchestratorFailureReport, 'navigationMenuItem'> => {+ const validationErrors: Pick<+ OrchestratorFailureReport,+ 'navigationMenuItem'+ > = {+ navigationMenuItem: [],+ };++ const createdNavigationMenuItemUniversalIdentifiers = new Set(+ orchestratorActionsReport.navigationMenuItem.create.map(+ (action) => action.flatEntity.universalIdentifier,+ ),+ );++ const updatedNavigationMenuItemUniversalIdentifiers = new Set<string>(+ orchestratorActionsReport.navigationMenuItem.update.map(+ (action) => action.universalIdentifier,+ ),+ );++ const touchedPageLayoutUniversalIdentifiers = new Set<string>([+ ...orchestratorActionsReport.pageLayout.create.map(+ (action) => action.flatEntity.universalIdentifier,+ ),+ ...orchestratorActionsReport.pageLayout.update.map(+ (action) => action.universalIdentifier,+ ),+ ]);++ const navigationMenuItemUniversalIdentifiersToValidate = new Set<string>([+ ...createdNavigationMenuItemUniversalIdentifiers,+ ...updatedNavigationMenuItemUniversalIdentifiers,+ ]);++ if (touchedPageLayoutUniversalIdentifiers.size > 0) {+ for (const navigationMenuItem of Object.values(+ optimisticUniversalFlatMaps.flatNavigationMenuItemMaps+ .byUniversalIdentifier,+ )) {+ if (+ isDefined(navigationMenuItem) &&+ isDefined(navigationMenuItem.pageLayoutUniversalIdentifier) &&+ touchedPageLayoutUniversalIdentifiers.has(+ navigationMenuItem.pageLayoutUniversalIdentifier,+ )+ ) {+ navigationMenuItemUniversalIdentifiersToValidate.add(+ navigationMenuItem.universalIdentifier,+ );+ }+ }+ }++ for (const universalIdentifier of navigationMenuItemUniversalIdentifiersToValidate) {+ const navigationMenuItem = findFlatEntityByUniversalIdentifier({+ universalIdentifier,+ flatEntityMaps: optimisticUniversalFlatMaps.flatNavigationMenuItemMaps,+ });++ if (+ !isDefined(navigationMenuItem) ||+ navigationMenuItem.type !== NavigationMenuItemType.PAGE_LAYOUT ||+ !isDefined(navigationMenuItem.pageLayoutUniversalIdentifier)+ ) {+ continue;+ }++ const referencedPageLayout = findFlatEntityByUniversalIdentifier({+ universalIdentifier: navigationMenuItem.pageLayoutUniversalIdentifier,+ flatEntityMaps: optimisticUniversalFlatMaps.flatPageLayoutMaps,+ });++ if (+ !isDefined(referencedPageLayout) ||+ referencedPageLayout.type === PageLayoutType.STANDALONE_PAGE+ ) {+ continue;+ }++ const failedValidation = getEmptyFlatEntityValidationError({+ flatEntityMinimalInformation: {+ universalIdentifier: navigationMenuItem.universalIdentifier,+ },+ metadataName: 'navigationMenuItem',+ type: createdNavigationMenuItemUniversalIdentifiers.has(+ universalIdentifier,+ )+ ? 'create'+ : 'update',+ });++ failedValidation.errors.push({+ code: NavigationMenuItemExceptionCode.INVALID_NAVIGATION_MENU_ITEM_INPUT,+ message: t`PAGE_LAYOUT navigation menu item must reference a STANDALONE_PAGE page layout`,+ userFriendlyMessage: msg`A page layout navigation menu item can only point to a standalone page`,+ });++ validationErrors.navigationMenuItem.push(failedValidation);+ }++ return validationErrors;+};diff --git a/packages/twenty-server/src/engine/metadata-modules/navigation-menu-item/tools/create-navigation-menu-item.tool.ts b/packages/twenty-server/src/engine/metadata-modules/navigation-menu-item/tools/create-navigation-menu-item.tool.tsindex bf66496521d..882d2112f80 100644--- a/packages/twenty-server/src/engine/metadata-modules/navigation-menu-item/tools/create-navigation-menu-item.tool.ts+++ b/packages/twenty-server/src/engine/metadata-modules/navigation-menu-item/tools/create-navigation-menu-item.tool.ts@@ -150,7 +150,7 @@ Type chooses the variant:- OBJECT: pins an object's standard view (label auto-derived from the object's plural name; only pass 'name' if the user wants a custom label).- VIEW: pins a saved view (label auto-derived from the view's name; only pass 'name' for a custom label).- RECORD: pins a single record (label auto-derived from the record's identifier; only pass 'name' for a custom label).-- PAGE_LAYOUT: pins a page layout, e.g. a dashboard (name required — no auto-derivation).+- PAGE_LAYOUT: pins a standalone page layout (name required — no auto-derivation). The referenced page layout must be of type STANDALONE_PAGE; dashboards and record/index page layouts are not reachable this way.Note: creating a new custom object via create_object_metadata already auto-creates an OBJECT navigation menu item — do not double-create.`,inputSchema: createNavigationMenuItemSchema,diff --git a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/utils/cross-entity-transversal-validation.util.ts b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/utils/cross-entity-transversal-validation.util.tsindex 3f9934b197a..a08109608fc 100644--- a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/utils/cross-entity-transversal-validation.util.ts+++ b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/utils/cross-entity-transversal-validation.util.ts@@ -1,3 +1,4 @@+import { validateNavigationMenuItemPageLayoutReferenceCrossEntity } from 'src/engine/metadata-modules/flat-navigation-menu-item/validators/utils/validate-navigation-menu-item-page-layout-reference-cross-entity.util';import { validateObjectMetadataCrossEntity } from 'src/engine/metadata-modules/flat-object-metadata/validators/utils/validate-object-metadata-cross-entity.util';import { validatePermissionFlagNotInUseCrossEntity } from 'src/engine/metadata-modules/flat-permission-flag/validators/utils/validate-permission-flag-not-in-use-cross-entity.util';import { validateViewFieldLabelIdentifierCrossEntity } from 'src/engine/metadata-modules/flat-view-field/validators/utils/validate-view-field-label-identifier-cross-entity.util';@@ -39,9 +40,16 @@ export const crossEntityTransversalValidation = ({orchestratorActionsReport.permissionFlag.delete,});+ const { navigationMenuItem } =+ validateNavigationMenuItemPageLayoutReferenceCrossEntity({+ optimisticUniversalFlatMaps,+ orchestratorActionsReport,+ });+crossEntityFailureReport.objectMetadata.push(...objectMetadata);crossEntityFailureReport.viewField.push(...viewField);crossEntityFailureReport.permissionFlag.push(...permissionFlag);+ crossEntityFailureReport.navigationMenuItem.push(...navigationMenuItem);validateUniversalIdentifierCrossEntityUniquenessThroughReportMutation({optimisticUniversalFlatMaps,