Skip to content

Commit 5c1ebfc

Browse files
authored
Merge pull request #88786 from parasharrajat/onyx/session-16
Refactor canIOUBePaid function
2 parents 82eeb78 + 18dde05 commit 5c1ebfc

13 files changed

Lines changed: 171 additions & 49 deletions

File tree

src/components/MoneyReportHeaderActions/MoneyReportHeaderSecondaryActions.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,9 @@ function MoneyReportHeaderSecondaryActionsInner({reportID, primaryAction, isRepo
213213
};
214214

215215
// Payment button derivations
216-
const canIOUBePaid = canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList, undefined, false, undefined, invoiceReceiverPolicy);
217-
const onlyShowPayElsewhere = !canIOUBePaid && canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList, undefined, true, undefined, invoiceReceiverPolicy);
216+
const canIOUBePaid = canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList, currentUserLogin ?? '', accountID, undefined, false, undefined, invoiceReceiverPolicy);
217+
const onlyShowPayElsewhere =
218+
!canIOUBePaid && canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList, currentUserLogin ?? '', accountID, undefined, true, undefined, invoiceReceiverPolicy);
218219
const shouldShowPayButton = isPaidAnimationRunning || canIOUBePaid || onlyShowPayElsewhere;
219220
const hasOnlyPendingTransactions = allTransactions.length > 0 && allTransactions.every((t) => isExpensifyCardTransaction(t) && isPending(t));
220221
const shouldShowApproveButton =

src/components/MoneyReportHeaderActions/MoneyReportHeaderSelectionDropdown.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,9 @@ function MoneyReportHeaderSelectionDropdown({reportID, primaryAction, isReportIn
223223

224224
const canAllowSettlement = hasUpdatedTotal(moneyRequestReport, policy);
225225
const totalAmount = getTotalAmountForIOUReportPreviewButton(moneyRequestReport, policy, CONST.REPORT.PRIMARY_ACTIONS.PAY, nonPendingDeleteTransactions, convertToDisplayString);
226-
const canIOUBePaid = canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList, undefined, false, undefined, invoiceReceiverPolicy);
227-
const onlyShowPayElsewhere = !canIOUBePaid && canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList, undefined, true, undefined, invoiceReceiverPolicy);
226+
const canIOUBePaid = canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList, currentUserLogin ?? '', accountID, undefined, false, undefined, invoiceReceiverPolicy);
227+
const onlyShowPayElsewhere =
228+
!canIOUBePaid && canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList, currentUserLogin ?? '', accountID, undefined, true, undefined, invoiceReceiverPolicy);
228229
const isPayable = hasPayAction && canIOUBePaid;
229230

230231
const confirmPayment = ({paymentType: type, payAsBusiness, methodID, paymentMethod}: PaymentActionParams) => {

src/components/MoneyReportHeaderModals.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, {useRef, useState} from 'react';
22
import type {ReactNode} from 'react';
33
// eslint-disable-next-line no-restricted-imports
44
import {InteractionManager} from 'react-native';
5+
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
56
import useDecisionModal from '@hooks/useDecisionModal';
67
import useHoldMenuModal from '@hooks/useHoldMenuModal';
78
import useLocalize from '@hooks/useLocalize';
@@ -39,10 +40,11 @@ function MoneyReportHeaderModals({reportID, children}: MoneyReportHeaderModalsPr
3940

4041
const {transactions: reportTransactions} = useTransactionsAndViolationsForReport(moneyRequestReport?.reportID);
4142
const transactions = Object.values(reportTransactions);
43+
const {accountID, login: currentUserLogin} = useCurrentUserPersonalDetails();
4244

4345
// Derive data for hold menu
44-
const canIOUBePaid = canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList);
45-
const onlyShowPayElsewhere = !canIOUBePaid && canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList, undefined, true);
46+
const canIOUBePaid = canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList, currentUserLogin ?? '', accountID);
47+
const onlyShowPayElsewhere = !canIOUBePaid && canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList, currentUserLogin ?? '', accountID, undefined, true);
4648
const shouldShowPayButton = canIOUBePaid || onlyShowPayElsewhere;
4749
const {nonHeldAmount, fullAmount, hasValidNonHeldAmount} = getNonHeldAndFullAmount(moneyRequestReport, shouldShowPayButton);
4850
const hasOnlyHeldExpenses = hasOnlyHeldExpensesReportUtils(moneyRequestReport?.reportID, transactions);

