Repository navigation
Remove Onyx.connect() usage for ONYXKEYS.COLLECTION.POLICY_TAGS in getMoneyRequestInformation function from src/libs/actions/IOU/index.ts #87236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3801120
f85d333
b68cf85
0622aac
c141201
eada528
024bcc5
071d8fd
bdea6d0
a337297
de4e37e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -378,7 +378,7 @@ | |
| }; | ||
|
|
||
| let allPersonalDetails: OnyxTypes.PersonalDetailsList = {}; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.PERSONAL_DETAILS_LIST, | ||
| callback: (value) => { | ||
| allPersonalDetails = value ?? {}; | ||
|
|
@@ -430,7 +430,7 @@ | |
| }; | ||
|
|
||
| let allTransactions: NonNullable<OnyxCollection<OnyxTypes.Transaction>> = {}; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.COLLECTION.TRANSACTION, | ||
| waitForCollectionCallback: true, | ||
| callback: (value) => { | ||
|
|
@@ -444,7 +444,7 @@ | |
| }); | ||
|
|
||
| let allTransactionDrafts: NonNullable<OnyxCollection<OnyxTypes.Transaction>> = {}; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.COLLECTION.TRANSACTION_DRAFT, | ||
| waitForCollectionCallback: true, | ||
| callback: (value) => { | ||
|
|
@@ -453,7 +453,7 @@ | |
| }); | ||
|
|
||
| let allTransactionViolations: NonNullable<OnyxCollection<OnyxTypes.TransactionViolations>> = {}; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, | ||
| waitForCollectionCallback: true, | ||
| callback: (value) => { | ||
|
|
@@ -467,7 +467,7 @@ | |
| }); | ||
|
|
||
| let allPolicyTags: OnyxCollection<OnyxTypes.PolicyTagLists> = {}; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.COLLECTION.POLICY_TAGS, | ||
| waitForCollectionCallback: true, | ||
| callback: (value) => { | ||
|
|
@@ -480,7 +480,7 @@ | |
| }); | ||
|
|
||
| let allReports: OnyxCollection<OnyxTypes.Report>; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.COLLECTION.REPORT, | ||
| waitForCollectionCallback: true, | ||
| callback: (value) => { | ||
|
|
@@ -489,7 +489,7 @@ | |
| }); | ||
|
|
||
| let allReportNameValuePairs: OnyxCollection<OnyxTypes.ReportNameValuePairs>; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, | ||
| waitForCollectionCallback: true, | ||
| callback: (value) => { | ||
|
|
@@ -499,7 +499,7 @@ | |
|
|
||
| let deprecatedUserAccountID = -1; | ||
| let deprecatedCurrentUserEmail = ''; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.SESSION, | ||
| callback: (value) => { | ||
| deprecatedCurrentUserEmail = value?.email ?? ''; | ||
|
|
@@ -508,7 +508,7 @@ | |
| }); | ||
|
|
||
| let deprecatedCurrentUserPersonalDetails: OnyxEntry<OnyxTypes.PersonalDetails>; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.PERSONAL_DETAILS_LIST, | ||
| callback: (value) => { | ||
| deprecatedCurrentUserPersonalDetails = value?.[deprecatedUserAccountID] ?? undefined; | ||
|
|
@@ -516,7 +516,7 @@ | |
| }); | ||
|
|
||
| let allReportActions: OnyxCollection<OnyxTypes.ReportActions>; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, | ||
| waitForCollectionCallback: true, | ||
| callback: (actions) => { | ||
|
|
@@ -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) ?? {}; | ||
| } | ||
|
|
||
| /** | ||
| * @private | ||
| * After finishing the action in RHP from the Inbox tab, besides dismissing the modal, we should open the report. | ||
|
|
@@ -2191,6 +2216,7 @@ | |
| } = moneyRequestInformation; | ||
| const {payeeAccountID = deprecatedUserAccountID, payeeEmail = deprecatedCurrentUserEmail, participant} = participantParams; | ||
| const {policy, policyCategories, policyTagList, policyRecentlyUsedCategories, policyRecentlyUsedTags} = policyParams; | ||
|
|
||
| const { | ||
| attendees, | ||
| amount, | ||
|
|
@@ -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, | ||
| }); | ||
|
|
@@ -3285,7 +3309,7 @@ | |
| optimisticReportPreviewActionID, | ||
| shouldDeferAutoSubmit, | ||
| } = distanceRequestInformation; | ||
| const {policy, policyCategories, policyTagList, policyRecentlyUsedCategories, policyRecentlyUsedTags} = policyParams; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We were passing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
|
@@ -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, | ||
| }, | ||
|
|
@@ -3910,6 +3935,8 @@ | |
| maybeUpdateReportNameForFormulaTitle, | ||
| getSearchOnyxUpdate, | ||
| getPolicyTags, | ||
| // eslint-disable-next-line @typescript-eslint/no-deprecated | ||
| getMoneyRequestPolicyTags, | ||
| setMoneyRequestTimeRate, | ||
| setMoneyRequestTimeCount, | ||
| handleNavigateAfterExpenseCreate, | ||
|
|
||
| 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]}); | ||
| }); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bump @Guccio163
There was a problem hiding this comment.
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):
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.
There was a problem hiding this comment.
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 👀