From 735fba52e7c3f7fb5648da3ecb43ccf000439564 Mon Sep 17 00:00:00 2001 From: Wojciech Boman Date: Mon, 18 Dec 2023 16:21:58 +0100 Subject: [PATCH 1/9] Add BrickRoadsUtils --- src/libs/BrickRoadsUtils.ts | 65 +++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/libs/BrickRoadsUtils.ts diff --git a/src/libs/BrickRoadsUtils.ts b/src/libs/BrickRoadsUtils.ts new file mode 100644 index 000000000000..63a03924bbf2 --- /dev/null +++ b/src/libs/BrickRoadsUtils.ts @@ -0,0 +1,65 @@ +import Onyx, {OnyxCollection} from 'react-native-onyx'; +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; +import {Report} from '@src/types/onyx'; +import * as OptionsListUtils from './OptionsListUtils'; +import * as ReportActionsUtils from './ReportActionsUtils'; +import * as ReportUtils from './ReportUtils'; + +let allReports: OnyxCollection; + +type BrickRoad = 'GBR' | 'RBR' | undefined; + +Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT, + waitForCollectionCallback: true, + callback: (value) => (allReports = value), +}); + +const getBrickRoadForPolicy = (policyReport: Report): BrickRoad | undefined => { + const policyReportAction = ReportActionsUtils.getAllReportActions(policyReport.reportID); + const reportErrors = OptionsListUtils.getAllReportErrors(policyReport, policyReportAction); + const brickRoadIndicator = Object.keys(reportErrors ?? {}).length !== 0 ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''; + if (brickRoadIndicator) { + return 'RBR'; + } + let itemParentReportAction = {}; + if (policyReport.parentReportID) { + const itemParentReportActions = ReportActionsUtils.getAllReportActions(policyReport.parentReportID); + itemParentReportAction = policyReport.parentReportActionID ? itemParentReportActions[policyReport.parentReportActionID] : {}; + } + const optionFromPolicyReport = {...policyReport, isUnread: ReportUtils.isUnread(policyReport), isUnreadWithMention: ReportUtils.isUnreadWithMention(policyReport)}; + const shouldShowGreenDotIndicator = ReportUtils.requiresAttentionFromCurrentUser(optionFromPolicyReport, itemParentReportAction); + return shouldShowGreenDotIndicator ? 'GBR' : undefined; +}; + +function getWorkspacesBrickRoads(): Record { + if (!allReports) { + return {}; + } + + const brickRoadsMap: Record = {}; + + if (!allReports) { + return brickRoadsMap; + } + + Object.keys(allReports).forEach((report) => { + const policyID = allReports?.[report]?.policyID; + const policyReport = allReports ? allReports[report] : null; + if (!policyID || !policyReport || brickRoadsMap[policyID] === 'RBR') { + return; + } + const policyBrickRoad = getBrickRoadForPolicy(policyReport); + + if (!policyBrickRoad && !!brickRoadsMap[policyID]) { + return; + } + + brickRoadsMap[policyID] = policyBrickRoad; + }); + + return brickRoadsMap; +} + +export {getBrickRoadForPolicy, getWorkspacesBrickRoads}; From 5cc30462927e0363bae26491eef8bfa9b3497976 Mon Sep 17 00:00:00 2001 From: Wojciech Boman Date: Mon, 18 Dec 2023 16:27:56 +0100 Subject: [PATCH 2/9] Refactor getBrickRoadForPolicy return type --- src/libs/BrickRoadsUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/BrickRoadsUtils.ts b/src/libs/BrickRoadsUtils.ts index 63a03924bbf2..03c11e8fcfd6 100644 --- a/src/libs/BrickRoadsUtils.ts +++ b/src/libs/BrickRoadsUtils.ts @@ -16,7 +16,7 @@ Onyx.connect({ callback: (value) => (allReports = value), }); -const getBrickRoadForPolicy = (policyReport: Report): BrickRoad | undefined => { +const getBrickRoadForPolicy = (policyReport: Report): BrickRoad => { const policyReportAction = ReportActionsUtils.getAllReportActions(policyReport.reportID); const reportErrors = OptionsListUtils.getAllReportErrors(policyReport, policyReportAction); const brickRoadIndicator = Object.keys(reportErrors ?? {}).length !== 0 ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''; From 27f5e48efbbd68d74c93a1b7bd4ec64f3eb6c970 Mon Sep 17 00:00:00 2001 From: Wojciech Boman Date: Mon, 18 Dec 2023 16:33:51 +0100 Subject: [PATCH 3/9] Refactor getBrickRoadForPolicy function --- src/libs/BrickRoadsUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/BrickRoadsUtils.ts b/src/libs/BrickRoadsUtils.ts index 03c11e8fcfd6..9fd8712c695e 100644 --- a/src/libs/BrickRoadsUtils.ts +++ b/src/libs/BrickRoadsUtils.ts @@ -19,8 +19,8 @@ Onyx.connect({ const getBrickRoadForPolicy = (policyReport: Report): BrickRoad => { const policyReportAction = ReportActionsUtils.getAllReportActions(policyReport.reportID); const reportErrors = OptionsListUtils.getAllReportErrors(policyReport, policyReportAction); - const brickRoadIndicator = Object.keys(reportErrors ?? {}).length !== 0 ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : ''; - if (brickRoadIndicator) { + const redBrickRoadIndicator = Object.keys(reportErrors ?? {}).length !== 0 ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined; + if (redBrickRoadIndicator) { return 'RBR'; } let itemParentReportAction = {}; From 0064b115f53009a17fe68ce8a787687303561b73 Mon Sep 17 00:00:00 2001 From: Wojciech Boman Date: Mon, 18 Dec 2023 17:25:44 +0100 Subject: [PATCH 4/9] Refactor getWorkspacesBrickRoads function --- src/libs/BrickRoadsUtils.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/libs/BrickRoadsUtils.ts b/src/libs/BrickRoadsUtils.ts index 9fd8712c695e..e4180e5044b8 100644 --- a/src/libs/BrickRoadsUtils.ts +++ b/src/libs/BrickRoadsUtils.ts @@ -40,10 +40,6 @@ function getWorkspacesBrickRoads(): Record { const brickRoadsMap: Record = {}; - if (!allReports) { - return brickRoadsMap; - } - Object.keys(allReports).forEach((report) => { const policyID = allReports?.[report]?.policyID; const policyReport = allReports ? allReports[report] : null; From 7af0ac7dc2bad3e4739fe8db3a4fcdb445fd4c40 Mon Sep 17 00:00:00 2001 From: Wojciech Boman Date: Mon, 18 Dec 2023 17:28:56 +0100 Subject: [PATCH 5/9] Add export type BrickRoad --- src/libs/BrickRoadsUtils.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/BrickRoadsUtils.ts b/src/libs/BrickRoadsUtils.ts index e4180e5044b8..0d292fd808cc 100644 --- a/src/libs/BrickRoadsUtils.ts +++ b/src/libs/BrickRoadsUtils.ts @@ -59,3 +59,4 @@ function getWorkspacesBrickRoads(): Record { } export {getBrickRoadForPolicy, getWorkspacesBrickRoads}; +export type {BrickRoad}; From e6f59411014f3ccb168446cf5c117d2863bd1923 Mon Sep 17 00:00:00 2001 From: Wojciech Boman Date: Tue, 19 Dec 2023 17:05:04 +0100 Subject: [PATCH 6/9] Refactor BrickRoad type, add commments in BrickRoadsUtils --- src/CONST.ts | 4 ++++ src/libs/BrickRoadsUtils.ts | 29 +++++++++++++++++++---------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index 2e2f591f7117..6aa89d951ec9 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -3034,6 +3034,10 @@ const CONST = { DEFAULT: 5, CAROUSEL: 3, }, + BRICK_ROAD: { + GBR: 'GBR', + RBR: 'RBR', + }, } as const; export default CONST; diff --git a/src/libs/BrickRoadsUtils.ts b/src/libs/BrickRoadsUtils.ts index 0d292fd808cc..8bbf1d4c437b 100644 --- a/src/libs/BrickRoadsUtils.ts +++ b/src/libs/BrickRoadsUtils.ts @@ -1,4 +1,5 @@ import Onyx, {OnyxCollection} from 'react-native-onyx'; +import {ValueOf} from 'type-fest'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import {Report} from '@src/types/onyx'; @@ -8,7 +9,7 @@ import * as ReportUtils from './ReportUtils'; let allReports: OnyxCollection; -type BrickRoad = 'GBR' | 'RBR' | undefined; +type BrickRoad = ValueOf | undefined; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, @@ -16,12 +17,16 @@ Onyx.connect({ callback: (value) => (allReports = value), }); +/** + * @param policyReport + * @returns BrickRoad for the policy passed as a param + */ const getBrickRoadForPolicy = (policyReport: Report): BrickRoad => { const policyReportAction = ReportActionsUtils.getAllReportActions(policyReport.reportID); const reportErrors = OptionsListUtils.getAllReportErrors(policyReport, policyReportAction); - const redBrickRoadIndicator = Object.keys(reportErrors ?? {}).length !== 0 ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined; - if (redBrickRoadIndicator) { - return 'RBR'; + const doesReportContainErrors = Object.keys(reportErrors ?? {}).length !== 0 ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined; + if (doesReportContainErrors) { + return CONST.BRICK_ROAD.RBR; } let itemParentReportAction = {}; if (policyReport.parentReportID) { @@ -30,32 +35,36 @@ const getBrickRoadForPolicy = (policyReport: Report): BrickRoad => { } const optionFromPolicyReport = {...policyReport, isUnread: ReportUtils.isUnread(policyReport), isUnreadWithMention: ReportUtils.isUnreadWithMention(policyReport)}; const shouldShowGreenDotIndicator = ReportUtils.requiresAttentionFromCurrentUser(optionFromPolicyReport, itemParentReportAction); - return shouldShowGreenDotIndicator ? 'GBR' : undefined; + return shouldShowGreenDotIndicator ? CONST.BRICK_ROAD.GBR : undefined; }; +/** + * @returns a map where the keys are policyIDs and the values are BrickRoads for each policy + */ function getWorkspacesBrickRoads(): Record { if (!allReports) { return {}; } - const brickRoadsMap: Record = {}; + //The key in this map is the workspace id + const workspacesBrickRoadsMap: Record = {}; Object.keys(allReports).forEach((report) => { const policyID = allReports?.[report]?.policyID; const policyReport = allReports ? allReports[report] : null; - if (!policyID || !policyReport || brickRoadsMap[policyID] === 'RBR') { + if (!policyID || !policyReport || workspacesBrickRoadsMap[policyID] === CONST.BRICK_ROAD.RBR) { return; } const policyBrickRoad = getBrickRoadForPolicy(policyReport); - if (!policyBrickRoad && !!brickRoadsMap[policyID]) { + if (!policyBrickRoad && !!workspacesBrickRoadsMap[policyID]) { return; } - brickRoadsMap[policyID] = policyBrickRoad; + workspacesBrickRoadsMap[policyID] = policyBrickRoad; }); - return brickRoadsMap; + return workspacesBrickRoadsMap; } export {getBrickRoadForPolicy, getWorkspacesBrickRoads}; From 2f3b3bba63ff8334e95d19a3f4d06f7378b2f592 Mon Sep 17 00:00:00 2001 From: Wojciech Boman Date: Tue, 19 Dec 2023 17:10:03 +0100 Subject: [PATCH 7/9] Fix lint in BrickRoadsUtils --- src/libs/BrickRoadsUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/BrickRoadsUtils.ts b/src/libs/BrickRoadsUtils.ts index 8bbf1d4c437b..ceee9b570268 100644 --- a/src/libs/BrickRoadsUtils.ts +++ b/src/libs/BrickRoadsUtils.ts @@ -46,7 +46,7 @@ function getWorkspacesBrickRoads(): Record { return {}; } - //The key in this map is the workspace id + // The key in this map is the workspace id const workspacesBrickRoadsMap: Record = {}; Object.keys(allReports).forEach((report) => { From e53d0c2ccf99888f8fab05d9dd7683e0896c3874 Mon Sep 17 00:00:00 2001 From: Wojciech Boman Date: Thu, 21 Dec 2023 09:44:16 +0100 Subject: [PATCH 8/9] Refactor BrickRoadsUtils --- src/libs/BrickRoadsUtils.ts | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/libs/BrickRoadsUtils.ts b/src/libs/BrickRoadsUtils.ts index ceee9b570268..5116ce4f2e92 100644 --- a/src/libs/BrickRoadsUtils.ts +++ b/src/libs/BrickRoadsUtils.ts @@ -18,23 +18,25 @@ Onyx.connect({ }); /** - * @param policyReport + * @param report * @returns BrickRoad for the policy passed as a param */ -const getBrickRoadForPolicy = (policyReport: Report): BrickRoad => { - const policyReportAction = ReportActionsUtils.getAllReportActions(policyReport.reportID); - const reportErrors = OptionsListUtils.getAllReportErrors(policyReport, policyReportAction); +const getBrickRoadForPolicy = (report: Report): BrickRoad => { + const reportActions = ReportActionsUtils.getAllReportActions(report.reportID); + const reportErrors = OptionsListUtils.getAllReportErrors(report, reportActions); const doesReportContainErrors = Object.keys(reportErrors ?? {}).length !== 0 ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined; if (doesReportContainErrors) { return CONST.BRICK_ROAD.RBR; } + + // To determine if the report requires attention from the current user, we need to load the parent report action let itemParentReportAction = {}; - if (policyReport.parentReportID) { - const itemParentReportActions = ReportActionsUtils.getAllReportActions(policyReport.parentReportID); - itemParentReportAction = policyReport.parentReportActionID ? itemParentReportActions[policyReport.parentReportActionID] : {}; + if (report.parentReportID) { + const itemParentReportActions = ReportActionsUtils.getAllReportActions(report.parentReportID); + itemParentReportAction = report.parentReportActionID ? itemParentReportActions[report.parentReportActionID] : {}; } - const optionFromPolicyReport = {...policyReport, isUnread: ReportUtils.isUnread(policyReport), isUnreadWithMention: ReportUtils.isUnreadWithMention(policyReport)}; - const shouldShowGreenDotIndicator = ReportUtils.requiresAttentionFromCurrentUser(optionFromPolicyReport, itemParentReportAction); + const reportOption = {...report, isUnread: ReportUtils.isUnread(report), isUnreadWithMention: ReportUtils.isUnreadWithMention(report)}; + const shouldShowGreenDotIndicator = ReportUtils.requiresAttentionFromCurrentUser(reportOption, itemParentReportAction); return shouldShowGreenDotIndicator ? CONST.BRICK_ROAD.GBR : undefined; }; @@ -55,13 +57,13 @@ function getWorkspacesBrickRoads(): Record { if (!policyID || !policyReport || workspacesBrickRoadsMap[policyID] === CONST.BRICK_ROAD.RBR) { return; } - const policyBrickRoad = getBrickRoadForPolicy(policyReport); + const workspaceBrickRoad = getBrickRoadForPolicy(policyReport); - if (!policyBrickRoad && !!workspacesBrickRoadsMap[policyID]) { + if (!workspaceBrickRoad && !!workspacesBrickRoadsMap[policyID]) { return; } - workspacesBrickRoadsMap[policyID] = policyBrickRoad; + workspacesBrickRoadsMap[policyID] = workspaceBrickRoad; }); return workspacesBrickRoadsMap; From 000049e8486ffd91b67632c7f1d76bc295ea4784 Mon Sep 17 00:00:00 2001 From: Wojciech Boman Date: Wed, 10 Jan 2024 15:36:11 +0100 Subject: [PATCH 9/9] Fix lint in BrickRoadsUtils --- src/libs/BrickRoadsUtils.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libs/BrickRoadsUtils.ts b/src/libs/BrickRoadsUtils.ts index 5116ce4f2e92..db7cc40a7940 100644 --- a/src/libs/BrickRoadsUtils.ts +++ b/src/libs/BrickRoadsUtils.ts @@ -1,8 +1,9 @@ -import Onyx, {OnyxCollection} from 'react-native-onyx'; -import {ValueOf} from 'type-fest'; +import type {OnyxCollection} from 'react-native-onyx'; +import Onyx from 'react-native-onyx'; +import type {ValueOf} from 'type-fest'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import {Report} from '@src/types/onyx'; +import type {Report} from '@src/types/onyx'; import * as OptionsListUtils from './OptionsListUtils'; import * as ReportActionsUtils from './ReportActionsUtils'; import * as ReportUtils from './ReportUtils';