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
61 changes: 61 additions & 0 deletions src/hooks/useTodos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {useMemo} from 'react';
import {isApproveAction, isExportAction, isPrimaryPayAction, isSubmitAction} from '@libs/ReportPrimaryActionUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Report, Transaction} from '@src/types/onyx';
import useOnyx from './useOnyx';

export default function useTodos() {
const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {canBeMissing: false});
const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: false});
const [allReportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, {canBeMissing: false});
const [allTransactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION, {canBeMissing: false});
const [allReportActions] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS, {canBeMissing: false});

return useMemo(() => {
const reportsToSubmit: Report[] = [];
const reportsToApprove: Report[] = [];
const reportsToPay: Report[] = [];
const reportsToExport: Report[] = [];

const reports = allReports ? Object.values(allReports) : [];
if (reports.length === 0) {
return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport};
}

const transactionsByReportID: Record<string, Transaction[]> = {};
if (allTransactions) {
for (const transaction of Object.values(allTransactions)) {
if (!transaction?.reportID) {
continue;
}
(transactionsByReportID[transaction.reportID] ??= []).push(transaction);
}
}

for (const report of reports) {
if (!report?.reportID || report.type !== CONST.REPORT.TYPE.EXPENSE) {
continue;
}
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`];
const reportNameValuePair = allReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.chatReportID}`];
const reportActions = Object.values(allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`] ?? []);
const reportTransactions = transactionsByReportID[report.reportID] ?? [];

if (isSubmitAction(report, reportTransactions, policy, reportNameValuePair)) {
reportsToSubmit.push(report);
}
if (isApproveAction(report, reportTransactions, policy)) {
reportsToApprove.push(report);
}
if (isPrimaryPayAction(report, policy, reportNameValuePair)) {

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.

Coming from #85861, useTodos counted payable reports via isPrimaryPayAction without filtering held expenses. SearchUIUtils.getActions() already excludes those with !hasHeldExpenses, so Home showed phantom "Pay N Reports" counts.

Fixed in #92499.

reportsToPay.push(report);
}
if (isExportAction(report, policy, reportActions)) {
reportsToExport.push(report);
}
}

return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport};
}, [allReports, allPolicies, allReportNameValuePairs, allTransactions, allReportActions]);
}
2 changes: 2 additions & 0 deletions src/libs/ReportPrimaryActionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ export {
isAddExpenseAction,
isPrimaryPayAction,
isExportAction,
isApproveAction,
isSubmitAction,
isMarkAsResolvedAction,
isPrimaryMarkAsResolvedAction,
getAllExpensesToHoldIfApplicable,
Expand Down
229 changes: 229 additions & 0 deletions tests/unit/useTodosTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
import {act, renderHook} from '@testing-library/react-native';
import type {OnyxMultiSetInput} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import useTodos from '@hooks/useTodos';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, Report, Transaction} from '@src/types/onyx';
import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct';

const CURRENT_USER_ACCOUNT_ID = 1;
const CURRENT_USER_EMAIL = 'tester@mail.com';
const OTHER_USER_ACCOUNT_ID = 2;

const POLICY_ID = 'policy123';
const POLICY_WITH_CONNECTION_ID = 'policy_with_connection';

// This keeps the error "@rnmapbox/maps native code not available." from causing the tests to fail
jest.mock('@components/ConfirmedRoute.tsx');

const createMockReport = (reportID: string, overrides: Partial<Report> = {}): Report =>
({
reportID,
chatReportID: `chat_${reportID}`,
policyID: POLICY_ID,
type: CONST.REPORT.TYPE.EXPENSE,
ownerAccountID: CURRENT_USER_ACCOUNT_ID,
managerID: OTHER_USER_ACCOUNT_ID,
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
...overrides,
}) as Report;

const createMockPolicy = (policyID: string, overrides: Partial<Policy> = {}): Policy =>
({
id: policyID,
type: CONST.POLICY.TYPE.TEAM,
approvalMode: CONST.POLICY.APPROVAL_MODE.BASIC,
role: CONST.POLICY.ROLE.USER,
...overrides,
}) as Policy;

const createMockTransaction = (transactionID: string, reportID: string, overrides: Partial<Transaction> = {}): Transaction =>
({
transactionID,
reportID,
amount: 100,
modifiedAmount: 0,
reimbursable: true,
...overrides,
}) as Transaction;

describe('useTodos', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
});
});

beforeEach(async () => {
await Onyx.clear();
await waitForBatchedUpdatesWithAct();
});

describe('categorizes reports correctly', () => {
// Report IDs for each category
const SUBMIT_REPORT_IDS = ['submit_1', 'submit_2', 'submit_3', 'submit_4'];
const APPROVE_REPORT_IDS = ['approve_1', 'approve_2', 'approve_3'];
const PAY_REPORT_IDS = ['pay_1', 'pay_2'];
const EXPORT_REPORT_ID = 'export_1';
const EXCLUDED_REPORT_IDS = ['excluded_1', 'excluded_2'];

beforeEach(async () => {
// Create 4 reports that can be submitted (open, owned by current user, with transactions)
const reportsToSubmit = SUBMIT_REPORT_IDS.map((id) =>
createMockReport(id, {
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
ownerAccountID: CURRENT_USER_ACCOUNT_ID,
}),
);

// Create 3 reports that can be approved (submitted, current user is manager, with transactions)
const reportsToApprove = APPROVE_REPORT_IDS.map((id) =>
createMockReport(id, {
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
ownerAccountID: OTHER_USER_ACCOUNT_ID,
managerID: CURRENT_USER_ACCOUNT_ID,
}),
);

// Create 2 reports that can be paid (approved, current user is admin/payer, with reimbursable transactions)
const reportsToPay = PAY_REPORT_IDS.map((id) =>
createMockReport(id, {
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
statusNum: CONST.REPORT.STATUS_NUM.APPROVED,
ownerAccountID: OTHER_USER_ACCOUNT_ID,
managerID: CURRENT_USER_ACCOUNT_ID,
total: -100,
}),
);

// Create 1 report that can be exported:
// - Approved status
// - User is admin
// - Policy has a valid accounting connection with auto-sync disabled
// - Not waiting on bank account
const reportToExport = createMockReport(EXPORT_REPORT_ID, {
policyID: POLICY_WITH_CONNECTION_ID,
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
statusNum: CONST.REPORT.STATUS_NUM.APPROVED,
ownerAccountID: OTHER_USER_ACCOUNT_ID,
isWaitingOnBankAccount: false,
});

// Create 2 reports that don't fit any condition:
// 1. A chat report (not expense type)
// 2. An expense report owned by another user that's not submitted (can't submit, approve, pay, or export)
const excludedReports = [
createMockReport(EXCLUDED_REPORT_IDS.at(0) ?? '', {
type: CONST.REPORT.TYPE.CHAT, // Not an expense report
}),
createMockReport(EXCLUDED_REPORT_IDS.at(1) ?? '', {
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
ownerAccountID: OTHER_USER_ACCOUNT_ID, // Not owned by current user, so can't submit
managerID: OTHER_USER_ACCOUNT_ID, // Not managed by current user, so can't approve
}),
];

// Create main policy (for submit, approve, pay reports)
const policy = createMockPolicy(POLICY_ID, {
approvalMode: CONST.POLICY.APPROVAL_MODE.BASIC,
role: CONST.POLICY.ROLE.ADMIN,
ownerAccountID: CURRENT_USER_ACCOUNT_ID,
reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES,
});

// Create policy with accounting connection (for export report)
// Cast to Policy to avoid strict type checking on connection config
const policyWithConnection = {
...createMockPolicy(POLICY_WITH_CONNECTION_ID, {
role: CONST.POLICY.ROLE.ADMIN,
}),
connections: {
// QuickBooks Online connection with auto-sync disabled
[CONST.POLICY.CONNECTIONS.NAME.QBO]: {
lastSync: {
isConnected: true,
isSuccessful: true,
isAuthenticationError: false,
source: 'DIRECT',
},
config: {
autoSync: {
jobID: 'job123',
enabled: false, // Auto-sync disabled so manual export is available
},
},
},
},
} as Policy;

// Create transactions for each report that needs them
const transactions: Record<string, Transaction> = {};

// Transactions for submit reports
for (const reportID of SUBMIT_REPORT_IDS) {
const transactionID = `trans_submit_${reportID}`;
transactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] = createMockTransaction(transactionID, reportID);
}

// Transactions for approve reports
for (const reportID of APPROVE_REPORT_IDS) {
const transactionID = `trans_approve_${reportID}`;
transactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] = createMockTransaction(transactionID, reportID);
}

// Transactions for pay reports (must be reimbursable)
for (const reportID of PAY_REPORT_IDS) {
const transactionID = `trans_pay_${reportID}`;
transactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] = createMockTransaction(transactionID, reportID, {
reimbursable: true,
});
}

// Build reports object
const reports: Record<string, Report> = {};
for (const report of [...reportsToSubmit, ...reportsToApprove, ...reportsToPay, reportToExport, ...excludedReports]) {
reports[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`] = report;
}

