diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 79f83539a47f..4ffb042a8838 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -718,6 +718,8 @@ type GetReportNameParams = { policies?: SearchPolicy[]; }; +type ReportByPolicyMap = Record; + let currentUserEmail: string | undefined; let currentUserPrivateDomain: string | undefined; let currentUserAccountID: number | undefined; @@ -779,6 +781,7 @@ Onyx.connect({ }); let allReports: OnyxCollection; +let reportsByPolicyID: ReportByPolicyMap; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, waitForCollectionCallback: true, @@ -794,12 +797,27 @@ Onyx.connect({ if (!value) { return; } - Object.values(value).forEach((report) => { + + reportsByPolicyID = Object.values(value).reduce((acc, report) => { if (!report) { - return; + return acc; } + handleReportChanged(report); - }); + + // Get all reports, which are the ones that are: + // - Owned by the same user + // - Are either open or submitted + // - Belong to the same workspace + if (report.policyID && report.ownerAccountID === currentUserAccountID && (report.stateNum ?? 0) <= 1) { + if (!acc[report.policyID]) { + acc[report.policyID] = []; + } + acc[report.policyID].push(report); + } + + return acc; + }, {}); }, }); @@ -6926,17 +6944,15 @@ function shouldDisplayViolationsRBRInLHN(report: OnyxEntry, transactionV if (!isCurrentUserSubmitter(report.reportID)) { return false; } + if (!report.policyID || !reportsByPolicyID) { + return false; + } - // Get all potential reports, which are the ones that are: - // - Owned by the same user - // - Are either open or submitted - // - Belong to the same workspace - // And if any have a violation, then it should have a RBR - const reports = Object.values(allReports ?? {}) as Report[]; - const potentialReports = reports.filter((r) => r?.ownerAccountID === currentUserAccountID && (r?.stateNum ?? 0) <= 1 && r?.policyID === report.policyID); - return potentialReports.some( - (potentialReport) => hasViolations(potentialReport.reportID, transactionViolations) || hasWarningTypeViolations(potentialReport.reportID, transactionViolations), - ); + // If any report has a violation, then it should have a RBR + const potentialReports = reportsByPolicyID[report.policyID] ?? []; + return potentialReports.some((potentialReport) => { + return hasViolations(potentialReport.reportID, transactionViolations) || hasWarningTypeViolations(potentialReport.reportID, transactionViolations); + }); } /** diff --git a/tests/unit/SidebarUtilsTest.ts b/tests/unit/SidebarUtilsTest.ts index 3d216aac9566..b6660d0c0216 100644 --- a/tests/unit/SidebarUtilsTest.ts +++ b/tests/unit/SidebarUtilsTest.ts @@ -61,12 +61,12 @@ describe('SidebarUtils', () => { }; await Onyx.multiSet({ - ...MOCK_REPORTS, - ...MOCK_TRANSACTION_VIOLATIONS, - [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${MOCK_REPORT.reportID}` as const]: MOCK_REPORT_ACTIONS, [ONYXKEYS.SESSION]: { accountID: 12345, }, + ...MOCK_REPORTS, + ...MOCK_TRANSACTION_VIOLATIONS, + [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${MOCK_REPORT.reportID}` as const]: MOCK_REPORT_ACTIONS, [`${ONYXKEYS.COLLECTION.TRANSACTION}${MOCK_TRANSACTION.transactionID}` as const]: MOCK_TRANSACTION, }); diff --git a/tests/unit/WorkspaceSettingsUtilsTest.ts b/tests/unit/WorkspaceSettingsUtilsTest.ts index 37f235eb73f9..088a97e2ca20 100644 --- a/tests/unit/WorkspaceSettingsUtilsTest.ts +++ b/tests/unit/WorkspaceSettingsUtilsTest.ts @@ -30,11 +30,11 @@ describe('WorkspacesSettingsUtils', () => { const transactions = mockData.transactions; await Onyx.multiSet({ + session, ...(reports as ReportCollectionDataSet), ...(reportActions as OnyxCollection), ...(transactionViolations as OnyxCollection), ...(transactions as OnyxCollection), - session, }); await waitForBatchedUpdates();