From 8765aa182f14c179fad66c5e2492377ed668551c Mon Sep 17 00:00:00 2001 From: Shane Melton Date: Thu, 28 Jul 2022 15:12:40 -0700 Subject: [PATCH] [EC-8] Reuse user billing history for orgs --- ...rganization-billing-history.component.html | 89 ------------------- .../organization-billing-history.component.ts | 65 -------------- .../billing/organization-billing.module.ts | 3 - .../organization-routing.module.ts | 4 +- .../user-billing-history.component.html | 13 +-- .../user-billing-history.component.ts | 53 +++++++++-- .../src/models/response/billingResponse.ts | 4 + 7 files changed, 54 insertions(+), 177 deletions(-) delete mode 100644 apps/web/src/app/modules/organizations/billing/organization-billing-history.component.html delete mode 100644 apps/web/src/app/modules/organizations/billing/organization-billing-history.component.ts diff --git a/apps/web/src/app/modules/organizations/billing/organization-billing-history.component.html b/apps/web/src/app/modules/organizations/billing/organization-billing-history.component.html deleted file mode 100644 index bbead2c74c6..00000000000 --- a/apps/web/src/app/modules/organizations/billing/organization-billing-history.component.html +++ /dev/null @@ -1,89 +0,0 @@ - - - - {{ "loading" | i18n }} - - -

{{ "invoices" | i18n }}

-

{{ "noInvoices" | i18n }}

- - - - - - - - - -
{{ i.date | date: "mediumDate" }} - - - - {{ "invoiceNumber" | i18n: i.number }} - {{ i.amount | currency: "$" }} - - - {{ "paid" | i18n }} - - - - {{ "unpaid" | i18n }} - -
-

{{ "transactions" | i18n }}

-

{{ "noTransactions" | i18n }}

