diff --git a/src/components/MoneyReportHeader.tsx b/src/components/MoneyReportHeader.tsx index a086cf45703e..9bfefa21da64 100644 --- a/src/components/MoneyReportHeader.tsx +++ b/src/components/MoneyReportHeader.tsx @@ -1542,10 +1542,10 @@ function MoneyReportHeader({ if (result.action !== ModalActions.CONFIRM) { return; } - reopenReport(moneyRequestReport, policy, accountID, email ?? '', hasViolations, isASAPSubmitBetaEnabled, nextStep); + reopenReport(moneyRequestReport, policy, accountID, email ?? '', hasViolations, isASAPSubmitBetaEnabled, nextStep, chatReport); return; } - reopenReport(moneyRequestReport, policy, accountID, email ?? '', hasViolations, isASAPSubmitBetaEnabled, nextStep); + reopenReport(moneyRequestReport, policy, accountID, email ?? '', hasViolations, isASAPSubmitBetaEnabled, nextStep, chatReport); }, }, [CONST.REPORT.SECONDARY_ACTIONS.REJECT]: { diff --git a/src/libs/actions/IOU/index.ts b/src/libs/actions/IOU/index.ts index ddac70a76053..455d36848469 100644 --- a/src/libs/actions/IOU/index.ts +++ b/src/libs/actions/IOU/index.ts @@ -10246,6 +10246,16 @@ function approveMoneyRequest( return optimisticHoldReportID; } +function determineIouReportID(chatReport: OnyxEntry, expenseReport: OnyxEntry): string | undefined { + const iouReportActions = getAllReportActions(chatReport?.iouReportID); + const expenseReportActions = getAllReportActions(expenseReport?.reportID); + const iouCreatedAction = Object.values(iouReportActions).find((action) => isCreatedAction(action)); + const expenseCreatedAction = Object.values(expenseReportActions).find((action) => isCreatedAction(action)); + + // The report created later will become the iouReportID of the chat report + return (iouCreatedAction?.created ?? '') > (expenseCreatedAction?.created ?? '') ? chatReport?.iouReportID : expenseReport?.reportID; +} + function reopenReport( expenseReport: OnyxEntry, policy: OnyxEntry, @@ -10254,6 +10264,7 @@ function reopenReport( hasViolations: boolean, isASAPSubmitBetaEnabled: boolean, expenseReportCurrentNextStepDeprecated: OnyxEntry, + chatReport: OnyxEntry, ) { if (!expenseReport) { return; @@ -10398,6 +10409,24 @@ function reopenReport( }); } + if (chatReport) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, + value: { + // The report created later will become the iouReportID of the chat report + iouReportID: determineIouReportID(chatReport, expenseReport), + }, + }); + failureData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, + value: { + iouReportID: chatReport.iouReportID, + }, + }); + } + const parameters: ReopenReportParams = { reportID: expenseReport.reportID, reportActionID: optimisticReopenedReportAction.reportActionID, @@ -10481,16 +10510,12 @@ function retractReport( const optimisticData: OnyxUpdate[] = [optimisticIOUReportData, optimisticReportActionsData, optimisticNextStepData]; if (chatReport) { - const iouReportActions = getAllReportActions(chatReport.iouReportID); - const expenseReportActions = getAllReportActions(expenseReport.reportID); - const iouCreatedAction = Object.values(iouReportActions).find((action) => isCreatedAction(action)); - const expenseCreatedAction = Object.values(expenseReportActions).find((action) => isCreatedAction(action)); optimisticData.push({ onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, value: { // The report created later will become the iouReportID of the chat report - iouReportID: (iouCreatedAction?.created ?? '') > (expenseCreatedAction?.created ?? '') ? chatReport?.iouReportID : expenseReport.reportID, + iouReportID: determineIouReportID(chatReport, expenseReport), }, }); }