From ef8f7f3197842ff161b156290564086e2da7aa96 Mon Sep 17 00:00:00 2001 From: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com> Date: Thu, 7 Jul 2022 15:32:09 +0600 Subject: [PATCH] Allow deactivate employee without account (#2225) Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com> --- server/account/src/index.ts | 42 +++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 23 deletions(-) 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 })