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
2 changes: 1 addition & 1 deletion src/components/MoneyReportHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ function MoneyReportHeader({
icon: Expensicons.CircularArrowBackwards,
value: CONST.REPORT.SECONDARY_ACTIONS.RETRACT,
onSelected: () => {
retractReport(moneyRequestReport);
retractReport(moneyRequestReport, chatReport);
},
},
[CONST.REPORT.SECONDARY_ACTIONS.REOPEN]: {
Expand Down
17 changes: 16 additions & 1 deletion src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10437,7 +10437,7 @@ function reopenReport(expenseReport: OnyxEntry<OnyxTypes.Report>) {
API.write(WRITE_COMMANDS.REOPEN_REPORT, parameters, {optimisticData, successData, failureData});
}

function retractReport(expenseReport: OnyxEntry<OnyxTypes.Report>) {
function retractReport(expenseReport: OnyxEntry<OnyxTypes.Report>, chatReport: OnyxEntry<OnyxTypes.Report>) {
if (!expenseReport) {
return;
}
Expand Down Expand Up @@ -10483,6 +10483,21 @@ function retractReport(expenseReport: OnyxEntry<OnyxTypes.Report>) {

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,
},
});
}

const successData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down
33 changes: 33 additions & 0 deletions tests/actions/IOUTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
replaceReceipt,
requestMoney,
resolveDuplicates,
retractReport,
saveSplitTransactions,
sendInvoice,
setDraftSplitTransaction,
Expand Down Expand Up @@ -8908,4 +8909,36 @@ describe('actions/IOU', () => {
);
});
});

describe('retractReport', () => {
it('should restore the chat report iouReportID', async () => {
// Given a chat report with no iouReportID
const chatReport: Report = {
...createRandomReport(0),
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
iouReportID: undefined,
};

const expenseReport: Report = {
...createRandomReport(1),
type: CONST.REPORT.TYPE.EXPENSE,
chatType: undefined,
};

// When retracting the submitted expense report
retractReport(expenseReport, chatReport);

// Then the chat report iouReportID should be set back to the retracted expense report
const iouReportID = await new Promise<string | undefined>((resolve) => {
const connection = Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`,
callback: (report) => {
Onyx.disconnect(connection);
resolve(report?.iouReportID);
},
});
});
expect(iouReportID).toBe(expenseReport.reportID);
});
});
});
Loading