-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
PolicyUtils.ts
387 lines (336 loc) · 14.2 KB
/
PolicyUtils.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import Str from 'expensify-common/lib/str';
import type {OnyxCollection, OnyxEntry} 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 ROUTES from '@src/ROUTES';
import type {Policy, PolicyCategories, PolicyEmployeeList, PolicyTagList, PolicyTags, TaxRate} from '@src/types/onyx';
import type {PolicyFeatureName, Rate} from '@src/types/onyx/Policy';
import type {EmptyObject} from '@src/types/utils/EmptyObject';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import getPolicyIDFromState from './Navigation/getPolicyIDFromState';
import Navigation, {navigationRef} from './Navigation/Navigation';
import type {RootStackParamList, State} from './Navigation/types';
import {getPersonalDetailByEmail} from './PersonalDetailsUtils';
type MemberEmailsToAccountIDs = Record<string, number>;
let allPolicies: OnyxCollection<Policy>;
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (value) => (allPolicies = value),
});
/**
* Filter out the active policies, which will exclude policies with pending deletion
* These are policies that we can use to create reports with in NewDot.
*/
function getActivePolicies(policies: OnyxCollection<Policy>): Policy[] | undefined {
return Object.values(policies ?? {}).filter<Policy>(
(policy): policy is Policy => policy !== null && policy && policy.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && !!policy.name && !!policy.id,
);
}
/**
* Checks if we have any errors stored within the policy?.employeeList. Determines whether we should show a red brick road error or not.
*/
function hasEmployeeListError(policy: OnyxEntry<Policy>): boolean {
return Object.values(policy?.employeeList ?? {}).some((employee) => Object.keys(employee?.errors ?? {}).length > 0);
}
/**
* Check if the policy has any tax rate errors.
*/
function hasTaxRateError(policy: OnyxEntry<Policy>): boolean {
return Object.values(policy?.taxRates?.taxes ?? {}).some((taxRate) => Object.keys(taxRate?.errors ?? {}).length > 0 || Object.values(taxRate?.errorFields ?? {}).some(Boolean));
}
/**
* Check if the policy has any errors within the categories.
*/
function hasPolicyCategoriesError(policyCategories: OnyxEntry<PolicyCategories>): boolean {
return Object.keys(policyCategories ?? {}).some((categoryName) => Object.keys(policyCategories?.[categoryName]?.errors ?? {}).length > 0);
}
/**
* Check if the policy has any error fields.
*/
function hasPolicyErrorFields(policy: OnyxEntry<Policy>): boolean {
return Object.values(policy?.errorFields ?? {}).some((fieldErrors) => Object.keys(fieldErrors ?? {}).length > 0);
}
/**
* Check if the policy has any errors, and if it doesn't, then check if it has any error fields.
*/
function hasPolicyError(policy: OnyxEntry<Policy>): boolean {
return Object.keys(policy?.errors ?? {}).length > 0 ? true : hasPolicyErrorFields(policy);
}
/**
* Checks if we have any errors stored within the policy custom units.
*/
function hasCustomUnitsError(policy: OnyxEntry<Policy>): boolean {
return Object.keys(policy?.customUnits?.errors ?? {}).length > 0;
}
function getNumericValue(value: number | string, toLocaleDigit: (arg: string) => string): number | string {
const numValue = parseFloat(value.toString().replace(toLocaleDigit('.'), '.'));
if (Number.isNaN(numValue)) {
return NaN;
}
return numValue.toFixed(CONST.CUSTOM_UNITS.RATE_DECIMALS);
}
function getRateDisplayValue(value: number, toLocaleDigit: (arg: string) => string): string {
const numValue = getNumericValue(value, toLocaleDigit);
if (Number.isNaN(numValue)) {
return '';
}
return numValue.toString().replace('.', toLocaleDigit('.')).substring(0, value.toString().length);
}
function getUnitRateValue(toLocaleDigit: (arg: string) => string, customUnitRate?: Rate) {
return getRateDisplayValue((customUnitRate?.rate ?? 0) / CONST.POLICY.CUSTOM_UNIT_RATE_BASE_OFFSET, toLocaleDigit);
}
/**
* Get the brick road indicator status for a policy. The policy has an error status if there is a policy member error, a custom unit error or a field error.
*/
function getPolicyBrickRoadIndicatorStatus(policy: OnyxEntry<Policy>): ValueOf<typeof CONST.BRICK_ROAD_INDICATOR_STATUS> | undefined {
if (hasEmployeeListError(policy) || hasCustomUnitsError(policy) || hasPolicyErrorFields(policy)) {
return CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR;
}
return undefined;
}
/**
* Check if the policy can be displayed
* If offline, always show the policy pending deletion.
* If online, show the policy pending deletion only if there is an error.
* Note: Using a local ONYXKEYS.NETWORK subscription will cause a delay in
* updating the screen. Passing the offline status from the component.
*/
function shouldShowPolicy(policy: OnyxEntry<Policy>, isOffline: boolean): boolean {
return (
!!policy &&
(policy?.isPolicyExpenseChatEnabled || Boolean(policy?.isJoinRequestPending)) &&
(isOffline || policy?.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || Object.keys(policy.errors ?? {}).length > 0)
);
}
function isExpensifyTeam(email: string | undefined): boolean {
const emailDomain = Str.extractEmailDomain(email ?? '');
return emailDomain === CONST.EXPENSIFY_PARTNER_NAME || emailDomain === CONST.EMAIL.GUIDES_DOMAIN;
}
/**
* Checks if the current user is an admin of the policy.
*/
const isPolicyAdmin = (policy: OnyxEntry<Policy> | EmptyObject): boolean => policy?.role === CONST.POLICY.ROLE.ADMIN;
/**
* Checks if the policy is a free group policy.
*/
const isFreeGroupPolicy = (policy: OnyxEntry<Policy> | EmptyObject): boolean => policy?.type === CONST.POLICY.TYPE.FREE;
const isPolicyEmployee = (policyID: string, policies: OnyxCollection<Policy>): boolean => Object.values(policies ?? {}).some((policy) => policy?.id === policyID);
/**
* Checks if the current user is an owner (creator) of the policy.
*/
const isPolicyOwner = (policy: OnyxEntry<Policy>, currentUserAccountID: number): boolean => policy?.ownerAccountID === currentUserAccountID;
/**
* Create an object mapping member emails to their accountIDs. Filter for members without errors, and get the login email from the personalDetail object using the accountID.
*
* We only return members without errors. Otherwise, the members with errors would immediately be removed before the user has a chance to read the error.
*/
function getMemberAccountIDsForWorkspace(employeeList: PolicyEmployeeList | undefined): MemberEmailsToAccountIDs {
const members = employeeList ?? {};
const memberEmailsToAccountIDs: MemberEmailsToAccountIDs = {};
Object.keys(members).forEach((email) => {
const member = members?.[email];
if (Object.keys(member?.errors ?? {})?.length > 0) {
return;
}
const personalDetail = getPersonalDetailByEmail(email);
if (!personalDetail?.login) {
return;
}
memberEmailsToAccountIDs[email] = Number(personalDetail.accountID);
});
return memberEmailsToAccountIDs;
}
/**
* Get login list that we should not show in the workspace invite options
*/
function getIneligibleInvitees(employeeList?: PolicyEmployeeList): string[] {
const policyEmployeeList = employeeList ?? {};
const memberEmailsToExclude: string[] = [...CONST.EXPENSIFY_EMAILS];
Object.keys(policyEmployeeList).forEach((email) => {
const policyEmployee = policyEmployeeList?.[email];
// Policy members that are pending delete or have errors are not valid and we should show them in the invite options (don't exclude them).
if (policyEmployee?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || Object.keys(policyEmployee?.errors ?? {}).length > 0) {
return;
}
if (!email) {
return;
}
memberEmailsToExclude.push(email);
});
return memberEmailsToExclude;
}
function getSortedTagKeys(policyTagList: OnyxEntry<PolicyTagList>): Array<keyof PolicyTagList> {
if (isEmptyObject(policyTagList)) {
return [];
}
return Object.keys(policyTagList).sort((key1, key2) => policyTagList[key1].orderWeight - policyTagList[key2].orderWeight);
}
/**
* Gets a tag name of policy tags based on a tag's orderWeight.
*/
function getTagListName(policyTagList: OnyxEntry<PolicyTagList>, orderWeight: number): string {
if (isEmptyObject(policyTagList)) {
return '';
}
return Object.values(policyTagList).find((tag) => tag.orderWeight === orderWeight)?.name ?? '';
}
/**
* Gets all tag lists of a policy
*/
function getTagLists(policyTagList: OnyxEntry<PolicyTagList>): Array<PolicyTagList[keyof PolicyTagList]> {
if (isEmptyObject(policyTagList)) {
return [];
}
return Object.values(policyTagList)
.filter((policyTagListValue) => policyTagListValue !== null)
.sort((tagA, tagB) => tagA.orderWeight - tagB.orderWeight);
}
/**
* Gets a tag list of a policy by a tag index
*/
function getTagList(policyTagList: OnyxEntry<PolicyTagList>, tagIndex: number): PolicyTagList[keyof PolicyTagList] {
const tagLists = getTagLists(policyTagList);
return (
tagLists[tagIndex] ?? {
name: '',
required: false,
tags: {},
}
);
}
/**
* Cleans up escaping of colons (used to create multi-level tags, e.g. "Parent: Child") in the tag name we receive from the backend
*/
function getCleanedTagName(tag: string) {
return tag?.replace(/\\{1,2}:/g, CONST.COLON);
}
/**
* Gets a count of enabled tags of a policy
*/
function getCountOfEnabledTagsOfList(policyTags: PolicyTags) {
return Object.values(policyTags).filter((policyTag) => policyTag.enabled).length;
}
function isPendingDeletePolicy(policy: OnyxEntry<Policy>): boolean {
return policy?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE;
}
function isPaidGroupPolicy(policy: OnyxEntry<Policy> | EmptyObject): boolean {
return policy?.type === CONST.POLICY.TYPE.TEAM || policy?.type === CONST.POLICY.TYPE.CORPORATE;
}
function isTaxTrackingEnabled(isPolicyExpenseChat: boolean, policy: OnyxEntry<Policy>): boolean {
return (isPolicyExpenseChat && (policy?.tax?.trackingEnabled ?? policy?.isTaxTrackingEnabled)) ?? false;
}
/**
* Checks if policy's scheduled submit / auto reporting frequency is "instant".
* Note: Free policies have "instant" submit always enabled.
*/
function isInstantSubmitEnabled(policy: OnyxEntry<Policy> | EmptyObject): boolean {
return policy?.type === CONST.POLICY.TYPE.FREE || (policy?.autoReporting === true && policy?.autoReportingFrequency === CONST.POLICY.AUTO_REPORTING_FREQUENCIES.INSTANT);
}
/**
* Checks if policy's approval mode is "optional", a.k.a. "Submit & Close"
*/
function isSubmitAndClose(policy: OnyxEntry<Policy> | EmptyObject): boolean {
return policy?.approvalMode === CONST.POLICY.APPROVAL_MODE.OPTIONAL;
}
function extractPolicyIDFromPath(path: string) {
return path.match(CONST.REGEX.POLICY_ID_FROM_PATH)?.[1];
}
/**
* Whether the policy has active accounting integration connections
*/
function hasAccountingConnections(policy: OnyxEntry<Policy>) {
return Boolean(policy?.connections);
}
function getPathWithoutPolicyID(path: string) {
return path.replace(CONST.REGEX.PATH_WITHOUT_POLICY_ID, '/');
}
function getPolicyEmployeeListByIdWithoutCurrentUser(policies: OnyxCollection<Pick<Policy, 'employeeList'>>, currentPolicyID?: string, currentUserAccountID?: number) {
const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${currentPolicyID}`] ?? null;
const policyMemberEmailsToAccountIDs = getMemberAccountIDsForWorkspace(policy?.employeeList);
return Object.values(policyMemberEmailsToAccountIDs)
.map((policyMemberAccountID) => Number(policyMemberAccountID))
.filter((policyMemberAccountID) => policyMemberAccountID !== currentUserAccountID);
}
function goBackFromInvalidPolicy() {
Navigation.navigate(ROUTES.SETTINGS_WORKSPACES);
}
/** Get a tax with given ID from policy */
function getTaxByID(policy: OnyxEntry<Policy>, taxID: string): TaxRate | undefined {
return policy?.taxRates?.taxes?.[taxID];
}
/**
* Whether the tax rate can be deleted and disabled
*/
function canEditTaxRate(policy: Policy, taxID: string): boolean {
return policy.taxRates?.defaultExternalID !== taxID;
}
function isPolicyFeatureEnabled(policy: OnyxEntry<Policy> | EmptyObject, featureName: PolicyFeatureName): boolean {
if (featureName === CONST.POLICY.MORE_FEATURES.ARE_TAXES_ENABLED) {
return Boolean(policy?.tax?.trackingEnabled);
}
return Boolean(policy?.[featureName]);
}
function getPersonalPolicy() {
return Object.values(allPolicies ?? {}).find((policy) => policy?.type === CONST.POLICY.TYPE.PERSONAL);
}
/**
* Get the currently selected policy ID stored in the navigation state.
*/
function getPolicyIDFromNavigationState() {
return getPolicyIDFromState(navigationRef.getRootState() as State<RootStackParamList>);
}
/**
* Returns the policy of the report
*/
function getPolicy(policyID: string | undefined): Policy | EmptyObject {
if (!allPolicies || !policyID) {
return {};
}
return allPolicies[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`] ?? {};
}
export {
getActivePolicies,
hasAccountingConnections,
hasEmployeeListError,
hasPolicyError,
hasPolicyErrorFields,
hasCustomUnitsError,
getNumericValue,
getUnitRateValue,
getPolicyBrickRoadIndicatorStatus,
shouldShowPolicy,
isExpensifyTeam,
isInstantSubmitEnabled,
isFreeGroupPolicy,
isPolicyAdmin,
isTaxTrackingEnabled,
isSubmitAndClose,
getMemberAccountIDsForWorkspace,
getIneligibleInvitees,
getTagLists,
getTagListName,
getSortedTagKeys,
canEditTaxRate,
getTagList,
getCleanedTagName,
getCountOfEnabledTagsOfList,
isPendingDeletePolicy,
isPolicyEmployee,
isPolicyOwner,
isPaidGroupPolicy,
extractPolicyIDFromPath,
getPathWithoutPolicyID,
getPolicyEmployeeListByIdWithoutCurrentUser,
goBackFromInvalidPolicy,
getPersonalPolicy,
isPolicyFeatureEnabled,
hasTaxRateError,
getTaxByID,
hasPolicyCategoriesError,
getPolicyIDFromNavigationState,
getPolicy,
};
export type {MemberEmailsToAccountIDs};