Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-15814]Alert owners of reseller-managed orgs to renewal events #12607

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions apps/web/src/app/billing/services/reseller-warning.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { Injectable } from "@angular/core";

Check warning on line 1 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L1

Added line #L1 was not covered by tests

import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { OrganizationBillingMetadataResponse } from "@bitwarden/common/billing/models/response/organization-billing-metadata.response";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";

Check warning on line 5 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L5

Added line #L5 was not covered by tests

export interface ResellerWarning {
type: "info" | "warning";
message: string;
}

@Injectable({ providedIn: "root" })
export class ResellerWarningService {
private readonly RENEWAL_WARNING_DAYS = 14;
private readonly GRACE_PERIOD_DAYS = 30;

Check warning on line 15 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L13-L15

Added lines #L13 - L15 were not covered by tests

constructor(private i18nService: I18nService) {}

getWarning(
organization: Organization,
organizationBillingMetadata: OrganizationBillingMetadataResponse,
): ResellerWarning | null {
if (!organization.hasReseller) {
return null; // If no reseller, return null immediately

Check warning on line 24 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L24

Added line #L24 was not covered by tests
}

// Check for past due warning first (highest priority)
if (this.shouldShowPastDueWarning(organizationBillingMetadata)) {
const gracePeriodEnd = this.getGracePeriodEndDate(organizationBillingMetadata.invoiceDueDate);

Check warning on line 29 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L29

Added line #L29 was not covered by tests
if (!gracePeriodEnd) {
return null;

Check warning on line 31 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L31

Added line #L31 was not covered by tests
}
return {

Check warning on line 33 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L33

Added line #L33 was not covered by tests
type: "warning",
message: this.i18nService.t(
"resellerPastDueWarning",
organization.providerName,
this.formatDate(gracePeriodEnd),
),
} as ResellerWarning;
}

// Check for open invoice warning
if (this.shouldShowInvoiceWarning(organizationBillingMetadata)) {
const invoiceCreatedDate = organizationBillingMetadata.invoiceCreatedDate;
const invoiceDueDate = organizationBillingMetadata.invoiceDueDate;

Check warning on line 46 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L45-L46

Added lines #L45 - L46 were not covered by tests
if (!invoiceCreatedDate || !invoiceDueDate) {
return null;

Check warning on line 48 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L48

Added line #L48 was not covered by tests
}
return {

Check warning on line 50 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L50

Added line #L50 was not covered by tests
type: "info",
message: this.i18nService.t(
"resellerOpenInvoiceWarning",
organization.providerName,
this.formatDate(organizationBillingMetadata.invoiceCreatedDate),
this.formatDate(organizationBillingMetadata.invoiceDueDate),
),
} as ResellerWarning;
}

// Check for renewal warning
if (this.shouldShowRenewalWarning(organizationBillingMetadata)) {
const subPeriodEndDate = organizationBillingMetadata.subPeriodEndDate;

Check warning on line 63 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L63

Added line #L63 was not covered by tests
if (!subPeriodEndDate) {
return null;

Check warning on line 65 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L65

Added line #L65 was not covered by tests
}

return {

Check warning on line 68 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L68

Added line #L68 was not covered by tests
type: "info",
message: this.i18nService.t(
"resellerRenewalWarning",
organization.providerName,
this.formatDate(organizationBillingMetadata.subPeriodEndDate),
),
} as ResellerWarning;
}

return null;

Check warning on line 78 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L78

Added line #L78 was not covered by tests
}

private shouldShowRenewalWarning(
organizationBillingMetadata: OrganizationBillingMetadataResponse,
): boolean {
if (
!organizationBillingMetadata.hasSubscription ||
!organizationBillingMetadata.subPeriodEndDate
) {
return false;

Check warning on line 88 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L88

Added line #L88 was not covered by tests
}
const renewalDate = new Date(organizationBillingMetadata.subPeriodEndDate);
const daysUntilRenewal = Math.ceil(

Check warning on line 91 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L90-L91

Added lines #L90 - L91 were not covered by tests
(renewalDate.getTime() - Date.now()) / (1000 * 60 * 60 * 24),
);
return daysUntilRenewal <= this.RENEWAL_WARNING_DAYS;

Check warning on line 94 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L94

Added line #L94 was not covered by tests
}

private shouldShowInvoiceWarning(
organizationBillingMetadata: OrganizationBillingMetadataResponse,
): boolean {
if (
!organizationBillingMetadata.hasOpenInvoice ||
!organizationBillingMetadata.invoiceDueDate
) {
return false;

Check warning on line 104 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L104

Added line #L104 was not covered by tests
}
const invoiceDueDate = new Date(organizationBillingMetadata.invoiceDueDate);
return invoiceDueDate > new Date();

Check warning on line 107 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L106-L107

Added lines #L106 - L107 were not covered by tests
}

private shouldShowPastDueWarning(
organizationBillingMetadata: OrganizationBillingMetadataResponse,
): boolean {
if (
!organizationBillingMetadata.hasOpenInvoice ||
!organizationBillingMetadata.invoiceDueDate
) {
return false;

Check warning on line 117 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L117

Added line #L117 was not covered by tests
}
const invoiceDueDate = new Date(organizationBillingMetadata.invoiceDueDate);

Check warning on line 119 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L119

Added line #L119 was not covered by tests
return invoiceDueDate <= new Date() && !organizationBillingMetadata.isSubscriptionUnpaid;
}

private getGracePeriodEndDate(dueDate: Date | null): Date | null {
if (!dueDate) {
return null;

Check warning on line 125 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L125

Added line #L125 was not covered by tests
}
const gracePeriodEnd = new Date(dueDate);
gracePeriodEnd.setDate(gracePeriodEnd.getDate() + this.GRACE_PERIOD_DAYS);
return gracePeriodEnd;

Check warning on line 129 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L127-L129

Added lines #L127 - L129 were not covered by tests
}

private formatDate(date: Date | null): string {
if (!date) {
return "N/A";

Check warning on line 134 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L134

Added line #L134 was not covered by tests
}
return new Date(date).toLocaleDateString("en-US", {

Check warning on line 136 in apps/web/src/app/billing/services/reseller-warning.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/reseller-warning.service.ts#L136

Added line #L136 was not covered by tests
month: "short",
day: "2-digit",
year: "numeric",
});
}
}
12 changes: 12 additions & 0 deletions apps/web/src/app/vault/org-vault/vault.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
</a>
</bit-banner>
</ng-container>
<ng-container *ngIf="resellerWarning$ | async as resellerWarning">
<bit-banner
id="reseller-warning-banner"
class="-tw-m-6 tw-flex tw-flex-col tw-pb-6"
icon="bwi-billing"
bannerType="info"
[showClose]="false"
*ngIf="!refreshing"
>
{{ resellerWarning?.message }}
</bit-banner>
</ng-container>

