Skip to content
Open
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: 2 additions & 0 deletions src/hooks/useDeleteTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,13 @@ function useDeleteTransactions({report, reportActions, policy}: UseDeleteTransac
const iouReportID = isMoneyRequestAction(action) ? getOriginalMessage(action)?.IOUReportID : undefined;
const iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`];
const chatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${iouReport?.chatReportID}`];
const transactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${action?.childReportID}`];
const chatIOUReportID = chatReport?.reportID;
const isChatIOUReportArchived = archivedReportsIdSet.has(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${chatIOUReportID}`);
deleteMoneyRequest({
transactionID,
reportAction: action,
transactionThreadReport,
transactions: duplicateTransactions,
violations: duplicateTransactionViolations,
iouReport,
Expand Down
43 changes: 31 additions & 12 deletions src/libs/actions/IOU/DeleteMoneyRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type DeleteMoneyRequestFunctionParams = {
allTransactionViolationsParam: OnyxCollection<OnyxTypes.TransactionViolations>;
currentUserAccountID: number;
currentUserEmail: string;
transactionThreadReport: OnyxEntry<OnyxTypes.Report>;
};

/**
Expand All @@ -62,6 +63,7 @@ type DeleteMoneyRequestFunctionParams = {
function prepareToCleanUpMoneyRequest(
transactionID: string,
reportAction: OnyxTypes.ReportAction,
transactionThreadReport: OnyxEntry<OnyxTypes.Report>,
iouReport: OnyxEntry<OnyxTypes.Report>,
chatReport: OnyxEntry<OnyxTypes.Report>,
isChatReportArchived: boolean | undefined,
Expand All @@ -71,7 +73,6 @@ function prepareToCleanUpMoneyRequest(
) {
const allTransactions = getAllTransactions();
const allTransactionViolations = getAllTransactionViolations();
const allReports = getAllReports();
const allReportActions = getAllReportActionsFromIOU();

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

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

// 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.
const updatedReportAction = {
Expand Down Expand Up @@ -234,8 +230,8 @@ function prepareToCleanUpMoneyRequest(
updatedReportAction,
updatedIOUReport,
updatedReportPreviewAction,
transactionThreadID,
transactionThread,
transactionThreadID: transactionThreadReport?.reportID,
transactionThreadReport,
transaction,
transactionViolations,
reportPreviewAction,
Expand All @@ -261,8 +257,17 @@ function getNavigationUrlOnMoneyRequestDelete(
if (!transactionID) {
return undefined;
}
const allReports = getAllReports();
const transactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportAction.childReportID}`];

const {shouldDeleteTransactionThread, shouldDeleteIOUReport} = prepareToCleanUpMoneyRequest(transactionID, reportAction, iouReport, chatReport, isChatReportArchived);
const {shouldDeleteTransactionThread, shouldDeleteIOUReport} = prepareToCleanUpMoneyRequest(
transactionID,
reportAction,
transactionThreadReport,
iouReport,
chatReport,
isChatReportArchived,
);

// Determine which report to navigate back to
if (iouReport && isSingleTransactionView && shouldDeleteTransactionThread && !shouldDeleteIOUReport) {
Expand Down Expand Up @@ -293,8 +298,11 @@ function cleanUpMoneyRequest(
originalReportID: string | undefined,
isSingleTransactionView = false,
) {
const allReports = getAllReports();
const transactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportAction.childReportID}`];

const {shouldDeleteTransactionThread, shouldDeleteIOUReport, updatedReportAction, updatedIOUReport, updatedReportPreviewAction, transactionThreadID, reportPreviewAction} =
prepareToCleanUpMoneyRequest(transactionID, reportAction, iouReport, chatReport, isChatIOUReportArchived, false);
prepareToCleanUpMoneyRequest(transactionID, reportAction, transactionThreadReport, iouReport, chatReport, isChatIOUReportArchived, false);

const urlToNavigateBack = getNavigationUrlOnMoneyRequestDelete(transactionID, reportAction, iouReport, chatReport, isChatIOUReportArchived, isSingleTransactionView);
// build Onyx data
Expand Down Expand Up @@ -637,6 +645,7 @@ function deleteMoneyRequest({
transactionID,
reportAction,
transactions,
transactionThreadReport,
violations,
iouReport,
chatReport,
Expand Down Expand Up @@ -664,7 +673,17 @@ function deleteMoneyRequest({
transactionViolations,
reportPreviewAction,
iouReportActions,
} = prepareToCleanUpMoneyRequest(transactionID, reportAction, iouReport, chatReport, isChatIOUReportArchived, false, transactionIDsPendingDeletion, selectedTransactionIDs);
} = prepareToCleanUpMoneyRequest(
transactionID,
reportAction,
transactionThreadReport,
iouReport,
chatReport,
isChatIOUReportArchived,
false,
transactionIDsPendingDeletion,
selectedTransactionIDs,
);

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

Expand Down
4 changes: 4 additions & 0 deletions src/libs/actions/IOU/TrackExpense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2762,10 +2762,14 @@ function deleteTrackExpense({

// STEP 1: Get all collections we're updating
if (!isSelfDM(chatReport)) {
const allReports = getAllReports();
const transactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportAction.childReportID}`];

deleteMoneyRequest({
transactionID,
reportAction,
transactions,
transactionThreadReport,
violations,
iouReport,
chatReport: chatIOUReport,
Expand Down
2 changes: 2 additions & 0 deletions src/libs/actions/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -959,11 +959,13 @@ function bulkDeleteReports({
const reportID = selectedTransactions[transactionID].report?.reportID;
const batchTransactionIDsForReport = reportID ? (transactionsByReport[reportID] ?? []) : [];
const chatReport = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${selectedTransactions[transactionID].report?.chatReportID}`];
const transactionThreadReport = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportAction?.childReportID}`];
const reportNameValuePair = allReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${chatReport?.reportID}`];

deleteMoneyRequest({
transactionID,
reportAction,
transactionThreadReport,
transactions,
violations: transactionsViolations,
iouReport: selectedTransactions[transactionID].report,
Expand Down
29 changes: 24 additions & 5 deletions tests/actions/IOUTest/DeleteMoneyRequestTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
let thread: OptimisticChatReport;
const TEST_USER_ACCOUNT_ID = 1;
const TEST_USER_LOGIN = 'test@test.com';
const expectedTransactionThreadParticipants = {
[TEST_USER_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, role: CONST.REPORT.ROLE.ADMIN},
};
let IOU_REPORT_ID: string | undefined;
let IOU_REPORT: OnyxEntry<Report>;
let reportActionID;
Expand Down Expand Up @@ -243,6 +246,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
);
expect(createIOUAction).toBeTruthy();
expect(createIOUAction && getOriginalMessage(createIOUAction)?.IOUReportID).toBe(iouReport?.reportID);
thread = (await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${createIOUAction?.childReportID}`)) as OptimisticChatReport;

// When fetching all transactions from Onyx
let allTransactions: OnyxCollection<Transaction>;
Expand Down Expand Up @@ -277,6 +281,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
violations: {},
iouReport,
chatReport,
transactionThreadReport: thread,
isChatIOUReportArchived: true,
allTransactionViolationsParam: {},
currentUserAccountID: TEST_USER_ACCOUNT_ID,
Expand Down Expand Up @@ -367,6 +372,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
violations: {},
iouReport,
chatReport,
transactionThreadReport: thread,
isChatIOUReportArchived: true,
allTransactionViolationsParam: {},
currentUserAccountID: TEST_USER_ACCOUNT_ID,
Expand Down Expand Up @@ -451,6 +457,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
violations: {},
iouReport,
chatReport,
transactionThreadReport: thread,
allTransactionViolationsParam: {},
currentUserAccountID: TEST_USER_ACCOUNT_ID,
currentUserEmail: TEST_USER_LOGIN,
Expand Down Expand Up @@ -505,7 +512,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
// Given a transaction thread
thread = buildTransactionThread(createIOUAction, iouReport, TEST_USER_ACCOUNT_ID);

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

Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`,
Expand Down Expand Up @@ -568,6 +575,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
violations: {},
iouReport,
chatReport,
transactionThreadReport: thread,
allTransactionViolationsParam: {},
currentUserAccountID: TEST_USER_ACCOUNT_ID,
currentUserEmail: TEST_USER_LOGIN,
Expand Down Expand Up @@ -723,6 +731,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
violations: {},
iouReport,
chatReport,
transactionThreadReport: thread,
allTransactionViolationsParam: {},
currentUserAccountID: TEST_USER_ACCOUNT_ID,
currentUserEmail: TEST_USER_LOGIN,
Expand Down Expand Up @@ -752,7 +761,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
// Given a transaction thread
thread = buildTransactionThread(createIOUAction, iouReport, TEST_USER_ACCOUNT_ID);

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

const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
const userLogins = getLoginsByAccountIDs(participantAccountIDs);
Expand Down Expand Up @@ -844,6 +853,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
violations: {},
iouReport,
chatReport,
transactionThreadReport: thread,
allTransactionViolationsParam: {},
currentUserAccountID: TEST_USER_ACCOUNT_ID,
currentUserEmail: TEST_USER_LOGIN,
Expand Down Expand Up @@ -891,7 +901,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
jest.advanceTimersByTime(10);
thread = buildTransactionThread(createIOUAction, iouReport, TEST_USER_ACCOUNT_ID);

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

Onyx.connect({
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${thread.reportID}`,
Expand Down Expand Up @@ -1038,6 +1048,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
violations: {},
iouReport,
chatReport,
transactionThreadReport: thread,
isChatIOUReportArchived: undefined,
allTransactionViolationsParam: {},
currentUserAccountID: TEST_USER_ACCOUNT_ID,
Expand Down Expand Up @@ -1152,6 +1163,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
violations: {},
iouReport,
chatReport,
transactionThreadReport: thread,
isChatIOUReportArchived: undefined,
allTransactionViolationsParam: {},
currentUserAccountID: TEST_USER_ACCOUNT_ID,
Expand Down Expand Up @@ -1213,7 +1225,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
jest.advanceTimersByTime(10);
thread = buildTransactionThread(createIOUAction, iouReport, TEST_USER_ACCOUNT_ID);

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

jest.advanceTimersByTime(10);
const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
Expand Down Expand Up @@ -1263,6 +1275,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
violations: {},
iouReport,
chatReport,
transactionThreadReport: thread,
isSingleTransactionView: true,
allTransactionViolationsParam: {},
currentUserAccountID: TEST_USER_ACCOUNT_ID,
Expand Down Expand Up @@ -1322,6 +1335,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
violations: {},
iouReport,
chatReport,
transactionThreadReport: thread,
allTransactionViolationsParam: {},
currentUserAccountID: TEST_USER_ACCOUNT_ID,
currentUserEmail: TEST_USER_LOGIN,
Expand Down Expand Up @@ -1393,7 +1407,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
// Given a transaction thread
thread = buildTransactionThread(createIOUAction, iouReport, TEST_USER_ACCOUNT_ID);

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

const participantAccountIDs = Object.keys(thread.participants ?? {}).map(Number);
const userLogins = getLoginsByAccountIDs(participantAccountIDs);
Expand Down Expand Up @@ -1494,6 +1508,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
violations: {},
iouReport,
chatReport,
transactionThreadReport: thread,
allTransactionViolationsParam: {},
currentUserAccountID: TEST_USER_ACCOUNT_ID,
currentUserEmail: TEST_USER_LOGIN,
Expand Down Expand Up @@ -1584,6 +1599,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
violations: {},
iouReport: expenseReport,
chatReport: expenseReport,
transactionThreadReport: undefined,
transactionIDsPendingDeletion: [],
selectedTransactionIDs,
allTransactionViolationsParam: {},
Expand All @@ -1597,6 +1613,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
violations: {},
iouReport: expenseReport,
chatReport: expenseReport,
transactionThreadReport: undefined,
transactionIDsPendingDeletion: [transaction1.transactionID],
selectedTransactionIDs,
allTransactionViolationsParam: {},
Expand Down Expand Up @@ -1676,6 +1693,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
violations: {},
iouReport: expenseReport,
chatReport: expenseReport,
transactionThreadReport: undefined,
allTransactionViolationsParam: transactionViolations,
currentUserAccountID: TEST_USER_ACCOUNT_ID,
currentUserEmail: TEST_USER_LOGIN,
Expand Down Expand Up @@ -1740,6 +1758,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
violations: {},
iouReport: expenseReport,
chatReport: expenseReport,
transactionThreadReport: undefined,
allTransactionViolationsParam: {},
currentUserAccountID: TEST_USER_ACCOUNT_ID,
currentUserEmail: TEST_USER_LOGIN,
Expand Down
Loading