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
26 changes: 15 additions & 11 deletions src/hooks/useSearchBulkActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
getPayMoneyOnSearchInvoiceParams,
getPayOption,
getPolicyFromSearchSnapshot,
getReportActionsFromSearchSnapshot,
getReportFromSearchSnapshot,
getReportType,
getChatReportWithFallback,
Expand Down Expand Up @@ -1213,15 +1214,16 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
return;
}
const validTransactions = Object.fromEntries(Object.entries(allTransactions ?? {}).filter((entry): entry is [string, Transaction] => entry[1] !== undefined));
const searchData = searchResults?.data;
if (isExpenseReportType) {
for (const reportID of selectedReportIDs) {
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
const report = getReportFromSearchSnapshot(reportID, searchData, allReports);
Comment thread
VickyStash marked this conversation as resolved.
Resolved
const reportActions = getReportActionsFromSearchSnapshot(reportID, searchData, allReportActions);
const parentReportActions = getReportActionsFromSearchSnapshot(report?.parentReportID, searchData, allReportActions);
deleteAppReport({
report,
reportActions: allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`],
parentReportAction: report?.parentReportActionID
? allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.parentReportID}`]?.[report?.parentReportActionID]
: undefined,
reportActions,
parentReportAction: report?.parentReportActionID ? parentReportActions?.[report.parentReportActionID] : undefined,
selfDMReport,
currentUserEmailParam: email ?? '',
currentUserAccountIDParam: accountID,
Expand Down Expand Up @@ -1265,20 +1267,21 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {

// Whole-report deletions keep their existing path.
for (const reportID of wholeReportIDs) {
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
const report = getReportFromSearchSnapshot(reportID, searchData, allReports);
Comment thread
VickyStash marked this conversation as resolved.
Resolved
const reportActions = getReportActionsFromSearchSnapshot(reportID, searchData, allReportActions);
const parentReportActions = getReportActionsFromSearchSnapshot(report?.parentReportID, searchData, allReportActions);
deleteAppReport({
report,
reportActions: allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`],
parentReportAction: report?.parentReportActionID
? allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report?.parentReportID}`]?.[report?.parentReportActionID]
: undefined,
reportActions,
parentReportAction: report?.parentReportActionID ? parentReportActions?.[report.parentReportActionID] : undefined,
selfDMReport,
currentUserEmailParam: email ?? '',
currentUserAccountIDParam: accountID,
reportTransactions: validTransactions,
allTransactionViolations: transactionsViolations,
bankAccountList,
delegateAccountID,
hash,
});
}
}
Expand All @@ -1297,6 +1300,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
bankAccountList,
clearSelectedTransactions,
allReports,
searchResults?.data,
selfDMReport,
email,
isExpenseReportType,
Expand Down Expand Up @@ -1647,7 +1651,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
if (!iouReport?.chatReportID) {
return false;
}
const chatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${iouReport.chatReportID}`];
const chatReport = getReportFromSearchSnapshot(iouReport.chatReportID, currentSearchResults?.data, allReports);
return isDM(chatReport);
})
);
Expand Down
19 changes: 19 additions & 0 deletions src/libs/actions/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,21 @@ function getReportFromSearchSnapshot(reportID: string | undefined, searchData: S
return snapshotReport ?? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
}

function getReportActionsFromSearchSnapshot(
reportID: string | undefined,
searchData: SearchResultDataType | undefined,
allReportActions: OnyxCollection<ReportActions>,
): OnyxEntry<ReportActions> {
if (!reportID) {
return undefined;
}

const key = `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}` as const;

// Prefer the search snapshot so the actions match what the user selected on the search page, fall back to live report actions.
return searchData?.[key] ?? allReportActions?.[key];
}

function getPolicyFromSearchSnapshot(policyID: string | undefined, searchData: SearchResultDataType | undefined, policies: OnyxCollection<Policy> | undefined): OnyxEntry<Policy> {
if (!policyID) {
return undefined;
Expand Down Expand Up @@ -1732,6 +1747,9 @@ function rejectMoneyRequestsOnSearch(
let urlToNavigateBack;
for (const [reportID, selectedTransactionIDs] of Object.entries(transactionsByReport)) {
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
if (!report) {
Log.info('[BulkReject] Report is missing from live Onyx', false, {reportID});
}
const totalReportTransactions = report?.transactionCount ?? 0;

// Subtract pending deletes to get accurate count when transactions are deleted offline
Expand Down Expand Up @@ -2403,6 +2421,7 @@ export {
openSearchCategoryFiltersPage,
getPolicyFromSearchSnapshot,
getReportFromSearchSnapshot,
getReportActionsFromSearchSnapshot,
resolveSearchPayPaymentMethod,
};
export type {TransactionPreviewData};
73 changes: 73 additions & 0 deletions tests/unit/hooks/useSearchBulkActionsDeleteTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type {SearchQueryJSON, SelectedReports, SelectedTransactions} from '@comp
import useSearchBulkActions from '@hooks/useSearchBulkActions';

import {deleteMoneyRequest} from '@libs/actions/IOU/DeleteMoneyRequest';
import {deleteAppReport} from '@libs/actions/Report';
import type * as SearchActions from '@libs/actions/Search';

import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -44,6 +46,8 @@ jest.mock('@libs/actions/Report', () => ({
}));

jest.mock('@libs/actions/Search', () => ({
getReportFromSearchSnapshot: jest.requireActual<typeof SearchActions>('@libs/actions/Search').getReportFromSearchSnapshot,
getReportActionsFromSearchSnapshot: jest.requireActual<typeof SearchActions>('@libs/actions/Search').getReportActionsFromSearchSnapshot,
getExportTemplates: jest.fn(() => ({customTemplates: [], defaultTemplates: []})),
exportSearchItemsToCSV: jest.fn(),
queueExportSearchItemsToCSV: jest.fn(),
Expand Down Expand Up @@ -463,4 +467,73 @@ describe('useSearchBulkActions - delete unreported expenses', () => {
// No deleteMoneyRequest call — no action was found.
expect(deleteMoneyRequest).not.toHaveBeenCalled();
});

it('passes the report and reportActions from the search snapshot to deleteAppReport when the report is not in Onyx', async () => {
const REPORT_ID = 'report-1';
const REPORT_ACTION_ID = 'report-action-1';

// Given: a whole-report row selected for bulk delete, where the report and its
// report actions exist in the search snapshot but not in the Onyx.
const snapshotAction = createMock<ReportAction>({
reportActionID: REPORT_ACTION_ID,
actionName: CONST.REPORT.ACTIONS.TYPE.CREATED,
actorAccountID: CURRENT_USER_ACCOUNT_ID,
created: '2026-01-01 10:00:00',
person: [],
shouldShow: true,
});
const searchResults = createMock<SearchResults>({
search: {
type: CONST.SEARCH.DATA_TYPES.EXPENSE,
offset: 0,
hasMoreResults: false,
hasResults: true,
isLoading: false,
count: 1,
total: 100,
currency: 'USD',
},
data: {},
});
const searchData: SearchResultDataType = searchResults.data;
searchData[`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`] = {
reportID: REPORT_ID,
reportName: 'Report',
type: CONST.REPORT.TYPE.EXPENSE,
};
searchData[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`] = {[REPORT_ACTION_ID]: snapshotAction};
mockCurrentSearchResults = searchResults;
mockSelectedTransactions = {
[REPORT_ID]: makeSelectedTransaction({reportID: REPORT_ID}),
};
mockShouldShowDeleteOption = true;
mockShowConfirmModal.mockResolvedValue({action: 'CONFIRM'});

const {result} = renderHookWithProvider(() => useSearchBulkActions({queryJSON: baseQueryJSON}));

await waitFor(() => {
expect(result.current.headerButtonsOptions.find((o) => o.value === CONST.SEARCH.BULK_ACTION_TYPES.DELETE)).toBeDefined();
});

// When: the user confirms bulk delete.
await act(async () => {
result.current.headerButtonsOptions.find((o) => o.value === CONST.SEARCH.BULK_ACTION_TYPES.DELETE)?.onSelected?.();
await Promise.resolve();
await Promise.resolve();
});

// Then: deleteAppReport still receives the report and reportActions resolved
// from the snapshot (snapshot-first fallback), instead of undefined.
await waitFor(() => {
expect(deleteAppReport).toHaveBeenCalledTimes(1);
expect(deleteAppReport).toHaveBeenCalledWith(
expect.objectContaining({
report: expect.objectContaining({reportID: REPORT_ID}),
reportActions: expect.objectContaining({
[REPORT_ACTION_ID]: expect.objectContaining({reportActionID: REPORT_ACTION_ID}),
}),
}),
);
});
});
});
Loading