<app-org-vault-header
[filter]="filter"
Expand Down
21 changes: 21 additions & 0 deletions apps/web/src/app/vault/org-vault/vault.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@

import { GroupApiService, GroupView } from "../../admin-console/organizations/core";
import { openEntityEventsDialog } from "../../admin-console/organizations/manage/entity-events.component";
import {

Check warning on line 89 in apps/web/src/app/vault/org-vault/vault.component.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/vault/org-vault/vault.component.ts#L89

Added line #L89 was not covered by tests
ResellerWarning,
ResellerWarningService,
} from "../../billing/services/reseller-warning.service";
import { TrialFlowService } from "../../billing/services/trial-flow.service";
import { FreeTrial } from "../../core/types/free-trial";
import { SharedModule } from "../../shared";
Expand Down Expand Up @@ -187,6 +191,7 @@
private hasSubscription$ = new BehaviorSubject<boolean>(false);
protected currentSearchText$: Observable<string>;
protected freeTrial$: Observable<FreeTrial>;
protected resellerWarning$: Observable<ResellerWarning | null>;
/**
* A list of collections that the user can assign items to and edit those items within.
* @protected
Expand All @@ -203,6 +208,7 @@
private destroy$ = new Subject<void>();
protected addAccessStatus$ = new BehaviorSubject<AddAccessStatusType>(0);
private extensionRefreshEnabled: boolean;
private resellerManagedOrgAlert: boolean;
private vaultItemDialogRef?: DialogRef<VaultItemDialogResult> | undefined;

private readonly unpaidSubscriptionDialog$ = this.organizationService.organizations$.pipe(
Expand Down Expand Up @@ -259,13 +265,18 @@
private trialFlowService: TrialFlowService,
protected billingApiService: BillingApiServiceAbstraction,
private organizationBillingService: OrganizationBillingServiceAbstraction,
private resellerWarningService: ResellerWarningService,

Check warning on line 268 in apps/web/src/app/vault/org-vault/vault.component.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/vault/org-vault/vault.component.ts#L268

Added line #L268 was not covered by tests
) {}

async ngOnInit() {
this.extensionRefreshEnabled = await this.configService.getFeatureFlag(
FeatureFlag.ExtensionRefresh,
);

this.resellerManagedOrgAlert = await this.configService.getFeatureFlag(

Check warning on line 276 in apps/web/src/app/vault/org-vault/vault.component.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/vault/org-vault/vault.component.ts#L276

Added line #L276 was not covered by tests
FeatureFlag.ResellerManagedOrgAlert,
);

this.trashCleanupWarning = this.i18nService.t(
this.platformUtilsService.isSelfHost()
? "trashCleanupWarningSelfHosted"
Expand Down Expand Up @@ -612,6 +623,16 @@
}),
);

this.resellerWarning$ = organization$.pipe(

Check warning on line 626 in apps/web/src/app/vault/org-vault/vault.component.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/vault/org-vault/vault.component.ts#L626

Added line #L626 was not covered by tests
filter((org) => org.isOwner && this.resellerManagedOrgAlert),
switchMap((org) =>
from(this.billingApiService.getOrganizationBillingMetadata(org.id)).pipe(
map((metadata) => ({ org, metadata })),

Check warning on line 630 in apps/web/src/app/vault/org-vault/vault.component.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/vault/org-vault/vault.component.ts#L629-L630

Added lines #L629 - L630 were not covered by tests
),
),
map(({ org, metadata }) => this.resellerWarningService.getWarning(org, metadata)),

Check warning on line 633 in apps/web/src/app/vault/org-vault/vault.component.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/vault/org-vault/vault.component.ts#L633

Added line #L633 was not covered by tests
);

firstSetup$
.pipe(
switchMap(() => this.refresh$),
Expand Down
43 changes: 43 additions & 0 deletions apps/web/src/locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -10011,5 +10011,48 @@
},
"organizationNameMaxLength": {
"message": "Organization name cannot exceed 50 characters."
},
"resellerRenewalWarning": {
"message": "Your subscription will renew soon. To insure uninterrupted service, contact $RESELLER$ to confirm your renewal before $RENEWAL_DATE$.",
"placeholders": {
"reseller": {
"content": "$1",
"example": "Reseller Name"
},
"renewal_date": {
"content": "$2",
"example": "01/01/2024"
}
}
},
"resellerOpenInvoiceWarning": {
"message": "An invoice for your subscription was issued on $ISSUED_DATE$. To insure uninterrupted service, contact $RESELLER$ to confirm your renewal before $DUE_DATE$.",
"placeholders": {
"reseller": {
"content": "$1",
"example": "Reseller Name"
},
"issued_date": {
"content": "$2",
"example": "01/01/2024"
},
"due_date": {
"content": "$3",
"example": "01/15/2024"
}
}
},
"resellerPastDueWarning": {
"message": "The invoice for your subscription has not been paid. To insure uninterrupted service, contact $RESELLER$ to confirm your renewal before $GRACE_PERIOD_END$.",
"placeholders": {
"reseller": {
"content": "$1",
"example": "Reseller Name"
},
"grace_period_end": {
"content": "$2",
"example": "02/14/2024"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
isOnSecretsManagerStandalone: boolean;
isSubscriptionUnpaid: boolean;
hasSubscription: boolean;
hasOpenInvoice: boolean;
invoiceDueDate: Date | null;
invoiceCreatedDate: Date | null;
subPeriodEndDate: Date | null;

constructor(response: any) {
super(response);
Expand All @@ -14,5 +18,14 @@
this.isOnSecretsManagerStandalone = this.getResponseProperty("IsOnSecretsManagerStandalone");
this.isSubscriptionUnpaid = this.getResponseProperty("IsSubscriptionUnpaid");
this.hasSubscription = this.getResponseProperty("HasSubscription");
this.hasOpenInvoice = this.getResponseProperty("HasOpenInvoice");

Check warning on line 21 in libs/common/src/billing/models/response/organization-billing-metadata.response.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/billing/models/response/organization-billing-metadata.response.ts#L21

Added line #L21 was not covered by tests

this.invoiceDueDate = this.parseDate(this.getResponseProperty("InvoiceDueDate"));
this.invoiceCreatedDate = this.parseDate(this.getResponseProperty("InvoiceCreatedDate"));
this.subPeriodEndDate = this.parseDate(this.getResponseProperty("SubPeriodEndDate"));

Check warning on line 25 in libs/common/src/billing/models/response/organization-billing-metadata.response.ts

View check run for this annotation

Codecov / codecov/patch

libs/common/src/billing/models/response/organization-billing-metadata.response.ts#L23-L25

Added lines #L23 - L25 were not covered by tests
}

private parseDate(dateString: any): Date | null {
return dateString ? new Date(dateString) : null;
}
}
2 changes: 2 additions & 0 deletions libs/common/src/enums/feature-flag.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export enum FeatureFlag {
PM11360RemoveProviderExportPermission = "pm-11360-remove-provider-export-permission",
PM12443RemovePagingLogic = "pm-12443-remove-paging-logic",
PrivateKeyRegeneration = "pm-12241-private-key-regeneration",
ResellerManagedOrgAlert = "PM-15814-alert-owners-of-reseller-managed-orgs",
}

export type AllowedFeatureFlagTypes = boolean | number | string;
Expand Down Expand Up @@ -96,6 +97,7 @@ export const DefaultFeatureFlagValue = {
[FeatureFlag.PM11360RemoveProviderExportPermission]: FALSE,
[FeatureFlag.PM12443RemovePagingLogic]: FALSE,
[FeatureFlag.PrivateKeyRegeneration]: FALSE,
[FeatureFlag.ResellerManagedOrgAlert]: FALSE,
} satisfies Record<FeatureFlag, AllowedFeatureFlagTypes>;

export type DefaultFeatureFlagValueType = typeof DefaultFeatureFlagValue;
Expand Down
Loading