src/components/MoneyReportHeaderPrimaryAction/PayPrimaryAction.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,32 @@ function PayPrimaryAction({reportID, chatReportID}: PayPrimaryActionProps) {
7373
const hasOnlyPendingTransactions = transactions.length > 0 && transactions.every((t) => isExpensifyCardTransaction(t) && isPending(t));
7474
const nonPendingDeleteTransactions = transactions.filter((t): t is Transaction => !!t && (isOffline || t.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE));
7575

76-
const canIOUBePaid = canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList, transaction ? [transaction] : undefined, false, undefined, invoiceReceiverPolicy);
76+
const canIOUBePaid = canIOUBePaidAction(
77+
moneyRequestReport,
78+
chatReport,
79+
policy,
80+
bankAccountList,
81+
currentUserLogin ?? '',
82+
accountID,
83+
transaction ? [transaction] : undefined,
84+
false,
85+
undefined,
86+
invoiceReceiverPolicy,
87+
);
7788
const onlyShowPayElsewhere =
78-
!canIOUBePaid && canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList, transaction ? [transaction] : undefined, true, undefined, invoiceReceiverPolicy);
89+
!canIOUBePaid &&
90+
canIOUBePaidAction(
91+
moneyRequestReport,
92+
chatReport,
93+
policy,
94+
bankAccountList,
95+
currentUserLogin ?? '',
96+
accountID,
97+
transaction ? [transaction] : undefined,
98+
true,
99+
undefined,
100+
invoiceReceiverPolicy,
101+
);
79102
const shouldShowPayButton = isPaidAnimationRunning || canIOUBePaid || onlyShowPayElsewhere;
80103
const shouldShowApproveButton = (canApproveIOU(moneyRequestReport, policy, reportMetadata, accountID, transactions) && !hasOnlyPendingTransactions) || isApprovedAnimationRunning;
81104
const shouldDisableApproveButton = shouldShowApproveButton && !isAllowedToApproveExpenseReport(moneyRequestReport);

