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
5 changes: 3 additions & 2 deletions src/libs/actions/IOU/DeleteMoneyRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ function getCleanUpTransactionThreadReportOnyxData({
updatedReportPreviewAction,
shouldAddUpdatedReportPreviewActionToOnyxData = true,
currentUserAccountID,
transactionThreadReportActionsParam,
}: {
transactionThreadID?: string;
shouldDeleteTransactionThread: boolean;
Expand All @@ -627,9 +628,9 @@ function getCleanUpTransactionThreadReportOnyxData({
updatedReportPreviewAction?: ReportAction;
shouldAddUpdatedReportPreviewActionToOnyxData?: boolean;
currentUserAccountID: number;
transactionThreadReportActionsParam?: OnyxEntry<OnyxTypes.ReportActions>;
}) {
const allReports = getAllReports();
const allReportActions = getAllReportActionsFromIOU();
const allReportNameValuePairs = getAllReportNameValuePairs();

const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS>> = [];
Expand All @@ -641,7 +642,7 @@ function getCleanUpTransactionThreadReportOnyxData({
let transactionThreadReportActions = null;
if (transactionThreadID) {
transactionThread = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadID}`] ?? null;
transactionThreadReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadID}`] ?? null;
transactionThreadReportActions = transactionThreadReportActionsParam ?? getAllReportActionsFromIOU()?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadID}`] ?? null;
Comment thread
cretadn22 marked this conversation as resolved.
Resolved
}

optimisticData.push(
Expand Down
1 change: 1 addition & 0 deletions src/libs/actions/IOU/Duplicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ function mergeDuplicates({
updatedReportPreviewAction,
shouldAddUpdatedReportPreviewActionToOnyxData: index === actions.length - 1,
currentUserAccountID,
transactionThreadReportActionsParam: allReportActionsList?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadID}`],
});
cleanUpTransactionThreadReportsOptimisticData.push(...cleanUp.optimisticData);
cleanUpTransactionThreadReportsSuccessData.push(...cleanUp.successData);
Expand Down
1 change: 1 addition & 0 deletions src/libs/actions/IOU/SplitTransactionUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,7 @@ function updateSplitTransactions({
reportAction: iouActionToCleanUp,
updatedReportPreviewAction: updatedReportPreviewAction as OnyxTypes.ReportAction,
currentUserAccountID: currentUserPersonalDetails.accountID,
transactionThreadReportActionsParam: allReportActionsList?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouActionToCleanUp.childReportID}`],
});

onyxData.optimisticData?.push(...optimisticData);
Expand Down
24 changes: 20 additions & 4 deletions src/libs/actions/MergeTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ import CONST from '@src/CONST';
import {getReportOwnerAccountIDAsAttendee, getReportOwnerAsAttendee, isDistanceRequest, isTransactionPendingDelete} from '@src/libs/TransactionUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import {DYNAMIC_ROUTES} from '@src/ROUTES';
import type {CardList, MergeTransaction, PersonalDetailsList, Policy, PolicyCategories, PolicyTagLists, Report, ReportActions, Transaction, TransactionViolations} from '@src/types/onyx';
import type {
CardList,
MergeTransaction,
PersonalDetailsList,
Policy,
PolicyCategories,
PolicyTagLists,
Report,
ReportAction,
ReportActions,
Transaction,
TransactionViolations,
} from '@src/types/onyx';
import type {Attendee} from '@src/types/onyx/IOU';

import type {OnyxCollection, OnyxEntry, OnyxMergeInput, OnyxUpdate} from 'react-native-onyx';
Expand Down Expand Up @@ -396,6 +408,8 @@ type MergeTransactionRequestParams = {
selfDMReportActions: OnyxEntry<ReportActions>;
reportPolicyTags: OnyxEntry<PolicyTagLists>;
isTrackIntentUser: boolean | undefined;
sourceTransactionThreadReportActions: OnyxEntry<ReportActions>;
sourceIOUAction: OnyxEntry<ReportAction>;
getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals'];
};
/**
Expand Down Expand Up @@ -427,6 +441,8 @@ function mergeTransactionRequest({
selfDMReportActions,
reportPolicyTags,
isTrackIntentUser,
sourceTransactionThreadReportActions,
sourceIOUAction,
getCurrencyDecimals,
}: MergeTransactionRequestParams) {
// For both unreported expenses and expense reports, negate the display amount when storing
Expand Down Expand Up @@ -590,14 +606,14 @@ function mergeTransactionRequest({

// Only clear the transaction thread report if we select the target report for merging
if (mergeTransaction.reportID === targetTransaction.reportID) {
const sourceIouAction = getIOUActionForReportID(sourceTransaction.reportID, sourceTransaction.transactionID);
const sourceTransactionThreadReportID = sourceIouAction?.childReportID;
const sourceTransactionThreadReportID = sourceIOUAction?.childReportID;
const shouldDeleteTransactionThread = !!sourceTransactionThreadReportID;
const cleanUpSourceTransactionThreadReportOnyxData = getCleanUpTransactionThreadReportOnyxData({
transactionThreadID: sourceTransactionThreadReportID,
shouldDeleteTransactionThread,
reportAction: sourceIouAction,
reportAction: sourceIOUAction,
currentUserAccountID: currentUserAccountIDParam,
transactionThreadReportActionsParam: sourceTransactionThreadReportActions,
});
optimisticSourceReportActionData.push(...cleanUpSourceTransactionThreadReportOnyxData.optimisticData);
successSourceReportActionData.push(...cleanUpSourceTransactionThreadReportOnyxData.successData);
Expand Down
7 changes: 7 additions & 0 deletions src/pages/TransactionMerge/DynamicConfirmationPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import isSearchTopmostFullScreenRoute from '@libs/Navigation/helpers/isSearchTop
import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import type {MergeTransactionNavigatorParamList} from '@libs/Navigation/types';
import {getIOUActionForTransactionID} from '@libs/ReportActionsUtils';
import {findSelfDMReportID, getReportTransactions} from '@libs/ReportUtils';

import CONST from '@src/CONST';
Expand Down Expand Up @@ -78,6 +79,10 @@ function DynamicConfirmationPage({route}: DynamicConfirmationPageProps) {
const selfDMReport = useSelfDMReport();
const [selfDMReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(selfDMReport?.reportID)}`);

