Skip to content

Commit fdd80f7

Browse files
authored
Merge pull request Expensify#86615 from truph01/fix/66457-part-2
fix: Remove Onyx.connect() for the key: ONYXKEYS.COLLECTION.POLICY
2 parents 43557be + 308bf9a commit fdd80f7

10 files changed

Lines changed: 144 additions & 17 deletions

File tree

src/components/FloatingCameraButton/BaseFloatingCameraButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function BaseFloatingCameraButton({icon}: BaseFloatingCameraButtonProps) {
6262
interceptAnonymousUser(() => {
6363
if (
6464
policyChatForActivePolicy?.policyID &&
65-
shouldRestrictUserBillableActions(policyChatForActivePolicy.policyID, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)
65+
shouldRestrictUserBillableActions(policyChatForActivePolicy.policyID, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed, activePolicy)
6666
) {
6767
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(policyChatForActivePolicy.policyID));
6868
return;

src/components/MoneyReportHeader.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1915,7 +1915,7 @@ function MoneyReportHeader({reportID: reportIDProp, shouldDisplayBackButton = fa
19151915
if (!moneyRequestReport?.reportID) {
19161916
return;
19171917
}
1918-
if (policy && shouldRestrictUserBillableActions(policy.id, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)) {
1918+
if (policy && shouldRestrictUserBillableActions(policy.id, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed, policy)) {
19191919
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(policy.id));
19201920
return;
19211921
}

src/components/MoneyRequestReportView/SearchMoneyRequestReportEmptyState.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function SearchMoneyRequestReportEmptyState({report, policy, onLayout}: {report:
4444
if (!reportId) {
4545
return;
4646
}
47-
if (policy && shouldRestrictUserBillableActions(policy.id, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)) {
47+
if (policy && shouldRestrictUserBillableActions(policy.id, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed, policy)) {
4848
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(policy.id));
4949
return;
5050
}
@@ -59,7 +59,7 @@ function SearchMoneyRequestReportEmptyState({report, policy, onLayout}: {report:
5959
if (!reportId) {
6060
return;
6161
}
62-
if (policy && shouldRestrictUserBillableActions(policy.id, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)) {
62+
if (policy && shouldRestrictUserBillableActions(policy.id, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed, policy)) {
6363
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(policy.id));
6464
return;
6565
}
@@ -71,7 +71,7 @@ function SearchMoneyRequestReportEmptyState({report, policy, onLayout}: {report:
7171
text: translate('iou.addUnreportedExpense'),
7272
icon: icons.ReceiptPlus,
7373
onSelected: () => {
74-
if (policy && shouldRestrictUserBillableActions(policy.id, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)) {
74+
if (policy && shouldRestrictUserBillableActions(policy.id, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed, policy)) {
7575
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(policy.id));
7676
return;
7777
}

src/components/Navigation/QuickCreationActionsBar/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,14 @@ function QuickCreationActionsBar() {
175175

176176
if (
177177
!workspaceIDForReportCreation ||
178-
(shouldRestrictUserBillableActions(workspaceIDForReportCreation, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds) && groupPoliciesWithChatEnabled.length > 1)
178+
(shouldRestrictUserBillableActions(workspaceIDForReportCreation, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, undefined, defaultChatEnabledPolicy) &&
179+
groupPoliciesWithChatEnabled.length > 1)
179180
) {
180181
Navigation.navigate(ROUTES.NEW_REPORT_WORKSPACE_SELECTION.getRoute());
181182
return;
182183
}
183184

184-
if (!shouldRestrictUserBillableActions(workspaceIDForReportCreation, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds)) {
185+
if (!shouldRestrictUserBillableActions(workspaceIDForReportCreation, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, undefined, defaultChatEnabledPolicy)) {
185186
if (shouldShowEmptyReportConfirmationForDefaultChatEnabledPolicy) {
186187
openCreateReportConfirmation();
187188
} else {
@@ -199,6 +200,7 @@ function QuickCreationActionsBar() {
199200
defaultChatEnabledPolicyID,
200201
userBillingGracePeriodEnds,
201202
ownerBillingGracePeriodEnd,
203+
defaultChatEnabledPolicy,
202204
groupPoliciesWithChatEnabled.length,
203205
shouldShowEmptyReportConfirmationForDefaultChatEnabledPolicy,
204206
openCreateReportConfirmation,

src/components/SettlementButton/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ function SettlementButton({
222222
return true;
223223
}
224224

225-
if (policy && shouldRestrictUserBillableActions(policy.id, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)) {
225+
if (policy && shouldRestrictUserBillableActions(policy.id, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed, policy)) {
226226
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(policy.id));
227227
return true;
228228
}

src/hooks/useReceiptScanDrop.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function useReceiptScanDrop() {
8181
if (
8282
isPaidGroupPolicy(activePolicy) &&
8383
activePolicy?.isPolicyExpenseChatEnabled &&
84-
!shouldRestrictUserBillableActions(activePolicy.id, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)
84+
!shouldRestrictUserBillableActions(activePolicy.id, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed, activePolicy)
8585
) {
8686
const shouldAutoReport = !!activePolicy?.autoReporting || !!personalPolicy?.autoReporting;
8787
const report = shouldAutoReport ? getPolicyExpenseChat(currentUserPersonalDetails.accountID, activePolicy?.id) : selfDMReport;

src/hooks/useSearchBulkActions.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import {canIOUBePaid, dismissRejectUseExplanation, initBulkEditDraftTransaction}
5656
import CONST from '@src/CONST';
5757
import ONYXKEYS from '@src/ONYXKEYS';
5858
import ROUTES from '@src/ROUTES';
59-
import type {BillingGraceEndPeriod, Report, SearchResults, Transaction, TransactionViolations} from '@src/types/onyx';
59+
import type {BillingGraceEndPeriod, Policy, Report, SearchResults, Transaction, TransactionViolations} from '@src/types/onyx';
6060
import type DeepValueOf from '@src/types/utils/DeepValueOf';
6161
import useAllTransactions from './useAllTransactions';
6262
import useBulkPayOptions from './useBulkPayOptions';
@@ -85,10 +85,15 @@ function getRestrictedPolicyID(
8585
billingGracePeriods: OnyxCollection<BillingGraceEndPeriod>,
8686
ownerBillingGracePeriodEnd: OnyxEntry<number>,
8787
amountOwed: OnyxEntry<number>,
88+
allPolicies: OnyxCollection<Policy>,
8889
): string | undefined {
8990
return items
9091
.map((item) => item.policyID)
91-
.find((policyID): policyID is string => !!policyID && shouldRestrictUserBillableActions(policyID, ownerBillingGracePeriodEnd, billingGracePeriods, amountOwed));
92+
.find(
93+
(policyID): policyID is string =>
94+
!!policyID &&
95+
shouldRestrictUserBillableActions(policyID, ownerBillingGracePeriodEnd, billingGracePeriods, amountOwed, allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]),
96+
);
9297
}
9398

9499
function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
@@ -400,7 +405,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
400405

401406
const selectedItems = selectedReports.length ? selectedReports : Object.values(selectedTransactions);
402407

403-
const restrictedPolicyID = getRestrictedPolicyID(selectedItems, userBillingGracePeriodEnds, ownerBillingGracePeriodEnd, amountOwed);
408+
const restrictedPolicyID = getRestrictedPolicyID(selectedItems, userBillingGracePeriodEnds, ownerBillingGracePeriodEnd, amountOwed, policies);
404409
if (restrictedPolicyID) {
405410
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(restrictedPolicyID));
406411
return;
@@ -428,6 +433,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
428433
userBillingGracePeriodEnds,
429434
ownerBillingGracePeriodEnd,
430435
amountOwed,
436+
policies,
431437
]);
432438

433439
const {expenseCount, uniqueReportCount} = useMemo(() => {
@@ -557,7 +563,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
557563
const selectedOptions = selectedReports.length ? selectedReports : Object.values(selectedTransactions);
558564
const expenseReportBankAccountID = additionalData?.bankAccountID;
559565

560-
const restrictedPolicyID = getRestrictedPolicyID(selectedOptions, userBillingGracePeriodEnds, ownerBillingGracePeriodEnd, amountOwed);
566+
const restrictedPolicyID = getRestrictedPolicyID(selectedOptions, userBillingGracePeriodEnds, ownerBillingGracePeriodEnd, amountOwed, policies);
561567
if (restrictedPolicyID) {
562568
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(restrictedPolicyID));
563569
return;
@@ -1018,7 +1024,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
10181024

10191025
const itemList = !selectedReports.length ? Object.values(selectedTransactions).map((transaction) => transaction) : (selectedReports?.filter((report) => !!report) ?? []);
10201026

1021-
const restrictedPolicyID = getRestrictedPolicyID(itemList, userBillingGracePeriodEnds, ownerBillingGracePeriodEnd, amountOwed);
1027+
const restrictedPolicyID = getRestrictedPolicyID(itemList, userBillingGracePeriodEnds, ownerBillingGracePeriodEnd, amountOwed, policies);
10221028
if (restrictedPolicyID) {
10231029
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(restrictedPolicyID));
10241030
return;

src/libs/SubscriptionUtils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,10 @@ function shouldRestrictUserBillableActions(
460460
ownerBillingGracePeriodEnd: OnyxEntry<number>,
461461
userBillingGracePeriodEnds: OnyxCollection<BillingGraceEndPeriod>,
462462
amountOwed: OnyxEntry<number> = privateAmountOwed,
463+
policy: OnyxEntry<Policy> = deprecatedAllPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`],
463464
): boolean {
464465
const currentDate = new Date();
465466

466-
const policy = deprecatedAllPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
467-
468467
// This logic will be executed if the user is a workspace's non-owner (normal user or admin).
469468
// We should restrict the workspace's non-owner actions if it's member of a workspace where the owner is
470469
// past due and is past its grace period end.

src/pages/AddUnreportedExpense.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,11 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) {
296296
{
297297
buttonText: translate('iou.createExpense'),
298298
buttonAction: () => {
299-
if (report && report.policyID && shouldRestrictUserBillableActions(report.policyID, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds)) {
299+
if (
300+
report &&
301+
report.policyID &&
302+
shouldRestrictUserBillableActions(report.policyID, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, undefined, policy)
303+
) {
300304
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(report.policyID));
301305
return;
302306
}

tests/unit/SubscriptionUtilsTest.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,122 @@ describe('SubscriptionUtils', () => {
522522
});
523523
});
524524

525+
describe('shouldRestrictUserBillableActions - policy parameter', () => {
526+
afterEach(async () => {
527+
await Onyx.clear();
528+
await Onyx.multiSet({
529+
[ONYXKEYS.SESSION]: null,
530+
[ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END]: null,
531+
[ONYXKEYS.NVP_PRIVATE_AMOUNT_OWED]: null,
532+
[ONYXKEYS.COLLECTION.POLICY]: null,
533+
});
534+
});
535+
536+
it('should restrict when policy is passed directly and owner is past due', async () => {
537+
const accountID = 1;
538+
const policyID = '2001';
539+
const policy = {
540+
...createRandomPolicy(Number(policyID)),
541+
ownerAccountID: accountID,
542+
};
543+
544+
await Onyx.multiSet({
545+
[ONYXKEYS.SESSION]: {email: '', accountID},
546+
[ONYXKEYS.NVP_PRIVATE_AMOUNT_OWED]: 8010,
547+
});
548+
549+
expect(shouldRestrictUserBillableActions(policyID, getUnixTime(subDays(new Date(), 3)), undefined, undefined, policy)).toBeTruthy();
550+
});
551+
552+
it('should not restrict when policy is passed directly but owner is not past due', async () => {
553+
const accountID = 1;
554+
const policyID = '2001';
555+
const policy = {
556+
...createRandomPolicy(Number(policyID)),
557+
ownerAccountID: accountID,
558+
};
559+
560+
await Onyx.multiSet({
561+
[ONYXKEYS.SESSION]: {email: '', accountID},
562+
[ONYXKEYS.NVP_PRIVATE_AMOUNT_OWED]: 8010,
563+
});
564+
565+
expect(shouldRestrictUserBillableActions(policyID, getUnixTime(addDays(new Date(), 3)), undefined, undefined, policy)).toBeFalsy();
566+
});
567+
568+
it('should not restrict when policy is passed as undefined', () => {
569+
expect(shouldRestrictUserBillableActions('nonexistent', getUnixTime(subDays(new Date(), 3)), undefined, 500, undefined)).toBeFalsy();
570+
});
571+
572+
it('should restrict for non-owner when policy is passed directly and billing grace period is overdue', async () => {
573+
const policyID = '2001';
574+
const ownerAccountID = 2001;
575+
const policy = {
576+
...createRandomPolicy(Number(policyID)),
577+
ownerAccountID,
578+
};
579+
580+
expect(
581+
shouldRestrictUserBillableActions(
582+
policyID,
583+
undefined,
584+
{
585+
[`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END}${ownerAccountID}` as const]: {
586+
...billingGraceEndPeriod,
587+
value: getUnixTime(subDays(new Date(), 3)),
588+
},
589+
},
590+
undefined,
591+
policy,
592+
),
593+
).toBeTruthy();
594+
});
595+
596+
it('should not restrict for non-owner when policy is passed directly but billing grace period is not overdue', async () => {
597+
const policyID = '2001';
598+
const ownerAccountID = 2001;
599+
const policy = {
600+
...createRandomPolicy(Number(policyID)),
601+
ownerAccountID,
602+
};
603+
604+
expect(
605+
shouldRestrictUserBillableActions(
606+
policyID,
607+
undefined,
608+
{
609+
[`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END}${ownerAccountID}` as const]: {
610+
...billingGraceEndPeriod,
611+
value: getUnixTime(addDays(new Date(), 3)),
612+
},
613+
},
614+
undefined,
615+
policy,
616+
),
617+
).toBeFalsy();
618+
});
619+
620+
it('should use passed policy and ignore Onyx-stored policies', async () => {
621+
const accountID = 1;
622+
const policyID = '2001';
623+
const differentOwnerPolicy = {
624+
...createRandomPolicy(Number(policyID)),
625+
ownerAccountID: 9999,
626+
};
627+
628+
await Onyx.multiSet({
629+
[ONYXKEYS.SESSION]: {email: '', accountID},
630+
[ONYXKEYS.NVP_PRIVATE_AMOUNT_OWED]: 8010,
631+
[`${ONYXKEYS.COLLECTION.POLICY}${policyID}` as const]: {
632+
...createRandomPolicy(Number(policyID)),
633+
ownerAccountID: accountID,
634+
},
635+
});
636+
637+
expect(shouldRestrictUserBillableActions(policyID, getUnixTime(subDays(new Date(), 3)), undefined, undefined, differentOwnerPolicy)).toBeFalsy();
638+
});
639+
});
640+
525641
describe('getSubscriptionStatus', () => {
526642
afterEach(async () => {
527643
await Onyx.clear();

0 commit comments

Comments
 (0)