From 6670150140270300666ad74ffe3525a490390c1d Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Thu, 4 Dec 2025 14:14:50 -0700 Subject: [PATCH 01/18] create useTodoHook --- src/hooks/useTodos.ts | 85 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/hooks/useTodos.ts diff --git a/src/hooks/useTodos.ts b/src/hooks/useTodos.ts new file mode 100644 index 000000000000..5bc8ec03ac74 --- /dev/null +++ b/src/hooks/useTodos.ts @@ -0,0 +1,85 @@ +import ONYXKEYS from '@src/ONYXKEYS'; +import useOnyx from './useOnyx'; +import { OnyxCollection } from 'react-native-onyx'; +import type {ReportNameValuePairs, Policy, Report, Transaction, TransactionViolation} from '@src/types/onyx'; +import CONST from '@src/CONST'; + +export default function useTodos() { + const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {canBeMissing: false, selector: (reports: OnyxCollection) => { + return Object.entries(reports ?? {}).reduce>((acc, [key, report]) => { + if (report?.type === CONST.REPORT.TYPE.EXPENSE) { + acc[key] = report; + } + return acc; + }, {}); + }}); + const reportIDs = new Set(); + const policyIDs = new Set(); + const transactionIDs = new Set(); + + if (allReports) { + Object.values(allReports).forEach(report => { + if (!report?.reportID || !report?.policyID) { + return; + } + reportIDs.add(report.reportID); + policyIDs.add(report.policyID); + }); + } + + const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, {canBeMissing: false, selector: (reportNameValuePairs: OnyxCollection) => { + return Object.entries(reportNameValuePairs ?? {}).reduce>((acc, [key, nvp]) => { + const reportID = key.replace(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, ''); + if (reportIDs.has(reportID) && nvp) { + acc[key] = nvp; + } + return acc; + }, {}); + }}); + + const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: false, selector: (policies: OnyxCollection) => { + return Object.entries(policies ?? {}).reduce>((acc, [key, policy]) => { + if (policy?.id && policyIDs.has(policy.id)) { + acc[key] = policy; + } + return acc; + }, {}); + }}); + + const [transactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION, {canBeMissing: false, selector: (transactions: OnyxCollection) => { + return Object.entries(transactions ?? {}).reduce>((acc, [key, transaction]) => { + if (transaction?.reportID && reportIDs.has(transaction.reportID)) { + acc[key] = transaction; + } + return acc; + }, {}); + }}); + + if (transactions) { + Object.values(transactions).forEach(transaction => { + if (!transaction?.transactionID) { + return; + } + transactionIDs.add(transaction.transactionID); + }); + } + + const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, {canBeMissing: false, selector: (transactionViolations: OnyxCollection) => { + return Object.entries(transactionViolations ?? {}).reduce>((acc, [key, violations]) => { + const transactionID = key.replace(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, ''); + if (transactionIDs.has(transactionID) && violations) { + acc[key] = violations; + } + return acc; + }, {}); + }}); + + const reportsToSubmit = []; + const reportsToApprove = []; + const reportsToPay = []; + const reportsToExport = []; + + + + return {}; +} From f2dec15fa92fd79a8521be68dea5d6ae31c9755b Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Thu, 4 Dec 2025 14:28:17 -0700 Subject: [PATCH 02/18] add loop to identify todos --- src/hooks/useTodos.ts | 114 +++++++++++++++++++++++++----------------- 1 file changed, 69 insertions(+), 45 deletions(-) diff --git a/src/hooks/useTodos.ts b/src/hooks/useTodos.ts index 5bc8ec03ac74..d7277af0c5f8 100644 --- a/src/hooks/useTodos.ts +++ b/src/hooks/useTodos.ts @@ -1,18 +1,32 @@ import ONYXKEYS from '@src/ONYXKEYS'; import useOnyx from './useOnyx'; -import { OnyxCollection } from 'react-native-onyx'; +import type {OnyxCollection} from 'react-native-onyx'; import type {ReportNameValuePairs, Policy, Report, Transaction, TransactionViolation} from '@src/types/onyx'; import CONST from '@src/CONST'; +import {canSubmitReport, canApproveIOU} from '@src/libs/actions/IOU'; +import {canPay, canExport} from '@src/libs/ReportPreviewActionUtils'; -export default function useTodos() { - const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {canBeMissing: false, selector: (reports: OnyxCollection) => { - return Object.entries(reports ?? {}).reduce>((acc, [key, report]) => { - if (report?.type === CONST.REPORT.TYPE.EXPENSE) { - acc[key] = report; +/** + * Creates a selector that filters an Onyx collection based on a predicate. + * The predicate receives the item and the ID extracted from the collection key. + */ +function createCollectionSelector(collectionPrefix: string, predicate: (item: T, id: string) => boolean) { + return (collection: OnyxCollection): Record => { + return Object.entries(collection ?? {}).reduce>((acc, [key, item]) => { + const id = key.replace(collectionPrefix, ''); + if (item && predicate(item, id)) { + acc[key] = item; } return acc; }, {}); - }}); + }; +} + +export default function useTodos() { + const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, { + canBeMissing: false, + selector: createCollectionSelector(ONYXKEYS.COLLECTION.REPORT, (report) => report.type === CONST.REPORT.TYPE.EXPENSE), + }); const reportIDs = new Set(); const policyIDs = new Set(); const transactionIDs = new Set(); @@ -27,33 +41,20 @@ export default function useTodos() { }); } - const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, {canBeMissing: false, selector: (reportNameValuePairs: OnyxCollection) => { - return Object.entries(reportNameValuePairs ?? {}).reduce>((acc, [key, nvp]) => { - const reportID = key.replace(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, ''); - if (reportIDs.has(reportID) && nvp) { - acc[key] = nvp; - } - return acc; - }, {}); - }}); + const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, { + canBeMissing: false, + selector: createCollectionSelector(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, (_, id) => reportIDs.has(id)), + }); - const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: false, selector: (policies: OnyxCollection) => { - return Object.entries(policies ?? {}).reduce>((acc, [key, policy]) => { - if (policy?.id && policyIDs.has(policy.id)) { - acc[key] = policy; - } - return acc; - }, {}); - }}); + const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, { + canBeMissing: false, + selector: createCollectionSelector(ONYXKEYS.COLLECTION.POLICY, (policy) => !!policy.id && policyIDs.has(policy.id)), + }); - const [transactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION, {canBeMissing: false, selector: (transactions: OnyxCollection) => { - return Object.entries(transactions ?? {}).reduce>((acc, [key, transaction]) => { - if (transaction?.reportID && reportIDs.has(transaction.reportID)) { - acc[key] = transaction; - } - return acc; - }, {}); - }}); + const [transactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION, { + canBeMissing: false, + selector: createCollectionSelector(ONYXKEYS.COLLECTION.TRANSACTION, (transaction) => !!transaction.reportID && reportIDs.has(transaction.reportID)), + }); if (transactions) { Object.values(transactions).forEach(transaction => { @@ -64,21 +65,44 @@ export default function useTodos() { }); } - const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, {canBeMissing: false, selector: (transactionViolations: OnyxCollection) => { - return Object.entries(transactionViolations ?? {}).reduce>((acc, [key, violations]) => { - const transactionID = key.replace(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, ''); - if (transactionIDs.has(transactionID) && violations) { - acc[key] = violations; - } - return acc; - }, {}); - }}); + const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, { + canBeMissing: false, + selector: createCollectionSelector(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, (_, id) => transactionIDs.has(id)), + }); + + const reportsToSubmit: string[] = []; + const reportsToApprove: string[] = []; + const reportsToPay: string[] = []; + const reportsToExport: string[] = []; + + if (!allReports) { + return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport}; + } - const reportsToSubmit = []; - const reportsToApprove = []; - const reportsToPay = []; - const reportsToExport = []; + Object.entries(allReports).forEach([key, value] => { + const report = value; + const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`]; + const transactions = transactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${report.reportID}`]; + const transactionViolations = transactionViolations?.[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${report.reportID}`]; + const isReportArchived = false; + if (canSubmitReport(value, policies?.[value.policyID], transactions, transactionViolations, false, '')) { + reportsToSubmit.push(key); + continue; + } + if (canApproveReport(value, transactions, transactionViolations, '')) { + reportsToApprove.push(key); + continue; + } + if (canPay(value, transactions, transactionViolations, '')) { + reportsToPay.push(key); + continue; + } + if (canExport(value, transactions, transactionViolations, '')) { + reportsToExport.push(key); + continue; + } + }); return {}; From 2fafb2a426eaae220cc866c35dcd3fe535a41400 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Thu, 4 Dec 2025 14:28:37 -0700 Subject: [PATCH 03/18] update return response --- src/hooks/useTodos.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/hooks/useTodos.ts b/src/hooks/useTodos.ts index d7277af0c5f8..2d4493ac3e72 100644 --- a/src/hooks/useTodos.ts +++ b/src/hooks/useTodos.ts @@ -103,7 +103,6 @@ export default function useTodos() { continue; } }); - - return {}; + return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport}; } From b6cf40289991d022d6944d613a4ac3118883440e Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Thu, 4 Dec 2025 14:35:59 -0700 Subject: [PATCH 04/18] refactor selectors --- src/hooks/useTodos.ts | 72 +++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/src/hooks/useTodos.ts b/src/hooks/useTodos.ts index 2d4493ac3e72..6054896a8a07 100644 --- a/src/hooks/useTodos.ts +++ b/src/hooks/useTodos.ts @@ -4,28 +4,18 @@ import type {OnyxCollection} from 'react-native-onyx'; import type {ReportNameValuePairs, Policy, Report, Transaction, TransactionViolation} from '@src/types/onyx'; import CONST from '@src/CONST'; import {canSubmitReport, canApproveIOU} from '@src/libs/actions/IOU'; -import {canPay, canExport} from '@src/libs/ReportPreviewActionUtils'; - -/** - * Creates a selector that filters an Onyx collection based on a predicate. - * The predicate receives the item and the ID extracted from the collection key. - */ -function createCollectionSelector(collectionPrefix: string, predicate: (item: T, id: string) => boolean) { - return (collection: OnyxCollection): Record => { - return Object.entries(collection ?? {}).reduce>((acc, [key, item]) => { - const id = key.replace(collectionPrefix, ''); - if (item && predicate(item, id)) { - acc[key] = item; - } - return acc; - }, {}); - }; -} export default function useTodos() { const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, { canBeMissing: false, - selector: createCollectionSelector(ONYXKEYS.COLLECTION.REPORT, (report) => report.type === CONST.REPORT.TYPE.EXPENSE), + selector: (collection: OnyxCollection): Record => { + return Object.entries(collection ?? {}).reduce>((acc, [key, item]) => { + if (item && item.reportID && item.type === CONST.REPORT.TYPE.EXPENSE) { + acc[item.reportID] = item; + } + return acc; + }, {}); + }, }); const reportIDs = new Set(); const policyIDs = new Set(); @@ -43,17 +33,39 @@ export default function useTodos() { const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, { canBeMissing: false, - selector: createCollectionSelector(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, (_, id) => reportIDs.has(id)), + selector: (collection: OnyxCollection): Record => { + return Object.entries(collection ?? {}).reduce>((acc, [key, item]) => { + const reportID = key.replace(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, ''); + if (item && reportIDs.has(reportID)) { + acc[reportID] = item; + } + return acc; + }, {}); + }, }); const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, { canBeMissing: false, - selector: createCollectionSelector(ONYXKEYS.COLLECTION.POLICY, (policy) => !!policy.id && policyIDs.has(policy.id)), + selector: (collection: OnyxCollection): Record => { + return Object.entries(collection ?? {}).reduce>((acc, [key, item]) => { + if (item && item.id && policyIDs.has(item.id)) { + acc[item.id] = item; + } + return acc; + }, {}); + }, }); const [transactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION, { canBeMissing: false, - selector: createCollectionSelector(ONYXKEYS.COLLECTION.TRANSACTION, (transaction) => !!transaction.reportID && reportIDs.has(transaction.reportID)), + selector: (collection: OnyxCollection): Record => { + return Object.entries(collection ?? {}).reduce>((acc, [key, item]) => { + if (item && item.reportID && reportIDs.has(item.reportID)) { + acc.hasOwnProperty(item.reportID) ? acc[item.reportID].push(item) : acc[item.reportID] = [item]; + } + return acc; + }, {}); + }, }); if (transactions) { @@ -67,7 +79,15 @@ export default function useTodos() { const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, { canBeMissing: false, - selector: createCollectionSelector(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, (_, id) => transactionIDs.has(id)), + selector: (collection: OnyxCollection): Record => { + return Object.entries(collection ?? {}).reduce>((acc, [key, item]) => { + const id = key.replace(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, ''); + if (item && transactionIDs.has(id)) { + acc[key] = item; + } + return acc; + }, {}); + }, }); const reportsToSubmit: string[] = []; @@ -94,14 +114,6 @@ export default function useTodos() { reportsToApprove.push(key); continue; } - if (canPay(value, transactions, transactionViolations, '')) { - reportsToPay.push(key); - continue; - } - if (canExport(value, transactions, transactionViolations, '')) { - reportsToExport.push(key); - continue; - } }); return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport}; From 824df724447ce113d556517291032a31672b726f Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Thu, 4 Dec 2025 14:50:37 -0700 Subject: [PATCH 05/18] refactor more code --- src/hooks/useTodos.ts | 150 +++++++++++++----------------------------- 1 file changed, 45 insertions(+), 105 deletions(-) diff --git a/src/hooks/useTodos.ts b/src/hooks/useTodos.ts index 6054896a8a07..3e3636294958 100644 --- a/src/hooks/useTodos.ts +++ b/src/hooks/useTodos.ts @@ -1,120 +1,60 @@ +import {useMemo} from 'react'; +import CONST from '@src/CONST'; +import {canSubmitReport} from '@src/libs/actions/IOU'; +import {isArchivedReport} from '@src/libs/ReportUtils'; import ONYXKEYS from '@src/ONYXKEYS'; +import type {Policy, Report, ReportNameValuePairs, Transaction, TransactionViolation} from '@src/types/onyx'; import useOnyx from './useOnyx'; -import type {OnyxCollection} from 'react-native-onyx'; -import type {ReportNameValuePairs, Policy, Report, Transaction, TransactionViolation} from '@src/types/onyx'; -import CONST from '@src/CONST'; -import {canSubmitReport, canApproveIOU} from '@src/libs/actions/IOU'; export default function useTodos() { - const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, { - canBeMissing: false, - selector: (collection: OnyxCollection): Record => { - return Object.entries(collection ?? {}).reduce>((acc, [key, item]) => { - if (item && item.reportID && item.type === CONST.REPORT.TYPE.EXPENSE) { - acc[item.reportID] = item; - } - return acc; - }, {}); - }, - }); - const reportIDs = new Set(); - const policyIDs = new Set(); - const transactionIDs = new Set(); - - if (allReports) { - Object.values(allReports).forEach(report => { - if (!report?.reportID || !report?.policyID) { - return; - } - reportIDs.add(report.reportID); - policyIDs.add(report.policyID); - }); - } - - const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, { - canBeMissing: false, - selector: (collection: OnyxCollection): Record => { - return Object.entries(collection ?? {}).reduce>((acc, [key, item]) => { - const reportID = key.replace(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, ''); - if (item && reportIDs.has(reportID)) { - acc[reportID] = item; - } - return acc; - }, {}); - }, - }); + const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: false}); + const currentUserEmail = session?.email ?? ''; + + 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 [allTransactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, {canBeMissing: false}); + + return useMemo(() => { + const reportsToSubmit: string[] = []; + const reportsToApprove: string[] = []; + const reportsToPay: string[] = []; + const reportsToExport: string[] = []; + + if (!allReports) { + return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport}; + } - const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, { - canBeMissing: false, - selector: (collection: OnyxCollection): Record => { - return Object.entries(collection ?? {}).reduce>((acc, [key, item]) => { - if (item && item.id && policyIDs.has(item.id)) { - acc[item.id] = item; + // Build a map of reportID -> transactions for efficient lookup + const transactionsByReportID: Record = {}; + if (allTransactions) { + Object.values(allTransactions).forEach((transaction) => { + if (!transaction?.reportID) { + return; } - return acc; - }, {}); - }, - }); - - const [transactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION, { - canBeMissing: false, - selector: (collection: OnyxCollection): Record => { - return Object.entries(collection ?? {}).reduce>((acc, [key, item]) => { - if (item && item.reportID && reportIDs.has(item.reportID)) { - acc.hasOwnProperty(item.reportID) ? acc[item.reportID].push(item) : acc[item.reportID] = [item]; + if (!transactionsByReportID[transaction.reportID]) { + transactionsByReportID[transaction.reportID] = []; } - return acc; - }, {}); - }, - }); + transactionsByReportID[transaction.reportID].push(transaction); + }); + } - if (transactions) { - Object.values(transactions).forEach(transaction => { - if (!transaction?.transactionID) { + Object.values(allReports).forEach((report) => { + if (!report?.reportID || report.type !== CONST.REPORT.TYPE.EXPENSE) { return; } - transactionIDs.add(transaction.transactionID); - }); - } - const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, { - canBeMissing: false, - selector: (collection: OnyxCollection): Record => { - return Object.entries(collection ?? {}).reduce>((acc, [key, item]) => { - const id = key.replace(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, ''); - if (item && transactionIDs.has(id)) { - acc[key] = item; - } - return acc; - }, {}); - }, - }); + const policy = report.policyID ? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`] : undefined; + const reportTransactions = transactionsByReportID[report.reportID] ?? []; + const reportNameValuePairs = allReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID}`]; + const isReportArchivedFlag = isArchivedReport(reportNameValuePairs); - const reportsToSubmit: string[] = []; - const reportsToApprove: string[] = []; - const reportsToPay: string[] = []; - const reportsToExport: string[] = []; + if (canSubmitReport(report, policy, reportTransactions, allTransactionViolations, isReportArchivedFlag, currentUserEmail)) { + reportsToSubmit.push(report.reportID); + } + }); - if (!allReports) { return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport}; - } - - Object.entries(allReports).forEach([key, value] => { - const report = value; - const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`]; - const transactions = transactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${report.reportID}`]; - const transactionViolations = transactionViolations?.[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${report.reportID}`]; - const isReportArchived = false; - - if (canSubmitReport(value, policies?.[value.policyID], transactions, transactionViolations, false, '')) { - reportsToSubmit.push(key); - continue; - } - if (canApproveReport(value, transactions, transactionViolations, '')) { - reportsToApprove.push(key); - continue; - } - }); - - return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport}; + }, [allReports, allPolicies, allReportNameValuePairs, allTransactions, allTransactionViolations, currentUserEmail]); } From 3bd7a505aee392091b6540e537418c23fa235cd9 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Thu, 4 Dec 2025 16:33:04 -0700 Subject: [PATCH 06/18] refactor code --- src/hooks/useTodos.ts | 63 ++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 34 deletions(-) diff --git a/src/hooks/useTodos.ts b/src/hooks/useTodos.ts index 3e3636294958..a60b85718c06 100644 --- a/src/hooks/useTodos.ts +++ b/src/hooks/useTodos.ts @@ -1,60 +1,55 @@ import {useMemo} from 'react'; import CONST from '@src/CONST'; -import {canSubmitReport} from '@src/libs/actions/IOU'; -import {isArchivedReport} from '@src/libs/ReportUtils'; +import {getActions} from '@src/libs/SearchUIUtils'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {Policy, Report, ReportNameValuePairs, Transaction, TransactionViolation} from '@src/types/onyx'; +import {emailSelector} from '@src/selectors/Session'; +import type {Report, SearchResults} from '@src/types/onyx'; import useOnyx from './useOnyx'; export default function useTodos() { - const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: false}); - const currentUserEmail = session?.email ?? ''; - + const [currentUserEmail] = useOnyx(ONYXKEYS.SESSION, {selector: emailSelector, canBeMissing: false}); 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 [allTransactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, {canBeMissing: false}); + const data = useMemo( + () => ({...allReports, ...allPolicies, ...allReportNameValuePairs, ...allTransactions}) as SearchResults['data'], + [allReports, allPolicies, allReportNameValuePairs, allTransactions], + ); + return useMemo(() => { - const reportsToSubmit: string[] = []; - const reportsToApprove: string[] = []; - const reportsToPay: string[] = []; - const reportsToExport: string[] = []; + const reportsToSubmit: Report[] = []; + const reportsToApprove: Report[] = []; + const reportsToPay: Report[] = []; + const reportsToExport: Report[] = []; if (!allReports) { return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport}; } - // Build a map of reportID -> transactions for efficient lookup - const transactionsByReportID: Record = {}; - if (allTransactions) { - Object.values(allTransactions).forEach((transaction) => { - if (!transaction?.reportID) { - return; - } - if (!transactionsByReportID[transaction.reportID]) { - transactionsByReportID[transaction.reportID] = []; - } - transactionsByReportID[transaction.reportID].push(transaction); - }); - } - - Object.values(allReports).forEach((report) => { + for (const report of Object.values(allReports)) { if (!report?.reportID || report.type !== CONST.REPORT.TYPE.EXPENSE) { - return; + continue; } - const policy = report.policyID ? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`] : undefined; - const reportTransactions = transactionsByReportID[report.reportID] ?? []; - const reportNameValuePairs = allReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID}`]; - const isReportArchivedFlag = isArchivedReport(reportNameValuePairs); + const actions = getActions(data, allTransactionViolations, `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, CONST.SEARCH.SEARCH_KEYS.EXPENSES, currentUserEmail ?? ''); - if (canSubmitReport(report, policy, reportTransactions, allTransactionViolations, isReportArchivedFlag, currentUserEmail)) { - reportsToSubmit.push(report.reportID); + if (actions.includes(CONST.SEARCH.ACTION_TYPES.SUBMIT)) { + reportsToSubmit.push(report); } - }); + if (actions.includes(CONST.SEARCH.ACTION_TYPES.APPROVE)) { + reportsToApprove.push(report); + } + if (actions.includes(CONST.SEARCH.ACTION_TYPES.PAY)) { + reportsToPay.push(report); + } + if (actions.includes(CONST.SEARCH.ACTION_TYPES.EXPORT_TO_ACCOUNTING)) { + reportsToExport.push(report); + } + } return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport}; - }, [allReports, allPolicies, allReportNameValuePairs, allTransactions, allTransactionViolations, currentUserEmail]); + }, [data, allReports, allTransactionViolations, currentUserEmail]); } From 64c33e3e7c7451b292d0db56412fe78a0a747a1c Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Thu, 4 Dec 2025 16:35:37 -0700 Subject: [PATCH 07/18] add search key --- src/hooks/useTodos.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/hooks/useTodos.ts b/src/hooks/useTodos.ts index a60b85718c06..5f5f0543da28 100644 --- a/src/hooks/useTodos.ts +++ b/src/hooks/useTodos.ts @@ -5,6 +5,7 @@ import ONYXKEYS from '@src/ONYXKEYS'; import {emailSelector} from '@src/selectors/Session'; import type {Report, SearchResults} from '@src/types/onyx'; import useOnyx from './useOnyx'; +import { useSearchContext } from '@components/Search/SearchContext'; export default function useTodos() { const [currentUserEmail] = useOnyx(ONYXKEYS.SESSION, {selector: emailSelector, canBeMissing: false}); @@ -13,6 +14,7 @@ export default function useTodos() { const [allReportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, {canBeMissing: false}); const [allTransactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION, {canBeMissing: false}); const [allTransactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, {canBeMissing: false}); + const {currentSearchKey = CONST.SEARCH.SEARCH_KEYS.EXPENSES} = useSearchContext(); const data = useMemo( () => ({...allReports, ...allPolicies, ...allReportNameValuePairs, ...allTransactions}) as SearchResults['data'], @@ -34,7 +36,7 @@ export default function useTodos() { continue; } - const actions = getActions(data, allTransactionViolations, `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, CONST.SEARCH.SEARCH_KEYS.EXPENSES, currentUserEmail ?? ''); + const actions = getActions(data, allTransactionViolations, `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, currentSearchKey, currentUserEmail ?? ''); if (actions.includes(CONST.SEARCH.ACTION_TYPES.SUBMIT)) { reportsToSubmit.push(report); From d410f648df81aabc99b3b68cec677325f94a0588 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Thu, 4 Dec 2025 17:00:34 -0700 Subject: [PATCH 08/18] update hook --- src/hooks/useTodos.ts | 4 ++-- src/pages/Search/SearchPage.tsx | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/hooks/useTodos.ts b/src/hooks/useTodos.ts index 5f5f0543da28..99b9c24b19d6 100644 --- a/src/hooks/useTodos.ts +++ b/src/hooks/useTodos.ts @@ -37,7 +37,7 @@ export default function useTodos() { } const actions = getActions(data, allTransactionViolations, `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, currentSearchKey, currentUserEmail ?? ''); - + console.log('over here', report.reportID, actions); if (actions.includes(CONST.SEARCH.ACTION_TYPES.SUBMIT)) { reportsToSubmit.push(report); } @@ -53,5 +53,5 @@ export default function useTodos() { } return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport}; - }, [data, allReports, allTransactionViolations, currentUserEmail]); + }, [data, allReports, allTransactionViolations, currentUserEmail, currentSearchKey]); } diff --git a/src/pages/Search/SearchPage.tsx b/src/pages/Search/SearchPage.tsx index 4c6cd65d4fa9..9ba33c57e616 100644 --- a/src/pages/Search/SearchPage.tsx +++ b/src/pages/Search/SearchPage.tsx @@ -89,6 +89,7 @@ import type {Report, SearchResults, Transaction} from '@src/types/onyx'; import type {FileObject} from '@src/types/utils/Attachment'; import SearchPageNarrow from './SearchPageNarrow'; import SearchPageWide from './SearchPageWide'; +import useTodos from '@hooks/useTodos'; type SearchPageProps = PlatformStackScreenProps; @@ -147,6 +148,8 @@ function SearchPage({route}: SearchPageProps) { // eslint-disable-next-line rulesdir/no-default-id-values const [currentSearchResults] = useOnyx(`${ONYXKEYS.COLLECTION.SNAPSHOT}${queryJSON?.hash ?? CONST.DEFAULT_NUMBER_ID}`, {canBeMissing: true}); + const todos = useTodos(); + console.log('over here', todos); const lastNonEmptySearchResults = useRef(undefined); const selectedTransactionReportIDs = useMemo(() => [...new Set(Object.values(selectedTransactions).map((transaction) => transaction.reportID))], [selectedTransactions]); const selectedReportIDs = Object.values(selectedReports).map((report) => report.reportID); From 7a5ec2ca1c24df26cf3caafe5dd4398244275be6 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Thu, 4 Dec 2025 17:18:34 -0700 Subject: [PATCH 09/18] refactor conditions for submit,approve --- src/hooks/useTodos.ts | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/hooks/useTodos.ts b/src/hooks/useTodos.ts index 99b9c24b19d6..3225f2deb9f7 100644 --- a/src/hooks/useTodos.ts +++ b/src/hooks/useTodos.ts @@ -1,14 +1,14 @@ import {useMemo} from 'react'; import CONST from '@src/CONST'; -import {getActions} from '@src/libs/SearchUIUtils'; import ONYXKEYS from '@src/ONYXKEYS'; -import {emailSelector} from '@src/selectors/Session'; -import type {Report, SearchResults} from '@src/types/onyx'; +import {accountIDSelector} from '@src/selectors/Session'; +import type {Report, Transaction} from '@src/types/onyx'; import useOnyx from './useOnyx'; import { useSearchContext } from '@components/Search/SearchContext'; +import {isArchivedReport} from '@libs/ReportUtils'; export default function useTodos() { - const [currentUserEmail] = useOnyx(ONYXKEYS.SESSION, {selector: emailSelector, canBeMissing: false}); + const [currentUserAccountID] = useOnyx(ONYXKEYS.SESSION, {selector: accountIDSelector, canBeMissing: false}); 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}); @@ -16,11 +16,6 @@ export default function useTodos() { const [allTransactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, {canBeMissing: false}); const {currentSearchKey = CONST.SEARCH.SEARCH_KEYS.EXPENSES} = useSearchContext(); - const data = useMemo( - () => ({...allReports, ...allPolicies, ...allReportNameValuePairs, ...allTransactions}) as SearchResults['data'], - [allReports, allPolicies, allReportNameValuePairs, allTransactions], - ); - return useMemo(() => { const reportsToSubmit: Report[] = []; const reportsToApprove: Report[] = []; @@ -31,27 +26,33 @@ export default function useTodos() { return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport}; } + const transactionsByReportID: Record = {}; + if (allTransactions) { + for (const transaction of Object.values(allTransactions)) { + if (!transaction?.reportID) { + continue; + } + (transactionsByReportID[transaction.reportID] ??= []).push(transaction); + } + } + for (const report of Object.values(allReports)) { if (!report?.reportID || report.type !== CONST.REPORT.TYPE.EXPENSE) { continue; } + const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`]; + const chatReportRNPV = allReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.chatReportID}`]; + const reportTransactions = transactionsByReportID[report.reportID] ?? []; - const actions = getActions(data, allTransactionViolations, `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, currentSearchKey, currentUserEmail ?? ''); - console.log('over here', report.reportID, actions); - if (actions.includes(CONST.SEARCH.ACTION_TYPES.SUBMIT)) { + if (report.stateNum === CONST.REPORT.STATE_NUM.OPEN && report.statusNum === CONST.REPORT.STATUS_NUM.OPEN && report.ownerAccountID === currentUserAccountID) { reportsToSubmit.push(report); } - if (actions.includes(CONST.SEARCH.ACTION_TYPES.APPROVE)) { + + if (report.stateNum === CONST.REPORT.STATE_NUM.SUBMITTED && report.statusNum === CONST.REPORT.STATUS_NUM.SUBMITTED && policy?.type !== CONST.POLICY.TYPE.PERSONAL && policy?.approvalMode !== CONST.POLICY.APPROVAL_MODE.OPTIONAL && reportTransactions.length > 0 && !isArchivedReport(chatReportRNPV) && report.managerID === currentUserAccountID) { reportsToApprove.push(report); } - if (actions.includes(CONST.SEARCH.ACTION_TYPES.PAY)) { - reportsToPay.push(report); - } - if (actions.includes(CONST.SEARCH.ACTION_TYPES.EXPORT_TO_ACCOUNTING)) { - reportsToExport.push(report); - } } return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport}; - }, [data, allReports, allTransactionViolations, currentUserEmail, currentSearchKey]); + }, [allReports, allTransactionViolations, currentUserAccountID, currentSearchKey]); } From e222c67f361f5b7d524bcf8a5852e0e2f5c1997d Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Thu, 4 Dec 2025 17:32:21 -0700 Subject: [PATCH 10/18] update conditionals --- src/hooks/useTodos.ts | 23 ++++++++++++++--------- src/libs/ReportPrimaryActionUtils.ts | 2 ++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/hooks/useTodos.ts b/src/hooks/useTodos.ts index 3225f2deb9f7..0de1f7ee676c 100644 --- a/src/hooks/useTodos.ts +++ b/src/hooks/useTodos.ts @@ -1,20 +1,19 @@ import {useMemo} from 'react'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import {accountIDSelector} from '@src/selectors/Session'; +import {accountIDSelector, emailSelector} from '@src/selectors/Session'; import type {Report, Transaction} from '@src/types/onyx'; import useOnyx from './useOnyx'; -import { useSearchContext } from '@components/Search/SearchContext'; -import {isArchivedReport} from '@libs/ReportUtils'; +import {isSubmitAction, isApproveAction, isPrimaryPayAction, isExportAction} from '@libs/ReportPrimaryActionUtils'; export default function useTodos() { const [currentUserAccountID] = useOnyx(ONYXKEYS.SESSION, {selector: accountIDSelector, canBeMissing: false}); + const [currentUserEmail] = useOnyx(ONYXKEYS.SESSION, {selector: emailSelector, canBeMissing: false}); 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 [allTransactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, {canBeMissing: false}); - const {currentSearchKey = CONST.SEARCH.SEARCH_KEYS.EXPENSES} = useSearchContext(); + const [allReportActions] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS, {canBeMissing: false}); return useMemo(() => { const reportsToSubmit: Report[] = []; @@ -42,17 +41,23 @@ export default function useTodos() { } const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`]; const chatReportRNPV = 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 (report.stateNum === CONST.REPORT.STATE_NUM.OPEN && report.statusNum === CONST.REPORT.STATUS_NUM.OPEN && report.ownerAccountID === currentUserAccountID) { + if (isSubmitAction(report, reportTransactions, policy, chatReportRNPV)) { reportsToSubmit.push(report); } - - if (report.stateNum === CONST.REPORT.STATE_NUM.SUBMITTED && report.statusNum === CONST.REPORT.STATUS_NUM.SUBMITTED && policy?.type !== CONST.POLICY.TYPE.PERSONAL && policy?.approvalMode !== CONST.POLICY.APPROVAL_MODE.OPTIONAL && reportTransactions.length > 0 && !isArchivedReport(chatReportRNPV) && report.managerID === currentUserAccountID) { + if (isApproveAction(report, reportTransactions, policy)) { reportsToApprove.push(report); } + if (isPrimaryPayAction(report, policy, chatReportRNPV)) { + reportsToPay.push(report); + } + if (isExportAction(report, policy, reportActions)) { + reportsToExport.push(report); + } } return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport}; - }, [allReports, allTransactionViolations, currentUserAccountID, currentSearchKey]); + }, [allReports, allPolicies, allReportNameValuePairs, allTransactions, currentUserAccountID, currentUserEmail]); } diff --git a/src/libs/ReportPrimaryActionUtils.ts b/src/libs/ReportPrimaryActionUtils.ts index f16b80ace338..7b17022bb347 100644 --- a/src/libs/ReportPrimaryActionUtils.ts +++ b/src/libs/ReportPrimaryActionUtils.ts @@ -485,6 +485,8 @@ export { isAddExpenseAction, isPrimaryPayAction, isExportAction, + isApproveAction, + isSubmitAction, isMarkAsResolvedAction, isPrimaryMarkAsResolvedAction, getAllExpensesToHoldIfApplicable, From 17020e7a3771d65ccc9dee6ea989c89e2f1f99e3 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Thu, 4 Dec 2025 17:33:33 -0700 Subject: [PATCH 11/18] rm unused imports --- src/hooks/useTodos.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/hooks/useTodos.ts b/src/hooks/useTodos.ts index 0de1f7ee676c..e4951fe9362b 100644 --- a/src/hooks/useTodos.ts +++ b/src/hooks/useTodos.ts @@ -1,14 +1,11 @@ import {useMemo} from 'react'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import {accountIDSelector, emailSelector} from '@src/selectors/Session'; import type {Report, Transaction} from '@src/types/onyx'; import useOnyx from './useOnyx'; import {isSubmitAction, isApproveAction, isPrimaryPayAction, isExportAction} from '@libs/ReportPrimaryActionUtils'; export default function useTodos() { - const [currentUserAccountID] = useOnyx(ONYXKEYS.SESSION, {selector: accountIDSelector, canBeMissing: false}); - const [currentUserEmail] = useOnyx(ONYXKEYS.SESSION, {selector: emailSelector, canBeMissing: false}); 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}); @@ -59,5 +56,5 @@ export default function useTodos() { } return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport}; - }, [allReports, allPolicies, allReportNameValuePairs, allTransactions, currentUserAccountID, currentUserEmail]); + }, [allReports, allPolicies, allReportNameValuePairs, allTransactions, allReportActions]); } From 3b8d1a53212afdddadd66964af6efbdedfae48f4 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Fri, 5 Dec 2025 14:47:40 -0700 Subject: [PATCH 12/18] fix lint issues --- src/hooks/useTodos.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/hooks/useTodos.ts b/src/hooks/useTodos.ts index e4951fe9362b..3bd8d04c5468 100644 --- a/src/hooks/useTodos.ts +++ b/src/hooks/useTodos.ts @@ -2,8 +2,8 @@ import {useMemo} from 'react'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Report, Transaction} from '@src/types/onyx'; -import useOnyx from './useOnyx'; import {isSubmitAction, isApproveAction, isPrimaryPayAction, isExportAction} from '@libs/ReportPrimaryActionUtils'; +import useOnyx from './useOnyx'; export default function useTodos() { const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {canBeMissing: false}); @@ -37,17 +37,17 @@ export default function useTodos() { continue; } const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`]; - const chatReportRNPV = allReportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.chatReportID}`]; + 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, chatReportRNPV)) { + if (isSubmitAction(report, reportTransactions, policy, reportNameValuePair)) { reportsToSubmit.push(report); } if (isApproveAction(report, reportTransactions, policy)) { reportsToApprove.push(report); } - if (isPrimaryPayAction(report, policy, chatReportRNPV)) { + if (isPrimaryPayAction(report, policy, reportNameValuePair)) { reportsToPay.push(report); } if (isExportAction(report, policy, reportActions)) { From 000d2733a8d3ad27d7115da299929636ea2c6bb2 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Fri, 5 Dec 2025 15:05:46 -0700 Subject: [PATCH 13/18] fix style --- src/hooks/useTodos.ts | 2 +- src/pages/Search/SearchPage.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hooks/useTodos.ts b/src/hooks/useTodos.ts index 3bd8d04c5468..3999a76fa947 100644 --- a/src/hooks/useTodos.ts +++ b/src/hooks/useTodos.ts @@ -1,8 +1,8 @@ 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 {isSubmitAction, isApproveAction, isPrimaryPayAction, isExportAction} from '@libs/ReportPrimaryActionUtils'; import useOnyx from './useOnyx'; export default function useTodos() { diff --git a/src/pages/Search/SearchPage.tsx b/src/pages/Search/SearchPage.tsx index 9ba33c57e616..6aba97318e20 100644 --- a/src/pages/Search/SearchPage.tsx +++ b/src/pages/Search/SearchPage.tsx @@ -32,6 +32,7 @@ import usePrevious from '@hooks/usePrevious'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; +import useTodos from '@hooks/useTodos'; import {confirmReadyToOpenApp} from '@libs/actions/App'; import {openWorkspace} from '@libs/actions/Policy/Policy'; import {moveIOUReportToPolicy, moveIOUReportToPolicyAndInviteSubmitter, openReport, searchInServer} from '@libs/actions/Report'; @@ -89,7 +90,6 @@ import type {Report, SearchResults, Transaction} from '@src/types/onyx'; import type {FileObject} from '@src/types/utils/Attachment'; import SearchPageNarrow from './SearchPageNarrow'; import SearchPageWide from './SearchPageWide'; -import useTodos from '@hooks/useTodos'; type SearchPageProps = PlatformStackScreenProps; From 6fe8dc09d8f6159ebbdff50778d59b20281f8344 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Fri, 5 Dec 2025 15:24:41 -0700 Subject: [PATCH 14/18] add test --- tests/unit/useTodosTest.ts | 229 +++++++++++++++++++++++++++++++++++++ 1 file changed, 229 insertions(+) create mode 100644 tests/unit/useTodosTest.ts diff --git a/tests/unit/useTodosTest.ts b/tests/unit/useTodosTest.ts new file mode 100644 index 000000000000..b73bf6554dcf --- /dev/null +++ b/tests/unit/useTodosTest.ts @@ -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 => + ({ + 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 => + ({ + 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 => + ({ + 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[0], { + type: CONST.REPORT.TYPE.CHAT, // Not an expense report + }), + createMockReport(EXCLUDED_REPORT_IDS[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 = {}; + + // Transactions for submit reports + SUBMIT_REPORT_IDS.forEach((reportID, index) => { + const transactionID = `trans_submit_${index}`; + transactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] = createMockTransaction(transactionID, reportID); + }); + + // Transactions for approve reports + APPROVE_REPORT_IDS.forEach((reportID, index) => { + const transactionID = `trans_approve_${index}`; + transactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] = createMockTransaction(transactionID, reportID); + }); + + // Transactions for pay reports (must be reimbursable) + PAY_REPORT_IDS.forEach((reportID, index) => { + const transactionID = `trans_pay_${index}`; + transactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] = createMockTransaction(transactionID, reportID, { + reimbursable: true, + }); + }); + + // Build reports object + const reports: Record = {}; + [...reportsToSubmit, ...reportsToApprove, ...reportsToPay, reportToExport, ...excludedReports].forEach((report) => { + 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); + + SUBMIT_REPORT_IDS.forEach((id) => { + expect(result.current.reportsToSubmit.map((r) => r.reportID)).toContain(id); + }); + APPROVE_REPORT_IDS.forEach((id) => { + expect(result.current.reportsToApprove.map((r) => r.reportID)).toContain(id); + }); + PAY_REPORT_IDS.forEach((id) => { + expect(result.current.reportsToPay.map((r) => r.reportID)).toContain(id); + }); + expect(result.current.reportsToExport.map((r) => r.reportID)).toContain(EXPORT_REPORT_ID); + }); + }); +}); From 1249cda0d02f618430292419596b7970e984c7e0 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Fri, 5 Dec 2025 15:32:47 -0700 Subject: [PATCH 15/18] fix eslint --- tests/unit/useTodosTest.ts | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/tests/unit/useTodosTest.ts b/tests/unit/useTodosTest.ts index b73bf6554dcf..7f3657655405 100644 --- a/tests/unit/useTodosTest.ts +++ b/tests/unit/useTodosTest.ts @@ -117,10 +117,10 @@ describe('useTodos', () => { // 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[0], { + createMockReport(EXCLUDED_REPORT_IDS.at(0) ?? '', { type: CONST.REPORT.TYPE.CHAT, // Not an expense report }), - createMockReport(EXCLUDED_REPORT_IDS[1], { + 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 @@ -165,30 +165,30 @@ describe('useTodos', () => { const transactions: Record = {}; // Transactions for submit reports - SUBMIT_REPORT_IDS.forEach((reportID, index) => { - const transactionID = `trans_submit_${index}`; + for (const reportID of SUBMIT_REPORT_IDS) { + const transactionID = `trans_submit_${reportID}`; transactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] = createMockTransaction(transactionID, reportID); - }); + } // Transactions for approve reports - APPROVE_REPORT_IDS.forEach((reportID, index) => { - const transactionID = `trans_approve_${index}`; + 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) - PAY_REPORT_IDS.forEach((reportID, index) => { - const transactionID = `trans_pay_${index}`; + 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 = {}; - [...reportsToSubmit, ...reportsToApprove, ...reportsToPay, reportToExport, ...excludedReports].forEach((report) => { + for (const report of [...reportsToSubmit, ...reportsToApprove, ...reportsToPay, reportToExport, ...excludedReports]) { reports[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`] = report; - }); + }; await act(async () => { await Onyx.multiSet({ @@ -214,15 +214,15 @@ describe('useTodos', () => { expect(result.current.reportsToPay.length).toBe(2); expect(result.current.reportsToExport.length).toBe(1); - SUBMIT_REPORT_IDS.forEach((id) => { + for (const id of SUBMIT_REPORT_IDS) { expect(result.current.reportsToSubmit.map((r) => r.reportID)).toContain(id); - }); - APPROVE_REPORT_IDS.forEach((id) => { + }; + for (const id of APPROVE_REPORT_IDS) { expect(result.current.reportsToApprove.map((r) => r.reportID)).toContain(id); - }); - PAY_REPORT_IDS.forEach((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); }); }); From c7b0edb1969804bffc401de58ae693236c392104 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Fri, 5 Dec 2025 15:38:35 -0700 Subject: [PATCH 16/18] fix lint --- tests/unit/useTodosTest.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit/useTodosTest.ts b/tests/unit/useTodosTest.ts index 7f3657655405..08c569096cca 100644 --- a/tests/unit/useTodosTest.ts +++ b/tests/unit/useTodosTest.ts @@ -188,7 +188,7 @@ describe('useTodos', () => { const reports: Record = {}; for (const report of [...reportsToSubmit, ...reportsToApprove, ...reportsToPay, reportToExport, ...excludedReports]) { reports[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`] = report; - }; + } await act(async () => { await Onyx.multiSet({ @@ -216,13 +216,13 @@ describe('useTodos', () => { 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); }); }); From e4f2740e54f63f602cc18fd3d2f30cb6824c27f1 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Fri, 5 Dec 2025 15:39:31 -0700 Subject: [PATCH 17/18] rm import/logs --- src/pages/Search/SearchPage.tsx | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/pages/Search/SearchPage.tsx b/src/pages/Search/SearchPage.tsx index 6aba97318e20..4c6cd65d4fa9 100644 --- a/src/pages/Search/SearchPage.tsx +++ b/src/pages/Search/SearchPage.tsx @@ -32,7 +32,6 @@ import usePrevious from '@hooks/usePrevious'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; -import useTodos from '@hooks/useTodos'; import {confirmReadyToOpenApp} from '@libs/actions/App'; import {openWorkspace} from '@libs/actions/Policy/Policy'; import {moveIOUReportToPolicy, moveIOUReportToPolicyAndInviteSubmitter, openReport, searchInServer} from '@libs/actions/Report'; @@ -148,8 +147,6 @@ function SearchPage({route}: SearchPageProps) { // eslint-disable-next-line rulesdir/no-default-id-values const [currentSearchResults] = useOnyx(`${ONYXKEYS.COLLECTION.SNAPSHOT}${queryJSON?.hash ?? CONST.DEFAULT_NUMBER_ID}`, {canBeMissing: true}); - const todos = useTodos(); - console.log('over here', todos); const lastNonEmptySearchResults = useRef(undefined); const selectedTransactionReportIDs = useMemo(() => [...new Set(Object.values(selectedTransactions).map((transaction) => transaction.reportID))], [selectedTransactions]); const selectedReportIDs = Object.values(selectedReports).map((report) => report.reportID); From 70be8ca2221d3d4242217951be65ee3195341678 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Tue, 9 Dec 2025 09:35:39 -0700 Subject: [PATCH 18/18] address comment --- src/hooks/useTodos.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/hooks/useTodos.ts b/src/hooks/useTodos.ts index 3999a76fa947..eea23c0df95d 100644 --- a/src/hooks/useTodos.ts +++ b/src/hooks/useTodos.ts @@ -18,7 +18,8 @@ export default function useTodos() { const reportsToPay: Report[] = []; const reportsToExport: Report[] = []; - if (!allReports) { + const reports = allReports ? Object.values(allReports) : []; + if (reports.length === 0) { return {reportsToSubmit, reportsToApprove, reportsToPay, reportsToExport}; } @@ -32,7 +33,7 @@ export default function useTodos() { } } - for (const report of Object.values(allReports)) { + for (const report of reports) { if (!report?.reportID || report.type !== CONST.REPORT.TYPE.EXPENSE) { continue; }