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
21 changes: 15 additions & 6 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ function openReport(

// This is a legacy transactions that doesn't have either a transaction thread or a money request preview
if (transactionID && !parentReportActionID) {
const transaction = allTransactions?.[transactionID];
const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`];

if (transaction) {
const selfDMReportID = findSelfDMReportID();
Expand All @@ -1109,13 +1109,22 @@ function openReport(
},
});

// Log how often the legacy transaction fallback path is taken
Log.info('[Report] Legacy transaction fallback: creating money request preview in self DM', false, {
Comment thread
ikevin127 marked this conversation as resolved.
Resolved
selfDMReportID,
transactionID,
reportID,
});

optimisticData.push({
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReportID}${generatedReportActionID}`,
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReportID}`,
value: {
...optimisticParentAction,
reportActionID: generatedReportActionID,
childReportID: reportID,
[generatedReportActionID]: {
...optimisticParentAction,
reportActionID: generatedReportActionID,
childReportID: reportID,
},
},
});

Expand Down
69 changes: 69 additions & 0 deletions tests/actions/ReportTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,75 @@ describe('actions/Report', () => {
TestHelper.expectAPICommandToHaveBeenCalled(WRITE_COMMANDS.OPEN_REPORT, 1);
});

it('openReport legacy preview fallback stores action under correct Onyx key and preserves existing actions', async () => {
global.fetch = TestHelper.getGlobalFetchMock();

const TEST_USER_ACCOUNT_ID = 1;
const TEST_USER_LOGIN = 'test@user.com';
const SELF_DM_ID = '555';
const CHILD_REPORT_ID = '9999';
const TXN_ID = 'txn_123';

// Sign in and set personal details
await TestHelper.signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN);
await TestHelper.setPersonalDetails(TEST_USER_LOGIN, TEST_USER_ACCOUNT_ID);

// Seed a self-DM report
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${SELF_DM_ID}`, {
reportID: SELF_DM_ID,
chatType: CONST.REPORT.CHAT_TYPE.SELF_DM,
} as OnyxTypes.Report);

// Seed an existing action in self DM to ensure MERGE does not overwrite
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${SELF_DM_ID}`, {
123: {
actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT,
message: [{type: 'COMMENT', html: 'Existing', text: 'Existing'}],
reportActionID: '123',
created: DateUtils.getDBTime(),
},
} as unknown as OnyxTypes.ReportActions);

// Seed a legacy transaction (no parentReportActionID)
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${TXN_ID}`, {
transactionID: TXN_ID,
amount: -101,
currency: CONST.CURRENCY.USD,
comment: {comment: 'Legacy expense'},
} as unknown as OnyxTypes.Transaction);

// Call openReport with transactionID to trigger the legacy preview flow
Report.openReport(CHILD_REPORT_ID, undefined, [], undefined, undefined, false, [], undefined, TXN_ID);
await waitForBatchedUpdates();

// Validate the correct Onyx key received the new action and existing one is preserved
const selfDMActions = (await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${SELF_DM_ID}` as const)) as OnyxTypes.ReportActions | undefined;
expect(selfDMActions).toBeTruthy();
// Existing action still present
expect(selfDMActions?.['123']).toBeTruthy();

// Derive the created parentReportActionID from the self DM actions by finding the one that links to the child report
const entries = Object.entries(selfDMActions ?? {}) as Array<[string, OnyxTypes.ReportAction]>;
const createdEntry = entries.find((tuple): tuple is [string, OnyxTypes.ReportAction] => tuple?.[1]?.childReportID === CHILD_REPORT_ID);
expect(createdEntry).toBeDefined();
// Type guard for TS; the expect above will fail the test if undefined
if (!createdEntry) {
return;
}
const [parentReportActionID, createdAction] = createdEntry;
expect(createdAction.childReportID).toBe(CHILD_REPORT_ID);

// Ensure we did not create a stray concatenated key like reportActions_<selfDMReportID><generatedActionID>
const wrongKeyValue = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${SELF_DM_ID}${parentReportActionID}` as const);
expect(wrongKeyValue).toBeUndefined();

// The API call should include moneyRequestPreviewReportActionID matching the created action
TestHelper.expectAPICommandToHaveBeenCalledWith(WRITE_COMMANDS.OPEN_REPORT, 0, {
reportID: CHILD_REPORT_ID,
moneyRequestPreviewReportActionID: parentReportActionID,
});
});

it('should replace duplicate OpenReport commands with the same reportID', async () => {
global.fetch = TestHelper.getGlobalFetchMock();

Expand Down