-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathWorkspacesSettingsUtils.ts
209 lines (169 loc) · 7.83 KB
/
WorkspacesSettingsUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import Onyx from 'react-native-onyx';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, PolicyMembers, ReimbursementAccount, Report} from '@src/types/onyx';
import * as OptionsListUtils from './OptionsListUtils';
import {hasCustomUnitsError, hasPolicyError, hasPolicyMemberError} from './PolicyUtils';
import * as ReportActionsUtils from './ReportActionsUtils';
import * as ReportUtils from './ReportUtils';
type CheckingMethod = () => boolean;
let allReports: OnyxCollection<Report>;
type BrickRoad = ValueOf<typeof CONST.BRICK_ROAD_INDICATOR_STATUS> | undefined;
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => (allReports = value),
});
let allPolicies: OnyxCollection<Policy>;
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (value) => (allPolicies = value),
});
let allPolicyMembers: OnyxCollection<PolicyMembers>;
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY_MEMBERS,
waitForCollectionCallback: true,
callback: (val) => {
allPolicyMembers = val;
},
});
let reimbursementAccount: OnyxEntry<ReimbursementAccount>;
Onyx.connect({
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
callback: (val) => {
reimbursementAccount = val;
},
});
/**
* @param report
* @returns BrickRoad for the policy passed as a param
*/
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_INDICATOR_STATUS.ERROR;
}
// To determine if the report requires attention from the current user, we need to load the parent report action
let itemParentReportAction = {};
if (report.parentReportID) {
const itemParentReportActions = ReportActionsUtils.getAllReportActions(report.parentReportID);
itemParentReportAction = report.parentReportActionID ? itemParentReportActions[report.parentReportActionID] : {};
}
const reportOption = {...report, isUnread: ReportUtils.isUnread(report), isUnreadWithMention: ReportUtils.isUnreadWithMention(report)};
const shouldShowGreenDotIndicator = ReportUtils.requiresAttentionFromCurrentUser(reportOption, itemParentReportAction);
return shouldShowGreenDotIndicator ? CONST.BRICK_ROAD_INDICATOR_STATUS.INFO : undefined;
};
function hasGlobalWorkspaceSettingsRBR(policies: OnyxCollection<Policy>, policyMembers: OnyxCollection<PolicyMembers>) {
// When attempting to open a policy with an invalid policyID, the policy collection is updated to include policy objects with error information.
// Only policies displayed on the policy list page should be verified. Otherwise, the user will encounter an RBR unrelated to any policies on the list.
const cleanPolicies = Object.fromEntries(Object.entries(policies ?? {}).filter(([, policy]) => policy?.id));
const cleanAllPolicyMembers = Object.fromEntries(Object.entries(policyMembers ?? {}).filter(([, policyMemberValues]) => !!policyMemberValues));
const errorCheckingMethods: CheckingMethod[] = [
() => Object.values(cleanPolicies).some(hasPolicyError),
() => Object.values(cleanPolicies).some(hasCustomUnitsError),
() => Object.values(cleanAllPolicyMembers).some(hasPolicyMemberError),
() => Object.keys(reimbursementAccount?.errors ?? {}).length > 0,
];
return errorCheckingMethods.some((errorCheckingMethod) => errorCheckingMethod());
}
function hasWorkspaceSettingsRBR(policy: Policy) {
const policyMemberError = allPolicyMembers ? hasPolicyMemberError(allPolicyMembers[`${ONYXKEYS.COLLECTION.POLICY_MEMBERS}${policy.id}`]) : false;
return Object.keys(reimbursementAccount?.errors ?? {}).length > 0 || hasPolicyError(policy) || hasCustomUnitsError(policy) || policyMemberError;
}
function getChatTabBrickRoad(policyID?: string): BrickRoad | undefined {
if (!allReports) {
return undefined;
}
// If policyID is undefined, then all reports are checked whether they contain any brick road
const policyReports = policyID ? Object.values(allReports).filter((report) => report?.policyID === policyID) : Object.values(allReports);
let hasChatTabGBR = false;
const hasChatTabRBR = policyReports.some((report) => {
const brickRoad = report ? getBrickRoadForPolicy(report) : undefined;
if (!hasChatTabGBR && brickRoad === CONST.BRICK_ROAD_INDICATOR_STATUS.INFO) {
hasChatTabGBR = true;
}
return brickRoad === CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR;
});
if (hasChatTabRBR) {
return CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR;
}
if (hasChatTabGBR) {
return CONST.BRICK_ROAD_INDICATOR_STATUS.INFO;
}
return undefined;
}
function checkIfWorkspaceSettingsTabHasRBR(policyID?: string) {
if (!policyID) {
return hasGlobalWorkspaceSettingsRBR(allPolicies, allPolicyMembers);
}
const policy = allPolicies ? allPolicies[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`] : null;
if (!policy) {
return false;
}
return hasWorkspaceSettingsRBR(policy);
}
/**
* @returns a map where the keys are policyIDs and the values are BrickRoads for each policy
*/
function getWorkspacesBrickRoads(): Record<string, BrickRoad> {
if (!allReports) {
return {};
}
// The key in this map is the workspace id
const workspacesBrickRoadsMap: Record<string, BrickRoad> = {};
Object.values(allPolicies ?? {}).forEach((policy) => {
// Only policies which user has access to on the list should be checked. Policies that don't have an ID and contain only information about the errors aren't displayed anywhere.
if (!policy?.id) {
return;
}
if (hasWorkspaceSettingsRBR(policy)) {
workspacesBrickRoadsMap[policy.id] = CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR;
}
});
Object.values(allReports).forEach((report) => {
const policyID = report?.policyID ?? CONST.POLICY.EMPTY;
if (!report || workspacesBrickRoadsMap[policyID] === CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR) {
return;
}
const workspaceBrickRoad = getBrickRoadForPolicy(report);
if (!workspaceBrickRoad && !!workspacesBrickRoadsMap[policyID]) {
return;
}
workspacesBrickRoadsMap[policyID] = workspaceBrickRoad;
});
return workspacesBrickRoadsMap;
}
/**
* @returns a map where the keys are policyIDs and the values are truthy booleans if policy has unread content
*/
function getWorkspacesUnreadStatuses(): Record<string, boolean> {
if (!allReports) {
return {};
}
const workspacesUnreadStatuses: Record<string, boolean> = {};
Object.values(allReports).forEach((report) => {
const policyID = report?.policyID;
if (!policyID || workspacesUnreadStatuses[policyID]) {
return;
}
// When the only message of a report is deleted lastVisibileActionCreated is not reset leading to wrongly
// setting it Unread so we add additional condition here to avoid read workspace indicator from being bold.
workspacesUnreadStatuses[policyID] = ReportUtils.isUnread(report) && !!report.lastActorAccountID;
});
return workspacesUnreadStatuses;
}
export {
getBrickRoadForPolicy,
getWorkspacesBrickRoads,
getWorkspacesUnreadStatuses,
hasGlobalWorkspaceSettingsRBR,
checkIfWorkspaceSettingsTabHasRBR,
hasWorkspaceSettingsRBR,
getChatTabBrickRoad,
};
export type {BrickRoad};