Skip to content
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
11 changes: 1 addition & 10 deletions src/libs/SubscriptionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,6 @@ Onyx.connect({
waitForCollectionCallback: true,
});

// Indicates if downgrading the current subscription plan is allowed for the user.
let canDowngrade = false;
Onyx.connect({
key: ONYXKEYS.ACCOUNT,
callback: (val) => {
canDowngrade = val?.canDowngrade ?? false;
},
});

/**
* @returns The date when the grace period ends.
*/
Expand Down Expand Up @@ -578,7 +569,7 @@ function shouldRestrictUserBillableActions(policyID: string): boolean {
return false;
}

function shouldCalculateBillNewDot(): boolean {
function shouldCalculateBillNewDot(canDowngrade: boolean | undefined = false): boolean {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function shouldCalculateBillNewDot(canDowngrade: boolean | undefined = false): boolean {
function shouldCalculateBillNewDot(canDowngrade = false): boolean {

return canDowngrade && getOwnedPaidPolicies(allPolicies, currentUserAccountID).length === 1;
}

Expand Down
7 changes: 4 additions & 3 deletions src/pages/workspace/WorkspaceOverviewPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function WorkspaceOverviewPage({policyDraft, policy: policyProp, route}: Workspa

const backTo = route.params.backTo;
const [currencyList = getEmptyObject<CurrencyList>()] = useOnyx(ONYXKEYS.CURRENCY_LIST, {canBeMissing: true});
const [account] = useOnyx(ONYXKEYS.ACCOUNT, {canBeMissing: true});
const [currentUserAccountID = -1] = useOnyx(ONYXKEYS.SESSION, {
selector: accountIDSelector,
canBeMissing: true,
Expand Down Expand Up @@ -250,14 +251,14 @@ function WorkspaceOverviewPage({policyDraft, policy: policyProp, route}: Workspa
}, [isFocused, isPendingDelete, prevIsPendingDelete, policyLastErrorMessage]);

const onDeleteWorkspace = useCallback(() => {
if (shouldCalculateBillNewDot()) {
if (shouldCalculateBillNewDot(account?.canDowngrade)) {
setIsDeletingPaidWorkspace(true);
calculateBillNewDot();
return;
}

continueDeleteWorkspace();
}, [continueDeleteWorkspace, setIsDeletingPaidWorkspace]);
}, [continueDeleteWorkspace, setIsDeletingPaidWorkspace, account?.canDowngrade]);

const handleBackButtonPress = () => {
if (isComingFromGlobalReimbursementsFlow) {
Expand Down Expand Up @@ -317,7 +318,7 @@ function WorkspaceOverviewPage({policyDraft, policy: policyProp, route}: Workspa
onSelected: onDeleteWorkspace,
disabled: isLoadingBill,
shouldShowLoadingSpinnerIcon: isLoadingBill,
shouldCloseModalOnSelect: !shouldCalculateBillNewDot(),
shouldCloseModalOnSelect: !shouldCalculateBillNewDot(account?.canDowngrade),
});
}
const isCurrentUserAdmin = policy?.employeeList?.[currentUserPersonalDetails?.login ?? '']?.role === CONST.POLICY.ROLE.ADMIN;
Expand Down
3 changes: 2 additions & 1 deletion src/pages/workspace/WorkspacesListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ function WorkspacesListPage() {
const [fundList] = useOnyx(ONYXKEYS.FUND_LIST, {canBeMissing: true});
const [duplicateWorkspace] = useOnyx(ONYXKEYS.DUPLICATE_WORKSPACE, {canBeMissing: true});
const {isRestrictedToPreferredPolicy} = usePreferredPolicy();
const [account] = useOnyx(ONYXKEYS.ACCOUNT, {canBeMissing: true});
const [reimbursementAccountError] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {canBeMissing: true, selector: reimbursementAccountErrorSelector});

// This hook preloads the screens of adjacent tabs to make changing tabs faster.
Expand Down Expand Up @@ -208,7 +209,7 @@ function WorkspacesListPage() {
dismissWorkspaceError(policyToDelete.id, policyToDelete.pendingAction);
};

const shouldCalculateBillNewDot: boolean = shouldCalculateBillNewDotFn();
const shouldCalculateBillNewDot: boolean = shouldCalculateBillNewDotFn(account?.canDowngrade);

const resetLoadingSpinnerIconIndex = useCallback(() => {
setLoadingSpinnerIconIndex(null);
Expand Down
116 changes: 116 additions & 0 deletions tests/unit/SubscriptionUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
hasUserFreeTrialEnded,
isUserOnFreeTrial,
PAYMENT_STATUS,
shouldCalculateBillNewDot,
shouldRestrictUserBillableActions,
shouldShowDiscountBanner,
shouldShowPreTrialBillingBanner,
Expand Down Expand Up @@ -659,4 +660,119 @@ describe('SubscriptionUtils', () => {
expect(shouldShowPreTrialBillingBanner(introSelected)).toBeFalsy();
});
});
describe('shouldCalculateBillNewDot', () => {
const testUserAccountID = 1; // A consistent account ID for tests
const paidPolicyID = '12345';
const freePolicyID = '67890';
const secondPaidPolicyID = '98765';

beforeEach(async () => {
// Clear Onyx and set up session for each test
await Onyx.clear();
await Onyx.set(ONYXKEYS.SESSION, {email: 'test@example.com', accountID: testUserAccountID});
// Ensure allPolicies is initialized as empty or cleared before each test
await Onyx.multiSet({
[ONYXKEYS.COLLECTION.POLICY]: null,
});
// Reset the mock for getOwnedPaidPolicies before each test
jest.clearAllMocks();
});

it('should return false if canDowngrade is false (default or explicitly passed)', async () => {
// Set up a policy that would normally count as owned and paid
await Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.POLICY}${paidPolicyID}` as const]: {
...createRandomPolicy(Number(paidPolicyID)),
ownerAccountID: testUserAccountID,
type: CONST.POLICY.TYPE.CORPORATE,
},
});
// Test with canDowngrade as false (explicitly)
expect(shouldCalculateBillNewDot(false)).toBeFalsy();
// Test with canDowngrade as undefined (defaults to false in the function signature)
expect(shouldCalculateBillNewDot(undefined)).toBeFalsy();
// Test without passing canDowngrade (defaults to false)
expect(shouldCalculateBillNewDot()).toBeFalsy();
});

it('should return false if the user owns zero paid policies', async () => {
// Only free policies or no policies at all
await Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.POLICY}${freePolicyID}` as const]: {
...createRandomPolicy(Number(freePolicyID)),
ownerAccountID: testUserAccountID,
type: CONST.POLICY.TYPE.PERSONAL,
},
});
expect(shouldCalculateBillNewDot(true)).toBeFalsy();
});

it('should return false if the user owns more than one paid policy', async () => {
await Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.POLICY}${paidPolicyID}` as const]: {
...createRandomPolicy(Number(paidPolicyID)),
ownerAccountID: testUserAccountID,
type: CONST.POLICY.TYPE.CORPORATE,
},
[`${ONYXKEYS.COLLECTION.POLICY}${secondPaidPolicyID}` as const]: {
...createRandomPolicy(Number(secondPaidPolicyID)),
ownerAccountID: testUserAccountID,
type: CONST.POLICY.TYPE.TEAM,
},
});
expect(shouldCalculateBillNewDot(true)).toBeFalsy();
});

it('should return true if canDowngrade is true and the user owns exactly one paid policy', async () => {
await Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.POLICY}${paidPolicyID}` as const]: {
...createRandomPolicy(Number(paidPolicyID)),
ownerAccountID: testUserAccountID,
type: CONST.POLICY.TYPE.CORPORATE,
},
[`${ONYXKEYS.COLLECTION.POLICY}${freePolicyID}` as const]: {
// Include a free policy to confirm it's correctly ignored
...createRandomPolicy(Number(freePolicyID)),
ownerAccountID: testUserAccountID,
type: CONST.POLICY.TYPE.PERSONAL,
},
});
expect(shouldCalculateBillNewDot(true)).toBeTruthy();
});

