Skip to content

Commit 5d2b788

Browse files
committed
Refactor canApproveIOU function
1 parent 80fe088 commit 5d2b788

8 files changed

Lines changed: 22 additions & 20 deletions

File tree

src/components/MoneyReportHeaderActions/MoneyReportHeaderSecondaryActions.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ function MoneyReportHeaderSecondaryActionsInner({reportID, primaryAction, isRepo
207207
const onlyShowPayElsewhere = !canIOUBePaid && canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList, undefined, true, undefined, invoiceReceiverPolicy);
208208
const shouldShowPayButton = isPaidAnimationRunning || canIOUBePaid || onlyShowPayElsewhere;
209209
const hasOnlyPendingTransactions = allTransactions.length > 0 && allTransactions.every((t) => isExpensifyCardTransaction(t) && isPending(t));
210-
const shouldShowApproveButton = (canApproveIOU(moneyRequestReport, policy, reportMetadata, allTransactions) && !hasOnlyPendingTransactions) || isApprovedAnimationRunning;
210+
const shouldShowApproveButton =
211+
(canApproveIOU(moneyRequestReport, policy, reportMetadata, currentUserPersonalDetails.accountID, allTransactions) && !hasOnlyPendingTransactions) || isApprovedAnimationRunning;
211212
const isApproveDisabled = shouldShowApproveButton && !isAllowedToApproveExpenseReport(moneyRequestReport);
212213

213214
const totalAmount = getTotalAmountForIOUReportPreviewButton(moneyRequestReport, policy, CONST.REPORT.PRIMARY_ACTIONS.PAY, nonPendingDeleteTransactions);

src/components/MoneyReportHeaderPrimaryAction/PayPrimaryAction.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function PayPrimaryAction({reportID, chatReportID}: PayPrimaryActionProps) {
6969
const onlyShowPayElsewhere =
7070
!canIOUBePaid && canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList, transaction ? [transaction] : undefined, true, undefined, invoiceReceiverPolicy);
7171
const shouldShowPayButton = isPaidAnimationRunning || canIOUBePaid || onlyShowPayElsewhere;
72-
const shouldShowApproveButton = (canApproveIOU(moneyRequestReport, policy, reportMetadata, transactions) && !hasOnlyPendingTransactions) || isApprovedAnimationRunning;
72+
const shouldShowApproveButton = (canApproveIOU(moneyRequestReport, policy, reportMetadata, accountID, transactions) && !hasOnlyPendingTransactions) || isApprovedAnimationRunning;
7373
const shouldDisableApproveButton = shouldShowApproveButton && !isAllowedToApproveExpenseReport(moneyRequestReport);
7474
const canAllowSettlement = hasUpdatedTotal(moneyRequestReport, policy);
7575
const totalAmount = getTotalAmountForIOUReportPreviewButton(moneyRequestReport, policy, CONST.REPORT.PRIMARY_ACTIONS.PAY, nonPendingDeleteTransactions);

src/hooks/useSelectionModeReportActions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function useSelectionModeReportActions({
165165

166166
const {nonHeldAmount, fullAmount, hasValidNonHeldAmount} = getNonHeldAndFullAmount(report, shouldShowPayButton);
167167

168-
const shouldShowApproveButton = canApproveIOU(report, policy, reportMetadata, transactions) && !hasOnlyPendingTransactions;
168+
const shouldShowApproveButton = canApproveIOU(report, policy, reportMetadata, currentUserAccountID, transactions) && !hasOnlyPendingTransactions;
169169

170170
const shouldDisableApproveButton = shouldShowApproveButton && !isAllowedToApproveExpenseReport(report);
171171

src/libs/ReportUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2900,7 +2900,7 @@ function hasOutstandingChildRequest(
29002900
const invoiceReceiverPolicy = getPolicy(invoiceReceiverPolicyID);
29012901
return (
29022902
canIOUBePaid(iouReport, chatReport, policy, bankAccountList, transactions, undefined, undefined, invoiceReceiverPolicy) ||
2903-
canApproveIOU(iouReport, policy, reportMetadata, transactions) ||
2903+
canApproveIOU(iouReport, policy, reportMetadata, currentUserAccountIDParam, transactions) ||
29042904
canSubmitAndIsAwaitingForCurrentUser(
29052905
iouReport,
29062906
chatReport,

src/libs/SearchUIUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2333,7 +2333,7 @@ function getActions(
23332333

23342334
// We're not supporting approve partial amount on search page now
23352335
if (
2336-
canApproveIOU(report, policy, reportMetadata, allReportTransactions) &&
2336+
canApproveIOU(report, policy, reportMetadata, currentUserAccountID, allReportTransactions) &&
23372337
isAllowedToApproveExpenseReport &&
23382338
!hasOnlyPendingCardOrScanningTransactions &&
23392339
!hasHeldExpenses(report.reportID, allReportTransactions)

src/libs/actions/IOU/ReportWorkflow.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ function canApproveIOU(
113113
iouReport: OnyxTypes.OnyxInputOrEntry<OnyxTypes.Report>,
114114
policy: OnyxTypes.OnyxInputOrEntry<OnyxTypes.Policy>,
115115
reportMetadata: OnyxEntry<OnyxTypes.ReportMetadata>,
116+
currentUserAccountID: number,
116117
iouTransactions?: OnyxTypes.Transaction[],
117118
) {
118119
// Only expense reports can be approved
@@ -131,7 +132,7 @@ function canApproveIOU(
131132
}
132133

133134
const managerID = iouReport?.managerID ?? CONST.DEFAULT_NUMBER_ID;
134-
const isCurrentUserManager = managerID === getUserAccountID();
135+
const isCurrentUserManager = managerID === currentUserAccountID;
135136
const isOpenExpenseReport = isOpenExpenseReportReportUtils(iouReport);
136137
const isApproved = isReportApproved({report: iouReport});
137138
const iouSettled = isSettled(iouReport);
@@ -278,7 +279,7 @@ function getBadgeFromIOUReport(
278279
) {
279280
return CONST.REPORT.ACTION_BADGE.PAY;
280281
}
281-
if (canApproveIOU(iouReport, policy, reportMetadata)) {
282+
if (canApproveIOU(iouReport, policy, reportMetadata, currentUserAccountID)) {
282283
return CONST.REPORT.ACTION_BADGE.APPROVE;
283284
}
284285
const isWaitingSubmitFromCurrentUser = canSubmitAndIsAwaitingForCurrentUser(

tests/actions/IOUTest/ReportWorkflowTest.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,11 +1456,11 @@ describe('actions/IOU/ReportWorkflow', () => {
14561456

14571457
await waitForBatchedUpdates();
14581458

1459-
expect(canApproveIOU(fakeReport, fakePolicy, {})).toBeFalsy();
1459+
expect(canApproveIOU(fakeReport, fakePolicy, {}, RORY_ACCOUNT_ID)).toBeFalsy();
14601460
// Then should return false when passing transactions directly as the fourth parameter instead of relying on Onyx data
14611461
const {result} = renderHook(() => useReportWithTransactionsAndViolations(reportID), {wrapper: OnyxListItemProvider});
14621462
await waitForBatchedUpdatesWithAct();
1463-
expect(canApproveIOU(result.current.at(0) as Report, fakePolicy, {}, result.current.at(1) as Transaction[])).toBeFalsy();
1463+
expect(canApproveIOU(result.current.at(0) as Report, fakePolicy, {}, RORY_ACCOUNT_ID, result.current.at(1) as Transaction[])).toBeFalsy();
14641464
});
14651465
it('should return false if we have only scanning transactions', async () => {
14661466
const policyID = '2';
@@ -1512,11 +1512,11 @@ describe('actions/IOU/ReportWorkflow', () => {
15121512

15131513
await waitForBatchedUpdates();
15141514

1515-
expect(canApproveIOU(fakeReport, fakePolicy, {})).toBeFalsy();
1515+
expect(canApproveIOU(fakeReport, fakePolicy, {}, RORY_ACCOUNT_ID)).toBeFalsy();
15161516
// Then should return false when passing transactions directly as the fourth parameter instead of relying on Onyx data
15171517
const {result} = renderHook(() => useReportWithTransactionsAndViolations(reportID), {wrapper: OnyxListItemProvider});
15181518
await waitForBatchedUpdatesWithAct();
1519-
expect(canApproveIOU(result.current.at(0) as Report, fakePolicy, {}, result.current.at(1) as Transaction[])).toBeFalsy();
1519+
expect(canApproveIOU(result.current.at(0) as Report, fakePolicy, {}, RORY_ACCOUNT_ID, result.current.at(1) as Transaction[])).toBeFalsy();
15201520
});
15211521
it('should return false if all transactions are pending card or scanning transaction', async () => {
15221522
const policyID = '2';
@@ -1559,11 +1559,11 @@ describe('actions/IOU/ReportWorkflow', () => {
15591559

15601560
await waitForBatchedUpdates();
15611561

1562-
expect(canApproveIOU(fakeReport, fakePolicy, {})).toBeFalsy();
1562+
expect(canApproveIOU(fakeReport, fakePolicy, {}, RORY_ACCOUNT_ID)).toBeFalsy();
15631563
// Then should return false when passing transactions directly as the fourth parameter instead of relying on Onyx data
15641564
const {result} = renderHook(() => useReportWithTransactionsAndViolations(reportID), {wrapper: OnyxListItemProvider});
15651565
await waitForBatchedUpdatesWithAct();
1566-
expect(canApproveIOU(result.current.at(0) as Report, fakePolicy, {}, result.current.at(1) as Transaction[])).toBeFalsy();
1566+
expect(canApproveIOU(result.current.at(0) as Report, fakePolicy, {}, RORY_ACCOUNT_ID, result.current.at(1) as Transaction[])).toBeFalsy();
15671567
});
15681568
it('should return true if at least one transaction is not pending card or scanning transaction', async () => {
15691569
const policyID = '2';
@@ -1612,11 +1612,11 @@ describe('actions/IOU/ReportWorkflow', () => {
16121612

16131613
await waitForBatchedUpdates();
16141614

1615-
expect(canApproveIOU(fakeReport, fakePolicy, {})).toBeTruthy();
1615+
expect(canApproveIOU(fakeReport, fakePolicy, {}, RORY_ACCOUNT_ID)).toBeTruthy();
16161616
// Then should return true when passing transactions directly as the fourth parameter instead of relying on Onyx data
16171617
const {result} = renderHook(() => useReportWithTransactionsAndViolations(reportID), {wrapper: OnyxListItemProvider});
16181618
await waitForBatchedUpdatesWithAct();
1619-
expect(canApproveIOU(result.current.at(0) as Report, fakePolicy, {}, result.current.at(1) as Transaction[])).toBeTruthy();
1619+
expect(canApproveIOU(result.current.at(0) as Report, fakePolicy, {}, RORY_ACCOUNT_ID, result.current.at(1) as Transaction[])).toBeTruthy();
16201620
});
16211621

16221622
it('should return false if the report is closed', async () => {
@@ -1645,9 +1645,9 @@ describe('actions/IOU/ReportWorkflow', () => {
16451645
});
16461646
await waitForBatchedUpdates();
16471647
// Then, canApproveIOU should return false since the report is closed
1648-
expect(canApproveIOU(fakeReport, fakePolicy, {})).toBeFalsy();
1648+
expect(canApproveIOU(fakeReport, fakePolicy, {}, RORY_ACCOUNT_ID)).toBeFalsy();
16491649
// Then should return false when passing transactions directly as the fourth parameter instead of relying on Onyx data
1650-
expect(canApproveIOU(fakeReport, fakePolicy, {}, [fakeTransaction])).toBeFalsy();
1650+
expect(canApproveIOU(fakeReport, fakePolicy, {}, RORY_ACCOUNT_ID, [fakeTransaction])).toBeFalsy();
16511651
});
16521652
});
16531653

tests/unit/IOUUtilsTest.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ describe('canApproveIOU', () => {
751751

752752
// When checking if approve action is available
753753
// Then it should return true because DEW approval is not in progress
754-
expect(canApproveIOU(report, policy, reportMetadata, [transaction])).toBe(true);
754+
expect(canApproveIOU(report, policy, reportMetadata, currentUserAccountID, [transaction])).toBe(true);
755755
});
756756

757757
it('should return false for DEW policy report with pending approval', async () => {
@@ -786,7 +786,7 @@ describe('canApproveIOU', () => {
786786

787787
// When checking if approve action is available while DEW approval is pending
788788
// Then it should return false because DEW is already processing an approval
789-
expect(canApproveIOU(report, policy, reportMetadata, [transaction])).toBe(false);
789+
expect(canApproveIOU(report, policy, reportMetadata, currentUserAccountID, [transaction])).toBe(false);
790790
});
791791

792792
it('should return false for non-expense report', async () => {
@@ -805,7 +805,7 @@ describe('canApproveIOU', () => {
805805
const reportMetadata: ReportMetadata = {};
806806

807807
// Then canApproveIOU should return false
808-
expect(canApproveIOU(report, policy, reportMetadata)).toBe(false);
808+
expect(canApproveIOU(report, policy, reportMetadata, currentUserAccountID)).toBe(false);
809809
});
810810
});
811811

0 commit comments

Comments
 (0)