Skip to content

Commit f5fa081

Browse files
committed
Extract getChatReportWithFallback helper
1 parent 5fa135e commit f5fa081

3 files changed

Lines changed: 40 additions & 21 deletions

File tree

src/components/Search/SearchList/ListItem/ActionCell/PayActionCell.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import useThemeStyles from '@hooks/useThemeStyles';
1414

1515
import {payInvoice, payMoneyRequest} from '@libs/actions/IOU/PayMoneyRequest';
1616
import {canIOUBePaid} from '@libs/actions/IOU/ReportWorkflow';
17-
import {getSearchPayOnyxData} from '@libs/actions/Search';
17+
import {getChatReportWithFallback, getSearchPayOnyxData} from '@libs/actions/Search';
1818
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
1919
import Log from '@libs/Log';
2020
import {getReimbursableTotal, isIndividualInvoiceRoom, isInvoiceReport} from '@libs/ReportUtils';
@@ -150,8 +150,8 @@ function PayActionCell({isLoading, policyID, reportID, hash, amount, shouldDisab
150150
// The chat report is only needed for optimistic chat updates, so when it isn't loaded, pay with a fallback
151151
// built from the known IDs and let the server fill in the chat data.
152152
const fallbackChatReportID = iouReport?.chatReportID ?? iouReport?.parentReportID;
153-
const fallbackChatReport = fallbackChatReportID ? {reportID: fallbackChatReportID, policyID: iouReport?.policyID ?? policyID} : undefined;
154-
const chatReportForPayment = chatReport ?? fallbackChatReport;
153+
const fallbackPolicyID = iouReport?.policyID ?? policyID;
154+
const {chatReport: chatReportForPayment, isFallbackChatReport} = getChatReportWithFallback(chatReport, fallbackChatReportID, fallbackPolicyID);
155155
if (!chatReportForPayment) {
156156
Log.info('[SearchPay] Dropping row pay: chat report is not loaded and no chatReportID is available', false, {reportID});
157157
return;
@@ -161,7 +161,7 @@ function PayActionCell({isLoading, policyID, reportID, hash, amount, shouldDisab
161161
getCurrencyDecimals,
162162
paymentType: type,
163163
chatReport: chatReportForPayment,
164-
isFallbackChatReport: !chatReport,
164+
isFallbackChatReport,
165165
iouReport,
166166
introSelected,
167167
currentUserAccountID,

src/hooks/useSearchBulkActions.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
getPolicyFromSearchSnapshot,
2626
getReportFromSearchSnapshot,
2727
getReportType,
28+
getChatReportWithFallback,
2829
getSearchApproveOnyxData,
2930
getSearchPayOnyxData,
3031
getTotalFormattedAmount,
@@ -1400,23 +1401,22 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
14001401
}
14011402

14021403
const isItemInvoice = isInvoiceReport(iouReport);
1403-
let chatReport = getChatReportForBulkPay(iouReport, item.chatReportID, searchData, allReports);
1404-
let isFallbackChatReport = false;
1405-
if (!chatReport) {
1406-
// The chat report is only needed for optimistic chat updates, so when it isn't loaded, pay with a fallback
1407-
// built from the known IDs and let the server fill in the chat data.
1408-
// Invoices are the exception. They genuinely need the invoice room data such as receiver type and pay-as-business.
1409-
const fallbackChatReportID = item.chatReportID ?? iouReport.chatReportID ?? iouReport.parentReportID;
1410-
if (isItemInvoice || !fallbackChatReportID) {
1411-
Log.info('[BulkPay] Skipping report: chat report not found in the search snapshot or Onyx', false, {
1412-
reportID: item.reportID,
1413-
chatReportID: fallbackChatReportID,
1414-
isItemInvoice,
1415-
});
1416-
continue;
1417-
}
1418-
chatReport = {reportID: fallbackChatReportID, policyID: item.policyID ?? iouReport.policyID};
1419-
isFallbackChatReport = true;
1404+
const fallbackChatReportID = item.chatReportID ?? iouReport.chatReportID ?? iouReport.parentReportID;
1405+
const fallbackPolicyID = iouReport.policyID ?? item.policyID;
1406+
const {chatReport, isFallbackChatReport} = getChatReportWithFallback(
1407+
getChatReportForBulkPay(iouReport, item.chatReportID, searchData, allReports),
1408+
fallbackChatReportID,
1409+
fallbackPolicyID,
1410+
);
1411+
// The fallback covers money requests only. Invoices genuinely need the invoice room data such as
1412+
// receiver type and pay-as-business, so skip them when the chat isn't loaded.
1413+
if (!chatReport || (isItemInvoice && isFallbackChatReport)) {
1414+
Log.info('[BulkPay] Skipping report: chat report not found in the search snapshot or Onyx', false, {
1415+
reportID: item.reportID,
1416+
chatReportID: fallbackChatReportID,
1417+
isItemInvoice,
1418+
});
1419+
continue;
14201420
}
14211421

14221422
const rawPaymentMethod = paymentMethod ?? getLastPolicyPaymentMethod(item.policyID, personalPolicyID, lastPaymentMethods, undefined, isIOUReportUtil(item.reportID));

src/libs/actions/Search.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,24 @@ function getChatReportForSearchPay(chatReport: OnyxEntry<Report>, snapshotReport
129129
return chatReport ?? snapshotChatReport ?? (chatReportID ? getReportOrDraftReport(chatReportID) : undefined);
130130
}
131131

132+
/**
133+
* Returns the chat report to pay with. When the chat isn't loaded, builds a fallback from the known IDs so the
134+
* payment isn't blocked; isFallbackChatReport tells payMoneyRequest to skip the optimistic chat updates.
135+
*/
136+
function getChatReportWithFallback(
137+
loadedChatReport: OnyxEntry<Report>,
138+
fallbackChatReportID: string | undefined,
139+
fallbackPolicyID: string | undefined,
140+
): {chatReport: OnyxEntry<Report>; isFallbackChatReport: boolean} {
141+
if (loadedChatReport) {
142+
return {chatReport: loadedChatReport, isFallbackChatReport: false};
143+
}
144+
if (!fallbackChatReportID) {
145+
return {chatReport: undefined, isFallbackChatReport: false};
146+
}
147+
return {chatReport: {reportID: fallbackChatReportID, policyID: fallbackPolicyID}, isFallbackChatReport: true};
148+
}
149+
132150
function getReportFromSearchSnapshot(reportID: string | undefined, searchData: SearchResultDataType | undefined, allReports: OnyxCollection<Report> | undefined): OnyxEntry<Report> {
133151
if (!reportID) {
134152
return undefined;
@@ -2183,6 +2201,7 @@ export {
21832201
setSearchContext,
21842202
deleteSavedSearch,
21852203
getSearchPayOnyxData,
2204+
getChatReportWithFallback,
21862205
getSearchApproveOnyxData,
21872206
handleActionButtonPress,
21882207
submitMoneyRequestOnSearch,

0 commit comments

Comments
 (0)