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
54 changes: 49 additions & 5 deletions src/hooks/useSearchBulkActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function getRestrictedPolicyID(

type ShouldShowBulkDuplicateParams = {
selectedTransactionsKeys: string[];
selectedTransactions: Record<string, {reportID?: string}>;
selectedTransactions: Record<string, {reportID?: string; transaction?: Transaction}>;
allTransactions: OnyxCollection<Transaction> | undefined;
allReports: OnyxCollection<Report> | undefined;
allTransactionViolations: OnyxCollection<TransactionViolations> | undefined;
Expand All @@ -131,6 +131,38 @@ type ShouldShowBulkDuplicateParams = {
searchData: Record<string, unknown> | undefined;
};

function getAllTransactionsForDuplicate({
selectedTransactionsKeys,
selectedTransactions,
allTransactions,
}: Pick<ShouldShowBulkDuplicateParams, 'selectedTransactionsKeys' | 'selectedTransactions' | 'allTransactions'>): NonNullable<OnyxCollection<Transaction>> {
let missingSelectedTransactions: NonNullable<OnyxCollection<Transaction>> | undefined;

for (const id of selectedTransactionsKeys) {
const transactionKey = `${ONYXKEYS.COLLECTION.TRANSACTION}${id}`;
if (allTransactions?.[transactionKey]) {
continue;
}

const transaction = selectedTransactions[id]?.transaction;
if (!transaction) {
continue;
}

missingSelectedTransactions = missingSelectedTransactions ?? {};
missingSelectedTransactions[transactionKey] = transaction;
}

if (!missingSelectedTransactions) {
return allTransactions ?? {};
}

return {
...(allTransactions ?? {}),
...missingSelectedTransactions,
};
}

/**
* Determines whether the bulk duplicate option should be shown for the selected transactions.
* Mirrors the single-duplicate guards from MoneyReportHeader/MoneyRequestHeader.
Expand Down Expand Up @@ -158,8 +190,10 @@ function shouldShowBulkDuplicateOption({
.filter((report): report is Report => report != null && 'reportID' in report)
: [];

const allTransactionsForDuplicate = getAllTransactionsForDuplicate({selectedTransactionsKeys, selectedTransactions, allTransactions});

return selectedTransactionsKeys.every((id) => {
const transaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${id}`];
const transaction = allTransactionsForDuplicate[`${ONYXKEYS.COLLECTION.TRANSACTION}${id}`];
if (!transaction || isManagedCardTransaction(transaction) || isScanning(transaction)) {
return false;
}
Expand Down Expand Up @@ -882,12 +916,22 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
[currentUserPersonalDetails.accountID, defaultExpensePolicy?.id],
);

const allTransactionsForDuplicate = useMemo(
() =>
getAllTransactionsForDuplicate({
selectedTransactionsKeys,
selectedTransactions,
allTransactions,
}),
[selectedTransactionsKeys, selectedTransactions, allTransactions],
);

const isDuplicateOptionVisible = useMemo(
() =>
shouldShowBulkDuplicateOption({
selectedTransactionsKeys,
selectedTransactions,
allTransactions,
allTransactions: allTransactionsForDuplicate,
allReports,
allTransactionViolations,
allReportNameValuePairs,
Expand All @@ -899,7 +943,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
[
selectedTransactionsKeys,
selectedTransactions,
allTransactions,
allTransactionsForDuplicate,
allReports,
allTransactionViolations,
allReportNameValuePairs,
Expand Down Expand Up @@ -1644,7 +1688,7 @@ function useSearchBulkActions({queryJSON}: UseSearchBulkActionsParams) {
setDuplicateHandler,
isDuplicateReportOptionVisible,
setDuplicateReportHandler,
allTransactions,
allTransactions: allTransactionsForDuplicate,
allReports,
searchData: currentSearchResults?.data,
};
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/hooks/useSearchBulkActionsDuplicateTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,46 @@ describe('useSearchBulkActions - duplicate option', () => {
);
});

it('should duplicate a grouped child transaction that only exists in selected transaction metadata', async () => {
const txnID = '1302';
const txn = {...createRandomTransaction(1), transactionID: txnID, reportID: 'report1', managedCard: false, category: 'Grouped child', amount: 9000};

mockSelectedTransactions = {
[txnID]: makeSelectedTransaction({reportID: 'report1', transaction: txn}),
};

const {result} = renderHook(() => useSearchBulkActionsWithDuplicate({queryJSON: baseQueryJSON}));

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

result.current.headerButtonsOptions.find((o) => o.value === CONST.SEARCH.BULK_ACTION_TYPES.DUPLICATE)?.onSelected?.();

expect(bulkDuplicateExpenses).toHaveBeenCalledWith(
expect.objectContaining({
transactionIDs: [txnID],
allTransactions: expect.objectContaining({
[`${ONYXKEYS.COLLECTION.TRANSACTION}${txnID}`]: expect.objectContaining({transactionID: txnID, category: 'Grouped child', amount: 9000}),
}),
}),
);
});

it('should not show duplicate option for a grouped child managed-card transaction from selected transaction metadata', async () => {
const txnID = '1303';
const txn = {...createRandomTransaction(1), transactionID: txnID, reportID: 'report1', managedCard: true};

mockSelectedTransactions = {
[txnID]: makeSelectedTransaction({reportID: 'report1', transaction: txn}),
};

const {result} = renderHook(() => useSearchBulkActions({queryJSON: baseQueryJSON}));
await waitFor(() => expect(result.current.headerButtonsOptions.length).toBeGreaterThan(0));

expect(result.current.headerButtonsOptions.find((o) => o.value === CONST.SEARCH.BULK_ACTION_TYPES.DUPLICATE)).toBeUndefined();
});

it('should not show duplicate option for per-diem expense on non-default workspace', async () => {
const defaultPolicyID = 'DEFAULT_POLICY';
const otherPolicyID = 'OTHER_POLICY';
Expand Down
Loading