const [sourceReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(sourceTransaction?.reportID)}`);
const sourceIOUAction = sourceTransaction ? getIOUActionForTransactionID(Object.values(sourceReportActions ?? {}), sourceTransaction.transactionID) : undefined;
const [sourceTransactionThreadReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(sourceIOUAction?.childReportID)}`);

// Build the merged transaction data for display
const mergedTransactionData = buildMergedTransactionData(targetTransaction, mergeTransaction);

Expand Down Expand Up @@ -116,6 +121,8 @@ function DynamicConfirmationPage({route}: DynamicConfirmationPageProps) {
selfDMReport,
selfDMReportActions,
reportPolicyTags,
sourceTransactionThreadReportActions,
sourceIOUAction,
});

const reportIDToDismiss = reportID !== CONST.REPORT.UNREPORTED_REPORT_ID ? reportID : undefined;
Expand Down
46 changes: 45 additions & 1 deletion tests/actions/IOUTest/DeleteMoneyRequestTest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {deleteMoneyRequest} from '@libs/actions/IOU/DeleteMoneyRequest';
import {deleteMoneyRequest, getCleanUpTransactionThreadReportOnyxData} from '@libs/actions/IOU/DeleteMoneyRequest';
import {getReportPreviewAction} from '@libs/actions/IOU/MoneyRequestBuilder';
import {requestMoney} from '@libs/actions/IOU/TrackExpense';
import {updateMoneyRequestAmountAndCurrency} from '@libs/actions/IOU/UpdateMoneyRequest';
Expand Down Expand Up @@ -2197,4 +2197,48 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
mockFetch?.resume?.();
});
});

