Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/libs/actions/IOU/Split.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ import {
getAllTransactions,
getMoneyRequestInformation,
getMoneyRequestParticipantsFromReport,
getMoneyRequestPolicyTags,
getOrCreateOptimisticSplitChatReport,
getReceiptError,
getReportPreviewAction,
Expand Down Expand Up @@ -1401,7 +1402,11 @@ function updateSplitTransactions({
} = getMoneyRequestInformation({
participantParams,
parentChatReport,
policyParams,
policyParams: {
...policyParams,
// eslint-disable-next-line @typescript-eslint/no-deprecated
policyTagList: getMoneyRequestPolicyTags({moneyRequestReportID: splitExpense?.reportID, parentChatReport, participant: participantParams.participant}),
},
transactionParams,
moneyRequestReportID: splitExpense?.reportID,
existingTransaction,
Expand Down
20 changes: 19 additions & 1 deletion src/libs/actions/IOU/TrackExpense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ import {
getAllTransactionViolations,
getCurrentUserEmail,
getMoneyRequestInformation,
getMoneyRequestPolicyTags,
getReceiptError,
getReportPreviewAction,
getSearchOnyxUpdate,
Expand Down Expand Up @@ -1645,7 +1646,16 @@ function requestMoney(requestMoneyInformation: RequestMoneyInformation): {iouRep
parentChatReport: isMovingTransactionFromTrackExpense ? undefined : currentChatReport,
existingIOUReport,
participantParams,
policyParams,
policyParams: {
...policyParams,
// eslint-disable-next-line @typescript-eslint/no-deprecated
policyTagList: getMoneyRequestPolicyTags({
existingIOUReport,
moneyRequestReportID,
parentChatReport: isMovingTransactionFromTrackExpense ? undefined : currentChatReport,
participant: participantParams.participant,
}),
},
transactionParams,
moneyRequestReportID,
existingTransactionID,
Expand Down Expand Up @@ -1999,6 +2009,14 @@ function convertBulkTrackedExpensesToIOU({
policyRecentlyUsedCurrencies,
personalDetails,
betas,
policyParams: {
// eslint-disable-next-line @typescript-eslint/no-deprecated
policyTagList: getMoneyRequestPolicyTags({
moneyRequestReportID: iouReportID,
parentChatReport: chatReport,
participant: participantParams.participant,
}),
},
});

const isDistanceRequest = isDistanceRequestTransactionUtils(transaction);
Expand Down
37 changes: 32 additions & 5 deletions src/libs/actions/IOU/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@
};

let allPersonalDetails: OnyxTypes.PersonalDetailsList = {};
Onyx.connect({

Check warning on line 381 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
allPersonalDetails = value ?? {};
Expand Down Expand Up @@ -430,7 +430,7 @@
};

let allTransactions: NonNullable<OnyxCollection<OnyxTypes.Transaction>> = {};
Onyx.connect({

Check warning on line 433 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -444,7 +444,7 @@
});

let allTransactionDrafts: NonNullable<OnyxCollection<OnyxTypes.Transaction>> = {};
Onyx.connect({

Check warning on line 447 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION_DRAFT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -453,7 +453,7 @@
});

let allTransactionViolations: NonNullable<OnyxCollection<OnyxTypes.TransactionViolations>> = {};
Onyx.connect({

Check warning on line 456 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -467,7 +467,7 @@
});

let allPolicyTags: OnyxCollection<OnyxTypes.PolicyTagLists> = {};
Onyx.connect({

Check warning on line 470 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY_TAGS,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -480,7 +480,7 @@
});

let allReports: OnyxCollection<OnyxTypes.Report>;
Onyx.connect({

Check warning on line 483 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -489,7 +489,7 @@
});

let allReportNameValuePairs: OnyxCollection<OnyxTypes.ReportNameValuePairs>;
Onyx.connect({

Check warning on line 492 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -499,7 +499,7 @@

let deprecatedUserAccountID = -1;
let deprecatedCurrentUserEmail = '';
Onyx.connect({

Check warning on line 502 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (value) => {
deprecatedCurrentUserEmail = value?.email ?? '';
Expand All @@ -508,7 +508,7 @@
});

let deprecatedCurrentUserPersonalDetails: OnyxEntry<OnyxTypes.PersonalDetails>;
Onyx.connect({

Check warning on line 511 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
deprecatedCurrentUserPersonalDetails = value?.[deprecatedUserAccountID] ?? undefined;
Expand All @@ -516,7 +516,7 @@
});

let allReportActions: OnyxCollection<OnyxTypes.ReportActions>;
Onyx.connect({

Check warning on line 519 in src/libs/actions/IOU/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (actions) => {
Expand Down Expand Up @@ -602,6 +602,31 @@
return allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {};
}

/**
* @deprecated This function uses Onyx.connect and should be replaced with useOnyx for reactive data access.
* TODO: remove `getMoneyRequestPolicyTags` from this file (https://github.com/Expensify/App/issues/72721)
* All usages of this function should be replaced with useOnyx hook in React components.
*/
function getMoneyRequestPolicyTags({
existingIOUReport,
moneyRequestReportID,
parentChatReport,
participant,
}: {
existingIOUReport?: OnyxEntry<OnyxTypes.Report>;
moneyRequestReportID?: string;
parentChatReport: OnyxEntry<OnyxTypes.Report>;
participant: Participant;
}): OnyxTypes.PolicyTagLists {
const iouReportPolicyID =
existingIOUReport?.policyID ??
(moneyRequestReportID ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${moneyRequestReportID}`]?.policyID : undefined) ??
parentChatReport?.policyID ??
allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${participant.reportID}`]?.policyID;
// eslint-disable-next-line @typescript-eslint/no-deprecated
return getPolicyTagsData(iouReportPolicyID) ?? {};
}
Comment on lines +610 to +628

@parasharrajat parasharrajat Apr 13, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the basis of this implementation? why are we using this many params?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bump @Guccio163

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getMoneyRequestPolicyTags exists to let callers of getMoneyRequestInformation resolve the correct policyTagList before passing it in, so the function no longer needs to call getPolicyTagsData
internally.

The multiple params are needed because the policy ID of the IOU report can come from several different sources depending on the call context, mirroring the exact fallback chain that
getMoneyRequestInformation uses internally when resolving iouReport (lines 2301–2308):

  1. existingIOUReport?.policyID — direct match to iouReport = existingIOUReport
  2. allReports[moneyRequestReportID]?.policyID — direct match to iouReport = allReports[moneyRequestReportID]
  3. parentChatReport?.policyID — equivalent to new optimistic report's policyID: chatReport.policyID where chatReport = parentChatReport
  4. allReports[participant.reportID]?.policyID — same, but for the policy expense chat path where chatReport was resolved from allReports[participant.reportID]

Cases 3 and 4 are equivalent to the internal iouReport.policyID because expense reports always inherit their policyID from the parent chat — either set directly on buildOptimisticExpenseReport({
policyID: chatReport.policyID }), or already stored that way on the existing report in Onyx.

In short: every param is a different source of the same policyID. Together they guarantee that getMoneyRequestPolicyTags returns the exact same PolicyTagLists that
getPolicyTagsData(iouReport.policyID) would have returned had it been called inside getMoneyRequestInformation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can get you a breakdown of how it maps to getMoneyRequestInformation's internal iouReport calculations if it might help, just let me know 👀


/**
* @private
* After finishing the action in RHP from the Inbox tab, besides dismissing the modal, we should open the report.
Expand Down Expand Up @@ -2191,6 +2216,7 @@
} = moneyRequestInformation;
const {payeeAccountID = deprecatedUserAccountID, payeeEmail = deprecatedCurrentUserEmail, participant} = participantParams;
const {policy, policyCategories, policyTagList, policyRecentlyUsedCategories, policyRecentlyUsedTags} = policyParams;

const {
attendees,
amount,
Expand Down Expand Up @@ -2392,9 +2418,7 @@

const optimisticPolicyRecentlyUsedCategories = mergePolicyRecentlyUsedCategories(category, policyRecentlyUsedCategories);
const optimisticPolicyRecentlyUsedTags = buildOptimisticPolicyRecentlyUsedTags({
// TODO: Replace getPolicyTagsData (https://github.com/Expensify/App/issues/72721) and getPolicyRecentlyUsedTagsData (https://github.com/Expensify/App/issues/71491) with useOnyx hook
// eslint-disable-next-line @typescript-eslint/no-deprecated
policyTags: getPolicyTagsData(iouReport.policyID),
policyTags: policyTagList ?? {},
policyRecentlyUsedTags,
transactionTags: tag,
});
Expand Down Expand Up @@ -3285,7 +3309,7 @@
optimisticReportPreviewActionID,
shouldDeferAutoSubmit,
} = distanceRequestInformation;
const {policy, policyCategories, policyTagList, policyRecentlyUsedCategories, policyRecentlyUsedTags} = policyParams;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were passing policyTagList here in params so why did we remove it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

policyTagList is removed because its value is never used beside policyParams in getMoneyRequestInformation. Since we're not certain that this exact param contains the exact same values as getPolicyTagsData(iouReport.policyID) inside getPolicyTagsData, I think that the best move forward is using policyTagList to pass down correct, bubbled-up value, extracted by us, but bubbling it step-by-step, just like until now - one function per PR.

const {policy, policyCategories, policyRecentlyUsedCategories, policyRecentlyUsedTags} = policyParams;
const parsedComment = getParsedComment(transactionParams.comment);
transactionParams.comment = parsedComment;
const {
Expand Down Expand Up @@ -3421,7 +3445,8 @@
policyParams: {
policy,
policyCategories,
policyTagList,
// eslint-disable-next-line @typescript-eslint/no-deprecated
policyTagList: getMoneyRequestPolicyTags({existingIOUReport, moneyRequestReportID, parentChatReport: currentChatReport, participant}),
policyRecentlyUsedCategories,
policyRecentlyUsedTags,
},
Expand Down Expand Up @@ -3910,6 +3935,8 @@
maybeUpdateReportNameForFormulaTitle,
getSearchOnyxUpdate,
getPolicyTags,
// eslint-disable-next-line @typescript-eslint/no-deprecated
getMoneyRequestPolicyTags,
setMoneyRequestTimeRate,
setMoneyRequestTimeCount,
handleNavigateAfterExpenseCreate,
Expand Down
213 changes: 213 additions & 0 deletions tests/actions/IOU/GetMoneyRequestInformationTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
import Onyx from 'react-native-onyx';
import {getMoneyRequestInformation} from '@libs/actions/IOU';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Beta, PolicyTagLists, Report} from '@src/types/onyx';
import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates';

jest.mock('@src/libs/Navigation/Navigation', () => ({
navigate: jest.fn(),
goBack: jest.fn(),
}));

const POLICY_ID = 'policy-test-1';
const CHAT_REPORT_ID = 'report-chat-1';
const PAYEE_ACCOUNT_ID = 100;
const PAYER_ACCOUNT_ID = 200;
const TAG_LIST = 'Department';
const TAG_NAME = 'Engineering';

const parentChatReport: Report = {
reportID: CHAT_REPORT_ID,
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
policyID: POLICY_ID,
isOwnPolicyExpenseChat: true,
type: CONST.REPORT.TYPE.CHAT,
};

const policyTagListA: PolicyTagLists = {
[TAG_LIST]: {
name: TAG_LIST,
orderWeight: 0,
required: false,
tags: {
[TAG_NAME]: {
name: TAG_NAME,
enabled: true,
},
},
},
};

const baseParams = {
parentChatReport,
participantParams: {
payeeAccountID: PAYEE_ACCOUNT_ID,
payeeEmail: 'payee@example.com',
participant: {
accountID: PAYER_ACCOUNT_ID,
login: 'payer@example.com',
isPolicyExpenseChat: true,
reportID: CHAT_REPORT_ID,
},
},
transactionParams: {
amount: 1000,
currency: 'USD',
created: '2024-01-01',
merchant: 'Test Merchant',
},
betas: [] as Beta[],
isASAPSubmitBetaEnabled: false,
currentUserAccountIDParam: PAYEE_ACCOUNT_ID,
currentUserEmailParam: 'payee@example.com',
transactionViolations: {},
quickAction: undefined,
policyRecentlyUsedCurrencies: [] as string[],
personalDetails: {},
} as const;

describe('getMoneyRequestInformation', () => {
beforeAll(() => {
Onyx.init({keys: ONYXKEYS});
return waitForBatchedUpdates();
});

beforeEach(async () => {
await Onyx.clear();
await waitForBatchedUpdates();
});

describe('optimistic recently used tags', () => {
it('should store recently used tags at the correct policy key when policyTagList and tag are provided', () => {
const result = getMoneyRequestInformation({
...baseParams,
policyParams: {
policyTagList: policyTagListA,
},
transactionParams: {
...baseParams.transactionParams,
tag: TAG_NAME,
},
});

const expectedKey = `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${POLICY_ID}`;
const tagEntry = result.onyxData.optimisticData?.find((entry) => entry.key === expectedKey);

expect(tagEntry).toBeDefined();
expect(tagEntry?.value).toEqual({[TAG_LIST]: [TAG_NAME]});
});

it('should not store recently used tags when tag is not provided', () => {
const result = getMoneyRequestInformation({
...baseParams,
policyParams: {
policyTagList: policyTagListA,
},
});

const expectedKey = `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${POLICY_ID}`;
const tagEntry = result.onyxData.optimisticData?.find((entry) => entry.key === expectedKey);

expect(tagEntry).toBeUndefined();
});

it('should store tags under empty-string list key when policyTagList has no named tag lists', () => {
const result = getMoneyRequestInformation({
...baseParams,
policyParams: {
policyTagList: {},
},
transactionParams: {
...baseParams.transactionParams,
tag: TAG_NAME,
},
});

const expectedKey = `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${POLICY_ID}`;
const tagEntry = result.onyxData.optimisticData?.find((entry) => entry.key === expectedKey);

expect(tagEntry).toBeDefined();
const value = tagEntry?.value as Record<string, string[]>;
expect(value['']).toEqual([TAG_NAME]);
});

it('should use parentChatReport.policyID for the recently used tags key', () => {
const otherPolicyID = 'policy-other';
const result = getMoneyRequestInformation({
...baseParams,
parentChatReport: {
...parentChatReport,
policyID: otherPolicyID,
},
policyParams: {
policyTagList: policyTagListA,
},
transactionParams: {
...baseParams.transactionParams,
tag: TAG_NAME,
},
});

const expectedKey = `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${otherPolicyID}`;
const wrongKey = `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${POLICY_ID}`;

expect(result.onyxData.optimisticData?.find((entry) => entry.key === expectedKey)).toBeDefined();
expect(result.onyxData.optimisticData?.find((entry) => entry.key === wrongKey)).toBeUndefined();
});

it('should use policyID from allReports when moneyRequestReportID is provided', async () => {
const moneyRequestReportID = 'iou-report-lookup-1';
const differentPolicyID = 'policy-different';
await Onyx.set(ONYXKEYS.SESSION, {accountID: PAYEE_ACCOUNT_ID, email: 'payee@example.com'});
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${differentPolicyID}`, {id: differentPolicyID, type: CONST.POLICY.TYPE.CORPORATE, name: 'Test Policy'});
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${moneyRequestReportID}`, {
reportID: moneyRequestReportID,
policyID: differentPolicyID,
type: CONST.REPORT.TYPE.EXPENSE,
ownerAccountID: PAYEE_ACCOUNT_ID,
currency: 'USD',
total: 0,
});
await waitForBatchedUpdates();

const result = getMoneyRequestInformation({
...baseParams,
moneyRequestReportID,
policyParams: {
policyTagList: policyTagListA,
},
transactionParams: {
...baseParams.transactionParams,
tag: TAG_NAME,
},
});

const expectedKey = `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${differentPolicyID}`;
const wrongKey = `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${POLICY_ID}`;

expect(result.onyxData.optimisticData?.find((entry) => entry.key === expectedKey)).toBeDefined();
expect(result.onyxData.optimisticData?.find((entry) => entry.key === wrongKey)).toBeUndefined();
});

it('should fall back to parentChatReport.policyID when moneyRequestReportID is empty string', () => {
const result = getMoneyRequestInformation({
...baseParams,
moneyRequestReportID: '',
policyParams: {
policyTagList: policyTagListA,
},
transactionParams: {
...baseParams.transactionParams,
tag: TAG_NAME,
},
});

const expectedKey = `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${POLICY_ID}`;
const tagEntry = result.onyxData.optimisticData?.find((entry) => entry.key === expectedKey);

expect(tagEntry).toBeDefined();
expect(tagEntry?.value).toEqual({[TAG_LIST]: [TAG_NAME]});
});
});
});
Loading