From 61a888294f9c5bff16333ee883f616d07b440c06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20K=C3=B6nig?= <33655937+jkoenig134@users.noreply.github.com> Date: Fri, 29 Nov 2024 14:03:00 +0100 Subject: [PATCH] Bring `getAccountsInDeletion` and `getAccountsNotInDeletion` to `AccountServices` (#352) * feat: add getAccountsInDeletion and getAccountsNotInDeletion to AccountServices * refactor: simplify getAccounts* methods * chore: test another method --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- .../src/multiAccount/AccountServices.ts | 10 ++++++++++ .../src/multiAccount/MultiAccountController.ts | 16 ++++++++-------- .../multiAccount/MultiAccountController.test.ts | 10 ++++++++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/packages/app-runtime/src/multiAccount/AccountServices.ts b/packages/app-runtime/src/multiAccount/AccountServices.ts index c292161ab..d71cf384c 100644 --- a/packages/app-runtime/src/multiAccount/AccountServices.ts +++ b/packages/app-runtime/src/multiAccount/AccountServices.ts @@ -23,6 +23,16 @@ export class AccountServices { return localAccounts.map((account) => LocalAccountMapper.toLocalAccountDTO(account)); } + public async getAccountsInDeletion(): Promise { + const localAccounts = await this.multiAccountController.getAccountsInDeletion(); + return localAccounts.map((account) => LocalAccountMapper.toLocalAccountDTO(account)); + } + + public async getAccountsNotInDeletion(): Promise { + const localAccounts = await this.multiAccountController.getAccountsNotInDeletion(); + return localAccounts.map((account) => LocalAccountMapper.toLocalAccountDTO(account)); + } + public async getAccount(id: string): Promise { const localAccount = await this.multiAccountController.getAccount(CoreId.from(id)); return LocalAccountMapper.toLocalAccountDTO(localAccount); diff --git a/packages/app-runtime/src/multiAccount/MultiAccountController.ts b/packages/app-runtime/src/multiAccount/MultiAccountController.ts index 252590aa1..08ce2db46 100644 --- a/packages/app-runtime/src/multiAccount/MultiAccountController.ts +++ b/packages/app-runtime/src/multiAccount/MultiAccountController.ts @@ -78,20 +78,20 @@ export class MultiAccountController { } public async getAccounts(): Promise { - const dbAccounts = await this._localAccounts.list(); - return dbAccounts.map((account) => LocalAccount.from(account)); + return await this._findAccounts(); } public async getAccountsInDeletion(): Promise { - const allAccounts = await this.getAccounts(); - const accountsInDeletion = allAccounts.filter((item) => item.deletionDate !== undefined); - return accountsInDeletion; + return await this._findAccounts({ deletionDate: { $exists: true } }); } public async getAccountsNotInDeletion(): Promise { - const allAccounts = await this.getAccounts(); - const accountsNotInDeletion = allAccounts.filter((item) => item.deletionDate === undefined); - return accountsNotInDeletion; + return await this._findAccounts({ deletionDate: { $exists: false } }); + } + + private async _findAccounts(query?: any) { + const accounts = await this._localAccounts.find(query); + return accounts.map((account) => LocalAccount.from(account)); } public async selectAccount(id: CoreId): Promise<[LocalAccount, AccountController]> { diff --git a/packages/app-runtime/test/multiAccount/MultiAccountController.test.ts b/packages/app-runtime/test/multiAccount/MultiAccountController.test.ts index 22863881b..3cbf0a8fc 100644 --- a/packages/app-runtime/test/multiAccount/MultiAccountController.test.ts +++ b/packages/app-runtime/test/multiAccount/MultiAccountController.test.ts @@ -41,6 +41,16 @@ describe("MultiAccountController", function () { afterAll(async () => await runtime.stop()); + test("should get all accounts", async function () { + const accounts = await runtime.multiAccountController.getAccounts(); + expect(accounts).toHaveLength(3); + + const addresses = accounts.map((account) => account.address!.toString()); + expect(addresses).toContain(account1.address); + expect(addresses).toContain(account2.address); + expect(addresses).toContain(account3.address); + }); + test("should get all accounts in deletion", async function () { await session1.transportServices.identityDeletionProcesses.initiateIdentityDeletionProcess(); await session2.transportServices.identityDeletionProcesses.initiateIdentityDeletionProcess();