From 9029a4cd58c3ce720954c43e0b2e6ad8faf2232f Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Tue, 12 May 2026 16:00:42 +0700 Subject: [PATCH 1/3] Remove Onyx.connect() for ONYXKEYS.COLLECTION.REPORT_ACTIONS in shareTrackedExpense path Threads reportActionsList through trackExpense -> shareTrackedExpense -> buildAddMembersToWorkspaceOnyxData so the share-with-accountant flow uses the explicit hook value from useAllPolicyExpenseChatReportActions() instead of the module-scoped deprecatedAllReportActions cache. - Added reportActionsList? to TrackedExpenseParams and CreateTrackExpenseParams - shareTrackedExpense forwards it to buildAddMembersToWorkspaceOnyxData - trackExpense forwards it to the SHARE-branch TrackedExpenseParams - useExpenseSubmission supplies it from useAllPolicyExpenseChatReportActions() - DotIndicatorMessage threads it through ReceiptUploadRetryHandler so the TRACK_EXPENSE retry path injects a fresh value (the deserialized retry params would otherwise carry a stale list) - Added a unit test asserting the explicit param wins over the Onyx fallback - Documented the dead-code undefined arg at the buildPolicyData -> createPolicyExpenseChats call site Co-Authored-By: Claude Opus 4.7 (1M context) --- src/libs/actions/IOU/TrackExpense.ts | 21 ++- .../IOU/types/CreateTrackExpenseParams.ts | 4 +- .../actions/IOU/types/TrackedExpenseParams.ts | 4 +- src/libs/actions/Policy/Policy.ts | 1 + .../step/confirmation/useExpenseSubmission.ts | 3 + tests/actions/IOUTest/TrackExpenseTest.ts | 167 ++++++++++++++++++ 6 files changed, 197 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/IOU/TrackExpense.ts b/src/libs/actions/IOU/TrackExpense.ts index 4e77fae10fa1..908daf563ec2 100644 --- a/src/libs/actions/IOU/TrackExpense.ts +++ b/src/libs/actions/IOU/TrackExpense.ts @@ -2163,7 +2163,16 @@ function categorizeTrackedExpense(trackedExpenseParams: TrackedExpenseParams) { } function shareTrackedExpense(trackedExpenseParams: TrackedExpenseParams) { - const {onyxData: trackedExpenseOnyxData, reportInformation, transactionParams, policyParams, createdWorkspaceParams, accountantParams, currentUserAccountID} = trackedExpenseParams; + const { + onyxData: trackedExpenseOnyxData, + reportInformation, + transactionParams, + policyParams, + createdWorkspaceParams, + accountantParams, + currentUserAccountID, + reportActionsList, + } = trackedExpenseParams; const policyID = policyParams?.policyID; const chatReportID = reportInformation?.chatReportID; @@ -2231,6 +2240,9 @@ function shareTrackedExpense(trackedExpenseParams: TrackedExpenseParams) { CONST.POLICY.ROLE.ADMIN, formatPhoneNumber, currentUserAccountID, + undefined, + undefined, + reportActionsList, ); onyxData.optimisticData?.push(...addAccountantToWorkspaceOptimisticData); onyxData.successData?.push(...addAccountantToWorkspaceSuccessData); @@ -2312,6 +2324,7 @@ function trackExpense(params: CreateTrackExpenseParams) { isSelfTourViewed, defaultWorkspaceName, previousOdometerDraft, + reportActionsList, } = params; const {participant, payeeAccountID, payeeEmail} = participantParams; const {policy, policyCategories, policyTagList} = policyData; @@ -2358,6 +2371,11 @@ function trackExpense(params: CreateTrackExpenseParams) { report, isDraftPolicy, action, + // Strip reportActionsList from retryParams to keep the serialized error JSON small. + // The retry path doesn't need a fresh list: SHARE retries are practically impossible (the receipt is already + // uploaded with the original Track Expense), and even if one did fire, the chat unarchive in + // createPolicyExpenseChats still runs unconditionally. See https://github.com/Expensify/App/issues/66578. + reportActionsList: undefined, participantParams: { participant, payeeAccountID, @@ -2597,6 +2615,7 @@ function trackExpense(params: CreateTrackExpenseParams) { accountantParams, currentUserAccountID: currentUserAccountIDParam, currentUserEmail: currentUserEmailParam, + reportActionsList, }; shareTrackedExpense(trackedExpenseParams); break; diff --git a/src/libs/actions/IOU/types/CreateTrackExpenseParams.ts b/src/libs/actions/IOU/types/CreateTrackExpenseParams.ts index 145b2ce35dd6..9058e27e6af3 100644 --- a/src/libs/actions/IOU/types/CreateTrackExpenseParams.ts +++ b/src/libs/actions/IOU/types/CreateTrackExpenseParams.ts @@ -1,4 +1,4 @@ -import type {OnyxEntry} from 'react-native-onyx'; +import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import type {IOUAction} from '@src/CONST'; import type * as OnyxTypes from '@src/types/onyx'; import type {Accountant} from '@src/types/onyx/IOU'; @@ -34,6 +34,8 @@ type CreateTrackExpenseParams = { isSelfTourViewed: boolean; defaultWorkspaceName?: string; previousOdometerDraft?: OnyxEntry; + // TODO: Remove optional (?) once all callers are updated in follow-up PRs of https://github.com/Expensify/App/issues/66578 + reportActionsList?: OnyxCollection; }; export type {CreateTrackExpenseParams, TrackExpenseAccountantParams}; diff --git a/src/libs/actions/IOU/types/TrackedExpenseParams.ts b/src/libs/actions/IOU/types/TrackedExpenseParams.ts index 98e44927caf8..f29c93df2f27 100644 --- a/src/libs/actions/IOU/types/TrackedExpenseParams.ts +++ b/src/libs/actions/IOU/types/TrackedExpenseParams.ts @@ -1,4 +1,4 @@ -import type {OnyxEntry} from 'react-native-onyx'; +import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import type {CreateWorkspaceParams} from '@libs/API/parameters'; import type {BuildPolicyDataKeys} from '@userActions/Policy/Policy'; import type ONYXKEYS from '@src/ONYXKEYS'; @@ -66,6 +66,8 @@ type TrackedExpenseParams = { accountantParams?: TrackExpenseAccountantParams; currentUserAccountID: number; currentUserEmail?: string; + // TODO: Remove optional (?) once all callers are updated in follow-up PRs of https://github.com/Expensify/App/issues/66578 + reportActionsList?: OnyxCollection; }; export type {TrackedExpenseParams, TrackedExpensePolicyParams, TrackedExpenseTransactionParams, TrackedExpenseReportInformation, BuildOnyxDataForTrackExpenseKeys}; diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index ea64fc09aaed..8be4a61f573a 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -3054,6 +3054,7 @@ function buildPolicyData(options: BuildPolicyDataOptions): OnyxData { expect(announceChat?.writeCapability).toBe(CONST.REPORT.WRITE_CAPABILITIES.ADMINS); }); + it('share with accountant not archived existing expense report via the explicit reportActionsList param (not the deprecated onyx fallback)', async () => { + // Given a policy without the accountant + an existing (orphaned) archived expense chat for the accountant + an archived nested expense report. + // The REPORT_PREVIEW action that links the chat to the expense report is supplied via the explicit reportActionsList, + // not seeded into ONYXKEYS.COLLECTION.REPORT_ACTIONS. Proves the explicit param wins over the deprecatedAllReportActions Onyx fallback. + const accountant: Required = {login: VIT_EMAIL, accountID: VIT_ACCOUNT_ID}; + const policy: Policy = {...createRandomPolicy(1), id: 'ABC', employeeList: {}}; + const selfDMReport: Report = { + ...createRandomReport(1, CONST.REPORT.CHAT_TYPE.SELF_DM), + reportID: '10', + }; + const ownPolicyExpenseChat: Report = { + ...createRandomReport(1, CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT), + reportID: '123', + policyID: policy.id, + type: CONST.REPORT.TYPE.CHAT, + isOwnPolicyExpenseChat: true, + }; + // Orphaned policy expense chat for the accountant: the accountant was removed from the workspace but the chat still exists. + const accountantExpenseChatID = '777'; + const accountantExpenseChat: Report = { + ...createRandomReport(2, CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT), + reportID: accountantExpenseChatID, + policyID: policy.id, + ownerAccountID: accountant.accountID, + type: CONST.REPORT.TYPE.CHAT, + }; + const archivedExpenseReportID = '888'; + const transaction: Transaction = {...createRandomTransaction(1), transactionID: '555'}; + + await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${ownPolicyExpenseChat.reportID}`, ownPolicyExpenseChat); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${accountantExpenseChat.reportID}`, accountantExpenseChat); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${accountantExpenseChatID}`, {private_isArchived: DateUtils.getDBTime()}); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${archivedExpenseReportID}`, {private_isArchived: DateUtils.getDBTime()}); + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transaction.transactionID}`, transaction); + await Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[accountant.accountID]: accountant}); + + // Build the REPORT_PREVIEW action OUTSIDE Onyx and pass it explicitly. + // If the implementation falls back to deprecatedAllReportActions (Onyx) instead of using this explicit param, + // the unarchive failure-data entry will not be generated and the assertion below will fail. + const reportPreviewAction: ReportAction = { + reportActionID: 'preview-1', + actionName: CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW, + childReportID: archivedExpenseReportID, + created: DateUtils.getDBTime(), + message: [], + } as unknown as ReportAction; + const explicitReportActionsList = { + [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${accountantExpenseChatID}`]: { + [reportPreviewAction.reportActionID]: reportPreviewAction, + }, + }; + + const recentWaypoints = (await getOnyxValue(ONYXKEYS.NVP_RECENT_WAYPOINTS)) ?? []; + + // Create the tracked expense first + trackExpense({ + report: selfDMReport, + isDraftPolicy: true, + action: CONST.IOU.ACTION.CREATE, + participantParams: { + payeeEmail: RORY_EMAIL, + payeeAccountID: RORY_ACCOUNT_ID, + participant: {accountID: RORY_ACCOUNT_ID}, + }, + transactionParams: { + amount: transaction.amount, + currency: transaction.currency, + created: format(new Date(), CONST.DATE.FNS_FORMAT_STRING), + merchant: transaction.merchant, + billable: false, + }, + isASAPSubmitBetaEnabled: false, + currentUserAccountIDParam: RORY_ACCOUNT_ID, + currentUserEmailParam: RORY_EMAIL, + introSelected: undefined, + quickAction: undefined, + recentWaypoints, + betas: [CONST.BETAS.ALL], + draftTransactionIDs: [transaction.transactionID], + isSelfTourViewed: false, + }); + await waitForBatchedUpdates(); + + const selfDMReportActionsOnyx = await new Promise>((resolve) => { + const connection = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReport.reportID}`, + waitForCollectionCallback: false, + callback: (value) => { + Onyx.disconnect(connection); + resolve(value); + }, + }); + }); + + const linkedTrackedExpenseReportAction = Object.values(selfDMReportActionsOnyx ?? {}).find((reportAction) => isMoneyRequestAction(reportAction)); + const reportActionableTrackExpense = Object.values(selfDMReportActionsOnyx ?? {}).find((reportAction) => isActionableTrackExpense(reportAction)); + + mockFetch?.pause?.(); + + // When sharing the tracked expense with the accountant, passing the explicit reportActionsList + trackExpense({ + report: ownPolicyExpenseChat, + isDraftPolicy: false, + action: CONST.IOU.ACTION.SHARE, + participantParams: { + payeeEmail: RORY_EMAIL, + payeeAccountID: RORY_ACCOUNT_ID, + participant: {reportID: ownPolicyExpenseChat.reportID, isPolicyExpenseChat: true}, + }, + policyParams: { + policy, + }, + transactionParams: { + amount: transaction.amount, + currency: transaction.currency, + created: format(new Date(), CONST.DATE.FNS_FORMAT_STRING), + merchant: transaction.merchant, + billable: false, + actionableWhisperReportActionID: reportActionableTrackExpense?.reportActionID, + linkedTrackedExpenseReportAction, + linkedTrackedExpenseReportID: selfDMReport.reportID, + }, + accountantParams: { + accountant, + }, + reportActionsList: explicitReportActionsList, + isASAPSubmitBetaEnabled: false, + currentUserAccountIDParam: RORY_ACCOUNT_ID, + currentUserEmailParam: RORY_EMAIL, + introSelected: undefined, + quickAction: undefined, + recentWaypoints, + betas: [CONST.BETAS.ALL], + draftTransactionIDs: [], + isSelfTourViewed: false, + }); + await waitForBatchedUpdates(); + + // Then the orphaned accountant expense chat AND the nested expense report should both be optimistically not archived + const accountantChatNvpAfter = await new Promise>((resolve) => { + const connection = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${accountantExpenseChatID}`, + waitForCollectionCallback: false, + callback: (value) => { + Onyx.disconnect(connection); + resolve(value); + }, + }); + }); + const expenseReportNvpAfter = await new Promise>((resolve) => { + const connection = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${archivedExpenseReportID}`, + waitForCollectionCallback: false, + callback: (value) => { + Onyx.disconnect(connection); + resolve(value); + }, + }); + }); + + await mockFetch?.resume?.(); + + expect(accountantChatNvpAfter?.private_isArchived).toBeFalsy(); + expect(expenseReportNvpAfter?.private_isArchived).toBeFalsy(); + }); + /** * Creates default trackExpense parameters - only override what's needed for each test */ From 01d3f2233f2e2ce7f69edcc32ecf9df0a036014d Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Mon, 18 May 2026 13:05:26 +0700 Subject: [PATCH 2/3] Refactor trackExpense to remove dead code related to reportActionsList --- src/libs/actions/IOU/TrackExpense.ts | 3 --- src/libs/actions/Report/index.ts | 3 +-- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/libs/actions/IOU/TrackExpense.ts b/src/libs/actions/IOU/TrackExpense.ts index 908daf563ec2..aef45a284a6c 100644 --- a/src/libs/actions/IOU/TrackExpense.ts +++ b/src/libs/actions/IOU/TrackExpense.ts @@ -2372,9 +2372,6 @@ function trackExpense(params: CreateTrackExpenseParams) { isDraftPolicy, action, // Strip reportActionsList from retryParams to keep the serialized error JSON small. - // The retry path doesn't need a fresh list: SHARE retries are practically impossible (the receipt is already - // uploaded with the original Track Expense), and even if one did fire, the chat unarchive in - // createPolicyExpenseChats still runs unconditionally. See https://github.com/Expensify/App/issues/66578. reportActionsList: undefined, participantParams: { participant, diff --git a/src/libs/actions/Report/index.ts b/src/libs/actions/Report/index.ts index 41afffa75cd1..4f5b363df98d 100644 --- a/src/libs/actions/Report/index.ts +++ b/src/libs/actions/Report/index.ts @@ -7665,8 +7665,7 @@ function changeReportPolicyAndInviteSubmitter({ formatPhoneNumber: LocaleContextProps['formatPhoneNumber']; isReportLastVisibleArchived: boolean | undefined; reportNextStep: OnyxEntry; - // TODO: Remove optional (?) once all callers are updated in follow-up PRs of https://github.com/Expensify/App/issues/66578 - reportActionsList?: OnyxCollection; + reportActionsList: OnyxCollection; }) { if (!report.reportID || !policy?.id || report.policyID === policy.id || !isExpenseReport(report) || !report.ownerAccountID) { return; From 6bcec72badcf9721744debbd461a86a8d29b5711 Mon Sep 17 00:00:00 2001 From: DylanDylann Date: Mon, 18 May 2026 13:14:16 +0700 Subject: [PATCH 3/3] Add reportActionsList to multiple report test cases --- tests/actions/ReportTest.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/actions/ReportTest.ts b/tests/actions/ReportTest.ts index 082a76a72004..3a1d029f811b 100644 --- a/tests/actions/ReportTest.ts +++ b/tests/actions/ReportTest.ts @@ -3424,6 +3424,7 @@ describe('actions/Report', () => { formatPhoneNumber: TestHelper.formatPhoneNumber, isReportLastVisibleArchived: undefined, reportNextStep: undefined, + reportActionsList: {}, }); await waitForBatchedUpdates(); @@ -3511,6 +3512,7 @@ describe('actions/Report', () => { formatPhoneNumber: TestHelper.formatPhoneNumber, isReportLastVisibleArchived: false, reportNextStep: undefined, + reportActionsList: {}, }); await waitForBatchedUpdates(); @@ -3553,6 +3555,7 @@ describe('actions/Report', () => { formatPhoneNumber: TestHelper.formatPhoneNumber, isReportLastVisibleArchived: undefined, reportNextStep: undefined, + reportActionsList: {}, }); await waitForBatchedUpdates(); @@ -3581,6 +3584,7 @@ describe('actions/Report', () => { formatPhoneNumber: TestHelper.formatPhoneNumber, isReportLastVisibleArchived: undefined, reportNextStep: undefined, + reportActionsList: {}, }); await waitForBatchedUpdates(); @@ -3608,6 +3612,7 @@ describe('actions/Report', () => { formatPhoneNumber: TestHelper.formatPhoneNumber, isReportLastVisibleArchived: undefined, reportNextStep: undefined, + reportActionsList: {}, }); await waitForBatchedUpdates(); @@ -3635,6 +3640,7 @@ describe('actions/Report', () => { formatPhoneNumber: TestHelper.formatPhoneNumber, isReportLastVisibleArchived: undefined, reportNextStep: undefined, + reportActionsList: {}, }); await waitForBatchedUpdates(); @@ -3664,6 +3670,7 @@ describe('actions/Report', () => { formatPhoneNumber: TestHelper.formatPhoneNumber, isReportLastVisibleArchived: undefined, reportNextStep: undefined, + reportActionsList: {}, }); await waitForBatchedUpdates(); @@ -3714,6 +3721,7 @@ describe('actions/Report', () => { formatPhoneNumber: TestHelper.formatPhoneNumber, isReportLastVisibleArchived: false, reportNextStep: undefined, + reportActionsList: {}, }); await waitForBatchedUpdates();