Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55891 from Expensify/youssef_no_workspace_no_disc…
Browse files Browse the repository at this point in the history
…ount

Hide discount banner if user has no workspaces or onboarding chat archived

(cherry picked from commit 4869ed0)

(CP triggered by yuwenmemon)
yuwenmemon authored and OSBotify committed Jan 29, 2025
1 parent cb8dd5a commit f427bba
Showing 3 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/libs/SubscriptionUtils.ts
Original file line number Diff line number Diff line change
@@ -245,6 +245,10 @@ function hasCardExpiringSoon(): boolean {
}

function shouldShowDiscountBanner(): boolean {
if (!getOwnedPaidPolicies(allPolicies, currentUserAccountID)?.length) {
return false;
}

if (!isUserOnFreeTrial()) {
return false;
}
4 changes: 3 additions & 1 deletion src/pages/home/HeaderView.tsx
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@ import {
getReportDescription,
getReportName,
hasReportNameError,
isArchivedReport,
isChatRoom as isChatRoomReportUtils,
isChatThread as isChatThreadReportUtils,
isChatUsedForOnboarding as isChatUsedForOnboardingReportUtils,
@@ -109,6 +110,7 @@ function HeaderView({report, parentReportAction, onNavigationMenuButtonClicked,
const [firstDayFreeTrial] = useOnyx(ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL);
const [lastDayFreeTrial] = useOnyx(ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL);
const [account] = useOnyx(ONYXKEYS.ACCOUNT);
const [reportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report?.reportID}`);

const {translate} = useLocalize();
const theme = useTheme();
@@ -186,7 +188,7 @@ function HeaderView({report, parentReportAction, onNavigationMenuButtonClicked,
// linked to the react lifecycle directly. Wait for trial dates to load, before calculating.
// eslint-disable-next-line react-compiler/react-compiler
// eslint-disable-next-line react-hooks/exhaustive-deps
const shouldShowDiscount = useMemo(() => shouldShowDiscountBanner(), [firstDayFreeTrial, lastDayFreeTrial]);
const shouldShowDiscount = useMemo(() => shouldShowDiscountBanner() && !isArchivedReport(reportNameValuePairs), [firstDayFreeTrial, lastDayFreeTrial, reportNameValuePairs]);

const shouldShowSubscript = shouldReportShowSubscript(report);
const defaultSubscriptSize = isExpenseRequest(report) ? CONST.AVATAR_SIZE.SMALL_NORMAL : CONST.AVATAR_SIZE.DEFAULT;
30 changes: 29 additions & 1 deletion tests/unit/SubscriptionUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -477,30 +477,58 @@ describe('SubscriptionUtils', () => {
});

describe('shouldShowDiscountBanner', () => {
const ownerAccountID = 234;
const policyID = '100012';
afterEach(async () => {
await Onyx.clear();
});

it('should return false if the user is not on a free trial', async () => {
await Onyx.multiSet({
[ONYXKEYS.SESSION]: {accountID: ownerAccountID},
[`${ONYXKEYS.COLLECTION.POLICY}${policyID}` as const]: {
...createRandomPolicy(Number(policyID)),
ownerAccountID,
},
[ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL]: null,
[ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL]: null,
});
expect(shouldShowDiscountBanner()).toBeFalsy();
});

it(`should return false if user has already added a payment method`, async () => {
await Onyx.set(ONYXKEYS.NVP_BILLING_FUND_ID, 8010);
await Onyx.multiSet({
[ONYXKEYS.SESSION]: {accountID: ownerAccountID},
[`${ONYXKEYS.COLLECTION.POLICY}${policyID}` as const]: {
...createRandomPolicy(Number(policyID)),
ownerAccountID,
},
[ONYXKEYS.NVP_BILLING_FUND_ID]: 8010,
});
expect(shouldShowDiscountBanner()).toBeFalsy();
});

it('should return true if the date is before the free trial end date or within the 8 days from the trial start date', async () => {
await Onyx.multiSet({
[ONYXKEYS.SESSION]: {accountID: ownerAccountID},
[`${ONYXKEYS.COLLECTION.POLICY}${policyID}` as const]: {
...createRandomPolicy(Number(policyID)),
ownerAccountID,
},
[ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL]: formatDate(subDays(new Date(), 1), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
[ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL]: formatDate(addDays(new Date(), 10), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
});
expect(shouldShowDiscountBanner()).toBeTruthy();
});

it("should return false if user's trial is during the discount period but has no workspaces", async () => {
await Onyx.multiSet({
[ONYXKEYS.SESSION]: {accountID: ownerAccountID},
[ONYXKEYS.NVP_FIRST_DAY_FREE_TRIAL]: formatDate(subDays(new Date(), 1), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
[ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL]: formatDate(addDays(new Date(), 10), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING),
});
expect(shouldShowDiscountBanner()).toBeFalsy();
});
});

describe('getEarlyDiscountInfo', () => {

0 comments on commit f427bba

Please sign in to comment.