await act(async () => {
await Onyx.multiSet({
[ONYXKEYS.SESSION]: {
email: CURRENT_USER_EMAIL,
accountID: CURRENT_USER_ACCOUNT_ID,
},
[`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`]: policy,
[`${ONYXKEYS.COLLECTION.POLICY}${POLICY_WITH_CONNECTION_ID}`]: policyWithConnection,
...reports,
...transactions,
} as unknown as OnyxMultiSetInput);
await waitForBatchedUpdatesWithAct();
});
});

it('returns correct number of reports for each category', async () => {
const {result} = renderHook(() => useTodos());
await waitForBatchedUpdatesWithAct();

expect(result.current.reportsToSubmit.length).toBe(4);
expect(result.current.reportsToApprove.length).toBe(3);
expect(result.current.reportsToPay.length).toBe(2);
expect(result.current.reportsToExport.length).toBe(1);

for (const id of SUBMIT_REPORT_IDS) {
expect(result.current.reportsToSubmit.map((r) => r.reportID)).toContain(id);
}
for (const id of APPROVE_REPORT_IDS) {
expect(result.current.reportsToApprove.map((r) => r.reportID)).toContain(id);
}
for (const id of PAY_REPORT_IDS) {
expect(result.current.reportsToPay.map((r) => r.reportID)).toContain(id);
}
expect(result.current.reportsToExport.map((r) => r.reportID)).toContain(EXPORT_REPORT_ID);
});
});
});
Loading