Skip to content

Commit b837397

Browse files
authored
Merge pull request Expensify#88780 from parasharrajat/onyx/session-14
Refactor `getBadgeFromIOUReport` and `getIOUReportActionWithBadge` to pass current user data
2 parents 9428ca0 + beb3037 commit b837397

8 files changed

Lines changed: 67 additions & 35 deletions

File tree

src/hooks/useOptimisticNextStep.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import useTransactionsAndViolationsForReport from './useTransactionsAndViolation
3030
function useOptimisticNextStep(reportID: string | undefined) {
3131
const theme = useTheme();
3232
const {isOffline} = useNetwork();
33-
const {accountID, email} = useCurrentUserPersonalDetails();
33+
const {accountID, email, login: currentUserLogin} = useCurrentUserPersonalDetails();
3434
const {areStrictPolicyRulesEnabled} = useStrictPolicyRules();
3535

3636
const [moneyRequestReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
@@ -75,7 +75,7 @@ function useOptimisticNextStep(reportID: string | undefined) {
7575
optimisticNextStep = buildOptimisticNextStepForDEWOffline();
7676
}
7777
} else if (moneyRequestReport?.statusNum === CONST.REPORT.STATUS_NUM.SUBMITTED) {
78-
const gbrResult = getReasonAndReportActionThatRequiresAttention(moneyRequestReport, undefined, isArchivedReport);
78+
const gbrResult = getReasonAndReportActionThatRequiresAttention(moneyRequestReport, currentUserLogin ?? '', accountID, undefined, isArchivedReport);
7979
const hasDEWApproveFailed = gbrResult?.reason === CONST.REQUIRES_ATTENTION_REASONS.HAS_DEW_APPROVE_FAILED;
8080
const isCurrentUserTheApprover = moneyRequestReport?.managerID === accountID;
8181
if (hasDEWApproveFailed && isCurrentUserTheApprover) {

src/libs/DebugUtils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ 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';
1213
import {getLinkedTransactionID} from './ReportActionsUtils';
1314
import {getReasonAndReportActionThatRequiresAttention, reasonForReportToBeInOptionList} from './ReportUtils';
1415
import SidebarUtils from './SidebarUtils';
@@ -1462,7 +1463,7 @@ function getReasonAndReportActionForGBRInLHNRow(report: OnyxEntry<Report>, isRep
14621463
return null;
14631464
}
14641465

1465-
const {reason, reportAction} = getReasonAndReportActionThatRequiresAttention(report, undefined, isReportArchived) ?? {};
1466+
const {reason, reportAction} = getReasonAndReportActionThatRequiresAttention(report, getCurrentUserEmail(), getUserAccountID(), undefined, isReportArchived) ?? {};
14661467

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

src/libs/ReportUtils.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,15 @@ import type {FileObject} from '@src/types/utils/Attachment';
8686
import {isEmptyObject, isEmptyValueObject} from '@src/types/utils/EmptyObject';
8787
import type IconAsset from '@src/types/utils/IconAsset';
8888
import {getBankAccountFromID} from './actions/BankAccounts';
89-
import {createDraftTransaction, setMoneyRequestParticipants, setMoneyRequestParticipantsFromReport, setMoneyRequestReportID, startDistanceRequest, startMoneyRequest} from './actions/IOU';
89+
import {
90+
createDraftTransaction,
91+
getUserAccountID,
92+
setMoneyRequestParticipants,
93+
setMoneyRequestParticipantsFromReport,
94+
setMoneyRequestReportID,
95+
startDistanceRequest,
96+
startMoneyRequest,
97+
} from './actions/IOU';
9098
import type {IOURequestType} from './actions/IOU';
9199
import {unholdRequest} from './actions/IOU/Hold';
92100
import {canApproveIOU, canIOUBePaid, canSubmitReport, getBadgeFromIOUReport, getIOUReportActionWithBadge} from './actions/IOU/ReportWorkflow';
@@ -4189,6 +4197,8 @@ function hasUnresolvedCardFraudAlert(reportOrOption: OnyxEntry<Report> | OptionD
41894197

41904198
function getReasonAndReportActionThatRequiresAttention(
41914199
optionOrReport: OnyxEntry<Report> | OptionData,
4200+
currentUserLogin: string,
4201+
currentUserAccountID: number,
41924202
parentReportAction?: OnyxEntry<ReportAction>,
41934203
isReportArchived = false,
41944204
): ReasonAndReportActionThatRequiresAttention | null {
@@ -4245,7 +4255,9 @@ function getReasonAndReportActionThatRequiresAttention(
42454255

42464256
if (actionTypeForAssigneeToComplete) {
42474257
const isAssigneeExpenseAction = actionTypeForAssigneeToComplete === CONST.REPORT.ACTION_TYPES_FOR_ASSIGNEE_TO_COMPLETE.EXPENSE;
4248-
const expenseBadge = isAssigneeExpenseAction ? getBadgeFromIOUReport(optionOrReport, undefined, policy, optionReportMetadata, invoiceReceiverPolicy) : undefined;
4258+
const expenseBadge = isAssigneeExpenseAction
4259+
? getBadgeFromIOUReport(optionOrReport, undefined, policy, optionReportMetadata, invoiceReceiverPolicy, currentUserLogin, currentUserAccountID)
4260+
: undefined;
42494261
return {
42504262
reason: CONST.REQUIRES_ATTENTION_REASONS.IS_WAITING_FOR_ASSIGNEE_TO_COMPLETE_ACTION,
42514263
reportAction: Object.values(reportActions)
@@ -4257,7 +4269,14 @@ function getReasonAndReportActionThatRequiresAttention(
42574269
};
42584270
}
42594271

4260-
const {reportAction: iouReportActionToApproveOrPay, actionBadge} = getIOUReportActionWithBadge(optionOrReport, policy, optionReportMetadata, invoiceReceiverPolicy);
4272+
const {reportAction: iouReportActionToApproveOrPay, actionBadge} = getIOUReportActionWithBadge(
4273+
optionOrReport,
4274+
policy,
4275+
optionReportMetadata,
4276+
invoiceReceiverPolicy,
4277+
currentUserLogin,
4278+
currentUserAccountID,
4279+
);
42614280
const iouReportID = getIOUReportIDFromReportActionPreview(iouReportActionToApproveOrPay);
42624281
const transactions = getReportTransactions(iouReportID);
42634282
const hasOnlyPendingTransactions = transactions.length > 0 && transactions.every((t) => isExpensifyCardTransaction(t) && isPending(t));
@@ -4313,7 +4332,7 @@ function getReasonAndReportActionThatRequiresAttention(
43134332
* @param parentReportAction (the report action the current report is a thread of)
43144333
*/
43154334
function requiresAttentionFromCurrentUser(optionOrReport: OnyxEntry<Report> | OptionData, parentReportAction?: OnyxEntry<ReportAction>, isReportArchived = false) {
4316-
return !!getReasonAndReportActionThatRequiresAttention(optionOrReport, parentReportAction, isReportArchived);
4335+
return !!getReasonAndReportActionThatRequiresAttention(optionOrReport, getCurrentUserEmail() ?? '', getUserAccountID(), parentReportAction, isReportArchived);
43174336
}
43184337

43194338
/**
@@ -12806,13 +12825,17 @@ function generateReportAttributes({
1280612825
isReportArchived = false,
1280712826
allTransactions,
1280812827
reports,
12828+
currentUserLogin,
12829+
currentUserAccountID,
1280912830
}: {
1281012831
report: OnyxEntry<Report>;
1281112832
chatReport: OnyxEntry<Report>;
1281212833
reportActions?: OnyxCollection<ReportActions>;
1281312834
transactionViolations: OnyxCollection<TransactionViolation[]>;
1281412835
isReportArchived: boolean;
1281512836
allTransactions: OnyxCollection<Transaction>;
12837+
currentUserLogin: string;
12838+
currentUserAccountID: number;
1281612839
actionBadge?: ValueOf<typeof CONST.REPORT.ACTION_BADGE>;
1281712840
actionTargetReportActionID?: string;
1281812841
reports?: OnyxCollection<Report>;
@@ -12825,7 +12848,7 @@ function generateReportAttributes({
1282512848
const hasErrors = Object.entries(reportErrors ?? {}).length > 0;
1282612849
const oneTransactionThreadReportID = getOneTransactionThreadReportID(report, chatReport, reportActionsList);
1282712850
const parentReportAction = report?.parentReportActionID ? parentReportActionsList?.[report.parentReportActionID] : undefined;
12828-
const {reason, actionBadge, reportAction} = getReasonAndReportActionThatRequiresAttention(report, parentReportAction, isReportArchived) ?? {};
12851+
const {reason, actionBadge, reportAction} = getReasonAndReportActionThatRequiresAttention(report, currentUserLogin, currentUserAccountID, parentReportAction, isReportArchived) ?? {};
1282912852

1283012853
return {
1283112854
hasViolationsToDisplayInLHN,

src/libs/actions/IOU/ReportWorkflow.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ function getBadgeFromIOUReport(
265265
policy: OnyxEntry<OnyxTypes.Policy>,
266266
reportMetadata: OnyxEntry<OnyxTypes.ReportMetadata>,
267267
invoiceReceiverPolicy: OnyxEntry<OnyxTypes.Policy>,
268+
currentUserLogin: string,
269+
currentUserAccountID: number,
268270
): ValueOf<typeof CONST.REPORT.ACTION_BADGE> | undefined {
269271
// Show to the actual payer, or to policy admins via the pay-elsewhere path for negative expenses
270272
if (
@@ -282,8 +284,8 @@ function getBadgeFromIOUReport(
282284
policy,
283285
getReportTransactions(iouReport?.reportID),
284286
getAllTransactionViolations(),
285-
getCurrentUserEmail(),
286-
getUserAccountID(),
287+
currentUserLogin,
288+
currentUserAccountID,
287289
getAllReportActions(iouReport?.reportID),
288290
);
289291
if (isWaitingSubmitFromCurrentUser) {
@@ -297,6 +299,8 @@ function getIOUReportActionWithBadge(
297299
policy: OnyxEntry<OnyxTypes.Policy>,
298300
reportMetadata: OnyxEntry<OnyxTypes.ReportMetadata>,
299301
invoiceReceiverPolicy: OnyxEntry<OnyxTypes.Policy>,
302+
currentUserLogin: string,
303+
currentUserAccountID: number,
300304
): {reportAction: OnyxEntry<ReportAction>; actionBadge?: ValueOf<typeof CONST.REPORT.ACTION_BADGE>} {
301305
const chatReportActions = getAllReportActionsFromIOU()?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport?.reportID}`] ?? {};
302306

@@ -306,7 +310,7 @@ function getIOUReportActionWithBadge(
306310
return false;
307311
}
308312
const iouReport = getReportOrDraftReport(action.childReportID);
309-
const badge = getBadgeFromIOUReport(iouReport, chatReport, policy, reportMetadata, invoiceReceiverPolicy);
313+
const badge = getBadgeFromIOUReport(iouReport, chatReport, policy, reportMetadata, invoiceReceiverPolicy, currentUserLogin, currentUserAccountID);
310314
if (badge) {
311315
actionBadge = badge;
312316
return true;

src/libs/actions/OnyxDerived/configs/reportAttributes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ export default createOnyxDerivedValueConfig({
273273
isReportArchived,
274274
allTransactions: transactions,
275275
reports,
276+
currentUserAccountID: session?.accountID ?? CONST.DEFAULT_NUMBER_ID,
277+
currentUserLogin: session?.email ?? '',
276278
});
277279

278280
const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`];

tests/actions/IOUTest/ReportWorkflowTest.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2695,7 +2695,7 @@ describe('actions/IOU/ReportWorkflow', () => {
26952695
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReportID}`, MOCK_REPORT_ACTIONS);
26962696
await waitForBatchedUpdates();
26972697

2698-
const result = getIOUReportActionWithBadge(fakeChatReport, fakePolicy, {}, undefined);
2698+
const result = getIOUReportActionWithBadge(fakeChatReport, fakePolicy, {}, undefined, RORY_EMAIL, RORY_ACCOUNT_ID);
26992699
expect(result.reportAction).toMatchObject(validReportAction);
27002700
expect(result.actionBadge).toBe(CONST.REPORT.ACTION_BADGE.APPROVE);
27012701
});
@@ -2753,7 +2753,7 @@ describe('actions/IOU/ReportWorkflow', () => {
27532753
});
27542754
await waitForBatchedUpdates();
27552755

2756-
const result = getIOUReportActionWithBadge(fakeChatReport, fakePolicy, {}, undefined);
2756+
const result = getIOUReportActionWithBadge(fakeChatReport, fakePolicy, {}, undefined, RORY_EMAIL, RORY_ACCOUNT_ID);
27572757
expect(result.reportAction).toMatchObject(reportPreviewAction);
27582758
expect(result.actionBadge).toBe(CONST.REPORT.ACTION_BADGE.APPROVE);
27592759
});
@@ -2816,7 +2816,7 @@ describe('actions/IOU/ReportWorkflow', () => {
28162816
});
28172817
await waitForBatchedUpdates();
28182818

2819-
const result = getIOUReportActionWithBadge(fakeChatReport, fakePolicy, {}, undefined);
2819+
const result = getIOUReportActionWithBadge(fakeChatReport, fakePolicy, {}, undefined, RORY_EMAIL, RORY_ACCOUNT_ID);
28202820
expect(result.reportAction).toMatchObject(reportPreviewAction);
28212821
expect(result.actionBadge).toBe(CONST.REPORT.ACTION_BADGE.PAY);
28222822
});
@@ -2880,7 +2880,7 @@ describe('actions/IOU/ReportWorkflow', () => {
28802880
});
28812881
await waitForBatchedUpdates();
28822882

2883-
const result = getIOUReportActionWithBadge(fakeChatReport, fakePolicy, {}, undefined);
2883+
const result = getIOUReportActionWithBadge(fakeChatReport, fakePolicy, {}, undefined, RORY_EMAIL, RORY_ACCOUNT_ID);
28842884
expect(result.reportAction).toMatchObject(reportPreviewAction);
28852885
expect(result.actionBadge).toBe(CONST.REPORT.ACTION_BADGE.SUBMIT);
28862886
});
@@ -2947,7 +2947,7 @@ describe('actions/IOU/ReportWorkflow', () => {
29472947
});
29482948
await waitForBatchedUpdates();
29492949

2950-
const result = getIOUReportActionWithBadge(fakeChatReport, fakePolicy, {}, undefined);
2950+
const result = getIOUReportActionWithBadge(fakeChatReport, fakePolicy, {}, undefined, RORY_EMAIL, RORY_ACCOUNT_ID);
29512951
expect(result.reportAction).toMatchObject(reportPreviewAction);
29522952
expect(result.actionBadge).toBe(CONST.REPORT.ACTION_BADGE.PAY);
29532953
});
@@ -3014,7 +3014,7 @@ describe('actions/IOU/ReportWorkflow', () => {
30143014
});
30153015
await waitForBatchedUpdates();
30163016

3017-
const result = getIOUReportActionWithBadge(fakeChatReport, fakePolicy, {}, undefined);
3017+
const result = getIOUReportActionWithBadge(fakeChatReport, fakePolicy, {}, undefined, RORY_EMAIL, RORY_ACCOUNT_ID);
30183018
expect(result.reportAction).toBeUndefined();
30193019
expect(result.actionBadge).toBeUndefined();
30203020
});
@@ -3073,7 +3073,7 @@ describe('actions/IOU/ReportWorkflow', () => {
30733073
});
30743074
await waitForBatchedUpdates();
30753075

3076-
const result = getIOUReportActionWithBadge(fakeChatReport, fakePolicy, {}, undefined);
3076+
const result = getIOUReportActionWithBadge(fakeChatReport, fakePolicy, {}, undefined, RORY_EMAIL, RORY_ACCOUNT_ID);
30773077
expect(result.reportAction).toBeUndefined();
30783078
expect(result.actionBadge).toBeUndefined();
30793079
});
@@ -3122,7 +3122,7 @@ describe('actions/IOU/ReportWorkflow', () => {
31223122
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${fakeTransaction.transactionID}`, fakeTransaction);
31233123
await waitForBatchedUpdates();
31243124

3125-
const result = getBadgeFromIOUReport(fakeIouReport, fakeChatReport, fakePolicy, {}, undefined);
3125+
const result = getBadgeFromIOUReport(fakeIouReport, fakeChatReport, fakePolicy, {}, undefined, RORY_EMAIL, RORY_ACCOUNT_ID);
31263126
expect(result).toBe(CONST.REPORT.ACTION_BADGE.APPROVE);
31273127
});
31283128

@@ -3173,7 +3173,7 @@ describe('actions/IOU/ReportWorkflow', () => {
31733173
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${fakeTransaction.transactionID}`, fakeTransaction);
31743174
await waitForBatchedUpdates();
31753175

3176-
const result = getBadgeFromIOUReport(fakeIouReport, fakeChatReport, fakePolicy, {}, undefined);
3176+
const result = getBadgeFromIOUReport(fakeIouReport, fakeChatReport, fakePolicy, {}, undefined, RORY_EMAIL, RORY_ACCOUNT_ID);
31773177
expect(result).toBe(CONST.REPORT.ACTION_BADGE.PAY);
31783178
});
31793179

@@ -3209,7 +3209,7 @@ describe('actions/IOU/ReportWorkflow', () => {
32093209
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`, fakeIouReport);
32103210
await waitForBatchedUpdates();
32113211

3212-
const result = getBadgeFromIOUReport(fakeIouReport, fakeChatReport, fakePolicy, {}, undefined);
3212+
const result = getBadgeFromIOUReport(fakeIouReport, fakeChatReport, fakePolicy, {}, undefined, RORY_EMAIL, RORY_ACCOUNT_ID);
32133213
expect(result).toBeUndefined();
32143214
});
32153215
});

0 commit comments

Comments
 (0)