mirror of
https://github.com/bitwarden/browser
synced 2026-02-28 02:23:25 +00:00
* fix(billing): only show Subscription menu option when premium user has subscription
* fix(billing): missed state service invocation changes
(cherry picked from commit b964cfc8e4)
This commit is contained in:
@@ -25,13 +25,6 @@ export abstract class BillingAccountProfileStateService {
|
||||
*/
|
||||
abstract hasPremiumFromAnySource$(userId: UserId): Observable<boolean>;
|
||||
|
||||
/**
|
||||
* Emits `true` when the subscription menu item should be shown in navigation.
|
||||
* This is hidden for organizations that provide premium, except if the user has premium personally
|
||||
* or has a billing history.
|
||||
*/
|
||||
abstract canViewSubscription$(userId: UserId): Observable<boolean>;
|
||||
|
||||
/**
|
||||
* Sets the user's premium status fields upon every full sync, either from their personal
|
||||
* subscription to premium, or an organization they're a part of that grants them premium.
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
import { firstValueFrom } from "rxjs";
|
||||
|
||||
import { BillingAccountProfile } from "@bitwarden/common/billing/abstractions";
|
||||
|
||||
import {
|
||||
FakeAccountService,
|
||||
mockAccountServiceWith,
|
||||
FakeStateProvider,
|
||||
FakeSingleUserState,
|
||||
} from "../../../../spec";
|
||||
import { ApiService } from "../../../abstractions/api.service";
|
||||
import { PlatformUtilsService } from "../../../platform/abstractions/platform-utils.service";
|
||||
import { UserId } from "../../../types/guid";
|
||||
import { BillingAccountProfile } from "../../abstractions/account/billing-account-profile-state.service";
|
||||
import { BillingHistoryResponse } from "../../models/response/billing-history.response";
|
||||
|
||||
import {
|
||||
BILLING_ACCOUNT_PROFILE_KEY_DEFINITION,
|
||||
@@ -22,26 +20,14 @@ describe("BillingAccountProfileStateService", () => {
|
||||
let sut: DefaultBillingAccountProfileStateService;
|
||||
let userBillingAccountProfileState: FakeSingleUserState<BillingAccountProfile>;
|
||||
let accountService: FakeAccountService;
|
||||
let platformUtilsService: jest.Mocked<PlatformUtilsService>;
|
||||
let apiService: jest.Mocked<ApiService>;
|
||||
|
||||
const userId = "fakeUserId" as UserId;
|
||||
|
||||
beforeEach(() => {
|
||||
accountService = mockAccountServiceWith(userId);
|
||||
stateProvider = new FakeStateProvider(accountService);
|
||||
platformUtilsService = {
|
||||
isSelfHost: jest.fn(),
|
||||
} as any;
|
||||
apiService = {
|
||||
getUserBillingHistory: jest.fn(),
|
||||
} as any;
|
||||
|
||||
sut = new DefaultBillingAccountProfileStateService(
|
||||
stateProvider,
|
||||
platformUtilsService,
|
||||
apiService,
|
||||
);
|
||||
sut = new DefaultBillingAccountProfileStateService(stateProvider);
|
||||
|
||||
userBillingAccountProfileState = stateProvider.singleUser.getFake(
|
||||
userId,
|
||||
@@ -131,74 +117,4 @@ describe("BillingAccountProfileStateService", () => {
|
||||
expect(await firstValueFrom(sut.hasPremiumFromAnySource$(userId))).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("canViewSubscription$", () => {
|
||||
beforeEach(() => {
|
||||
platformUtilsService.isSelfHost.mockReturnValue(false);
|
||||
apiService.getUserBillingHistory.mockResolvedValue(
|
||||
new BillingHistoryResponse({ invoices: [], transactions: [] }),
|
||||
);
|
||||
});
|
||||
|
||||
it("returns true when user has premium personally", async () => {
|
||||
userBillingAccountProfileState.nextState({
|
||||
hasPremiumPersonally: true,
|
||||
hasPremiumFromAnyOrganization: true,
|
||||
});
|
||||
|
||||
expect(await firstValueFrom(sut.canViewSubscription$(userId))).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true when user has no premium from any source", async () => {
|
||||
userBillingAccountProfileState.nextState({
|
||||
hasPremiumPersonally: false,
|
||||
hasPremiumFromAnyOrganization: false,
|
||||
});
|
||||
|
||||
expect(await firstValueFrom(sut.canViewSubscription$(userId))).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true when user has billing history in cloud environment", async () => {
|
||||
userBillingAccountProfileState.nextState({
|
||||
hasPremiumPersonally: false,
|
||||
hasPremiumFromAnyOrganization: true,
|
||||
});
|
||||
platformUtilsService.isSelfHost.mockReturnValue(false);
|
||||
apiService.getUserBillingHistory.mockResolvedValue(
|
||||
new BillingHistoryResponse({
|
||||
invoices: [{ id: "1" }],
|
||||
transactions: [{ id: "2" }],
|
||||
}),
|
||||
);
|
||||
|
||||
expect(await firstValueFrom(sut.canViewSubscription$(userId))).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false when user has no premium personally, has org premium, and no billing history", async () => {
|
||||
userBillingAccountProfileState.nextState({
|
||||
hasPremiumPersonally: false,
|
||||
hasPremiumFromAnyOrganization: true,
|
||||
});
|
||||
platformUtilsService.isSelfHost.mockReturnValue(false);
|
||||
apiService.getUserBillingHistory.mockResolvedValue(
|
||||
new BillingHistoryResponse({
|
||||
invoices: [],
|
||||
transactions: [],
|
||||
}),
|
||||
);
|
||||
|
||||
expect(await firstValueFrom(sut.canViewSubscription$(userId))).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when user has no premium personally, has org premium, in self-hosted environment", async () => {
|
||||
userBillingAccountProfileState.nextState({
|
||||
hasPremiumPersonally: false,
|
||||
hasPremiumFromAnyOrganization: true,
|
||||
});
|
||||
platformUtilsService.isSelfHost.mockReturnValue(true);
|
||||
|
||||
expect(await firstValueFrom(sut.canViewSubscription$(userId))).toBe(false);
|
||||
expect(apiService.getUserBillingHistory).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import { map, Observable, combineLatest, concatMap } from "rxjs";
|
||||
import { map, Observable } from "rxjs";
|
||||
|
||||
import { ApiService } from "../../../abstractions/api.service";
|
||||
import { PlatformUtilsService } from "../../../platform/abstractions/platform-utils.service";
|
||||
import { BILLING_DISK, StateProvider, UserKeyDefinition } from "../../../platform/state";
|
||||
import { UserId } from "../../../types/guid";
|
||||
import {
|
||||
BillingAccountProfile,
|
||||
BillingAccountProfileStateService,
|
||||
} from "../../abstractions/account/billing-account-profile-state.service";
|
||||
} from "@bitwarden/common/billing/abstractions";
|
||||
import { BILLING_DISK, StateProvider, UserKeyDefinition } from "@bitwarden/state";
|
||||
|
||||
import { UserId } from "../../../types/guid";
|
||||
|
||||
export const BILLING_ACCOUNT_PROFILE_KEY_DEFINITION = new UserKeyDefinition<BillingAccountProfile>(
|
||||
BILLING_DISK,
|
||||
@@ -19,11 +18,7 @@ export const BILLING_ACCOUNT_PROFILE_KEY_DEFINITION = new UserKeyDefinition<Bill
|
||||
);
|
||||
|
||||
export class DefaultBillingAccountProfileStateService implements BillingAccountProfileStateService {
|
||||
constructor(
|
||||
private readonly stateProvider: StateProvider,
|
||||
private readonly platformUtilsService: PlatformUtilsService,
|
||||
private readonly apiService: ApiService,
|
||||
) {}
|
||||
constructor(private readonly stateProvider: StateProvider) {}
|
||||
|
||||
hasPremiumFromAnyOrganization$(userId: UserId): Observable<boolean> {
|
||||
return this.stateProvider
|
||||
@@ -69,26 +64,4 @@ export class DefaultBillingAccountProfileStateService implements BillingAccountP
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
canViewSubscription$(userId: UserId): Observable<boolean> {
|
||||
return combineLatest([
|
||||
this.hasPremiumPersonally$(userId),
|
||||
this.hasPremiumFromAnyOrganization$(userId),
|
||||
]).pipe(
|
||||
concatMap(async ([hasPremiumPersonally, hasPremiumFromOrg]) => {
|
||||
if (hasPremiumPersonally === true || !hasPremiumFromOrg === true) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const isCloud = !this.platformUtilsService.isSelfHost();
|
||||
|
||||
if (isCloud) {
|
||||
const billing = await this.apiService.getUserBillingHistory();
|
||||
return !billing?.hasNoHistory;
|
||||
}
|
||||
|
||||
return false;
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user