From da0a17a2b676bea7301806b310d3618890cb8adc Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Tue, 18 Oct 2022 16:02:11 -0400 Subject: [PATCH] PS-1620 Move AccountService.delete to Internal (#3687) * Move AccountService.delete to Internal * Remove unnecessary promise --- .../app/accounts/delete-account.component.ts | 6 ++-- .../app/settings/delete-account.component.ts | 6 ++-- .../src/services/jslib-services.module.ts | 33 +++++++++++-------- .../account/account-api.service.ts | 5 +++ .../account/account.service.abstraction.ts | 5 --- .../abstractions/account/account.service.ts | 5 +++ .../services/account/account-api.service.ts | 27 +++++++++++---- .../src/services/account/account.service.ts | 18 +++------- 8 files changed, 61 insertions(+), 44 deletions(-) create mode 100644 libs/common/src/abstractions/account/account-api.service.ts delete mode 100644 libs/common/src/abstractions/account/account.service.abstraction.ts create mode 100644 libs/common/src/abstractions/account/account.service.ts diff --git a/apps/desktop/src/app/accounts/delete-account.component.ts b/apps/desktop/src/app/accounts/delete-account.component.ts index c708ba5741..74e79b2290 100644 --- a/apps/desktop/src/app/accounts/delete-account.component.ts +++ b/apps/desktop/src/app/accounts/delete-account.component.ts @@ -1,7 +1,7 @@ import { Component } from "@angular/core"; import { FormBuilder } from "@angular/forms"; -import { AccountService } from "@bitwarden/common/abstractions/account/account.service.abstraction"; +import { AccountApiService } from "@bitwarden/common/abstractions/account/account-api.service"; import { I18nService } from "@bitwarden/common/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/abstractions/log.service"; import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service"; @@ -23,7 +23,7 @@ export class DeleteAccountComponent { private i18nService: I18nService, private platformUtilsService: PlatformUtilsService, private formBuilder: FormBuilder, - private accountService: AccountService, + private accountApiService: AccountApiService, private logService: LogService ) {} @@ -34,7 +34,7 @@ export class DeleteAccountComponent { async submit() { try { const verification = this.deleteForm.get("verification").value; - this.formPromise = this.accountService.delete(verification); + this.formPromise = this.accountApiService.deleteAccount(verification); await this.formPromise; this.platformUtilsService.showToast( "success", diff --git a/apps/web/src/app/settings/delete-account.component.ts b/apps/web/src/app/settings/delete-account.component.ts index db58f27b81..1162945864 100644 --- a/apps/web/src/app/settings/delete-account.component.ts +++ b/apps/web/src/app/settings/delete-account.component.ts @@ -1,7 +1,7 @@ import { Component } from "@angular/core"; import { FormBuilder } from "@angular/forms"; -import { AccountService } from "@bitwarden/common/abstractions/account/account.service.abstraction"; +import { AccountApiService } from "@bitwarden/common/abstractions/account/account-api.service"; import { I18nService } from "@bitwarden/common/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/abstractions/log.service"; import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service"; @@ -22,14 +22,14 @@ export class DeleteAccountComponent { private i18nService: I18nService, private platformUtilsService: PlatformUtilsService, private formBuilder: FormBuilder, - private accountService: AccountService, + private accountApiService: AccountApiService, private logService: LogService ) {} async submit() { try { const verification = this.deleteForm.get("verification").value; - this.formPromise = this.accountService.delete(verification); + this.formPromise = this.accountApiService.deleteAccount(verification); await this.formPromise; this.platformUtilsService.showToast( "success", diff --git a/libs/angular/src/services/jslib-services.module.ts b/libs/angular/src/services/jslib-services.module.ts index 4243b1e81f..ee472c2f35 100644 --- a/libs/angular/src/services/jslib-services.module.ts +++ b/libs/angular/src/services/jslib-services.module.ts @@ -1,8 +1,11 @@ import { Injector, LOCALE_ID, NgModule } from "@angular/core"; import { AbstractEncryptService } from "@bitwarden/common/abstractions/abstractEncrypt.service"; -import { AccountApiService as AccountApiServiceAbstraction } from "@bitwarden/common/abstractions/account/account-api.service.abstraction"; -import { AccountService as AccountServiceAbstraction } from "@bitwarden/common/abstractions/account/account.service.abstraction"; +import { AccountApiService as AccountApiServiceAbstraction } from "@bitwarden/common/abstractions/account/account-api.service"; +import { + InternalAccountService, + AccountService as AccountServiceAbstraction, +} from "@bitwarden/common/abstractions/account/account.service"; import { AnonymousHubService as AnonymousHubServiceAbstraction } from "@bitwarden/common/abstractions/anonymousHub.service"; import { ApiService as ApiServiceAbstraction } from "@bitwarden/common/abstractions/api.service"; import { AppIdService as AppIdServiceAbstraction } from "@bitwarden/common/abstractions/appId.service"; @@ -61,8 +64,8 @@ import { VaultTimeoutSettingsService as VaultTimeoutSettingsServiceAbstraction } import { StateFactory } from "@bitwarden/common/factories/stateFactory"; import { Account } from "@bitwarden/common/models/domain/account"; import { GlobalState } from "@bitwarden/common/models/domain/global-state"; -import { AccountApiService } from "@bitwarden/common/services/account/account-api.service"; -import { AccountService } from "@bitwarden/common/services/account/account.service"; +import { AccountApiServiceImplementation } from "@bitwarden/common/services/account/account-api.service"; +import { AccountServiceImplementation } from "@bitwarden/common/services/account/account.service"; import { AnonymousHubService } from "@bitwarden/common/services/anonymousHub.service"; import { ApiService } from "@bitwarden/common/services/api.service"; import { AppIdService } from "@bitwarden/common/services/appId.service"; @@ -257,18 +260,22 @@ import { AbstractThemingService } from "./theming/theming.service.abstraction"; }, { provide: AccountApiServiceAbstraction, - useClass: AccountApiService, - deps: [ApiServiceAbstraction], + useClass: AccountApiServiceImplementation, + deps: [ + ApiServiceAbstraction, + UserVerificationServiceAbstraction, + LogService, + InternalAccountService, + ], }, { provide: AccountServiceAbstraction, - useClass: AccountService, - deps: [ - AccountApiServiceAbstraction, - UserVerificationServiceAbstraction, - MessagingServiceAbstraction, - LogService, - ], + useClass: AccountServiceImplementation, + deps: [MessagingServiceAbstraction, LogService], + }, + { + provide: InternalAccountService, + useExisting: AccountServiceAbstraction, }, { provide: LogService, useFactory: () => new ConsoleLogService(false) }, { diff --git a/libs/common/src/abstractions/account/account-api.service.ts b/libs/common/src/abstractions/account/account-api.service.ts new file mode 100644 index 0000000000..751a020b15 --- /dev/null +++ b/libs/common/src/abstractions/account/account-api.service.ts @@ -0,0 +1,5 @@ +import { Verification } from "../../types/verification"; + +export abstract class AccountApiService { + abstract deleteAccount(verification: Verification): Promise; +} diff --git a/libs/common/src/abstractions/account/account.service.abstraction.ts b/libs/common/src/abstractions/account/account.service.abstraction.ts deleted file mode 100644 index d87f9349a1..0000000000 --- a/libs/common/src/abstractions/account/account.service.abstraction.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { Verification } from "../../types/verification"; - -export abstract class AccountService { - abstract delete(verification: Verification): Promise; -} diff --git a/libs/common/src/abstractions/account/account.service.ts b/libs/common/src/abstractions/account/account.service.ts new file mode 100644 index 0000000000..26c260eb6d --- /dev/null +++ b/libs/common/src/abstractions/account/account.service.ts @@ -0,0 +1,5 @@ +export abstract class AccountService {} + +export abstract class InternalAccountService extends AccountService { + abstract delete(): void; +} diff --git a/libs/common/src/services/account/account-api.service.ts b/libs/common/src/services/account/account-api.service.ts index ffc032eb9f..fe094a9891 100644 --- a/libs/common/src/services/account/account-api.service.ts +++ b/libs/common/src/services/account/account-api.service.ts @@ -1,11 +1,26 @@ -import { AccountApiService as AccountApiServiceAbstraction } from "../../abstractions/account/account-api.service.abstraction"; +import { AccountApiService } from "../../abstractions/account/account-api.service"; +import { InternalAccountService } from "../../abstractions/account/account.service"; import { ApiService } from "../../abstractions/api.service"; -import { SecretVerificationRequest } from "../../models/request/secret-verification.request"; +import { LogService } from "../../abstractions/log.service"; +import { UserVerificationService } from "../../abstractions/userVerification/userVerification.service.abstraction"; +import { Verification } from "../../types/verification"; -export class AccountApiService implements AccountApiServiceAbstraction { - constructor(private apiService: ApiService) {} +export class AccountApiServiceImplementation implements AccountApiService { + constructor( + private apiService: ApiService, + private userVerificationService: UserVerificationService, + private logService: LogService, + private accountService: InternalAccountService + ) {} - deleteAccount(request: SecretVerificationRequest): Promise { - return this.apiService.send("DELETE", "/accounts", request, true, false); + async deleteAccount(verification: Verification): Promise { + try { + const verificationRequest = await this.userVerificationService.buildRequest(verification); + await this.apiService.send("DELETE", "/accounts", verificationRequest, true, false); + this.accountService.delete(); + } catch (e) { + this.logService.error(e); + throw e; + } } } diff --git a/libs/common/src/services/account/account.service.ts b/libs/common/src/services/account/account.service.ts index 417f20425d..dc11330973 100644 --- a/libs/common/src/services/account/account.service.ts +++ b/libs/common/src/services/account/account.service.ts @@ -1,22 +1,12 @@ -import { AccountApiService } from "../../abstractions/account/account-api.service.abstraction"; -import { AccountService as AccountServiceAbstraction } from "../../abstractions/account/account.service.abstraction"; +import { InternalAccountService } from "../../abstractions/account/account.service"; import { LogService } from "../../abstractions/log.service"; import { MessagingService } from "../../abstractions/messaging.service"; -import { UserVerificationService } from "../../abstractions/userVerification/userVerification.service.abstraction"; -import { Verification } from "../../types/verification"; -export class AccountService implements AccountServiceAbstraction { - constructor( - private accountApiService: AccountApiService, - private userVerificationService: UserVerificationService, - private messagingService: MessagingService, - private logService: LogService - ) {} +export class AccountServiceImplementation implements InternalAccountService { + constructor(private messagingService: MessagingService, private logService: LogService) {} - async delete(verification: Verification): Promise { + async delete(): Promise { try { - const verificationRequest = await this.userVerificationService.buildRequest(verification); - await this.accountApiService.deleteAccount(verificationRequest); this.messagingService.send("logout"); } catch (e) { this.logService.error(e);