describe('getCleanUpTransactionThreadReportOnyxData', () => {
it('uses transactionThreadReportActionsParam for the failureData rollback when provided', () => {
const transactionThreadID = 'thread-1';
const passedInReportActions: ReportActions = {action1: createRandomReportAction(1)};

const result = getCleanUpTransactionThreadReportOnyxData({
transactionThreadID,
shouldDeleteTransactionThread: true,
currentUserAccountID: RORY_ACCOUNT_ID,
transactionThreadReportActionsParam: passedInReportActions,
});

const failureReportActionsEntry = result.failureData.find((entry) => entry.key === `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadID}`);
expect(failureReportActionsEntry?.value).toEqual(passedInReportActions);
});

it('falls back to the cached report actions when transactionThreadReportActionsParam is not provided', async () => {
const transactionThreadID = 'thread-2';
const cachedReportActions: ReportActions = {action1: createRandomReportAction(2)};
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadID}`, cachedReportActions);
await waitForBatchedUpdates();

const result = getCleanUpTransactionThreadReportOnyxData({
transactionThreadID,
shouldDeleteTransactionThread: true,
currentUserAccountID: RORY_ACCOUNT_ID,
});

const failureReportActionsEntry = result.failureData.find((entry) => entry.key === `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadID}`);
expect(failureReportActionsEntry?.value).toEqual(cachedReportActions);
});

it('does not push transaction-thread report/report-action data when shouldDeleteTransactionThread is false', () => {
const result = getCleanUpTransactionThreadReportOnyxData({
shouldDeleteTransactionThread: false,
currentUserAccountID: RORY_ACCOUNT_ID,
transactionThreadReportActionsParam: {action1: createRandomReportAction(3)},
});

expect(result.optimisticData.some((entry) => entry.key.startsWith(ONYXKEYS.COLLECTION.REPORT_ACTIONS))).toBe(false);
expect(result.failureData.some((entry) => entry.key.startsWith(ONYXKEYS.COLLECTION.REPORT_ACTIONS))).toBe(false);
});
});
});
76 changes: 76 additions & 0 deletions tests/actions/IOUTest/DuplicateTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,82 @@ describe('actions/Duplicate', () => {
);
});

it('threads allReportActionsList into the transaction-thread cleanup instead of relying on the deprecated report-actions cache', async () => {
// Given: A duplicate whose transaction thread already has report actions cached in Onyx
const reportID = 'report456';
const mainTransactionID = 'main456';
const duplicate1ID = 'dup999';
const childReportID = 'child456';

const mainTransaction = createMockTransaction(mainTransactionID, reportID);
const duplicateTransaction1 = createMockTransaction(duplicate1ID, reportID, 100);
const expenseReport = createMockReport(reportID, 200);
const mainViolations = createMockViolations();
const duplicate1Violations = createMockViolations();

const iouAction1 = createMockIouAction(duplicate1ID, 'action789', childReportID);

// The value already cached via the deprecated Onyx.connect-backed report actions accessor —
// deliberately different from what's passed via allReportActionsList below.
const cachedChildReportActions = {staleComment: createMock<ReportAction>({reportActionID: 'staleComment', actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT})};

await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${mainTransactionID}`, mainTransaction);
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${duplicate1ID}`, duplicateTransaction1);
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, expenseReport);
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${mainTransactionID}`, mainViolations);
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${duplicate1ID}`, duplicate1Violations);
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, {action789: iouAction1});
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${childReportID}`, cachedChildReportActions);
await waitForBatchedUpdates();

// The value explicitly passed in for the child report's actions, distinct from the cached one above.
const passedInChildReportActions = {freshComment: createMock<ReportAction>({reportActionID: 'freshComment', actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT})};

// When: Call mergeDuplicates, passing allReportActionsList with a DIFFERENT value for the child report
mergeDuplicates({
transactionID: mainTransactionID,
transactionIDList: [duplicate1ID],
created: '2024-01-01 12:00:00',
merchant: 'Updated Merchant',
amount: 200,
currency: CONST.CURRENCY.EUR,
category: 'Travel',
comment: 'Updated comment',
billable: true,
reimbursable: false,
tag: 'UpdatedProject',
taxCode: '',
receiptID: 123,
reportID,
currentUserLogin: RORY_EMAIL,
currentUserAccountID: RORY_ACCOUNT_ID,
allTransactionViolations: {
[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${mainTransactionID}`]: mainViolations,
[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${duplicate1ID}`]: duplicate1Violations,
},
allReportActionsList: {
[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`]: {action789: iouAction1},
[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${childReportID}`]: passedInChildReportActions,
},
});
await waitForBatchedUpdates();

