The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Fix defaultTabToFocusOnMobileAndSidePanel resolution when the tab is created in the same sync (#24840)
martmull7bef8236056a
diff --git a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/services/utils/__tests__/enrich-create-workspace-migration-action-with-ids.util.spec.ts b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/services/utils/__tests__/enrich-create-workspace-migration-action-with-ids.util.spec.tsindex 6c0efd2408e..6b08a333994 100644--- a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/services/utils/__tests__/enrich-create-workspace-migration-action-with-ids.util.spec.ts+++ b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/services/utils/__tests__/enrich-create-workspace-migration-action-with-ids.util.spec.ts@@ -1,7 +1,14 @@import { enrichCreateWorkspaceMigrationActionsWithIds } from 'src/engine/workspace-manager/workspace-migration/services/utils/enrich-create-workspace-migration-action-with-ids.util';import { type UniversalCreateFieldAction } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-builder/builders/field/types/workspace-migration-field-action';+import { type UniversalCreatePageLayoutTabAction } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-builder/builders/page-layout-tab/types/workspace-migration-page-layout-tab-action.type';+import { type UniversalCreatePageLayoutAction } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-builder/builders/page-layout/types/workspace-migration-page-layout-action.type';import { type WorkspaceMigration } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-builder/types/workspace-migration.type';+type SpecMigrationAction =+ | UniversalCreateFieldAction+ | UniversalCreatePageLayoutAction+ | UniversalCreatePageLayoutTabAction;+const buildCreateFieldAction = ({fieldUniversalIdentifier,relatedFieldUniversalIdentifier,@@ -25,8 +32,41 @@ const buildCreateFieldAction = ({: undefined,}) as unknown as UniversalCreateFieldAction;+const buildCreatePageLayoutAction = ({+ pageLayoutUniversalIdentifier,+ defaultTabUniversalIdentifier,+}: {+ pageLayoutUniversalIdentifier: string;+ defaultTabUniversalIdentifier?: string;+}): UniversalCreatePageLayoutAction =>+ ({+ type: 'create',+ metadataName: 'pageLayout',+ flatEntity: {+ universalIdentifier: pageLayoutUniversalIdentifier,+ defaultTabToFocusOnMobileAndSidePanelUniversalIdentifier:+ defaultTabUniversalIdentifier ?? null,+ },+ }) as unknown as UniversalCreatePageLayoutAction;++const buildCreatePageLayoutTabAction = ({+ tabUniversalIdentifier,+ pageLayoutUniversalIdentifier,+}: {+ tabUniversalIdentifier: string;+ pageLayoutUniversalIdentifier: string;+}): UniversalCreatePageLayoutTabAction =>+ ({+ type: 'create',+ metadataName: 'pageLayoutTab',+ flatEntity: {+ universalIdentifier: tabUniversalIdentifier,+ pageLayoutUniversalIdentifier,+ },+ }) as unknown as UniversalCreatePageLayoutTabAction;+const buildWorkspaceMigration = (- actions: UniversalCreateFieldAction[],+ actions: SpecMigrationAction[],): WorkspaceMigration =>({applicationUniversalIdentifier: 'app',@@ -130,4 +170,66 @@ describe('enrichCreateWorkspaceMigrationActionsWithIds', () => {expect(enrichedAction.id).toBe('external-id');});++ it('should stamp a pageLayout create with the id minted for a pageLayoutTab created in the same migration', () => {+ const pageLayoutAction = buildCreatePageLayoutAction({+ pageLayoutUniversalIdentifier: 'layout',+ defaultTabUniversalIdentifier: 'tab',+ });+ const pageLayoutTabAction = buildCreatePageLayoutTabAction({+ tabUniversalIdentifier: 'tab',+ pageLayoutUniversalIdentifier: 'layout',+ });++ const workspaceMigration = enrichCreateWorkspaceMigrationActionsWithIds({+ workspaceMigration: buildWorkspaceMigration([+ pageLayoutAction,+ pageLayoutTabAction,+ ]),+ idByUniversalIdentifierByMetadataName: {},+ });++ const [enrichedPageLayoutAction, enrichedPageLayoutTabAction] =+ workspaceMigration.actions as [+ UniversalCreatePageLayoutAction,+ UniversalCreatePageLayoutTabAction,+ ];++ expect(enrichedPageLayoutTabAction.id).toEqual(expect.any(String));+ expect(enrichedPageLayoutAction.tabIdByUniversalIdentifier?.tab).toBe(+ enrichedPageLayoutTabAction.id,+ );+ });++ it('should use the provided pageLayoutTab id for both the tab create and the pageLayout create', () => {+ const pageLayoutAction = buildCreatePageLayoutAction({+ pageLayoutUniversalIdentifier: 'layout',+ defaultTabUniversalIdentifier: 'tab',+ });+ const pageLayoutTabAction = buildCreatePageLayoutTabAction({+ tabUniversalIdentifier: 'tab',+ pageLayoutUniversalIdentifier: 'layout',+ });++ const workspaceMigration = enrichCreateWorkspaceMigrationActionsWithIds({+ workspaceMigration: buildWorkspaceMigration([+ pageLayoutAction,+ pageLayoutTabAction,+ ]),+ idByUniversalIdentifierByMetadataName: {+ pageLayoutTab: { tab: 'external-tab-id' },+ },+ });++ const [enrichedPageLayoutAction, enrichedPageLayoutTabAction] =+ workspaceMigration.actions as [+ UniversalCreatePageLayoutAction,+ UniversalCreatePageLayoutTabAction,+ ];++ expect(enrichedPageLayoutTabAction.id).toBe('external-tab-id');+ expect(enrichedPageLayoutAction.tabIdByUniversalIdentifier?.tab).toBe(+ 'external-tab-id',+ );+ });});diff --git a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/services/utils/enrich-create-workspace-migration-action-with-ids.util.ts b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/services/utils/enrich-create-workspace-migration-action-with-ids.util.tsindex 3312a475434..8ef5b0a0edc 100644--- a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/services/utils/enrich-create-workspace-migration-action-with-ids.util.ts+++ b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/services/utils/enrich-create-workspace-migration-action-with-ids.util.ts@@ -60,6 +60,38 @@ const buildTabIdByUniversalIdentifier = ({return tabIdByUniversalIdentifier;};+const buildTabIdByUniversalIdentifierForTabActions = ({+ actions,+ providedTabIdByUniversalIdentifier,+}: {+ actions: WorkspaceMigration['actions'];+ providedTabIdByUniversalIdentifier?: Record<string, string>;+}): Record<string, string> | undefined => {+ const tabIdByUniversalIdentifier = {+ ...providedTabIdByUniversalIdentifier,+ };++ for (const action of actions) {+ if (action.type !== 'create' || action.metadataName !== 'pageLayoutTab') {+ continue;+ }++ const { universalIdentifier } = action.flatEntity;++ if (isDefined(tabIdByUniversalIdentifier[universalIdentifier])) {+ continue;+ }++ tabIdByUniversalIdentifier[universalIdentifier] = action.id ?? v4();+ }++ if (Object.keys(tabIdByUniversalIdentifier).length === 0) {+ return undefined;+ }++ return tabIdByUniversalIdentifier;+};+const buildFieldIdByUniversalIdentifierForFieldActions = ({actions,providedFieldIdByUniversalIdentifier,@@ -189,6 +221,12 @@ export const enrichCreateWorkspaceMigrationActionsWithIds = ({fieldMetadataIdByUniversalIdentifier,});+ const tabIdByUniversalIdentifierForTabActions =+ buildTabIdByUniversalIdentifierForTabActions({+ actions: workspaceMigration.actions,+ providedTabIdByUniversalIdentifier: pageLayoutTabIdByUniversalIdentifier,+ });+const enrichedActions = workspaceMigration.actions.map((action) => {if (action.type !== 'create') {return action;@@ -202,7 +240,7 @@ export const enrichCreateWorkspaceMigrationActionsWithIds = ({action.metadataName !== 'objectMetadata' &&!isDefined(idByUniversalIdentifier) &&!isDefined(fieldMetadataIdByUniversalIdentifier) &&- !isDefined(pageLayoutTabIdByUniversalIdentifier)+ !isDefined(tabIdByUniversalIdentifierForTabActions)) {return action;}@@ -265,11 +303,12 @@ export const enrichCreateWorkspaceMigrationActionsWithIds = ({? idByUniversalIdentifier[action.flatEntity.universalIdentifier]: undefined;const tabIdByUniversalIdentifier = isDefined(- pageLayoutTabIdByUniversalIdentifier,+ tabIdByUniversalIdentifierForTabActions,)? buildTabIdByUniversalIdentifier({action,- pageLayoutTabIdByUniversalIdentifier,+ pageLayoutTabIdByUniversalIdentifier:+ tabIdByUniversalIdentifierForTabActions,}): undefined;@@ -279,6 +318,17 @@ export const enrichCreateWorkspaceMigrationActionsWithIds = ({tabIdByUniversalIdentifier,};}+ case 'pageLayoutTab': {+ const id =+ tabIdByUniversalIdentifierForTabActions?.[+ action.flatEntity.universalIdentifier+ ] ?? action.id;++ return {+ ...action,+ id,+ };+ }case 'view':case 'viewField':case 'viewGroup':@@ -294,7 +344,6 @@ export const enrichCreateWorkspaceMigrationActionsWithIds = ({case 'agent':case 'skill':case 'pageLayoutWidget':- case 'pageLayoutTab':case 'commandMenuItem':case 'navigationMenuItem':case 'frontComponent':diff --git a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/utils/__tests__/compute-ordered-migration-actions.util.spec.ts b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/utils/__tests__/compute-ordered-migration-actions.util.spec.tsnew file mode 100644index 00000000000..7d9c8d49769--- /dev/null+++ b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/utils/__tests__/compute-ordered-migration-actions.util.spec.ts@@ -0,0 +1,74 @@+import { createEmptyOrchestratorActionsReport } from 'src/engine/workspace-manager/workspace-migration/constant/empty-orchestrator-actions-report.constant';+import { computeOrderedMigrationActions } from 'src/engine/workspace-manager/workspace-migration/utils/compute-ordered-migration-actions.util';+import { type UniversalCreatePageLayoutTabAction } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-builder/builders/page-layout-tab/types/workspace-migration-page-layout-tab-action.type';+import {+ type UniversalCreatePageLayoutAction,+ type UniversalUpdatePageLayoutAction,+} from 'src/engine/workspace-manager/workspace-migration/workspace-migration-builder/builders/page-layout/types/workspace-migration-page-layout-action.type';++describe('computeOrderedMigrationActions', () => {+ it('should run pageLayout updates after pageLayoutTab creates so defaultTabToFocusOnMobileAndSidePanel can reference a tab created in the same migration', () => {+ const pageLayoutUpdateAction = {+ type: 'update',+ metadataName: 'pageLayout',+ universalIdentifier: 'layout',+ update: {+ defaultTabToFocusOnMobileAndSidePanelUniversalIdentifier: 'tab',+ },+ } as unknown as UniversalUpdatePageLayoutAction;+ const pageLayoutTabCreateAction = {+ type: 'create',+ metadataName: 'pageLayoutTab',+ flatEntity: {+ universalIdentifier: 'tab',+ pageLayoutUniversalIdentifier: 'layout',+ },+ } as unknown as UniversalCreatePageLayoutTabAction;++ const orchestratorActionsReport = createEmptyOrchestratorActionsReport();++ orchestratorActionsReport.pageLayout.update.push(pageLayoutUpdateAction);+ orchestratorActionsReport.pageLayoutTab.create.push(+ pageLayoutTabCreateAction,+ );++ const orderedActions = computeOrderedMigrationActions(+ orchestratorActionsReport,+ );++ expect(orderedActions.indexOf(pageLayoutTabCreateAction)).toBeLessThan(+ orderedActions.indexOf(pageLayoutUpdateAction),+ );+ });++ it('should run pageLayout creates before pageLayoutTab creates', () => {+ const pageLayoutCreateAction = {+ type: 'create',+ metadataName: 'pageLayout',+ flatEntity: { universalIdentifier: 'layout' },+ } as unknown as UniversalCreatePageLayoutAction;+ const pageLayoutTabCreateAction = {+ type: 'create',+ metadataName: 'pageLayoutTab',+ flatEntity: {+ universalIdentifier: 'tab',+ pageLayoutUniversalIdentifier: 'layout',+ },+ } as unknown as UniversalCreatePageLayoutTabAction;++ const orchestratorActionsReport = createEmptyOrchestratorActionsReport();++ orchestratorActionsReport.pageLayout.create.push(pageLayoutCreateAction);+ orchestratorActionsReport.pageLayoutTab.create.push(+ pageLayoutTabCreateAction,+ );++ const orderedActions = computeOrderedMigrationActions(+ orchestratorActionsReport,+ );++ expect(orderedActions.indexOf(pageLayoutCreateAction)).toBeLessThan(+ orderedActions.indexOf(pageLayoutTabCreateAction),+ );+ });+});diff --git a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/utils/compute-ordered-migration-actions.util.ts b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/utils/compute-ordered-migration-actions.util.tsindex f51e38fbd74..0c3669aa8b3 100644--- a/packages/twenty-server/src/engine/workspace-manager/workspace-migration/utils/compute-ordered-migration-actions.util.ts+++ b/packages/twenty-server/src/engine/workspace-manager/workspace-migration/utils/compute-ordered-migration-actions.util.ts@@ -89,12 +89,13 @@ export const computeOrderedMigrationActions = (...aggregatedOrchestratorActionsReport.pageLayout.delete,...aggregatedOrchestratorActionsReport.pageLayout.create,- ...aggregatedOrchestratorActionsReport.pageLayout.update,...aggregatedOrchestratorActionsReport.pageLayoutTab.delete,...aggregatedOrchestratorActionsReport.pageLayoutTab.create,...aggregatedOrchestratorActionsReport.pageLayoutTab.update,+ ...aggregatedOrchestratorActionsReport.pageLayout.update,+...aggregatedOrchestratorActionsReport.pageLayoutWidget.delete,...aggregatedOrchestratorActionsReport.pageLayoutWidget.create,...aggregatedOrchestratorActionsReport.pageLayoutWidget.update,diff --git a/packages/twenty-server/test/integration/metadata/suites/application/__snapshots__/successful-sync-application-workspace-migration.integration-spec.ts.snap b/packages/twenty-server/test/integration/metadata/suites/application/__snapshots__/successful-sync-application-workspace-migration.integration-spec.ts.snapindex 2a5ba24e3ad..4a0582a8cbf 100644--- a/packages/twenty-server/test/integration/metadata/suites/application/__snapshots__/successful-sync-application-workspace-migration.integration-spec.ts.snap+++ b/packages/twenty-server/test/integration/metadata/suites/application/__snapshots__/successful-sync-application-workspace-migration.integration-spec.ts.snap@@ -1140,6 +1140,7 @@ exports[`syncApplication should delete old field and create equivalent one when"updatedAt": Any<String>,},"metadataName": "pageLayout",+ "tabIdByUniversalIdentifier": Any<Object>,"type": "create",},{@@ -1158,6 +1159,7 @@ exports[`syncApplication should delete old field and create equivalent one when"universalIdentifier": Any<String>,"updatedAt": Any<String>,},+ "id": Any<String>,"metadataName": "pageLayoutTab","type": "create",},@@ -1177,6 +1179,7 @@ exports[`syncApplication should delete old field and create equivalent one when"universalIdentifier": Any<String>,"updatedAt": Any<String>,},+ "id": Any<String>,"metadataName": "pageLayoutTab","type": "create",},@@ -1196,6 +1199,7 @@ exports[`syncApplication should delete old field and create equivalent one when"universalIdentifier": Any<String>,"updatedAt": Any<String>,},+ "id": Any<String>,"metadataName": "pageLayoutTab","type": "create",},@@ -1215,6 +1219,7 @@ exports[`syncApplication should delete old field and create equivalent one when"universalIdentifier": Any<String>,"updatedAt": Any<String>,},+ "id": Any<String>,"metadataName": "pageLayoutTab","type": "create",},@@ -1234,6 +1239,7 @@ exports[`syncApplication should delete old field and create equivalent one when"universalIdentifier": Any<String>,"updatedAt": Any<String>,},+ "id": Any<String>,"metadataName": "pageLayoutTab","type": "create",},@@ -2629,6 +2635,7 @@ This is a test skill.","updatedAt": Any<String>,},"metadataName": "pageLayout",+ "tabIdByUniversalIdentifier": Any<Object>,"type": "create",},{@@ -2647,6 +2654,7 @@ This is a test skill.","universalIdentifier": Any<String>,"updatedAt": Any<String>,},+ "id": Any<String>,"metadataName": "pageLayoutTab","type": "create",},@@ -2666,6 +2674,7 @@ This is a test skill.","universalIdentifier": Any<String>,"updatedAt": Any<String>,},+ "id": Any<String>,"metadataName": "pageLayoutTab","type": "create",},@@ -2685,6 +2694,7 @@ This is a test skill.","universalIdentifier": Any<String>,"updatedAt": Any<String>,},+ "id": Any<String>,"metadataName": "pageLayoutTab","type": "create",},@@ -2704,6 +2714,7 @@ This is a test skill.","universalIdentifier": Any<String>,"updatedAt": Any<String>,},+ "id": Any<String>,"metadataName": "pageLayoutTab","type": "create",},@@ -2723,6 +2734,7 @@ This is a test skill.","universalIdentifier": Any<String>,"updatedAt": Any<String>,},+ "id": Any<String>,"metadataName": "pageLayoutTab","type": "create",},diff --git a/packages/twenty-server/test/integration/metadata/suites/application/successful-manifest-page-layout-default-tab.integration-spec.ts b/packages/twenty-server/test/integration/metadata/suites/application/successful-manifest-page-layout-default-tab.integration-spec.tsnew file mode 100644index 00000000000..04d3d3399f6--- /dev/null+++ b/packages/twenty-server/test/integration/metadata/suites/application/successful-manifest-page-layout-default-tab.integration-spec.ts@@ -0,0 +1,149 @@+import { buildBaseManifest } from 'test/integration/metadata/suites/application/utils/build-base-manifest.util';+import { cleanupApplicationAndAppRegistration } from 'test/integration/metadata/suites/application/utils/cleanup-application-and-app-registration.util';+import { setupApplicationForSync } from 'test/integration/metadata/suites/application/utils/setup-application-for-sync.util';+import { syncApplication } from 'test/integration/metadata/suites/application/utils/sync-application.util';+import { type Manifest } from 'twenty-shared/application';+import { PageLayoutType } from 'twenty-shared/types';+import { v4 as uuidv4 } from 'uuid';++const TEST_APP_ID = uuidv4();+const TEST_ROLE_ID = uuidv4();+const TEST_PAGE_LAYOUT_ID = uuidv4();+const TEST_FIRST_TAB_ID = uuidv4();+const TEST_SECOND_TAB_ID = uuidv4();++const buildManifest = (overrides?: Partial<Pick<Manifest, 'pageLayouts'>>) =>+ buildBaseManifest({+ appId: TEST_APP_ID,+ roleId: TEST_ROLE_ID,+ overrides,+ });++const findTestPageLayoutRow = async () => {+ const pageLayoutRows = await globalThis.testDataSource.query(+ `SELECT id, "defaultTabToFocusOnMobileAndSidePanelId" FROM core."pageLayout" WHERE "universalIdentifier" = $1`,+ [TEST_PAGE_LAYOUT_ID],+ );++ return pageLayoutRows[0];+};++const findTestPageLayoutTabRow = async (tabUniversalIdentifier: string) => {+ const pageLayoutTabRows = await globalThis.testDataSource.query(+ `SELECT id, "pageLayoutId" FROM core."pageLayoutTab" WHERE "universalIdentifier" = $1`,+ [tabUniversalIdentifier],+ );++ return pageLayoutTabRows[0];+};++describe('Manifest sync - page layout default tab', () => {+ beforeEach(async () => {+ await setupApplicationForSync({+ applicationUniversalIdentifier: TEST_APP_ID,+ name: 'Test Application',+ description: 'App for testing page layout default tab manifest sync',+ sourcePath: 'test-manifest-page-layout-default-tab',+ });+ }, 60000);++ afterEach(async () => {+ await cleanupApplicationAndAppRegistration({+ applicationUniversalIdentifier: TEST_APP_ID,+ });+ });++ it('should resolve defaultTabToFocusOnMobileAndSidePanel to a tab created in the same first sync', async () => {+ await syncApplication({+ manifest: buildManifest({+ pageLayouts: [+ {+ universalIdentifier: TEST_PAGE_LAYOUT_ID,+ name: 'Test Page',+ type: PageLayoutType.STANDALONE_PAGE,+ defaultTabToFocusOnMobileAndSidePanelUniversalIdentifier:+ TEST_FIRST_TAB_ID,+ tabs: [+ {+ universalIdentifier: TEST_FIRST_TAB_ID,+ title: 'Overview',+ position: 0,+ },+ ],+ },+ ],+ }),+ expectToFail: false,+ });++ const pageLayoutRow = await findTestPageLayoutRow();+ const firstTabRow = await findTestPageLayoutTabRow(TEST_FIRST_TAB_ID);++ expect(pageLayoutRow).toBeDefined();+ expect(firstTabRow).toBeDefined();+ expect(firstTabRow.pageLayoutId).toBe(pageLayoutRow.id);+ expect(pageLayoutRow.defaultTabToFocusOnMobileAndSidePanelId).toBe(+ firstTabRow.id,+ );+ }, 60000);++ it('should resolve defaultTabToFocusOnMobileAndSidePanel to a tab added in the same second sync', async () => {+ await syncApplication({+ manifest: buildManifest({+ pageLayouts: [+ {+ universalIdentifier: TEST_PAGE_LAYOUT_ID,+ name: 'Test Page',+ type: PageLayoutType.STANDALONE_PAGE,+ defaultTabToFocusOnMobileAndSidePanelUniversalIdentifier:+ TEST_FIRST_TAB_ID,+ tabs: [+ {+ universalIdentifier: TEST_FIRST_TAB_ID,+ title: 'Overview',+ position: 0,+ },+ ],+ },+ ],+ }),+ expectToFail: false,+ });++ await syncApplication({+ manifest: buildManifest({+ pageLayouts: [+ {+ universalIdentifier: TEST_PAGE_LAYOUT_ID,+ name: 'Test Page',+ type: PageLayoutType.STANDALONE_PAGE,+ defaultTabToFocusOnMobileAndSidePanelUniversalIdentifier:+ TEST_SECOND_TAB_ID,+ tabs: [+ {+ universalIdentifier: TEST_FIRST_TAB_ID,+ title: 'Overview',+ position: 0,+ },+ {+ universalIdentifier: TEST_SECOND_TAB_ID,+ title: 'Insights',+ position: 1,+ },+ ],+ },+ ],+ }),+ expectToFail: false,+ });++ const pageLayoutRow = await findTestPageLayoutRow();+ const secondTabRow = await findTestPageLayoutTabRow(TEST_SECOND_TAB_ID);++ expect(secondTabRow).toBeDefined();+ expect(secondTabRow.pageLayoutId).toBe(pageLayoutRow.id);+ expect(pageLayoutRow.defaultTabToFocusOnMobileAndSidePanelId).toBe(+ secondTabRow.id,+ );+ }, 60000);+});