src/components/ReportActionItem/MoneyRequestReportPreview/PayActionButton.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,32 @@ function PayActionButton({
102102
const canAllowSettlement = hasUpdatedTotal(iouReport, policy);
103103
const hasViolations = hasViolationsReportUtils(iouReport?.reportID, transactionViolations, currentUserAccountID, currentUserEmail);
104104

105-
const canIOUBePaid = canIOUBePaidIOUActions(iouReport, chatReport, policy, bankAccountList, transactions, false, undefined, invoiceReceiverPolicy);
106-
const onlyShowPayElsewhere = !canIOUBePaid && canIOUBePaidIOUActions(iouReport, chatReport, policy, bankAccountList, transactions, true, undefined, invoiceReceiverPolicy);
105+
const canIOUBePaid = canIOUBePaidIOUActions(
106+
iouReport,
107+
chatReport,
108+
policy,
109+
bankAccountList,
110+
currentUserDetails.login ?? '',
111+
currentUserDetails.accountID,
112+
transactions,
113+
false,
114+
undefined,
115+
invoiceReceiverPolicy,
116+
);
117+
const onlyShowPayElsewhere =
118+
!canIOUBePaid &&
119+
canIOUBePaidIOUActions(
120+
iouReport,
121+
chatReport,
122+
policy,
123+
bankAccountList,
124+
currentUserDetails.login ?? '',
125+
currentUserDetails.accountID,
126+
transactions,
127+
true,
128+
undefined,
129+
invoiceReceiverPolicy,
130+
);
107131
const shouldShowPayButton = isPaidAnimationRunning || canIOUBePaid || onlyShowPayElsewhere;
108132
const shouldShowOnlyPayElsewhere = !canIOUBePaid && onlyShowPayElsewhere;
109133
const canIOUBePaidAndApproved = canIOUBePaid;

src/components/ReportActionItem/MoneyRequestReportPreview/ReportPreviewActionButton.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,32 @@ function ReportPreviewActionButton({
9393
const isDEWSubmitPending = hasPendingDEWSubmit(iouReportMetadata, isDEWPolicy);
9494
const connectedIntegration = getConnectedIntegration(policy);
9595

96-
const canIOUBePaid = canIOUBePaidIOUActions(iouReport, chatReport, policy, bankAccountList, transactions, false, undefined, invoiceReceiverPolicy);
97-
const onlyShowPayElsewhere = !canIOUBePaid && canIOUBePaidIOUActions(iouReport, chatReport, policy, bankAccountList, transactions, true, undefined, invoiceReceiverPolicy);
96+
const canIOUBePaid = canIOUBePaidIOUActions(
97+
iouReport,
98+
chatReport,
99+
policy,
100+
bankAccountList,
101+
currentUserDetails.login ?? '',
102+
currentUserDetails.accountID,
103+
transactions,
104+
false,
105+
undefined,
106+
invoiceReceiverPolicy,
107+
);
108+
const onlyShowPayElsewhere =
109+
!canIOUBePaid &&
110+
canIOUBePaidIOUActions(
111+
iouReport,
112+
chatReport,
113+
policy,
114+
bankAccountList,
115+
currentUserDetails.login ?? '',
116+
currentUserDetails.accountID,
117+
transactions,
118+
true,
119+
undefined,
120+
invoiceReceiverPolicy,
121+
);
98122
const shouldShowPayButton = isPaidAnimationRunning || canIOUBePaid || onlyShowPayElsewhere;
99123

100124
const buttonMaxWidth =

src/components/Search/SearchList/ListItem/ActionCell/PayActionCell.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {SearchScopeProvider} from '@components/Search/SearchScopeProvider';
55
import SettlementButton from '@components/SettlementButton';
66
import type {PaymentActionParams} from '@components/SettlementButton/types';
77
import {useCurrencyListActions} from '@hooks/useCurrencyList';
8+
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
89
import useNetwork from '@hooks/useNetwork';
910
import useOnyx from '@hooks/useOnyx';
1011
import usePolicy from '@hooks/usePolicy';
@@ -39,8 +40,11 @@ function PayActionCell({isLoading, policyID, reportID, hash, amount, extraSmall,
3940
const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${iouReport?.chatReportID}`);
4041
const invoiceReceiverPolicyID = chatReport?.invoiceReceiver && 'policyID' in chatReport.invoiceReceiver ? chatReport.invoiceReceiver.policyID : undefined;
4142
const invoiceReceiverPolicy = usePolicy(invoiceReceiverPolicyID);
42-
const canBePaid = canIOUBePaid(iouReport, chatReport, policy, bankAccountList, transactions, false, undefined, invoiceReceiverPolicy);
43-
const shouldOnlyShowElsewhere = !canBePaid && canIOUBePaid(iouReport, chatReport, policy, bankAccountList, transactions, true, undefined, invoiceReceiverPolicy);
43+
const {login: currentUserLogin, accountID: currentUserAccountID} = useCurrentUserPersonalDetails();
44+
45+
const canBePaid = canIOUBePaid(iouReport, chatReport, policy, bankAccountList, currentUserLogin ?? '', currentUserAccountID, transactions, false, undefined, invoiceReceiverPolicy);
46+
const shouldOnlyShowElsewhere =
47+
!canBePaid && canIOUBePaid(iouReport, chatReport, policy, bankAccountList, currentUserLogin ?? '', currentUserAccountID, transactions, true, undefined, invoiceReceiverPolicy);
4448

4549
const {currency} = iouReport ?? {};
4650

src/hooks/useSearchBulkActions.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,41 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
328328
const invoiceReceiverPolicy = invoiceReceiverPolicyID ? currentSearchResults?.data?.[`${ONYXKEYS.COLLECTION.POLICY}${invoiceReceiverPolicyID}`] : undefined;
329329
return (
330330
report &&
331-
!canIOUBePaid(report, chatReport, selectedPolicy, bankAccountList, undefined, false, undefined, invoiceReceiverPolicy) &&
332-
canIOUBePaid(report, chatReport, selectedPolicy, bankAccountList, undefined, true, undefined, invoiceReceiverPolicy)
331+
!canIOUBePaid(
332+
report,
333+
chatReport,
334+
selectedPolicy,
335+
bankAccountList,
336+
currentUserPersonalDetails?.login ?? '',
337+
currentUserPersonalDetails.accountID,
338+
undefined,
339+
false,
340+
undefined,
341+
invoiceReceiverPolicy,
342+
) &&
343+
canIOUBePaid(
344+
report,
345+
chatReport,
346+
selectedPolicy,
347+
bankAccountList,
348+
currentUserPersonalDetails?.login ?? '',
349+
currentUserPersonalDetails.accountID,
350+
undefined,
351+
true,
352+
undefined,
353+
invoiceReceiverPolicy,
354+
)
333355
);
334356
});
335-
}, [currentSearchResults?.data, selectedPolicyIDs, selectedReportIDs, selectedTransactionReportIDs, bankAccountList]);
357+
}, [
358+
selectedPolicyIDs,
359+
currentSearchResults?.data,
360+
selectedTransactionReportIDs,
361+
selectedReportIDs,
362+
bankAccountList,
363+
currentUserPersonalDetails?.login,
364+
currentUserPersonalDetails.accountID,
365+
]);
336366

337367
const {bulkPayButtonOptions, businessBankAccountOptions, shouldShowBusinessBankAccountOptions} = useBulkPayOptions({
338368
selectedPolicyID: selectedPolicyIDs.at(0),

src/hooks/useSelectionModeReportActions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ function useSelectionModeReportActions({
163163
const nonPendingDeleteTransactions = transactions.filter((t): t is OnyxTypes.Transaction => !!t && (isOffline || t.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE));
164164

165165
const getCanIOUBePaid = (onlyShowPayElsewhere = false) =>
166-
canIOUBePaidAction(report, chatReport, policy, bankAccountList, transactions, onlyShowPayElsewhere, undefined, invoiceReceiverPolicy);
166+
canIOUBePaidAction(report, chatReport, policy, bankAccountList, currentUserLogin ?? '', currentUserAccountID, transactions, onlyShowPayElsewhere, undefined, invoiceReceiverPolicy);
167167

168168
const canIOUBePaid = getCanIOUBePaid();
169169
const onlyShowPayElsewhere = !canIOUBePaid && getCanIOUBePaid(true);

src/libs/ReportUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2893,7 +2893,7 @@ function hasOutstandingChildRequest(
28932893
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
28942894
const invoiceReceiverPolicy = getPolicy(invoiceReceiverPolicyID);
28952895
return (
2896-
canIOUBePaid(iouReport, chatReport, policy, bankAccountList, transactions, undefined, undefined, invoiceReceiverPolicy) ||
2896+
canIOUBePaid(iouReport, chatReport, policy, bankAccountList, currentUserEmailParam, currentUserAccountIDParam, transactions, undefined, undefined, invoiceReceiverPolicy) ||
28972897
canApproveIOU(iouReport, policy, reportMetadata, currentUserAccountIDParam, transactions) ||
28982898
canSubmitAndIsAwaitingForCurrentUser(
28992899
iouReport,

0 commit comments

Comments
 (0)