mirror of
https://github.com/hcengineering/platform.git
synced 2025-04-21 15:59:15 +00: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(
|
await Promise.all(
|
||||||
workspaces.map(async (ws) => {
|
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> {
|
export async function leaveWorkspace (db: Db, token: string, email: string): Promise<void> {
|
||||||
const tokenData = decodeToken(token)
|
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)
|
const currentAccount = await getAccount(db, tokenData.email)
|
||||||
if (currentAccount === null) {
|
if (currentAccount === null) {
|
||||||
throw new PlatformError(
|
throw new PlatformError(
|
||||||
new Status(Severity.ERROR, accountPlugin.status.AccountNotFound, { account: tokenData.email })
|
new Status(Severity.ERROR, accountPlugin.status.AccountNotFound, { account: tokenData.email })
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const workspace = await getWorkspace(db, tokenData.workspace)
|
const workspace = await getWorkspace(db, tokenData.workspace)
|
||||||
if (workspace === null) {
|
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
|
await db
|
||||||
.collection<Workspace>(WORKSPACE_COLLECTION)
|
.collection<Workspace>(WORKSPACE_COLLECTION)
|
||||||
.updateOne({ _id: workspace._id }, { $pull: { accounts: account._id } })
|
.updateOne({ _id: workspace._id }, { $pull: { accounts: account._id } })
|
||||||
await db
|
await db
|
||||||
.collection<Account>(ACCOUNT_COLLECTION)
|
.collection<Account>(ACCOUNT_COLLECTION)
|
||||||
.updateOne({ _id: account._id }, { $pull: { workspaces: workspace._id } })
|
.updateOne({ _id: account._id }, { $pull: { workspaces: workspace._id } })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deactivateEmployeeAccount (account: Account, workspace: string): Promise<void> {
|
async function deactivateEmployeeAccount (email: string, workspace: string): Promise<void> {
|
||||||
const connection = await connect(getTransactor(), workspace, account.email)
|
const connection = await connect(getTransactor(), workspace, email)
|
||||||
try {
|
try {
|
||||||
const ops = new TxOperations(connection, core.account.System)
|
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) {
|
if (existingAccount !== undefined) {
|
||||||
const employee = await ops.findOne(contact.class.Employee, { _id: existingAccount.employee })
|
const employee = await ops.findOne(contact.class.Employee, { _id: existingAccount.employee })
|
||||||
|
Loading…
Reference in New Issue
Block a user