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
21 changes: 21 additions & 0 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2847,6 +2847,26 @@ function isOneOnOneChat(report: OnyxEntry<Report>, currentUserAccountID?: number
);
}

/**
* Returns the other participant of a cached 1:1 DM as OpenReport participant info, so the server can
* resolve a stale/optimistic reportID to the real chat (returned as preexistingReportID) instead of
* failing with "Report not found". Returns an empty list for any report that is not a 1:1 DM.
*/
function getOneOnOneChatParticipants(
report: OnyxEntry<Report>,
personalDetails: OnyxEntry<PersonalDetailsList>,
currentUserAccountID: number | undefined,
): Array<{login: string; accountID: number}> {
if (!currentUserAccountID || !isOneOnOneChat(report, currentUserAccountID)) {
return [];
}
return Object.keys(report?.participants ?? {})
.map(Number)
.filter((accountID) => accountID !== currentUserAccountID)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to exclude currentUserAccountID?

@elirangoshen elirangoshen Aug 19, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes because emailList/accountIDList containt "the other side of the chat," not "everyone in it." so we need to exclude the user and only include the other participants.

.map((accountID) => ({login: personalDetails?.[accountID]?.login ?? '', accountID}))
.filter((participant) => !!participant.login);
}

/**
* Checks if the current user is a payer of the expense
*/
Expand Down Expand Up @@ -14247,6 +14267,7 @@ export {
getNonHeldAndFullAmount,
getReimbursableTotal,
getUnheldReimbursableTotal,
getOneOnOneChatParticipants,
getOptimisticDataForAncestors,
getOriginalReportID,
getOutstandingChildRequest,
Expand Down
6 changes: 5 additions & 1 deletion src/pages/inbox/ReportFetchHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {CancelHandle} from '@libs/Navigation/TransitionTracker';
import {isSupportedInviteOnboardingChoice, isSupportedPendingInviteOnboarding} from '@libs/OnboardingUtils';
import {getFilteredReportActionsForReportView, getIOUActionForReportID, getOneTransactionThreadReportID, isCreatedAction} from '@libs/ReportActionsUtils';
import {
getOneOnOneChatParticipants,
isChatThread,
isHiddenForCurrentUser,
isMoneyRequestReport,
Expand Down Expand Up @@ -199,7 +200,10 @@ function ReportFetchHandler() {
return;
}

openReport({reportID: reportIDFromRoute, introSelected, reportActionID: reportActionIDFromRoute, betas, hasReportActions, currentUserAccountID});
// For a cached 1:1 DM, pass the other participant so the server can resolve a stale/optimistic
// reportID to the real chat (via preexistingReportID) instead of failing with "Report not found".
const dmParticipants = getOneOnOneChatParticipants(report, personalDetails, currentUserAccountID);
openReport({reportID: reportIDFromRoute, introSelected, reportActionID: reportActionIDFromRoute, participants: dmParticipants, betas, hasReportActions, currentUserAccountID});
});

const createOneTransactionThread = useEffectEvent(() => {
Expand Down
23 changes: 23 additions & 0 deletions tests/actions/ReportTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5217,6 +5217,29 @@ describe('actions/Report', () => {
});
});

describe('openReport with participants', () => {
it('should send passed participants as emailList/accountIDList so the server can resolve a stale optimistic reportID', async () => {
global.fetch = TestHelper.createGlobalFetchMock();
const REPORT_ID = 'dm1';

Report.openReport({
reportID: REPORT_ID,
introSelected: undefined,
betas: undefined,
hasReportActions: true,
currentUserAccountID: 1,
participants: [{login: 'other@test.com', accountID: 2}],
});
await waitForBatchedUpdates();

TestHelper.expectAPICommandToHaveBeenCalledWith(WRITE_COMMANDS.OPEN_REPORT, 0, {
reportID: REPORT_ID,
emailList: 'other@test.com',
accountIDList: '2',
});
});
});

describe('setOptimisticTransactionThread', () => {
it('should set optimistic transaction thread data with the provided parameters', async () => {
const reportID = 'report12';
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/ReportUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ import {
getMovedTransactionMessage,
getNextApproverAccountID,
getNonHeldAndFullAmount,
getOneOnOneChatParticipants,
getOriginalReportID,
getOutstandingChildRequest,
getParentNavigationSubtitle,
Expand Down Expand Up @@ -11599,6 +11600,39 @@ describe('ReportUtils', () => {
});
});

describe('getOneOnOneChatParticipants', () => {
const OTHER_ACCOUNT_ID = 222;
const personalDetailsList: PersonalDetailsList = {
[OTHER_ACCOUNT_ID]: {accountID: OTHER_ACCOUNT_ID, login: 'other@test.com'},
};
const dmReport: Report = {
...createRandomReport(0, undefined),
type: CONST.REPORT.TYPE.CHAT,
policyID: CONST.POLICY.ID_FAKE,
participants: buildParticipantsFromAccountIDs([currentUserAccountID, OTHER_ACCOUNT_ID]),
};

it('should return the other participant of a 1:1 DM with their login and accountID', () => {
expect(getOneOnOneChatParticipants(dmReport, personalDetailsList, currentUserAccountID)).toEqual([{login: 'other@test.com', accountID: OTHER_ACCOUNT_ID}]);
});

it('should return an empty list for reports that are not 1:1 DMs', () => {
const roomReport: Report = {
...createRandomReport(1, CONST.REPORT.CHAT_TYPE.POLICY_ADMINS),
participants: buildParticipantsFromAccountIDs([currentUserAccountID, OTHER_ACCOUNT_ID]),
};
expect(getOneOnOneChatParticipants(roomReport, personalDetailsList, currentUserAccountID)).toEqual([]);
});

it('should return an empty list when the other participant has no known login', () => {
expect(getOneOnOneChatParticipants(dmReport, {}, currentUserAccountID)).toEqual([]);
});

it('should return an empty list when currentUserAccountID is missing', () => {
expect(getOneOnOneChatParticipants(dmReport, personalDetailsList, undefined)).toEqual([]);
});
});

describe('getReportNotificationPreference', () => {
it('should read the notification preference of the passed currentUserAccountID', () => {
const report: Report = {
Expand Down
Loading