diff --git a/src/libs/actions/IOU/Split.ts b/src/libs/actions/IOU/Split.ts index cb62bc3fbe57..bcf4719707b8 100644 --- a/src/libs/actions/IOU/Split.ts +++ b/src/libs/actions/IOU/Split.ts @@ -99,6 +99,7 @@ import { getAllTransactions, getMoneyRequestInformation, getMoneyRequestParticipantsFromReport, + getMoneyRequestPolicyTags, getOrCreateOptimisticSplitChatReport, getReceiptError, getReportPreviewAction, @@ -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, diff --git a/src/libs/actions/IOU/TrackExpense.ts b/src/libs/actions/IOU/TrackExpense.ts index 4eb5235da1f5..91e0a456c87b 100644 --- a/src/libs/actions/IOU/TrackExpense.ts +++ b/src/libs/actions/IOU/TrackExpense.ts @@ -110,6 +110,7 @@ import { getAllTransactionViolations, getCurrentUserEmail, getMoneyRequestInformation, + getMoneyRequestPolicyTags, getReceiptError, getReportPreviewAction, getSearchOnyxUpdate, @@ -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, @@ -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); diff --git a/src/libs/actions/IOU/index.ts b/src/libs/actions/IOU/index.ts index 0f25e9d1fcbb..f98132896b50 100644 --- a/src/libs/actions/IOU/index.ts +++ b/src/libs/actions/IOU/index.ts @@ -602,6 +602,31 @@ function getPolicyTagsData(policyID: string | undefined) { 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; + moneyRequestReportID?: string; + parentChatReport: OnyxEntry; + 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) ?? {}; +} + /** * @private * After finishing the action in RHP from the Inbox tab, besides dismissing the modal, we should open the report. @@ -2191,6 +2216,7 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma } = moneyRequestInformation; const {payeeAccountID = deprecatedUserAccountID, payeeEmail = deprecatedCurrentUserEmail, participant} = participantParams; const {policy, policyCategories, policyTagList, policyRecentlyUsedCategories, policyRecentlyUsedTags} = policyParams; + const { attendees, amount, @@ -2392,9 +2418,7 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma 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, }); @@ -3285,7 +3309,7 @@ function createDistanceRequest(distanceRequestInformation: CreateDistanceRequest optimisticReportPreviewActionID, shouldDeferAutoSubmit, } = distanceRequestInformation; - const {policy, policyCategories, policyTagList, policyRecentlyUsedCategories, policyRecentlyUsedTags} = policyParams; + const {policy, policyCategories, policyRecentlyUsedCategories, policyRecentlyUsedTags} = policyParams; const parsedComment = getParsedComment(transactionParams.comment); transactionParams.comment = parsedComment; const { @@ -3421,7 +3445,8 @@ function createDistanceRequest(distanceRequestInformation: CreateDistanceRequest policyParams: { policy, policyCategories, - policyTagList, + // eslint-disable-next-line @typescript-eslint/no-deprecated + policyTagList: getMoneyRequestPolicyTags({existingIOUReport, moneyRequestReportID, parentChatReport: currentChatReport, participant}), policyRecentlyUsedCategories, policyRecentlyUsedTags, }, @@ -3910,6 +3935,8 @@ export { maybeUpdateReportNameForFormulaTitle, getSearchOnyxUpdate, getPolicyTags, + // eslint-disable-next-line @typescript-eslint/no-deprecated + getMoneyRequestPolicyTags, setMoneyRequestTimeRate, setMoneyRequestTimeCount, handleNavigateAfterExpenseCreate, diff --git a/tests/actions/IOU/GetMoneyRequestInformationTest.ts b/tests/actions/IOU/GetMoneyRequestInformationTest.ts new file mode 100644 index 000000000000..95881dd797e9 --- /dev/null +++ b/tests/actions/IOU/GetMoneyRequestInformationTest.ts @@ -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; + 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]}); + }); + }); +});