diff --git a/server/account/src/index.ts b/server/account/src/index.ts index 5c82b656b6..b9ea6f8072 100644 --- a/server/account/src/index.ts +++ b/server/account/src/index.ts @@ -637,7 +637,7 @@ export async function dropAccount (db: Db, email: string): Promise { await Promise.all( workspaces.map(async (ws) => { - return await deactivateEmployeeAccount(account, ws.workspace) + return await deactivateEmployeeAccount(account.email, ws.workspace) }) ) @@ -653,18 +653,11 @@ export async function dropAccount (db: Db, email: string): Promise { export async function leaveWorkspace (db: Db, token: string, email: string): Promise { const tokenData = decodeToken(token) - const account = await getAccount(db, email) - if (account === null) { - throw new PlatformError(new Status(Severity.ERROR, accountPlugin.status.AccountNotFound, { account: email })) - } - - if (tokenData.email !== email) { - const currentAccount = await getAccount(db, tokenData.email) - if (currentAccount === null) { - throw new PlatformError( - new Status(Severity.ERROR, accountPlugin.status.AccountNotFound, { account: tokenData.email }) - ) - } + const currentAccount = await getAccount(db, tokenData.email) + if (currentAccount === null) { + throw new PlatformError( + new Status(Severity.ERROR, accountPlugin.status.AccountNotFound, { account: tokenData.email }) + ) } const workspace = await getWorkspace(db, tokenData.workspace) @@ -674,22 +667,25 @@ export async function leaveWorkspace (db: Db, token: string, email: string): Pro ) } - await deactivateEmployeeAccount(account, workspace.workspace) + await deactivateEmployeeAccount(email, workspace.workspace) - await db - .collection(WORKSPACE_COLLECTION) - .updateOne({ _id: workspace._id }, { $pull: { accounts: account._id } }) - await db - .collection(ACCOUNT_COLLECTION) - .updateOne({ _id: account._id }, { $pull: { workspaces: workspace._id } }) + const account = tokenData.email !== email ? await getAccount(db, email) : currentAccount + if (account !== null) { + await db + .collection(WORKSPACE_COLLECTION) + .updateOne({ _id: workspace._id }, { $pull: { accounts: account._id } }) + await db + .collection(ACCOUNT_COLLECTION) + .updateOne({ _id: account._id }, { $pull: { workspaces: workspace._id } }) + } } -async function deactivateEmployeeAccount (account: Account, workspace: string): Promise { - const connection = await connect(getTransactor(), workspace, account.email) +async function deactivateEmployeeAccount (email: string, workspace: string): Promise { + const connection = await connect(getTransactor(), workspace, email) try { const ops = new TxOperations(connection, core.account.System) - const existingAccount = await ops.findOne(contact.class.EmployeeAccount, { email: account.email }) + const existingAccount = await ops.findOne(contact.class.EmployeeAccount, { email }) if (existingAccount !== undefined) { const employee = await ops.findOne(contact.class.Employee, { _id: existingAccount.employee })