From 564e6ce669d4c39e8e3a9295af6290a6c496e4a4 Mon Sep 17 00:00:00 2001
From: Tomasz Misiukiewicz <tomasz.misiukiewicz@callstack.com>
Date: Fri, 7 Feb 2025 15:06:17 +0100
Subject: [PATCH 1/6] avoid loop for each report

---
 src/libs/ReportUtils.ts | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts
index c7c92ac79469..f9daa33c7d3a 100644
--- a/src/libs/ReportUtils.ts
+++ b/src/libs/ReportUtils.ts
@@ -778,6 +778,7 @@ Onyx.connect({
 });
 
 let allReports: OnyxCollection<Report>;
+let reportsByPolicyID: Record<string, Report[]>;
 Onyx.connect({
     key: ONYXKEYS.COLLECTION.REPORT,
     waitForCollectionCallback: true,
@@ -793,12 +794,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;
+        }, {} as Record<string, Report[]>);
     },
 });
 
@@ -6925,13 +6941,8 @@ function shouldDisplayViolationsRBRInLHN(report: OnyxEntry<Report>, transactionV
         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);
+    const potentialReports = reportsByPolicyID?.[report.policyID] ?? [];
     return potentialReports.some(
         (potentialReport) => hasViolations(potentialReport.reportID, transactionViolations) || hasWarningTypeViolations(potentialReport.reportID, transactionViolations),
     );

From 778bb1867096425977477980029c6099003c5c94 Mon Sep 17 00:00:00 2001
From: Tomasz Misiukiewicz <tomasz.misiukiewicz@callstack.com>
Date: Mon, 10 Feb 2025 08:46:25 +0100
Subject: [PATCH 2/6] fix types

---
 src/libs/ReportUtils.ts | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts
index f9daa33c7d3a..143e747c8027 100644
--- a/src/libs/ReportUtils.ts
+++ b/src/libs/ReportUtils.ts
@@ -717,6 +717,8 @@ type GetReportNameParams = {
     policies?: SearchPolicy[];
 };
 
+type ReportByPolicyMap = Record<string, Report[]>;
+
 let currentUserEmail: string | undefined;
 let currentUserPrivateDomain: string | undefined;
 let currentUserAccountID: number | undefined;
@@ -778,7 +780,7 @@ Onyx.connect({
 });
 
 let allReports: OnyxCollection<Report>;