- - - - - - - - - -
{{ t.createdDate | date: "mediumDate" }} - - {{ "chargeNoun" | i18n }} - - {{ "refundNoun" | i18n }} - - - {{ t.details }} - - {{ t.amount | currency: "$" }} -
- * {{ "chargesStatement" | i18n: "BITWARDEN" }} -
diff --git a/apps/web/src/app/modules/organizations/billing/organization-billing-history.component.ts b/apps/web/src/app/modules/organizations/billing/organization-billing-history.component.ts deleted file mode 100644 index 9f993ec1c19..00000000000 --- a/apps/web/src/app/modules/organizations/billing/organization-billing-history.component.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { Component, OnInit } from "@angular/core"; -import { ActivatedRoute } from "@angular/router"; - -import { ApiService } from "@bitwarden/common/abstractions/api.service"; -import { PaymentMethodType } from "@bitwarden/common/enums/paymentMethodType"; -import { TransactionType } from "@bitwarden/common/enums/transactionType"; -import { BillingResponse } from "@bitwarden/common/models/response/billingResponse"; - -@Component({ - selector: "app-org-billing-history", - templateUrl: "./organization-billing-history.component.html", -}) -export class OrganizationBillingHistoryComponent implements OnInit { - loading = false; - firstLoaded = false; - billing: BillingResponse; - paymentMethodType = PaymentMethodType; - transactionType = TransactionType; - organizationId: string; - - constructor(private apiService: ApiService, private route: ActivatedRoute) {} - - async ngOnInit() { - this.route.parent.parent.params.subscribe(async (params) => { - this.organizationId = params.organizationId; - await this.load(); - this.firstLoaded = true; - }); - } - - async load() { - if (this.loading) { - return; - } - this.loading = true; - if (this.organizationId != null) { - this.billing = await this.apiService.getOrganizationBilling(this.organizationId); - } - this.loading = false; - } - - get invoices() { - return this.billing != null ? this.billing.invoices : null; - } - - get transactions() { - return this.billing != null ? this.billing.transactions : null; - } - - paymentMethodClasses(type: PaymentMethodType) { - switch (type) { - case PaymentMethodType.Card: - return ["bwi-credit-card"]; - case PaymentMethodType.BankAccount: - case PaymentMethodType.WireTransfer: - return ["bwi-bank"]; - case PaymentMethodType.BitPay: - return ["bwi-bitcoin text-warning"]; - case PaymentMethodType.PayPal: - return ["bwi-paypal text-primary"]; - default: - return []; - } - } -} diff --git a/apps/web/src/app/modules/organizations/billing/organization-billing.module.ts b/apps/web/src/app/modules/organizations/billing/organization-billing.module.ts index fba14d0b2c3..60b20b0623e 100644 --- a/apps/web/src/app/modules/organizations/billing/organization-billing.module.ts +++ b/apps/web/src/app/modules/organizations/billing/organization-billing.module.ts @@ -4,7 +4,6 @@ import { LooseComponentsModule } from "../../loose-components.module"; import { SharedModule } from "../../shared.module"; import { BillingSyncApiKeyComponent } from "./billing-sync-api-key.component"; -import { OrganizationBillingHistoryComponent } from "./organization-billing-history.component"; import { OrganizationBillingTabComponent } from "./organization-billing-tab.component"; import { OrganizationPaymentMethodComponent } from "./organization-payment-method.component"; import { OrganizationSubscriptionComponent } from "./organization-subscription.component"; @@ -13,14 +12,12 @@ import { OrganizationSubscriptionComponent } from "./organization-subscription.c imports: [SharedModule, LooseComponentsModule], declarations: [ BillingSyncApiKeyComponent, - OrganizationBillingHistoryComponent, OrganizationBillingTabComponent, OrganizationPaymentMethodComponent, OrganizationSubscriptionComponent, ], exports: [ BillingSyncApiKeyComponent, - OrganizationBillingHistoryComponent, OrganizationBillingTabComponent, OrganizationPaymentMethodComponent, OrganizationSubscriptionComponent, diff --git a/apps/web/src/app/organizations/organization-routing.module.ts b/apps/web/src/app/organizations/organization-routing.module.ts index 12082bf745f..d2b0dd9a85f 100644 --- a/apps/web/src/app/organizations/organization-routing.module.ts +++ b/apps/web/src/app/organizations/organization-routing.module.ts @@ -4,7 +4,6 @@ import { RouterModule, Routes } from "@angular/router"; import { AuthGuard } from "@bitwarden/angular/guards/auth.guard"; import { Permissions } from "@bitwarden/common/enums/permissions"; -import { OrganizationBillingHistoryComponent } from "../modules/organizations/billing/organization-billing-history.component"; import { OrganizationBillingTabComponent } from "../modules/organizations/billing/organization-billing-tab.component"; import { OrganizationPaymentMethodComponent } from "../modules/organizations/billing/organization-payment-method.component"; import { OrganizationSubscriptionComponent } from "../modules/organizations/billing/organization-subscription.component"; @@ -12,6 +11,7 @@ import { ReportListComponent } from "../modules/organizations/reporting/report-l import { ReportingComponent } from "../modules/organizations/reporting/reporting.component"; import { OrganizationVaultModule } from "../modules/vault/modules/organization-vault/organization-vault.module"; +import { UserBillingHistoryComponent } from "./../settings/user-billing-history.component"; import { PermissionsGuard } from "./guards/permissions.guard"; import { OrganizationLayoutComponent } from "./layouts/organization-layout.component"; import { EventsComponent } from "./manage/events.component"; @@ -168,7 +168,7 @@ const routes: Routes = [ }, { path: "history", - component: OrganizationBillingHistoryComponent, + component: UserBillingHistoryComponent, canActivate: [PermissionsGuard], data: { titleId: "billingHistory", permissions: [Permissions.ManageBilling] }, }, diff --git a/apps/web/src/app/settings/user-billing-history.component.html b/apps/web/src/app/settings/user-billing-history.component.html index 804830c84ed..37127ce1fdb 100644 --- a/apps/web/src/app/settings/user-billing-history.component.html +++ b/apps/web/src/app/settings/user-billing-history.component.html @@ -1,4 +1,4 @@ -
+

{{ "billingHistory" | i18n }}

@@ -23,7 +23,7 @@ {{ "loading" | i18n }} -

