Skip to content

Commit 4488fd5

Browse files
authored
Merge pull request Expensify#83054 from bernhardoj/chore/66456-remove-onyx-connect-shared-billing-grace-period-subscription-utils-3
#3 - Get the billing grace end period collection with useOnyx
2 parents 5b4e825 + d037a99 commit 4488fd5

7 files changed

Lines changed: 72 additions & 9 deletions

File tree

src/libs/ReportUtils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11079,6 +11079,7 @@ function createDraftTransactionAndNavigateToParticipantSelector(
1107911079
introSelected: OnyxEntry<IntroSelected>,
1108011080
allTransactionDrafts: OnyxCollection<Transaction>,
1108111081
activePolicy: OnyxEntry<Policy>,
11082+
userBillingGraceEndPeriodCollection: OnyxCollection<BillingGraceEndPeriod>,
1108211083
isRestrictedToPreferredPolicy = false,
1108311084
preferredPolicyID?: string,
1108411085
): void {
@@ -11144,7 +11145,7 @@ function createDraftTransactionAndNavigateToParticipantSelector(
1114411145
}
1114511146

1114611147
if (actionName === CONST.IOU.ACTION.CATEGORIZE) {
11147-
if (activePolicy && shouldRestrictUserBillableActions(activePolicy.id)) {
11148+
if (activePolicy && shouldRestrictUserBillableActions(activePolicy.id, userBillingGraceEndPeriodCollection)) {
1114811149
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(activePolicy.id));
1114911150
return;
1115011151
}

src/pages/ReportDetailsPage.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
164164
const expensifyIcons = useMemoizedLazyExpensifyIcons(['Users', 'Gear', 'Send', 'Folder', 'UserPlus', 'Pencil', 'Checkmark', 'Building', 'Exit', 'Bug', 'Camera', 'Trashcan']);
165165
const backTo = route.params.backTo;
166166

167+
const [userBillingGraceEndPeriodCollection] = useOnyx(ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END);
167168
const [parentReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${report.parentReportID}`);
168169
const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${report.chatReportID}`);
169170
const [quickAction] = useOnyx(ONYXKEYS.NVP_QUICK_ACTION_GLOBAL_CREATE);
@@ -458,6 +459,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
458459
introSelected,
459460
allTransactionDrafts,
460461
activePolicy,
462+
undefined,
461463
isRestrictedToPreferredPolicy,
462464
preferredPolicyID,
463465
);
@@ -479,6 +481,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
479481
introSelected,
480482
allTransactionDrafts,
481483
activePolicy,
484+
userBillingGraceEndPeriodCollection,
482485
);
483486
},
484487
});
@@ -497,6 +500,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
497500
introSelected,
498501
allTransactionDrafts,
499502
activePolicy,
503+
undefined,
500504
);
501505
},
502506
});
@@ -623,6 +627,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail
623627
activePolicy,
624628
parentReport,
625629
reportActionsForOriginalReportID,
630+
userBillingGraceEndPeriodCollection,
626631
]);
627632