-let reportsByPolicyID: Record<string, Report[]>;
+let reportsByPolicyID: ReportByPolicyMap;
 Onyx.connect({
     key: ONYXKEYS.COLLECTION.REPORT,
     waitForCollectionCallback: true,
@@ -795,7 +797,7 @@ Onyx.connect({
             return;
         }
 
-        reportsByPolicyID = Object.values(value).reduce((acc, report) => {
+        reportsByPolicyID = Object.values(value).reduce<ReportByPolicyMap>((acc, report) => {
             if (!report) {
                 return acc;
             }
@@ -814,7 +816,7 @@ Onyx.connect({
             }
 
             return acc;
-        }, {} as Record<string, Report[]>);
+        }, {});
     },
 });
 
@@ -6942,7 +6944,7 @@ function shouldDisplayViolationsRBRInLHN(report: OnyxEntry<Report>, transactionV
     }
 
     // And if any have a violation, then it should have a RBR
-    const potentialReports = reportsByPolicyID?.[report.policyID] ?? [];
+    const potentialReports = reportsByPolicyID[report.policyID ?? ''] ?? [];
     return potentialReports.some(
         (potentialReport) => hasViolations(potentialReport.reportID, transactionViolations) || hasWarningTypeViolations(potentialReport.reportID, transactionViolations),
     );

From 442bda8615a0aa7ad375e53bd151095d987ffa64 Mon Sep 17 00:00:00 2001
From: Tomasz Misiukiewicz <tomasz.misiukiewicz@callstack.com>
Date: Mon, 10 Feb 2025 09:00:01 +0100
Subject: [PATCH 3/6] update comment

---
 src/libs/ReportUtils.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts
index 143e747c8027..966ee254d876 100644
--- a/src/libs/ReportUtils.ts
+++ b/src/libs/ReportUtils.ts
@@ -6943,7 +6943,7 @@ function shouldDisplayViolationsRBRInLHN(report: OnyxEntry<Report>, transactionV
         return false;
     }
 
-    // And if any have a violation, then it should have a RBR
+    // If any report has a violation, then it should have a RBR
     const potentialReports = reportsByPolicyID[report.policyID ?? ''] ?? [];
     return potentialReports.some(
         (potentialReport) => hasViolations(potentialReport.reportID, transactionViolations) || hasWarningTypeViolations(potentialReport.reportID, transactionViolations),

From 9dc145e66505889dc62f6dba9e8eaac2d50fd757 Mon Sep 17 00:00:00 2001
From: Tomasz Misiukiewicz <tomasz.misiukiewicz@callstack.com>
Date: Wed, 12 Feb 2025 09:20:57 +0100
Subject: [PATCH 4/6] fix linter

---
 src/libs/ReportUtils.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts
index 0fd8a5f18f4c..2bb4510fa005 100644
--- a/src/libs/ReportUtils.ts
+++ b/src/libs/ReportUtils.ts
@@ -6946,7 +6946,7 @@ function shouldDisplayViolationsRBRInLHN(report: OnyxEntry<Report>, transactionV
     }
 
     // If any report has a violation, then it should have a RBR
-    const potentialReports = reportsByPolicyID[report.policyID ?? ''] ?? [];
+    const potentialReports = report.policyID ? reportsByPolicyID[report.policyID] : [];
     return potentialReports.some(
         (potentialReport) => hasViolations(potentialReport.reportID, transactionViolations) || hasWarningTypeViolations(potentialReport.reportID, transactionViolations),
     );

From 13818acb040c148ce8fea91db3b6021e7ab5d5bd Mon Sep 17 00:00:00 2001
From: Tomasz Misiukiewicz <tomasz.misiukiewicz@callstack.com>
Date: Wed, 12 Feb 2025 11:01:14 +0100
Subject: [PATCH 5/6] fix sidebar utils

---
 src/libs/ReportUtils.ts        | 11 +++++++----
 tests/unit/SidebarUtilsTest.ts |  6 +++---
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts
index 2bb4510fa005..bac95d8ced45 100644
--- a/src/libs/ReportUtils.ts
+++ b/src/libs/ReportUtils.ts
@@ -6944,12 +6944,15 @@ function shouldDisplayViolationsRBRInLHN(report: OnyxEntry<Report>, transactionV
     if (!isCurrentUserSubmitter(report.reportID)) {
         return false;
     }
+    if (!report.policyID || !reportsByPolicyID) {
+        return false;
+    }
 
     // If any report has a violation, then it should have a RBR
-    const potentialReports = report.policyID ? reportsByPolicyID[report.policyID] : [];
-    return potentialReports.some(
-        (potentialReport) => hasViolations(potentialReport.reportID, transactionViolations) || hasWarningTypeViolations(potentialReport.reportID, transactionViolations),
-    );
+    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 a666d7711f1f..bb5f13d1d90c 100644
--- a/tests/unit/SidebarUtilsTest.ts
+++ b/tests/unit/SidebarUtilsTest.ts
@@ -58,12 +58,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,
             });
 

From f73f5e79fd3e42f987cc06011c7e5a3b68b19a66 Mon Sep 17 00:00:00 2001
From: Tomasz Misiukiewicz <tomasz.misiukiewicz@callstack.com>
Date: Wed, 12 Feb 2025 11:02:54 +0100
Subject: [PATCH 6/6] fix WorkspaceSettingsUtilsTest

---
 tests/unit/WorkspaceSettingsUtilsTest.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

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<ReportActions>),
                 ...(transactionViolations as OnyxCollection<TransactionViolations>),
                 ...(transactions as OnyxCollection<Transaction>),
-                session,
             });
 
             await waitForBatchedUpdates();