it('should return false if the user owns exactly one paid policy but is not the owner', async () => {
// Set up a paid policy owned by another user
const thirdUserAccountID = 2;
await Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.POLICY}${paidPolicyID}` as const]: {
...createRandomPolicy(Number(paidPolicyID)),
ownerAccountID: thirdUserAccountID, // Owned by someone else
type: CONST.POLICY.TYPE.CORPORATE,
},
});
expect(shouldCalculateBillNewDot(true)).toBeFalsy();
});

it('should return true if canDowngrade is true and the single paid policy is a team policy', async () => {
await Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.POLICY}${paidPolicyID}` as const]: {
...createRandomPolicy(Number(paidPolicyID)),
ownerAccountID: testUserAccountID,
type: CONST.POLICY.TYPE.TEAM,
},
});
expect(shouldCalculateBillNewDot(true)).toBeTruthy();
});

it('should return true if canDowngrade is true and the single paid policy is a corporate policy', async () => {
await Onyx.multiSet({
[`${ONYXKEYS.COLLECTION.POLICY}${paidPolicyID}` as const]: {
...createRandomPolicy(Number(paidPolicyID)),
ownerAccountID: testUserAccountID,
type: CONST.POLICY.TYPE.CORPORATE,
},
});
expect(shouldCalculateBillNewDot(true)).toBeTruthy();
});
});
});
Loading