{{ "invoices" | i18n }}

+

{{ "invoices" | i18n }}

{{ "noInvoices" | i18n }}

@@ -74,14 +74,7 @@ class="bwi bwi-fw" *ngIf="t.paymentMethodType" aria-hidden="true" - [ngClass]="{ - 'bwi-credit-card': t.paymentMethodType === paymentMethodType.Card, - 'bwi-bank': - t.paymentMethodType === paymentMethodType.BankAccount || - t.paymentMethodType === paymentMethodType.WireTransfer, - 'bwi-bitcoin text-warning': t.paymentMethodType === paymentMethodType.BitPay, - 'bwi-paypal text-primary': t.paymentMethodType === paymentMethodType.PayPal - }" + [ngClass]="paymentMethodClasses(t.paymentMethodType)" > {{ t.details }} diff --git a/apps/web/src/app/settings/user-billing-history.component.ts b/apps/web/src/app/settings/user-billing-history.component.ts index dfc3216d2fe..bce371200a2 100644 --- a/apps/web/src/app/settings/user-billing-history.component.ts +++ b/apps/web/src/app/settings/user-billing-history.component.ts @@ -1,5 +1,5 @@ import { Component, OnInit } from "@angular/core"; -import { Router } from "@angular/router"; +import { ActivatedRoute, Router } from "@angular/router"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { I18nService } from "@bitwarden/common/abstractions/i18n.service"; @@ -18,20 +18,27 @@ export class UserBillingHistoryComponent implements OnInit { billing: BillingHistoryResponse; paymentMethodType = PaymentMethodType; transactionType = TransactionType; + organizationId?: string; constructor( protected apiService: ApiService, protected i18nService: I18nService, protected platformUtilsService: PlatformUtilsService, - private router: Router + private router: Router, + private route: ActivatedRoute ) {} async ngOnInit() { - if (this.platformUtilsService.isSelfHost()) { - this.router.navigate(["/settings/subscription"]); - } - await this.load(); - this.firstLoaded = true; + this.route.params.subscribe(async (params) => { + if (params.organizationId) { + this.organizationId = params.organizationId; + } else if (this.platformUtilsService.isSelfHost()) { + this.router.navigate(["/settings/subscription"]); + return; + } + await this.load(); + this.firstLoaded = true; + }); } async load() { @@ -39,7 +46,13 @@ export class UserBillingHistoryComponent implements OnInit { return; } this.loading = true; - this.billing = await this.apiService.getUserBillingHistory(); + + if (this.forOrganization) { + this.billing = await this.apiService.getOrganizationBilling(this.organizationId); + } else { + this.billing = await this.apiService.getUserBillingHistory(); + } + this.loading = false; } @@ -50,4 +63,28 @@ export class UserBillingHistoryComponent implements OnInit { get transactions() { return this.billing != null ? this.billing.transactions : null; } + + get forOrganization() { + return this.organizationId != null; + } + + get headerClass() { + return this.forOrganization ? ["page-header"] : ["tabbed-header"]; + } + + paymentMethodClasses(type: PaymentMethodType) { + switch (type) { + case PaymentMethodType.Card: + return ["bwi-credit-card"]; + case PaymentMethodType.BankAccount: + case PaymentMethodType.WireTransfer: + return ["bwi-bank"]; + case PaymentMethodType.BitPay: + return ["bwi-bitcoin text-warning"]; + case PaymentMethodType.PayPal: + return ["bwi-paypal text-primary"]; + default: + return []; + } + } } diff --git a/libs/common/src/models/response/billingResponse.ts b/libs/common/src/models/response/billingResponse.ts index 8d09cf9311d..a98434a25e6 100644 --- a/libs/common/src/models/response/billingResponse.ts +++ b/libs/common/src/models/response/billingResponse.ts @@ -23,6 +23,10 @@ export class BillingResponse extends BaseResponse { this.invoices = invoices.map((i: any) => new BillingInvoiceResponse(i)); } } + + get hasNoHistory() { + return this.invoices.length == 0 && this.transactions.length == 0; + } } export class BillingSourceResponse extends BaseResponse {