Skip to content

Commit 95801bd

Browse files
authored
Merge pull request Expensify#88885 from truph01/fix/66457-part-8
fix: Remove Onyx.connect() for the key: ONYXKEYS.COLLECTION.POLICY (part 8)
2 parents ba58ef6 + 855ae0b commit 95801bd

9 files changed

Lines changed: 21 additions & 18 deletions

File tree

src/components/Search/SearchList/ListItem/ExpenseReportListItem.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ function ExpenseReportListItem<TItem extends ListItem>({
144144
goToItem: () => onSelectRow(reportItem as unknown as TItem),
145145
snapshotReport,
146146
snapshotPolicy,
147+
policy: parentPolicy,
147148
lastPaymentMethod,
148149
userBillingGracePeriodEnds,
149150
currentSearchKey,
@@ -160,6 +161,7 @@ function ExpenseReportListItem<TItem extends ListItem>({
160161
onSelectRow,
161162
snapshotReport,
162163
snapshotPolicy,
164+
parentPolicy,
163165
lastPaymentMethod,
164166
userBillingGracePeriodEnds,
165167
personalPolicyID,

src/components/Search/SearchList/ListItem/ReportListItemHeader.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout';
1414
import useStyleUtils from '@hooks/useStyleUtils';
1515
import useTheme from '@hooks/useTheme';
1616
import useThemeStyles from '@hooks/useThemeStyles';
17+
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
1718
import {handleActionButtonPress} from '@userActions/Search';
1819
import CONST from '@src/CONST';
1920
import ONYXKEYS from '@src/ONYXKEYS';
@@ -219,6 +220,7 @@ function ReportListItemHeader<TItem extends ListItem>({
219220
const snapshotPolicy = useMemo(() => {
220221
return (snapshot?.data?.[`${ONYXKEYS.COLLECTION.POLICY}${reportItem.policyID}`] ?? {}) as Policy;
221222
}, [snapshot, reportItem.policyID]);
223+
const [parentPolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${getNonEmptyStringOnyxID(snapshotReport?.policyID ?? reportItem.policyID)}`);
222224
const {isDelegateAccessRestricted} = useDelegateNoAccessState();
223225
const {showDelegateNoAccessModal} = useDelegateNoAccessActions();
224226
const [amountOwed] = useOnyx(ONYXKEYS.NVP_PRIVATE_AMOUNT_OWED);
@@ -233,6 +235,7 @@ function ReportListItemHeader<TItem extends ListItem>({
233235
goToItem: () => onSelectRow(reportItem as unknown as TItem),
234236
snapshotReport,
235237
snapshotPolicy,
238+
policy: parentPolicy,
236239
lastPaymentMethod,
237240
userBillingGracePeriodEnds,
238241
currentSearchKey,

src/components/Search/SearchList/ListItem/TransactionListItem.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ function TransactionListItem<TItem extends ListItem>({
181181
goToItem: () => onSelectRow(item, transactionPreviewData),
182182
snapshotReport,
183183
snapshotPolicy,
184+
policy: parentPolicy,
184185
lastPaymentMethod,
185186
userBillingGracePeriodEnds,
186187
currentSearchKey,

src/libs/SubscriptionUtils.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,6 @@ Onyx.connect({
6060
},
6161
});
6262

63-
let deprecatedAllPolicies: OnyxCollection<Policy>;
64-
Onyx.connect({
65-
key: ONYXKEYS.COLLECTION.POLICY,
66-
callback: (value) => (deprecatedAllPolicies = value),
67-
waitForCollectionCallback: true,
68-
});
69-
7063
/**
7164
* @returns Whether the workspace owner's grace period is overdue.
7265
*/
@@ -489,13 +482,12 @@ function canCancelSubscription(
489482
* Whether the user's billable actions should be restricted.
490483
*/
491484
function shouldRestrictUserBillableActions(
492-
policyIDOrPolicy: string | OnyxEntry<Policy>,
485+
policy: OnyxEntry<Policy>,
493486
ownerBillingGracePeriodEnd: OnyxEntry<number>,
494487
userBillingGracePeriodEnds: OnyxCollection<BillingGraceEndPeriod>,
495488
amountOwed: OnyxEntry<number>,
496489
currentUserAccountID: number = deprecatedCurrentUserAccountID,
497490
): boolean {
498-
const policy = typeof policyIDOrPolicy === 'string' ? deprecatedAllPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyIDOrPolicy}`] : policyIDOrPolicy;
499491
const currentDate = new Date();
500492

501493
// This logic will be executed if the user is a workspace's non-owner (normal user or admin).

src/libs/actions/IOU/ReportWorkflow.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ function submitReport({
12511251
if (!expenseReport) {
12521252
return;
12531253
}
1254-
if (expenseReport.policyID && shouldRestrictUserBillableActions(expenseReport.policyID, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)) {
1254+
if (expenseReport.policyID && shouldRestrictUserBillableActions(policy, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)) {
12551255
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(expenseReport.policyID));
12561256
return;
12571257
}

src/libs/actions/Search.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ type HandleActionButtonPressParams = {
103103
goToItem: () => void;
104104
snapshotReport: Report;
105105
snapshotPolicy: Policy;
106+
policy: OnyxEntry<Policy>;
106107
lastPaymentMethod: OnyxEntry<LastPaymentMethod>;
107108
userBillingGracePeriodEnds: OnyxCollection<BillingGraceEndPeriod>;
108109
currentSearchKey?: SearchKey;
@@ -134,6 +135,7 @@ function handleActionButtonPress({
134135
goToItem,
135136
snapshotReport,
136137
snapshotPolicy,
138+
policy,
137139
lastPaymentMethod,
138140
userBillingGracePeriodEnds,
139141
currentSearchKey,
@@ -166,7 +168,7 @@ function handleActionButtonPress({
166168
onDelegateAccessRestricted?.();
167169
return;
168170
}
169-
if (snapshotReport.policyID && shouldRestrictUserBillableActions(snapshotReport.policyID, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)) {
171+
if (snapshotReport.policyID && shouldRestrictUserBillableActions(policy, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)) {
170172
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(snapshotReport.policyID));
171173
return;
172174
}
@@ -177,7 +179,7 @@ function handleActionButtonPress({
177179
onDelegateAccessRestricted?.();
178180
return;
179181
}
180-
if (snapshotReport.policyID && shouldRestrictUserBillableActions(snapshotReport.policyID, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)) {
182+
if (snapshotReport.policyID && shouldRestrictUserBillableActions(policy, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)) {
181183
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(snapshotReport.policyID));
182184
return;
183185
}
@@ -188,7 +190,7 @@ function handleActionButtonPress({
188190
approveMoneyRequestOnSearch(hash, item.reportID ? [item.reportID] : [], currentSearchKey);
189191
return;
190192
case CONST.SEARCH.ACTION_TYPES.SUBMIT: {
191-
if (snapshotReport.policyID && shouldRestrictUserBillableActions(snapshotReport.policyID, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)) {
193+
if (snapshotReport.policyID && shouldRestrictUserBillableActions(policy, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)) {
192194
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(snapshotReport.policyID));
193195
return;
194196
}
@@ -200,8 +202,8 @@ function handleActionButtonPress({
200202
return;
201203
}
202204

203-
const policy = snapshotPolicy ?? {};
204-
const connectedIntegration = getValidConnectedIntegration(policy);
205+
const exportPolicy = snapshotPolicy ?? {};
206+
const connectedIntegration = getValidConnectedIntegration(exportPolicy);
205207

206208
if (!connectedIntegration) {
207209
return;
@@ -1410,7 +1412,7 @@ function handleBulkPayItemSelected(params: {
14101412
return;
14111413
}
14121414

1413-
if (policy && shouldRestrictUserBillableActions(policy?.id, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)) {
1415+
if (policy && shouldRestrictUserBillableActions(policy, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)) {
14141416
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(policy?.id));
14151417
return;
14161418
}

src/pages/iou/request/step/BaseRequestStepWorkspace.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function BaseRequestStepWorkspace({transaction, getPolicies, onSelectWorkspace}:
8282
const selectWorkspace = (item: WorkspaceListItem) => {
8383
const policyID = item.policyID;
8484
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
85-
if (shouldRestrictUserBillableActions(policy ?? policyID, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed, currentUserAccountID)) {
85+
if (shouldRestrictUserBillableActions(policy, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed, currentUserAccountID)) {
8686
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(policyID));
8787
return;
8888
}

tests/unit/Search/handleActionButtonPressTest.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ describe('handleActionButtonPress', () => {
328328
amountOwed: undefined,
329329
userBillingGracePeriodEnds: undefined,
330330
onHoldMenuOpen: jest.fn(),
331+
policy: snapshotPolicy as Policy,
331332
});
332333
expect(goToItem).not.toHaveBeenCalled();
333334
});
@@ -346,6 +347,7 @@ describe('handleActionButtonPress', () => {
346347
ownerBillingGracePeriodEnd: undefined,
347348
amountOwed: undefined,
348349
onHoldMenuOpen,
350+
policy: snapshotPolicy as Policy,
349351
});
350352

351353
expect(onHoldMenuOpen).toHaveBeenCalledWith(mockReportItemWithHold, CONST.IOU.REPORT_ACTION_TYPE.APPROVE);
@@ -364,6 +366,7 @@ describe('handleActionButtonPress', () => {
364366
ownerBillingGracePeriodEnd: undefined,
365367
amountOwed: undefined,
366368
userBillingGracePeriodEnds: undefined,
369+
policy: snapshotPolicy as Policy,
367370
});
368371
expect(goToItem).toHaveBeenCalledTimes(0);
369372
});

tests/unit/SubscriptionUtilsTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ describe('SubscriptionUtils', () => {
326326
});
327327

328328
it("should return false if the user isn't a workspace's owner or isn't a member of any past due billing workspace", () => {
329-
expect(shouldRestrictUserBillableActions('1', undefined, undefined, undefined)).toBeFalsy();
329+
expect(shouldRestrictUserBillableActions(undefined, undefined, undefined, undefined)).toBeFalsy();
330330
});
331331

332332
it('should return false if the user is a non-owner of a workspace that is not in the shared NVP collection', () => {

0 commit comments

Comments
 (0)