// Then: The rollback data for the transaction thread restores the PASSED-IN actions, not the stale
// cached ones — proving getCleanUpTransactionThreadReportOnyxData used the explicit allReportActionsList slice.
expect(writeSpy).toHaveBeenCalledWith(
WRITE_COMMANDS.MERGE_DUPLICATES,
expect.anything(),
expect.objectContaining({
failureData: expect.arrayContaining([
expect.objectContaining({
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${childReportID}`,
value: passedInChildReportActions,
}),
]),
}),
);
});

it('should handle empty duplicate transaction list', async () => {
// Given: Set up test data with only main transaction
const reportID = 'report123';
Expand Down
22 changes: 21 additions & 1 deletion tests/actions/MergeTransactionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type CrossReportMergeToSourceReportFixtures = {
mergeTransactionID: string;
sourceExpenseReport: Report;
targetReport: Report;
sourceIOUAction: ReportAction;
sourceIOUActionID: string;
targetIOUActionID: string;
targetTransactionThreadID: string;
Expand Down Expand Up @@ -175,6 +176,7 @@ async function setupCrossReportMergeToSourceReportFixtures(): Promise<CrossRepor
mergeTransactionID,
sourceExpenseReport,
targetReport,
sourceIOUAction,
sourceIOUActionID,
targetIOUActionID,
targetTransactionThreadID,
Expand All @@ -184,7 +186,7 @@ async function setupCrossReportMergeToSourceReportFixtures(): Promise<CrossRepor
}

function runCrossReportMergeToSourceReportRequest(fixtures: CrossReportMergeToSourceReportFixtures) {
const {mergeTransactionID, mergeTransaction, targetTransaction, sourceTransaction, mockViolations, targetReport} = fixtures;
const {mergeTransactionID, mergeTransaction, targetTransaction, sourceTransaction, mockViolations, targetReport, sourceIOUAction} = fixtures;

mergeTransactionRequest({
iouReportOwnerLogin: undefined,
Expand All @@ -206,6 +208,8 @@ function runCrossReportMergeToSourceReportRequest(fixtures: CrossReportMergeToSo
selfDMReportActions: undefined,
delegateAccountID: undefined,
isTrackIntentUser: false,
sourceTransactionThreadReportActions: undefined,
sourceIOUAction,
getCurrencyDecimals: getCurrencyDecimalsLocal,
});
}
Expand Down Expand Up @@ -324,6 +328,8 @@ describe('mergeTransactionRequest', () => {
selfDMReport: undefined,
selfDMReportActions: undefined,
isTrackIntentUser: false,
sourceTransactionThreadReportActions: undefined,
sourceIOUAction: undefined,
getCurrencyDecimals: getCurrencyDecimalsLocal,
});

Expand Down Expand Up @@ -443,6 +449,8 @@ describe('mergeTransactionRequest', () => {
selfDMReport: undefined,
selfDMReportActions: undefined,
isTrackIntentUser: false,
sourceTransactionThreadReportActions: undefined,
sourceIOUAction: undefined,
getCurrencyDecimals: getCurrencyDecimalsLocal,
});

Expand Down Expand Up @@ -547,6 +555,8 @@ describe('mergeTransactionRequest', () => {
selfDMReport: undefined,
selfDMReportActions: undefined,
isTrackIntentUser: false,
sourceTransactionThreadReportActions: undefined,
sourceIOUAction: undefined,
getCurrencyDecimals: getCurrencyDecimalsLocal,
});

Expand Down Expand Up @@ -713,6 +723,8 @@ describe('mergeTransactionRequest', () => {
selfDMReport: undefined,
selfDMReportActions: undefined,
isTrackIntentUser: false,
sourceTransactionThreadReportActions: undefined,
sourceIOUAction: undefined,
getCurrencyDecimals: getCurrencyDecimalsLocal,
});

Expand Down Expand Up @@ -820,6 +832,8 @@ describe('mergeTransactionRequest', () => {
selfDMReport: undefined,
selfDMReportActions: undefined,
isTrackIntentUser: false,
sourceTransactionThreadReportActions: undefined,
sourceIOUAction: undefined,
getCurrencyDecimals: getCurrencyDecimalsLocal,
});

Expand Down Expand Up @@ -1052,6 +1066,8 @@ describe('mergeTransactionRequest', () => {
selfDMReport: undefined,
selfDMReportActions: undefined,
isTrackIntentUser: false,
sourceTransactionThreadReportActions: undefined,
sourceIOUAction: undefined,
getCurrencyDecimals: getCurrencyDecimalsLocal,
});

Expand Down Expand Up @@ -1255,6 +1271,8 @@ describe('mergeTransactionRequest', () => {
selfDMReport: undefined,
selfDMReportActions: undefined,
isTrackIntentUser: false,
sourceTransactionThreadReportActions: undefined,
sourceIOUAction,
getCurrencyDecimals: getCurrencyDecimalsLocal,
});

Expand Down Expand Up @@ -1407,6 +1425,8 @@ describe('mergeTransactionRequest', () => {
selfDMReport,
selfDMReportActions: undefined,
isTrackIntentUser: false,
sourceTransactionThreadReportActions: undefined,
sourceIOUAction: undefined,
getCurrencyDecimals: getCurrencyDecimalsLocal,
});

Expand Down
Loading