@@ -637,7 +637,7 @@ export async function dropAccount (db: Db, email: string): Promise<void> {
637637
638638 await Promise . all (
639639 workspaces . map ( async ( ws ) => {
640- return await deactivateEmployeeAccount ( account , ws . workspace )
640+ return await deactivateEmployeeAccount ( account . email , ws . workspace )
641641 } )
642642 )
643643
@@ -653,18 +653,11 @@ export async function dropAccount (db: Db, email: string): Promise<void> {
653653export async function leaveWorkspace ( db : Db , token : string , email : string ) : Promise < void > {
654654 const tokenData = decodeToken ( token )
655655
656- const account = await getAccount ( db , email )
657- if ( account === null ) {
658- throw new PlatformError ( new Status ( Severity . ERROR , accountPlugin . status . AccountNotFound , { account : email } ) )
659- }
660-
661- if ( tokenData . email !== email ) {
662- const currentAccount = await getAccount ( db , tokenData . email )
663- if ( currentAccount === null ) {
664- throw new PlatformError (
665- new Status ( Severity . ERROR , accountPlugin . status . AccountNotFound , { account : tokenData . email } )
666- )
667- }
656+ const currentAccount = await getAccount ( db , tokenData . email )
657+ if ( currentAccount === null ) {
658+ throw new PlatformError (
659+ new Status ( Severity . ERROR , accountPlugin . status . AccountNotFound , { account : tokenData . email } )
660+ )
668661 }
669662
670663 const workspace = await getWorkspace ( db , tokenData . workspace )
@@ -674,22 +667,25 @@ export async function leaveWorkspace (db: Db, token: string, email: string): Pro
674667 )
675668 }
676669
677- await deactivateEmployeeAccount ( account , workspace . workspace )
670+ await deactivateEmployeeAccount ( email , workspace . workspace )
678671
679- await db
680- . collection < Workspace > ( WORKSPACE_COLLECTION )
681- . updateOne ( { _id : workspace . _id } , { $pull : { accounts : account . _id } } )
682- await db
683- . collection < Account > ( ACCOUNT_COLLECTION )
684- . updateOne ( { _id : account . _id } , { $pull : { workspaces : workspace . _id } } )
672+ const account = tokenData . email !== email ? await getAccount ( db , email ) : currentAccount
673+ if ( account !== null ) {
674+ await db
675+ . collection < Workspace > ( WORKSPACE_COLLECTION )
676+ . updateOne ( { _id : workspace . _id } , { $pull : { accounts : account . _id } } )
677+ await db
678+ . collection < Account > ( ACCOUNT_COLLECTION )
679+ . updateOne ( { _id : account . _id } , { $pull : { workspaces : workspace . _id } } )
680+ }
685681}
686682
687- async function deactivateEmployeeAccount ( account : Account , workspace : string ) : Promise < void > {
688- const connection = await connect ( getTransactor ( ) , workspace , account . email )
683+ async function deactivateEmployeeAccount ( email : string , workspace : string ) : Promise < void > {
684+ const connection = await connect ( getTransactor ( ) , workspace , email )
689685 try {
690686 const ops = new TxOperations ( connection , core . account . System )
691687
692- const existingAccount = await ops . findOne ( contact . class . EmployeeAccount , { email : account . email } )
688+ const existingAccount = await ops . findOne ( contact . class . EmployeeAccount , { email } )
693689
694690 if ( existingAccount !== undefined ) {
695691 const employee = await ops . findOne ( contact . class . Employee , { _id : existingAccount . employee } )
0 commit comments