mirror of
https://github.com/hcengineering/platform.git
synced 2024-11-26 13:47:26 +03:00
Allow deactivate employee without account (#2225)
Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com>
This commit is contained in:
parent
7f793a7966
commit
ef8f7f3197
@ -637,7 +637,7 @@ export async function dropAccount (db: Db, email: string): Promise<void> {
|
||||
|
||||
await Promise.all(
|
||||
workspaces.map(async (ws) => {
|
||||
return await deactivateEmployeeAccount(account, ws.workspace)
|
||||
return await deactivateEmployeeAccount(account.email, ws.workspace)
|
||||
})
|
||||
)
|
||||
|
||||
@ -653,19 +653,12 @@ export async function dropAccount (db: Db, email: string): Promise<void> {
|
||||
export async function leaveWorkspace (db: Db, token: string, email: string): Promise<void> {
|
||||
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 workspace = await getWorkspace(db, tokenData.workspace)
|
||||
if (workspace === null) {
|
||||
@ -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)
|
||||
|
||||
const account = tokenData.email !== email ? await getAccount(db, email) : currentAccount
|
||||
if (account !== null) {
|
||||
await db
|
||||
.collection<Workspace>(WORKSPACE_COLLECTION)
|
||||
.updateOne({ _id: workspace._id }, { $pull: { accounts: account._id } })
|
||||
await db
|
||||
.collection<Account>(ACCOUNT_COLLECTION)
|
||||
.updateOne({ _id: account._id }, { $pull: { workspaces: workspace._id } })
|
||||
}
|
||||
}
|
||||
|
||||
async function deactivateEmployeeAccount (account: Account, workspace: string): Promise<void> {
|
||||
const connection = await connect(getTransactor(), workspace, account.email)
|
||||
async function deactivateEmployeeAccount (email: string, workspace: string): Promise<void> {
|
||||
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 })
|
||||
|
Loading…
Reference in New Issue
Block a user