628633
const displayNamesWithTooltips = useMemo(() => {

src/pages/inbox/report/PureReportActionItem.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ type PureReportActionItemProps = {
403403
introSelected: OnyxEntry<OnyxTypes.IntroSelected>,
404404
allTransactionDrafts: OnyxCollection<OnyxTypes.Transaction>,
405405
activePolicy: OnyxEntry<OnyxTypes.Policy>,
406+
userBillingGraceEndPeriodCollection: OnyxCollection<OnyxTypes.BillingGraceEndPeriod>,
406407
isRestrictedToPreferredPolicy?: boolean,
407408
preferredPolicyID?: string,
408409
) => void;
@@ -482,6 +483,9 @@ type PureReportActionItemProps = {
482483

483484
/** Report metadata for the report */
484485
reportMetadata?: OnyxEntry<OnyxTypes.ReportMetadata>;
486+
487+
/** The billing grace end period's shared NVP collection */
488+
userBillingGraceEndPeriodCollection: OnyxCollection<OnyxTypes.BillingGraceEndPeriod>;
485489
};
486490

487491
// This is equivalent to returning a negative boolean in normal functions, but we can keep the element return type
@@ -557,6 +561,7 @@ function PureReportActionItem({
557561
reportNameValuePairsOrigin,
558562
reportNameValuePairsOriginalID,
559563
reportMetadata,
564+
userBillingGraceEndPeriodCollection,
560565
}: PureReportActionItemProps) {
561566
const {transitionActionSheetState} = ActionSheetAwareScrollView.useActionSheetAwareScrollViewActions();
562567
const {translate, formatPhoneNumber, localeCompare, formatTravelDate, getLocalDateFromDatetime, datetimeToCalendarTime} = useLocalize();
@@ -955,6 +960,7 @@ function PureReportActionItem({
955960
introSelected,
956961
allTransactionDrafts,
957962
activePolicy,
963+
undefined,
958964
isRestrictedToPreferredPolicy,
959965
preferredPolicyID,
960966
);
@@ -976,6 +982,7 @@ function PureReportActionItem({
976982
introSelected,
977983
allTransactionDrafts,
978984
activePolicy,
985+
userBillingGraceEndPeriodCollection,
979986
);
980987
},
981988
},
@@ -991,6 +998,7 @@ function PureReportActionItem({
991998
introSelected,
992999
allTransactionDrafts,
9931000
activePolicy,
1001+
undefined,
9941002
);
9951003
},
9961004
},
@@ -1135,6 +1143,7 @@ function PureReportActionItem({
11351143
report,
11361144
originalReport,
11371145
personalPolicyID,
1146+
userBillingGraceEndPeriodCollection,
11381147
]);
11391148

11401149
/**

src/pages/inbox/report/ReportActionItem.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import PureReportActionItem from './PureReportActionItem';
3838

3939
type ReportActionItemProps = Omit<
4040
PureReportActionItemProps,
41-
'taskReport' | 'linkedReport' | 'iouReportOfLinkedReport' | 'currentUserAccountID' | 'personalPolicyID' | 'allTransactionDrafts'
41+
'taskReport' | 'linkedReport' | 'iouReportOfLinkedReport' | 'currentUserAccountID' | 'personalPolicyID' | 'allTransactionDrafts' | 'userBillingGraceEndPeriodCollection'
4242
> & {
4343
/** All the data of the report collection */
4444
allReports: OnyxCollection<Report>;
@@ -110,6 +110,7 @@ function ReportActionItem({
110110
const [cardList] = useOnyx(ONYXKEYS.CARD_LIST);
111111
const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST);
112112
const [personalPolicyID] = useOnyx(ONYXKEYS.PERSONAL_POLICY_ID);
113+
const [userBillingGraceEndPeriodCollection] = useOnyx(ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END);
113114
const transactionsOnIOUReport = useReportTransactions(iouReport?.reportID);
114115
const transactionID = isMoneyRequestAction(action) && getOriginalMessage(action)?.IOUTransactionID;
115116

@@ -192,6 +193,7 @@ function ReportActionItem({
192193
isTryNewDotNVPDismissed={isTryNewDotNVPDismissed}
193194
bankAccountList={bankAccountList}
194195
reportMetadata={reportMetadata}
196+
userBillingGraceEndPeriodCollection={userBillingGraceEndPeriodCollection}
195197
/>
196198
);
197199
}

tests/actions/IOUTest.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ describe('actions/IOU', () => {
589589
{choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
590590
{},
591591
undefined,
592+
undefined,
592593
);
593594
await waitForBatchedUpdates();
594595

@@ -1148,6 +1149,7 @@ describe('actions/IOU', () => {
11481149
{choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
11491150
{},
11501151
undefined,
1152+
undefined,
11511153
);
11521154
await waitForBatchedUpdates();
11531155

@@ -1558,6 +1560,7 @@ describe('actions/IOU', () => {
15581560
{choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
15591561
allTransactionDrafts,
15601562
undefined,
1563+
undefined,
15611564
);
15621565
await waitForBatchedUpdates();
15631566

@@ -1604,6 +1607,7 @@ describe('actions/IOU', () => {
16041607
{choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
16051608
{},
16061609
undefined,
1610+
undefined,
16071611
);
16081612
await waitForBatchedUpdates();
16091613

@@ -1639,6 +1643,7 @@ describe('actions/IOU', () => {
16391643
{choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
16401644
{},
16411645
undefined,
1646+
undefined,
16421647
);
16431648
await waitForBatchedUpdates();
16441649

@@ -1669,6 +1674,7 @@ describe('actions/IOU', () => {
16691674
{choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM},
16701675
{},
16711676
undefined,
1677+
undefined,
16721678
);
16731679
await waitForBatchedUpdates();
16741680

tests/ui/PureReportActionItemTest.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ describe('PureReportActionItem', () => {
112112
iouReportOfLinkedReport={undefined}
113113
currentUserAccountID={ACTOR_ACCOUNT_ID}
114114
allTransactionDrafts={undefined}
115+
userBillingGraceEndPeriodCollection={undefined}
115116
/>
116117
</PortalProvider>
117118
</ScreenWrapper>
@@ -319,6 +320,7 @@ describe('PureReportActionItem', () => {
319320
reportMetadata={reportMetadata}
320321
currentUserAccountID={ACTOR_ACCOUNT_ID}
321322
allTransactionDrafts={undefined}
323+
userBillingGraceEndPeriodCollection={undefined}
322324
/>
323325
</PortalProvider>
324326
</ScreenWrapper>
@@ -377,6 +379,7 @@ describe('PureReportActionItem', () => {
377379
iouReportOfLinkedReport={undefined}
378380
currentUserAccountID={ACTOR_ACCOUNT_ID}
379381
allTransactionDrafts={undefined}
382+
userBillingGraceEndPeriodCollection={undefined}
380383
/>
381384
</PortalProvider>
382385
</ScreenWrapper>
@@ -447,6 +450,7 @@ describe('PureReportActionItem', () => {
447450
iouReportOfLinkedReport={undefined}
448451
currentUserAccountID={ACTOR_ACCOUNT_ID}
449452
allTransactionDrafts={undefined}
453+
userBillingGraceEndPeriodCollection={undefined}
450454
/>
451455
</PortalProvider>
452456
</ScreenWrapper>
@@ -512,6 +516,7 @@ describe('PureReportActionItem', () => {
512516
iouReportOfLinkedReport={undefined}
513517
currentUserAccountID={ACTOR_ACCOUNT_ID}
514518
allTransactionDrafts={undefined}
519+
userBillingGraceEndPeriodCollection={undefined}
515520
/>
516521
</PortalProvider>
517522
</ScreenWrapper>
@@ -564,6 +569,7 @@ describe('PureReportActionItem', () => {
564569
currentUserAccountID={ACTOR_ACCOUNT_ID}
565570
allTransactionDrafts={undefined}
566571
modifiedExpenseMessage={modifiedExpenseMessage}
572+
userBillingGraceEndPeriodCollection={undefined}
567573
/>
568574
</PortalProvider>
569575
</ScreenWrapper>

tests/unit/ReportUtilsTest.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12311,7 +12311,7 @@ describe('ReportUtils', () => {
1231112311
await Onyx.set(ONYXKEYS.SESSION, {email: currentUserEmail, accountID: currentUserAccountID});
1231212312
});
1231312313

12314-
it("should navigate to the restricted action page if the active policy's billable actions are restricted", async () => {
12314+
it("should navigate to the restricted action page if the policy owner's billable actions are restricted", async () => {
1231512315
// Given a transaction and an active policy where billable actions are restricted
1231612316
const transaction = createRandomTransaction(1);
1231712317
const activePolicy: Policy = {
@@ -12326,7 +12326,41 @@ describe('ReportUtils', () => {
1232612326
await Onyx.merge(ONYXKEYS.NVP_PRIVATE_OWNER_BILLING_GRACE_PERIOD_END, Math.floor(Date.now() / 1000) - 3600);
1232712327

1232812328
// When we call createDraftTransactionAndNavigateToParticipantSelector with the restricted policy
12329-
createDraftTransactionAndNavigateToParticipantSelector(transaction.transactionID, '1', CONST.IOU.ACTION.CATEGORIZE, '1', undefined, undefined, activePolicy);
12329+
createDraftTransactionAndNavigateToParticipantSelector(transaction.transactionID, '1', CONST.IOU.ACTION.CATEGORIZE, '1', undefined, undefined, activePolicy, undefined);
12330+
12331+
// Then it should navigate to the restricted action page
12332+
expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.RESTRICTED_ACTION.getRoute(activePolicy.id));
12333+
});
12334+
12335+
it("should navigate to the restricted action page if the policy non-owner's billable actions are restricted", async () => {
12336+
// Given a transaction and an active policy where billable actions are restricted for non-owner
12337+
const transaction = createRandomTransaction(3);
12338+
const ownerAccountID = 123;
12339+
const activePolicy: Policy = {
12340+
...createRandomPolicy(102),
12341+
ownerAccountID,
12342+
};
12343+
12344+
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction);
12345+
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${1}`, {});
12346+
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${activePolicy.id}`, activePolicy);
12347+
const userBillingGraceEndPeriodCollection = {
12348+
[`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END}${ownerAccountID}`]: {
12349+
value: 1,
12350+
},
12351+
};
12352+
12353+
// When we call createDraftTransactionAndNavigateToParticipantSelector
12354+
createDraftTransactionAndNavigateToParticipantSelector(
12355+
transaction.transactionID,
12356+
'1',
12357+
CONST.IOU.ACTION.CATEGORIZE,
12358+
'1',
12359+
undefined,
12360+
undefined,
12361+
activePolicy,
12362+
userBillingGraceEndPeriodCollection,
12363+
);
1233012364

1233112365
// Then it should navigate to the restricted action page
1233212366
expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.RESTRICTED_ACTION.getRoute(activePolicy.id));
@@ -12354,7 +12388,7 @@ describe('ReportUtils', () => {
1235412388
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${policyExpenseReport.reportID}`, policyExpenseReport);
1235512389

1235612390
// When we call createDraftTransactionAndNavigateToParticipantSelector
12357-
createDraftTransactionAndNavigateToParticipantSelector(transaction.transactionID, '1', CONST.IOU.ACTION.CATEGORIZE, '1', undefined, undefined, activePolicy);
12391+
createDraftTransactionAndNavigateToParticipantSelector(transaction.transactionID, '1', CONST.IOU.ACTION.CATEGORIZE, '1', undefined, undefined, activePolicy, undefined);
1235812392

1235912393
// Then it should navigate to the category step
1236012394
expect(Navigation.navigate).toHaveBeenCalledWith(
@@ -12384,7 +12418,7 @@ describe('ReportUtils', () => {
1238412418
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${policyExpenseReport.reportID}`, policyExpenseReport);
1238512419

1238612420
// When we call createDraftTransactionAndNavigateToParticipantSelector with undefined activePolicy
12387-
createDraftTransactionAndNavigateToParticipantSelector(transaction.transactionID, '2', CONST.IOU.ACTION.CATEGORIZE, '2', undefined, undefined, undefined);
12421+
createDraftTransactionAndNavigateToParticipantSelector(transaction.transactionID, '2', CONST.IOU.ACTION.CATEGORIZE, '2', undefined, undefined, undefined, undefined);
1238812422

1238912423
// Then it should automatically pick the available policy and navigate to the category step
1239012424
expect(Navigation.navigate).toHaveBeenCalledWith(
@@ -12401,7 +12435,7 @@ describe('ReportUtils', () => {
1240112435
await Onyx.setCollection(ONYXKEYS.COLLECTION.POLICY, {});
1240212436

1240312437
// When we call createDraftTransactionAndNavigateToParticipantSelector with undefined activePolicy
12404-
createDraftTransactionAndNavigateToParticipantSelector(transaction.transactionID, '1', CONST.IOU.ACTION.CATEGORIZE, '1', undefined, undefined, undefined);
12438+
createDraftTransactionAndNavigateToParticipantSelector(transaction.transactionID, '1', CONST.IOU.ACTION.CATEGORIZE, '1', undefined, undefined, undefined, undefined);
1240512439

1240612440
// Then it should navigate to the upgrade page because no policies were found to categorize with
1240712441
expect(Navigation.navigate).toHaveBeenCalledWith(
@@ -12439,7 +12473,7 @@ describe('ReportUtils', () => {
1243912473
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy2.id}`, policy2);
1244012474

1244112475
// When we call createDraftTransactionAndNavigateToParticipantSelector with undefined activePolicy
12442-
createDraftTransactionAndNavigateToParticipantSelector(transaction.transactionID, '1', CONST.IOU.ACTION.CATEGORIZE, '1', undefined, undefined, undefined);
12476+
createDraftTransactionAndNavigateToParticipantSelector(transaction.transactionID, '1', CONST.IOU.ACTION.CATEGORIZE, '1', undefined, undefined, undefined, undefined);
1244312477

1244412478
// Then it should navigate to the upgrade page because it's ambiguous which policy to use
1244512479
expect(Navigation.navigate).toHaveBeenCalledWith(
@@ -12473,7 +12507,7 @@ describe('ReportUtils', () => {
1247312507
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${activePolicy.id}`, activePolicy);
1247412508

1247512509
// When we call createDraftTransactionAndNavigateToParticipantSelector
12476-
createDraftTransactionAndNavigateToParticipantSelector(transaction.transactionID, '1', CONST.IOU.ACTION.CATEGORIZE, '1', undefined, undefined, activePolicy);
12510+
createDraftTransactionAndNavigateToParticipantSelector(transaction.transactionID, '1', CONST.IOU.ACTION.CATEGORIZE, '1', undefined, undefined, activePolicy, undefined);
1247712511

1247812512
// Then it should log a warning and not navigate
1247912513
expect(logWarnSpy).toHaveBeenCalledWith('policyExpenseReportID is not valid during expense categorizing');

0 commit comments

Comments
 (0)