Skip to content

Commit c56fd4e

Browse files
authored
Merge pull request #90878 from parasharrajat/onyx/report-2
Refactor `deleteMoneyRequest` to pass transactionThreadReport
2 parents 9c8da19 + 25646bc commit c56fd4e

5 files changed

Lines changed: 63 additions & 17 deletions

File tree

src/hooks/useDeleteTransactions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,13 @@ function useDeleteTransactions({report, reportActions, policy}: UseDeleteTransac
263263
const iouReportID = isMoneyRequestAction(action) ? getOriginalMessage(action)?.IOUReportID : undefined;
264264
const iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`];
265265
const chatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${iouReport?.chatReportID}`];
266+
const transactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${action?.childReportID}`];
266267
const chatIOUReportID = chatReport?.reportID;
267268
const isChatIOUReportArchived = archivedReportsIdSet.has(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${chatIOUReportID}`);
268269
deleteMoneyRequest({
269270
transactionID,
270271
reportAction: action,
272+
transactionThreadReport,
271273
transactions: duplicateTransactions,
272274
violations: duplicateTransactionViolations,
273275
iouReport,

src/libs/actions/IOU/DeleteMoneyRequest.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type DeleteMoneyRequestFunctionParams = {
5151
allTransactionViolationsParam: OnyxCollection<OnyxTypes.TransactionViolations>;
5252
currentUserAccountID: number;
5353
currentUserEmail: string;
54+
transactionThreadReport: OnyxEntry<OnyxTypes.Report>;
5455
};
5556

5657
/**
@@ -62,6 +63,7 @@ type DeleteMoneyRequestFunctionParams = {
6263
function prepareToCleanUpMoneyRequest(
6364
transactionID: string,
6465
reportAction: OnyxTypes.ReportAction,
66+
transactionThreadReport: OnyxEntry<OnyxTypes.Report>,
6567
iouReport: OnyxEntry<OnyxTypes.Report>,
6668
chatReport: OnyxEntry<OnyxTypes.Report>,
6769
isChatReportArchived: boolean | undefined,
@@ -71,7 +73,6 @@ function prepareToCleanUpMoneyRequest(
7173
) {
7274
const allTransactions = getAllTransactions();
7375
const allTransactionViolations = getAllTransactionViolations();
74-
const allReports = getAllReports();
7576
const allReportActions = getAllReportActionsFromIOU();
7677

7778
// STEP 1: Get all collections we're updating
@@ -80,18 +81,13 @@ function prepareToCleanUpMoneyRequest(
8081
const transaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`];
8182
const isTransactionOnHold = isOnHold(transaction);
8283
const transactionViolations = allTransactionViolations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`];
83-
const transactionThreadID = reportAction.childReportID;
84-
let transactionThread = null;
85-
if (transactionThreadID) {
86-
transactionThread = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadID}`] ?? null;
87-
}
8884

8985
// STEP 2: Decide if we need to:
9086
// 1. Delete the transactionThread - delete if there are no visible comments in the thread
9187
// 2. Update the moneyRequestPreview to show [Deleted expense] - update if the transactionThread exists AND it isn't being deleted
9288
// The current state is that we want to get rid of the [Deleted expense] breadcrumb,
9389
// so we never want to display it if transactionThreadID is present.
94-
const shouldDeleteTransactionThread = !!transactionThreadID;
90+
const shouldDeleteTransactionThread = !!transactionThreadReport?.reportID;
9591

9692
// STEP 3: Update the IOU reportAction and decide if the iouReport should be deleted. We delete the iouReport if there are no visible comments left in the report.
9793
const updatedReportAction = {
@@ -234,8 +230,8 @@ function prepareToCleanUpMoneyRequest(
234230
updatedReportAction,
235231
updatedIOUReport,
236232
updatedReportPreviewAction,
237-
transactionThreadID,
238-
transactionThread,
233+
transactionThreadID: transactionThreadReport?.reportID,
234+
transactionThreadReport,
239235
transaction,
240236
transactionViolations,
241237
reportPreviewAction,
@@ -261,8 +257,17 @@ function getNavigationUrlOnMoneyRequestDelete(
261257
if (!transactionID) {
262258
return undefined;
263259
}
260+
const allReports = getAllReports();
261+
const transactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportAction.childReportID}`];
264262

265-
const {shouldDeleteTransactionThread, shouldDeleteIOUReport} = prepareToCleanUpMoneyRequest(transactionID, reportAction, iouReport, chatReport, isChatReportArchived);
263+
const {shouldDeleteTransactionThread, shouldDeleteIOUReport} = prepareToCleanUpMoneyRequest(
264+
transactionID,
265+
reportAction,
266+
transactionThreadReport,
267+
iouReport,
268+
chatReport,
269+
isChatReportArchived,
270+
);
266271

267272
// Determine which report to navigate back to
268273
if (iouReport && isSingleTransactionView && shouldDeleteTransactionThread && !shouldDeleteIOUReport) {
@@ -293,8 +298,11 @@ function cleanUpMoneyRequest(
293298
originalReportID: string | undefined,
294299
isSingleTransactionView = false,
295300
) {
301+
const allReports = getAllReports();
302+
const transactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportAction.childReportID}`];
303+
296304
const {shouldDeleteTransactionThread, shouldDeleteIOUReport, updatedReportAction, updatedIOUReport, updatedReportPreviewAction, transactionThreadID, reportPreviewAction} =
297-
prepareToCleanUpMoneyRequest(transactionID, reportAction, iouReport, chatReport, isChatIOUReportArchived, false);
305+
prepareToCleanUpMoneyRequest(transactionID, reportAction, transactionThreadReport, iouReport, chatReport, isChatIOUReportArchived, false);
298306

299307
const urlToNavigateBack = getNavigationUrlOnMoneyRequestDelete(transactionID, reportAction, iouReport, chatReport, isChatIOUReportArchived, isSingleTransactionView);
300308
// build Onyx data
@@ -637,6 +645,7 @@ function deleteMoneyRequest({
637645
transactionID,
638646
reportAction,
639647
transactions,
648+
transactionThreadReport,
640649
violations,
641650
iouReport,
642651
chatReport,
@@ -664,7 +673,17 @@ function deleteMoneyRequest({
664673
transactionViolations,
665674
reportPreviewAction,
666675
iouReportActions,
667-
} = prepareToCleanUpMoneyRequest(transactionID, reportAction, iouReport, chatReport, isChatIOUReportArchived, false, transactionIDsPendingDeletion, selectedTransactionIDs);
676+
} = prepareToCleanUpMoneyRequest(
677+
transactionID,
678+
reportAction,
679+
transactionThreadReport,
680+
iouReport,
681+
chatReport,
682+
isChatIOUReportArchived,
683+
false,
684+
transactionIDsPendingDeletion,
685+
selectedTransactionIDs,
686+
);
668687

669688
const urlToNavigateBack = getNavigationUrlOnMoneyRequestDelete(transactionID, reportAction, iouReport, chatReport, isChatIOUReportArchived, isSingleTransactionView);
670689

src/libs/actions/IOU/TrackExpense.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,10 +2765,14 @@ function deleteTrackExpense({
27652765

27662766
// STEP 1: Get all collections we're updating
27672767
if (!isSelfDM(chatReport)) {
2768+
const allReports = getAllReports();
2769+
const transactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportAction.childReportID}`];
2770+
27682771
deleteMoneyRequest({
27692772
transactionID,
27702773
reportAction,
27712774
transactions,
2775+
transactionThreadReport,
27722776
violations,
27732777
iouReport,
27742778
chatReport: chatIOUReport,

src/libs/actions/Search.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,11 +959,13 @@ function bulkDeleteReports({
959959
const reportID = selectedTransactions[transactionID].report?.reportID;
960960
const batchTransactionIDsForReport = reportID ? (transactionsByReport[reportID] ?? []) : [];
961961
const chatReport = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${selectedTransactions[transactionID].report?.chatReportID}`];
962+
const transactionThreadReport = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportAction?.childReportID}`];
962963
const reportNameValuePair = allReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${chatReport?.reportID}`];
963964

964965
deleteMoneyRequest({
965966
transactionID,
966967
reportAction,
968+
transactionThreadReport,
967969
transactions,
968970
violations: transactionsViolations,
969971
iouReport: selectedTransactions[transactionID].report,

tests/actions/IOUTest/DeleteMoneyRequestTest.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
124124
let thread: OptimisticChatReport;
125125
const TEST_USER_ACCOUNT_ID = 1;
126126
const TEST_USER_LOGIN = 'test@test.com';
127+
const expectedTransactionThreadParticipants = {
128+
[TEST_USER_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN},
129+
};
127130
let IOU_REPORT_ID: string | undefined;
128131
let IOU_REPORT: OnyxEntry<Report>;
129132
let reportActionID;
@@ -243,6 +246,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
243246
);
244247
expect(createIOUAction).toBeTruthy();
245248
expect(createIOUAction && getOriginalMessage(createIOUAction)?.IOUReportID).toBe(iouReport?.reportID);
249+
thread = (await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${createIOUAction?.childReportID}`)) as OptimisticChatReport;
246250

247251
// When fetching all transactions from Onyx
248252
let allTransactions: OnyxCollection<Transaction>;
@@ -277,6 +281,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
277281
violations: {},
278282
iouReport,
279283
chatReport,
284+
transactionThreadReport: thread,
280285
isChatIOUReportArchived: true,
281286
allTransactionViolationsParam: {},
282287
currentUserAccountID: TEST_USER_ACCOUNT_ID,
@@ -367,6 +372,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
367372
violations: {},
368373
iouReport,
369374
chatReport,
375+
transactionThreadReport: thread,
370376
isChatIOUReportArchived: true,
371377
allTransactionViolationsParam: {},
372378
currentUserAccountID: TEST_USER_ACCOUNT_ID,
@@ -451,6 +457,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
451457
violations: {},
452458
iouReport,
453459
chatReport,
460+
transactionThreadReport: thread,
454461
allTransactionViolationsParam: {},
455462
currentUserAccountID: TEST_USER_ACCOUNT_ID,
456463
currentUserEmail: TEST_USER_LOGIN,
@@ -505,7 +512,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
505512
// Given a transaction thread
506513
thread = buildTransactionThread(createIOUAction, iouReport, TEST_USER_ACCOUNT_ID);
507514

508-
expect(thread.participants).toStrictEqual({[CARLOS_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}});
515+
expect(thread.participants).toStrictEqual(expectedTransactionThreadParticipants);
509516

510517
Onyx.connect({
511518
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`,
@@ -568,6 +575,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
568575
violations: {},
569576
iouReport,
570577
chatReport,
578+
transactionThreadReport: thread,
571579
allTransactionViolationsParam: {},
572580
currentUserAccountID: TEST_USER_ACCOUNT_ID,
573581
currentUserEmail: TEST_USER_LOGIN,
@@ -723,6 +731,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
723731
violations: {},
724732
iouReport,
725733
chatReport,
734+
transactionThreadReport: thread,
726735
allTransactionViolationsParam: {},
727736
currentUserAccountID: TEST_USER_ACCOUNT_ID,
728737
currentUserEmail: TEST_USER_LOGIN,
@@ -752,7 +761,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
752761
// Given a transaction thread
753762
thread = buildTransactionThread(createIOUAction, iouReport, TEST_USER_ACCOUNT_ID);
754763

755-
expect(thread.participants).toEqual({[CARLOS_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}});
764+
expect(thread.participants).toEqual(expectedTransactionThreadParticipants);
756765

757766
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
758767
const userLogins = getLoginsByAccountIDs(participantAccountIDs);
@@ -844,6 +853,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
844853
violations: {},
845854
iouReport,
846855
chatReport,
856+
transactionThreadReport: thread,
847857
allTransactionViolationsParam: {},
848858
currentUserAccountID: TEST_USER_ACCOUNT_ID,
849859
currentUserEmail: TEST_USER_LOGIN,
@@ -891,7 +901,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
891901
jest.advanceTimersByTime(10);
892902
thread = buildTransactionThread(createIOUAction, iouReport, TEST_USER_ACCOUNT_ID);
893903

894-
expect(thread.participants).toStrictEqual({[CARLOS_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}});
904+
expect(thread.participants).toStrictEqual(expectedTransactionThreadParticipants);
895905

896906
Onyx.connect({
897907
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`,
@@ -1038,6 +1048,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
10381048
violations: {},
10391049
iouReport,
10401050
chatReport,
1051+
transactionThreadReport: thread,
10411052
isChatIOUReportArchived: undefined,
10421053
allTransactionViolationsParam: {},
10431054
currentUserAccountID: TEST_USER_ACCOUNT_ID,
@@ -1152,6 +1163,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
11521163
violations: {},
11531164
iouReport,
11541165
chatReport,
1166+
transactionThreadReport: thread,
11551167
isChatIOUReportArchived: undefined,
11561168
allTransactionViolationsParam: {},
11571169
currentUserAccountID: TEST_USER_ACCOUNT_ID,
@@ -1213,7 +1225,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
12131225
jest.advanceTimersByTime(10);
12141226
thread = buildTransactionThread(createIOUAction, iouReport, TEST_USER_ACCOUNT_ID);
12151227

1216-
expect(thread.participants).toStrictEqual({[CARLOS_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}});
1228+
expect(thread.participants).toStrictEqual(expectedTransactionThreadParticipants);
12171229

12181230
jest.advanceTimersByTime(10);
12191231
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
@@ -1263,6 +1275,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
12631275
violations: {},
12641276
iouReport,
12651277
chatReport,
1278+
transactionThreadReport: thread,
12661279
isSingleTransactionView: true,
12671280
allTransactionViolationsParam: {},
12681281
currentUserAccountID: TEST_USER_ACCOUNT_ID,
@@ -1322,6 +1335,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
13221335
violations: {},
13231336
iouReport,
13241337
chatReport,
1338+
transactionThreadReport: thread,
13251339
allTransactionViolationsParam: {},
13261340
currentUserAccountID: TEST_USER_ACCOUNT_ID,
13271341
currentUserEmail: TEST_USER_LOGIN,
@@ -1393,7 +1407,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
13931407
// Given a transaction thread
13941408
thread = buildTransactionThread(createIOUAction, iouReport, TEST_USER_ACCOUNT_ID);
13951409

1396-
expect(thread.participants).toEqual({[CARLOS_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN}});
1410+
expect(thread.participants).toEqual(expectedTransactionThreadParticipants);
13971411

13981412
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
13991413
const userLogins = getLoginsByAccountIDs(participantAccountIDs);
@@ -1494,6 +1508,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
14941508
violations: {},
14951509
iouReport,
14961510
chatReport,
1511+
transactionThreadReport: thread,
14971512
allTransactionViolationsParam: {},
14981513
currentUserAccountID: TEST_USER_ACCOUNT_ID,
14991514
currentUserEmail: TEST_USER_LOGIN,
@@ -1584,6 +1599,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
15841599
violations: {},
15851600
iouReport: expenseReport,
15861601
chatReport: expenseReport,
1602+
transactionThreadReport: undefined,
15871603
transactionIDsPendingDeletion: [],
15881604
selectedTransactionIDs,
15891605
allTransactionViolationsParam: {},
@@ -1597,6 +1613,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
15971613
violations: {},
15981614
iouReport: expenseReport,
15991615
chatReport: expenseReport,
1616+
transactionThreadReport: undefined,
16001617
transactionIDsPendingDeletion: [transaction1.transactionID],
16011618
selectedTransactionIDs,
16021619
allTransactionViolationsParam: {},
@@ -1676,6 +1693,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
16761693
violations: {},
16771694
iouReport: expenseReport,
16781695
chatReport: expenseReport,
1696+
transactionThreadReport: undefined,
16791697
allTransactionViolationsParam: transactionViolations,
16801698
currentUserAccountID: TEST_USER_ACCOUNT_ID,
16811699
currentUserEmail: TEST_USER_LOGIN,
@@ -1740,6 +1758,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
17401758
violations: {},
17411759
iouReport: expenseReport,
17421760
chatReport: expenseReport,
1761+
transactionThreadReport: undefined,
17431762
allTransactionViolationsParam: {},
17441763
currentUserAccountID: TEST_USER_ACCOUNT_ID,
17451764
currentUserEmail: TEST_USER_LOGIN,

0 commit comments

Comments
 (0)