Skip to content

Commit b349b81

Browse files
authored
Merge pull request #88781 from parasharrajat/onyx/session-15
Refactor canApproveIOU function and debug utils
2 parents 6c1fb88 + 442121f commit b349b81

12 files changed

Lines changed: 115 additions & 62 deletions

File tree

src/components/MoneyReportHeaderActions/MoneyReportHeaderSecondaryActions.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ function MoneyReportHeaderSecondaryActionsInner({reportID, primaryAction, isRepo
218218
const onlyShowPayElsewhere = !canIOUBePaid && canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList, undefined, true, undefined, invoiceReceiverPolicy);
219219
const shouldShowPayButton = isPaidAnimationRunning || canIOUBePaid || onlyShowPayElsewhere;
220220
const hasOnlyPendingTransactions = allTransactions.length > 0 && allTransactions.every((t) => isExpensifyCardTransaction(t) && isPending(t));
221-
const shouldShowApproveButton = (canApproveIOU(moneyRequestReport, policy, reportMetadata, allTransactions) && !hasOnlyPendingTransactions) || isApprovedAnimationRunning;
221+
const shouldShowApproveButton =
222+
(canApproveIOU(moneyRequestReport, policy, reportMetadata, currentUserPersonalDetails.accountID, allTransactions) && !hasOnlyPendingTransactions) || isApprovedAnimationRunning;
222223
const isApproveDisabled = shouldShowApproveButton && !isAllowedToApproveExpenseReport(moneyRequestReport);
223224

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

src/components/MoneyReportHeaderPrimaryAction/PayPrimaryAction.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function PayPrimaryAction({reportID, chatReportID}: PayPrimaryActionProps) {
7777
const onlyShowPayElsewhere =
7878
!canIOUBePaid && canIOUBePaidAction(moneyRequestReport, chatReport, policy, bankAccountList, transaction ? [transaction] : undefined, true, undefined, invoiceReceiverPolicy);
7979
const shouldShowPayButton = isPaidAnimationRunning || canIOUBePaid || onlyShowPayElsewhere;
80-
const shouldShowApproveButton = (canApproveIOU(moneyRequestReport, policy, reportMetadata, transactions) && !hasOnlyPendingTransactions) || isApprovedAnimationRunning;
80+
const shouldShowApproveButton = (canApproveIOU(moneyRequestReport, policy, reportMetadata, accountID, transactions) && !hasOnlyPendingTransactions) || isApprovedAnimationRunning;
8181
const shouldDisableApproveButton = shouldShowApproveButton && !isAllowedToApproveExpenseReport(moneyRequestReport);
8282
const canAllowSettlement = hasUpdatedTotal(moneyRequestReport, policy);
8383
const totalAmount = getTotalAmountForIOUReportPreviewButton(moneyRequestReport, policy, CONST.REPORT.PRIMARY_ACTIONS.PAY, nonPendingDeleteTransactions, convertToDisplayString);

src/hooks/useSelectionModeReportActions.ts

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

173173
const {nonHeldAmount, fullAmount, hasValidNonHeldAmount} = getNonHeldAndFullAmount(report, shouldShowPayButton);
174174

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

177177
const shouldDisableApproveButton = shouldShowApproveButton && !isAllowedToApproveExpenseReport(report);
178178

src/libs/DebugUtils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import type {Beta, Report, ReportAction, ReportActions, ReportNameValuePairs, Tr
99
import type {Errors} from '@src/types/onyx/OnyxCommon';
1010
import type {Comment} from '@src/types/onyx/Transaction';
1111
import SafeString from '@src/utils/SafeString';
12-
import {getCurrentUserEmail, getUserAccountID} from './actions/IOU';
1312
import {getLinkedTransactionID} from './ReportActionsUtils';
1413
import {getReasonAndReportActionThatRequiresAttention, reasonForReportToBeInOptionList} from './ReportUtils';
1514
import SidebarUtils from './SidebarUtils';
@@ -1460,12 +1459,17 @@ type GBRReasonAndReportAction = {
14601459
/**
14611460
* Gets the reason and report action that is causing the GBR to show up in LHN row
14621461
*/
1463-
function getReasonAndReportActionForGBRInLHNRow(report: OnyxEntry<Report>, isReportArchived = false): GBRReasonAndReportAction | null {
1462+
function getReasonAndReportActionForGBRInLHNRow(
1463+
report: OnyxEntry<Report>,
1464+
currentUserLogin: string,
1465+
currentUserAccountID: number,
1466+
isReportArchived = false,
1467+
): GBRReasonAndReportAction | null {
14641468
if (!report) {
14651469
return null;
14661470
}
14671471

1468-
const {reason, reportAction} = getReasonAndReportActionThatRequiresAttention(report, getCurrentUserEmail(), getUserAccountID(), undefined, isReportArchived) ?? {};
1472+
const {reason, reportAction} = getReasonAndReportActionThatRequiresAttention(report, currentUserLogin, currentUserAccountID, undefined, isReportArchived) ?? {};
14691473

14701474
if (reason) {
14711475
return {reason: `debug.reasonGBR.${reason}`, reportAction};

src/libs/ReportUtils.ts

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

src/libs/SearchUIUtils.ts

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

23552355
// We're not supporting approve partial amount on search page now
23562356
if (
2357-
canApproveIOU(report, policy, reportMetadata, allReportTransactions) &&
2357+
canApproveIOU(report, policy, reportMetadata, currentUserAccountID, allReportTransactions) &&
23582358
isAllowedToApproveExpenseReport &&
23592359
!hasOnlyPendingCardOrScanningTransactions &&
23602360
!hasHeldExpenses(report.reportID, allReportTransactions)

src/libs/actions/IOU/ReportWorkflow.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ function canApproveIOU(
110110
iouReport: OnyxTypes.OnyxInputOrEntry<OnyxTypes.Report>,
111111
policy: OnyxTypes.OnyxInputOrEntry<OnyxTypes.Policy>,
112112
reportMetadata: OnyxEntry<OnyxTypes.ReportMetadata>,
113+
currentUserAccountID: number,
113114
iouTransactions?: OnyxTypes.Transaction[],
114115
) {
115116
// Only expense reports can be approved
@@ -128,7 +129,7 @@ function canApproveIOU(
128129
}
129130

130131
const managerID = iouReport?.managerID ?? CONST.DEFAULT_NUMBER_ID;
131-
const isCurrentUserManager = managerID === getUserAccountID();
132+
const isCurrentUserManager = managerID === currentUserAccountID;
132133
const isOpenExpenseReport = isOpenExpenseReportReportUtils(iouReport);
133134
const isApproved = isReportApproved({report: iouReport});
134135
const iouSettled = isSettled(iouReport);
@@ -275,7 +276,7 @@ function getBadgeFromIOUReport(
275276
) {
276277
return CONST.REPORT.ACTION_BADGE.PAY;
277278
}
278-
if (canApproveIOU(iouReport, policy, reportMetadata)) {
279+
if (canApproveIOU(iouReport, policy, reportMetadata, currentUserAccountID)) {
279280
return CONST.REPORT.ACTION_BADGE.APPROVE;
280281
}
281282
const isWaitingSubmitFromCurrentUser = canSubmitAndIsAwaitingForCurrentUser(

src/pages/Debug/Report/DebugReportPage.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function DebugReportPage({
7676
const [conciergeReportID] = useOnyx(ONYXKEYS.CONCIERGE_REPORT_ID);
7777
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED);
7878
const [isSelfTourViewed] = useOnyx(ONYXKEYS.NVP_ONBOARDING, {selector: hasSeenTourSelector});
79-
const {accountID: currentUserAccountID} = useCurrentUserPersonalDetails();
79+
const {accountID: currentUserAccountID, login: currentUserLogin} = useCurrentUserPersonalDetails();
8080
const transactionID = DebugUtils.getTransactionID(report, reportActions);
8181
const isReportArchived = useReportIsArchived(reportID);
8282

@@ -87,7 +87,8 @@ function DebugReportPage({
8787

8888
const shouldDisplayViolations = !!getViolatingReportIDForRBRInLHN(report, transactionViolations);
8989
const hasViolations = !!shouldDisplayViolations;
90-
const {reason: reasonGBR, reportAction: reportActionGBR} = DebugUtils.getReasonAndReportActionForGBRInLHNRow(report, isReportArchived) ?? {};
90+
const {reason: reasonGBR, reportAction: reportActionGBR} =
91+
DebugUtils.getReasonAndReportActionForGBRInLHNRow(report, currentUserLogin ?? '', currentUserAccountID, isReportArchived) ?? {};
9192
const {reason: reasonRBR, reportAction: reportActionRBR} =
9293
DebugUtils.getReasonAndReportActionForRBRInLHNRow(
9394
report,
@@ -156,7 +157,22 @@ function DebugReportPage({
156157
: undefined,
157158
},
158159
];
159-
}, [report, transactionViolations, isReportArchived, chatReport, reportActions, transactions, reportAttributes?.reportErrors, betas, priorityMode, draftComment, translate, isOffline]);
160+
}, [
161+
report,
162+
transactionViolations,
163+
currentUserLogin,
164+
currentUserAccountID,
165+
isReportArchived,
166+
chatReport,
167+
reportActions,
168+
transactions,
169+
reportAttributes?.reportErrors,
170+
isOffline,
171+
betas,
172+
priorityMode,
173+
draftComment,
174+
translate,
175+
]);
160176

161177
const icons = useMemoizedLazyExpensifyIcons(['Eye']);
162178

tests/actions/IOUTest/ReportWorkflowTest.ts

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

14631463
await waitForBatchedUpdates();
14641464

1465-
expect(canApproveIOU(fakeReport, fakePolicy, {})).toBeFalsy();
1465+
expect(canApproveIOU(fakeReport, fakePolicy, {}, RORY_ACCOUNT_ID)).toBeFalsy();
14661466
// Then should return false when passing transactions directly as the fourth parameter instead of relying on Onyx data
14671467
const {result} = renderHook(() => useReportWithTransactionsAndViolations(reportID), {wrapper: OnyxListItemProvider});
14681468
await waitForBatchedUpdatesWithAct();
1469-
expect(canApproveIOU(result.current.at(0) as Report, fakePolicy, {}, result.current.at(1) as Transaction[])).toBeFalsy();
1469+
expect(canApproveIOU(result.current.at(0) as Report, fakePolicy, {}, RORY_ACCOUNT_ID, result.current.at(1) as Transaction[])).toBeFalsy();
14701470
});
14711471
it('should return false if we have only scanning transactions', async () => {
14721472
const policyID = '2';
@@ -1518,11 +1518,11 @@ describe('actions/IOU/ReportWorkflow', () => {
15181518

15191519
await waitForBatchedUpdates();
15201520

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

15661566
await waitForBatchedUpdates();
15671567

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

16191619
await waitForBatchedUpdates();
16201620

1621-
expect(canApproveIOU(fakeReport, fakePolicy, {})).toBeTruthy();
1621+
expect(canApproveIOU(fakeReport, fakePolicy, {}, RORY_ACCOUNT_ID)).toBeTruthy();
16221622
// Then should return true when passing transactions directly as the fourth parameter instead of relying on Onyx data
16231623
const {result} = renderHook(() => useReportWithTransactionsAndViolations(reportID), {wrapper: OnyxListItemProvider});
16241624
await waitForBatchedUpdatesWithAct();
1625-
expect(canApproveIOU(result.current.at(0) as Report, fakePolicy, {}, result.current.at(1) as Transaction[])).toBeTruthy();
1625+
expect(canApproveIOU(result.current.at(0) as Report, fakePolicy, {}, RORY_ACCOUNT_ID, result.current.at(1) as Transaction[])).toBeTruthy();
16261626
});
16271627

16281628
it('should return false if the report is closed', async () => {
@@ -1651,9 +1651,9 @@ describe('actions/IOU/ReportWorkflow', () => {
16511651
});
16521652
await waitForBatchedUpdates();
16531653
// Then, canApproveIOU should return false since the report is closed
1654-
expect(canApproveIOU(fakeReport, fakePolicy, {})).toBeFalsy();
1654+
expect(canApproveIOU(fakeReport, fakePolicy, {}, RORY_ACCOUNT_ID)).toBeFalsy();
16551655
// Then should return false when passing transactions directly as the fourth parameter instead of relying on Onyx data
1656-
expect(canApproveIOU(fakeReport, fakePolicy, {}, [fakeTransaction])).toBeFalsy();
1656+
expect(canApproveIOU(fakeReport, fakePolicy, {}, RORY_ACCOUNT_ID, [fakeTransaction])).toBeFalsy();
16571657
});
16581658
});
16591659

0 commit comments

Comments
 (0)