From f427bbaf362aa627c3cd17ab71117325699aedb3 Mon Sep 17 00:00:00 2001 From: Yuwen Memon Date: Tue, 28 Jan 2025 16:52:51 -0800 Subject: [PATCH] Merge pull request #55891 from Expensify/youssef_no_workspace_no_discount Hide discount banner if user has no workspaces or onboarding chat archived (cherry picked from commit 4869ed0c5aed093f38b444f2a31b57f352615daa) (CP triggered by yuwenmemon) --- src/libs/SubscriptionUtils.ts | 4 ++++ src/pages/home/HeaderView.tsx | 4 +++- tests/unit/SubscriptionUtilsTest.ts | 30 ++++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/libs/SubscriptionUtils.ts b/src/libs/SubscriptionUtils.ts index b4ecbf5dd09e..2a7f935b07e6 100644 --- a/src/libs/SubscriptionUtils.ts +++ b/src/libs/SubscriptionUtils.ts @@ -245,6 +245,10 @@ function hasCardExpiringSoon(): boolean { } function shouldShowDiscountBanner(): boolean { + if (!getOwnedPaidPolicies(allPolicies, currentUserAccountID)?.length) { + return false; + } + if (!isUserOnFreeTrial()) { return false; } diff --git a/src/pages/home/HeaderView.tsx b/src/pages/home/HeaderView.tsx index a453f03ed645..c287676578a9 100644 --- a/src/pages/home/HeaderView.tsx +++ b/src/pages/home/HeaderView.tsx @@ -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; diff --git a/tests/unit/SubscriptionUtilsTest.ts b/tests/unit/SubscriptionUtilsTest.ts index 411f3221d223..32890f4ce282 100644 --- a/tests/unit/SubscriptionUtilsTest.ts +++ b/tests/unit/SubscriptionUtilsTest.ts @@ -477,12 +477,19 @@ 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, }); @@ -490,17 +497,38 @@ describe('SubscriptionUtils', () => { }); 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', () => {