Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly filter report transactions #53613

Merged
merged 3 commits into from
Dec 9, 2024
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
11 changes: 2 additions & 9 deletions src/libs/actions/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,9 @@ function handleActionButtonPress(hash: number, item: TransactionListItemType | R
// The transactionIDList is needed to handle actions taken on `status:all` where transactions on single expense reports can be approved/paid.
// We need the transactionID to display the loading indicator for that list item's action.
const transactionID = isTransactionListItemType(item) ? [item.transactionID] : undefined;
const data = (allSnapshots?.[`${ONYXKEYS.COLLECTION.SNAPSHOT}${hash}`]?.data ?? {}) as SearchResults['data'];
const allReportTransactions = (
isReportListItemType(item)
? Object.entries(data)
.filter(([itemKey, value]) => itemKey.startsWith(ONYXKEYS.COLLECTION.REPORT) && (value as SearchTransaction)?.reportID === item.reportID)
.map((report) => report[1])
: [data[`${ONYXKEYS.COLLECTION.TRANSACTION}${item.transactionID}`]]
) as SearchTransaction[];

const allReportTransactions = (isReportListItemType(item) ? item.transactions : [item]) as SearchTransaction[];
const hasHeldExpense = ReportUtils.hasHeldExpenses('', allReportTransactions);

if (hasHeldExpense) {
goToItem();
return;
Expand Down
6 changes: 3 additions & 3 deletions src/types/onyx/SearchResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,16 @@ type SearchTransaction = {
hasEReceipt?: boolean;

/** The transaction description */
description: string;
description?: string;

/** The transaction sender ID */
accountID: number;

/** The transaction recipient ID */
managerID: number;

/** If the transaction has a Ereceipt */
hasViolation: boolean;
/** If the transaction has violations */
hasViolation?: boolean;

/** The transaction tax amount */
taxAmount?: number;
Expand Down
217 changes: 217 additions & 0 deletions tests/unit/Search/handleActionButtonPressTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
/* eslint-disable @typescript-eslint/naming-convention */
import type {ReportListItemType} from '@components/SelectionList/types';
import {handleActionButtonPress} from '@libs/actions/Search';

const mockReportItemWithHold = {
shouldAnimateInHighlight: false,
accountID: 1206,
action: 'approve',
chatReportID: '2108006919825366',
created: '2024-12-04 23:18:33',
currency: 'USD',
isOneTransactionReport: false,
isPolicyExpenseChat: false,
isWaitingOnBankAccount: false,
managerID: 1206,
nonReimbursableTotal: 0,
ownerAccountID: 1206,
policyID: '48D7178DE42EE9F9',
private_isArchived: '',
reportID: '1350959062018695',
reportName: 'Expense Report #1350959062018695',
stateNum: 1,
statusNum: 1,
total: -13500,
type: 'expense',
unheldTotal: -12300,
keyForList: '1350959062018695',
from: {
accountID: 1206,
avatar: 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/default-avatar_7.png',
displayName: 'aesf',
firstName: 'aesf',
lastName: '',
login: 'apb@apb.com',
pronouns: '',
timezone: {
automatic: true,
selected: 'America/Edmonton',
},
phoneNumber: '',
validated: false,
},
to: {
accountID: 1206,
avatar: 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/default-avatar_7.png',
displayName: 'aesf',
firstName: 'aesf',
lastName: '',
login: 'apb@apb.com',
pronouns: '',
timezone: {
automatic: true,
selected: 'America/Edmonton',
},
phoneNumber: '',
validated: false,
},
transactions: [
{
accountID: 1206,
action: 'view',
amount: -1200,
canDelete: true,
canHold: false,
canUnhold: true,
category: '',
comment: {
comment: '',
hold: '3042630993757922770',
},
created: '2024-12-04',
currency: 'USD',
hasEReceipt: false,
isFromOneTransactionReport: false,
managerID: 1206,
merchant: 'qewr',
modifiedAmount: 0,
modifiedCreated: '',
modifiedCurrency: '',
modifiedMerchant: '',
parentTransactionID: '',
policyID: '48D7178DE42EE9F9',
reportID: '1350959062018695',
reportType: 'expense',
tag: '',
transactionID: '1049531721038862176',
transactionThreadReportID: '2957345659269055',
transactionType: 'cash',
from: {
accountID: 1206,
avatar: 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/default-avatar_7.png',
displayName: 'aesf',
firstName: 'aesf',
lastName: '',
login: 'apb@apb.com',
pronouns: '',
timezone: {
automatic: true,
selected: 'America/Edmonton',
},
phoneNumber: '',
validated: false,
},
to: {
accountID: 1206,
avatar: 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/default-avatar_7.png',
displayName: 'aesf',
firstName: 'aesf',
lastName: '',
login: 'apb@apb.com',
pronouns: '',
timezone: {
automatic: true,
selected: 'America/Edmonton',
},
phoneNumber: '',
validated: false,
},
formattedFrom: 'aesf',
formattedTo: 'aesf',
formattedTotal: 1200,
formattedMerchant: 'qewr',
date: '2024-12-04',
shouldShowMerchant: true,
shouldShowCategory: true,
shouldShowTag: false,
shouldShowTax: false,
keyForList: '1049531721038862176',
shouldShowYear: false,
shouldAnimateInHighlight: false,
},
{
accountID: 1206,
action: 'view',
amount: -12300,
canDelete: true,
canHold: true,
canUnhold: false,
category: '',
comment: {
comment: '',
},
created: '2024-12-04',
currency: 'USD',
hasEReceipt: false,
isFromOneTransactionReport: false,
managerID: 1206,
merchant: 'fgdfgadfaf',
modifiedAmount: 0,
modifiedCreated: '',
modifiedCurrency: '',
modifiedMerchant: '',
parentTransactionID: '',
policyID: '48D7178DE42EE9F9',
reportID: '1350959062018695',
reportType: 'expense',
tag: '',
transactionID: '5345995386715609966',
transactionThreadReportID: '740282333335072',
transactionType: 'cash',
from: {
accountID: 1206,
avatar: 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/default-avatar_7.png',
displayName: 'aesf',
login: 'apb@apb.com',
},
to: {
accountID: 1206,
avatar: 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/default-avatar_7.png',
displayName: 'aesf',
},
formattedFrom: 'aesf',
formattedTo: 'aesf',
formattedTotal: 12300,
formattedMerchant: 'fgdfgadfaf',
date: '2024-12-04',
shouldShowMerchant: true,
shouldShowCategory: true,
shouldShowTag: false,
shouldShowTax: false,
keyForList: '5345995386715609966',
shouldShowYear: false,
shouldAnimateInHighlight: false,
},
],
isSelected: false,
} as ReportListItemType;

const updatedMockReportItem = {
...mockReportItemWithHold,
transactions: mockReportItemWithHold.transactions.map((transaction, index) => {
if (index === 0) {
return {
...transaction,
comment: {
comment: '',
},
};
}
return transaction;
}),
};

describe('handleActionButtonPress', () => {
const searchHash = 1;
test('Should navigate to item when report has one transaction on hold', () => {
const goToItem = jest.fn(() => {});
handleActionButtonPress(searchHash, mockReportItemWithHold, goToItem);
expect(goToItem).toHaveBeenCalledTimes(1);
});

test('Should not navigate to item when the hold is removed', () => {
const goToItem = jest.fn(() => {});
handleActionButtonPress(searchHash, updatedMockReportItem, goToItem);
expect(goToItem).toHaveBeenCalledTimes(0);
});
});
Loading