From c12327867cf3d476bb5c11e21a4bff2205fb703b Mon Sep 17 00:00:00 2001 From: Alexey Zinoviev Date: Mon, 15 Apr 2024 08:16:35 +0400 Subject: [PATCH] uberf-6537: fix teamspace creation (#5354) Signed-off-by: Alexey Zinoviev --- models/document/src/migration.ts | 7 +++-- models/lead/src/migration.ts | 44 ++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/models/document/src/migration.ts b/models/document/src/migration.ts index bdf39a45b6..e1b99379a8 100644 --- a/models/document/src/migration.ts +++ b/models/document/src/migration.ts @@ -314,15 +314,18 @@ export const documentOperation: MigrateOperation = { }, async upgrade (client: MigrationUpgradeClient): Promise { + const tx = new TxOperations(client, core.account.System) await tryUpgrade(client, documentId, [ { state: 'u-default-project', func: async (client) => { - const tx = new TxOperations(client, core.account.System) await createSpace(tx) - await createDefaultTeamspaceType(tx) } } ]) + + // Currently space type has to be recreated every time as it's in the model + // created by the system user + await createDefaultTeamspaceType(tx) } } diff --git a/models/lead/src/migration.ts b/models/lead/src/migration.ts index 41c35ca60d..df927bdfcc 100644 --- a/models/lead/src/migration.ts +++ b/models/lead/src/migration.ts @@ -32,7 +32,29 @@ async function createSpace (tx: TxOperations): Promise { _id: lead.space.DefaultFunnel }) if (current === undefined) { - const type = await createProjectType( + await tx.createDoc( + lead.class.Funnel, + core.space.Space, + { + name: 'Funnel', + description: 'Default funnel', + private: false, + archived: false, + members: [], + type: lead.template.DefaultFunnel + }, + lead.space.DefaultFunnel + ) + } +} + +async function createSpaceType (tx: TxOperations): Promise { + const current = await tx.findOne(task.class.ProjectType, { + _id: lead.template.DefaultFunnel + }) + + if (current === undefined) { + await createProjectType( tx, { name: 'Default funnel', @@ -101,19 +123,6 @@ async function createSpace (tx: TxOperations): Promise { ], lead.template.DefaultFunnel ) - await tx.createDoc( - lead.class.Funnel, - core.space.Space, - { - name: 'Funnel', - description: 'Default funnel', - private: false, - archived: false, - members: [], - type - }, - lead.space.DefaultFunnel - ) } } @@ -179,14 +188,17 @@ export const leadOperation: MigrateOperation = { ]) }, async upgrade (client: MigrationUpgradeClient): Promise { + const ops = new TxOperations(client, core.account.System) await tryUpgrade(client, leadId, [ { state: 'u-default-funnel', - func: async (client) => { - const ops = new TxOperations(client, core.account.System) + func: async () => { await createDefaults(ops) } } ]) + // Currently space type has to be recreated every time as it's in the model + // created by the system user + await createSpaceType(ops) } }