-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathViolationsUtils.ts
353 lines (325 loc) · 16.2 KB
/
ViolationsUtils.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
import reject from 'lodash/reject';
import Onyx from 'react-native-onyx';
import type {OnyxUpdate} from 'react-native-onyx';
import type {LocaleContextProps} from '@components/LocaleContextProvider';
import {getDistanceRateCustomUnitRate, getSortedTagKeys} from '@libs/PolicyUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Policy, PolicyCategories, PolicyTagLists, Transaction, TransactionViolation, ViolationName} from '@src/types/onyx';
/**
* Calculates tag out of policy and missing tag violations for the given transaction
*/
function getTagViolationsForSingleLevelTags(
updatedTransaction: Transaction,
transactionViolations: TransactionViolation[],
policyRequiresTags: boolean,
policyTagList: PolicyTagLists,
): TransactionViolation[] {
const policyTagKeys = Object.keys(policyTagList);
const policyTagListName = policyTagKeys.at(0) ?? '';
const policyTags = policyTagList[policyTagListName]?.tags;
const hasTagOutOfPolicyViolation = transactionViolations.some((violation) => violation.name === CONST.VIOLATIONS.TAG_OUT_OF_POLICY);
const hasMissingTagViolation = transactionViolations.some((violation) => violation.name === CONST.VIOLATIONS.MISSING_TAG);
const isTagInPolicy = policyTags ? !!policyTags[updatedTransaction.tag ?? '']?.enabled : false;
let newTransactionViolations = [...transactionViolations];
// Add 'tagOutOfPolicy' violation if tag is not in policy
if (!hasTagOutOfPolicyViolation && updatedTransaction.tag && !isTagInPolicy) {
newTransactionViolations.push({name: CONST.VIOLATIONS.TAG_OUT_OF_POLICY, type: CONST.VIOLATION_TYPES.VIOLATION});
}
// Remove 'tagOutOfPolicy' violation if tag is in policy
if (hasTagOutOfPolicyViolation && updatedTransaction.tag && isTagInPolicy) {
newTransactionViolations = reject(newTransactionViolations, {name: CONST.VIOLATIONS.TAG_OUT_OF_POLICY});
}
// Remove 'missingTag' violation if tag is valid according to policy
if (hasMissingTagViolation && isTagInPolicy) {
newTransactionViolations = reject(newTransactionViolations, {name: CONST.VIOLATIONS.MISSING_TAG});
}
// Add 'missingTag violation' if tag is required and not set
if (!hasMissingTagViolation && !updatedTransaction.tag && policyRequiresTags) {
newTransactionViolations.push({name: CONST.VIOLATIONS.MISSING_TAG, type: CONST.VIOLATION_TYPES.VIOLATION});
}
return newTransactionViolations;
}
/**
* Calculates missing tag violations for policies with dependent tags
*/
function getTagViolationsForDependentTags(policyTagList: PolicyTagLists, transactionViolations: TransactionViolation[], tagName: string) {
const tagViolations = [...transactionViolations];
if (!tagName) {
Object.values(policyTagList).forEach((tagList) =>
tagViolations.push({
name: CONST.VIOLATIONS.MISSING_TAG,
type: CONST.VIOLATION_TYPES.VIOLATION,
data: {tagName: tagList.name},
}),
);
} else {
const tags = TransactionUtils.getTagArrayFromName(tagName);
if (Object.keys(policyTagList).length !== tags.length || tags.includes('')) {
tagViolations.push({
name: CONST.VIOLATIONS.ALL_TAG_LEVELS_REQUIRED,
type: CONST.VIOLATION_TYPES.VIOLATION,
data: {},
});
}
}
return tagViolations;
}
/**
* Calculates missing tag violations for policies with independent tags
*/
function getTagViolationForIndependentTags(policyTagList: PolicyTagLists, transactionViolations: TransactionViolation[], transaction: Transaction) {
const policyTagKeys = getSortedTagKeys(policyTagList);
const selectedTags = TransactionUtils.getTagArrayFromName(transaction?.tag ?? '');
let newTransactionViolations = [...transactionViolations];
newTransactionViolations = newTransactionViolations.filter(
(violation) => violation.name !== CONST.VIOLATIONS.SOME_TAG_LEVELS_REQUIRED && violation.name !== CONST.VIOLATIONS.TAG_OUT_OF_POLICY,
);
// We first get the errorIndexes for someTagLevelsRequired. If it's not empty, we puth SOME_TAG_LEVELS_REQUIRED in Onyx.
// Otherwise, we put TAG_OUT_OF_POLICY in Onyx (when applicable)
const errorIndexes = [];
for (let i = 0; i < policyTagKeys.length; i++) {
const isTagRequired = policyTagList[policyTagKeys[i]].required ?? true;
const isTagSelected = !!selectedTags.at(i);
if (isTagRequired && (!isTagSelected || (selectedTags.length === 1 && selectedTags.at(0) === ''))) {
errorIndexes.push(i);
}
}
if (errorIndexes.length !== 0) {
newTransactionViolations.push({
name: CONST.VIOLATIONS.SOME_TAG_LEVELS_REQUIRED,
type: CONST.VIOLATION_TYPES.VIOLATION,
data: {
errorIndexes,
},
});
} else {
let hasInvalidTag = false;
for (let i = 0; i < policyTagKeys.length; i++) {
const selectedTag = selectedTags.at(i);
const tags = policyTagList[policyTagKeys[i]].tags;
const isTagInPolicy = Object.values(tags).some((tag) => tag.name === selectedTag && !!tag.enabled);
if (!isTagInPolicy && selectedTag) {
newTransactionViolations.push({
name: CONST.VIOLATIONS.TAG_OUT_OF_POLICY,
type: CONST.VIOLATION_TYPES.VIOLATION,
data: {
tagName: policyTagKeys.at(i),
},
});
hasInvalidTag = true;
break;
}
}
if (!hasInvalidTag) {
newTransactionViolations = reject(newTransactionViolations, {
name: CONST.VIOLATIONS.TAG_OUT_OF_POLICY,
});
}
}
return newTransactionViolations;
}
/**
* Calculates tag violations for a transaction on a policy with multi level tags
*/
function getTagViolationsForMultiLevelTags(
updatedTransaction: Transaction,
transactionViolations: TransactionViolation[],
policyTagList: PolicyTagLists,
hasDependentTags: boolean,
): TransactionViolation[] {
const tagViolations = [
CONST.VIOLATIONS.SOME_TAG_LEVELS_REQUIRED,
CONST.VIOLATIONS.TAG_OUT_OF_POLICY,
CONST.VIOLATIONS.MISSING_TAG,
CONST.VIOLATIONS.ALL_TAG_LEVELS_REQUIRED,
] as ViolationName[];
const filteredTransactionViolations = transactionViolations.filter((violation) => !tagViolations.includes(violation.name));
if (hasDependentTags) {
return getTagViolationsForDependentTags(policyTagList, filteredTransactionViolations, updatedTransaction.tag ?? '');
}
return getTagViolationForIndependentTags(policyTagList, filteredTransactionViolations, updatedTransaction);
}
const ViolationsUtils = {
/**
* Checks a transaction for policy violations and returns an object with Onyx method, key and updated transaction
* violations.
*/
getViolationsOnyxData(
updatedTransaction: Transaction,
transactionViolations: TransactionViolation[],
policy: Policy,
policyTagList: PolicyTagLists,
policyCategories: PolicyCategories,
hasDependentTags: boolean,
): OnyxUpdate {
const isPartialTransaction = TransactionUtils.isPartialMerchant(TransactionUtils.getMerchant(updatedTransaction)) && TransactionUtils.isAmountMissing(updatedTransaction);
if (isPartialTransaction) {
return {
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${updatedTransaction.transactionID}`,
value: transactionViolations,
};
}
let newTransactionViolations = [...transactionViolations];
// Calculate client-side category violations
const policyRequiresCategories = !!policy.requiresCategory;
if (policyRequiresCategories) {
const hasCategoryOutOfPolicyViolation = transactionViolations.some((violation) => violation.name === 'categoryOutOfPolicy');
const hasMissingCategoryViolation = transactionViolations.some((violation) => violation.name === 'missingCategory');
const categoryKey = updatedTransaction.category;
const isCategoryInPolicy = categoryKey ? policyCategories?.[categoryKey]?.enabled : false;
// Add 'categoryOutOfPolicy' violation if category is not in policy
if (!hasCategoryOutOfPolicyViolation && categoryKey && !isCategoryInPolicy) {
newTransactionViolations.push({name: 'categoryOutOfPolicy', type: CONST.VIOLATION_TYPES.VIOLATION});
}
// Remove 'categoryOutOfPolicy' violation if category is in policy
if (hasCategoryOutOfPolicyViolation && updatedTransaction.category && isCategoryInPolicy) {
newTransactionViolations = reject(newTransactionViolations, {name: 'categoryOutOfPolicy'});
}
// Remove 'missingCategory' violation if category is valid according to policy
if (hasMissingCategoryViolation && isCategoryInPolicy) {
newTransactionViolations = reject(newTransactionViolations, {name: 'missingCategory'});
}
// Add 'missingCategory' violation if category is required and not set
if (!hasMissingCategoryViolation && policyRequiresCategories && !categoryKey) {
newTransactionViolations.push({name: 'missingCategory', type: CONST.VIOLATION_TYPES.VIOLATION});
}
}
// Calculate client-side tag violations
const policyRequiresTags = !!policy.requiresTag;
if (policyRequiresTags) {
newTransactionViolations =
Object.keys(policyTagList).length === 1
? getTagViolationsForSingleLevelTags(updatedTransaction, newTransactionViolations, policyRequiresTags, policyTagList)
: getTagViolationsForMultiLevelTags(updatedTransaction, newTransactionViolations, policyTagList, hasDependentTags);
}
if (updatedTransaction?.comment?.customUnit?.customUnitRateID && !!getDistanceRateCustomUnitRate(policy, updatedTransaction?.comment?.customUnit?.customUnitRateID)) {
newTransactionViolations = reject(newTransactionViolations, {name: CONST.VIOLATIONS.CUSTOM_UNIT_OUT_OF_POLICY});
}
return {
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${updatedTransaction.transactionID}`,
value: newTransactionViolations,
};
},
/**
* Gets the translated message for each violation type.
*
* Necessary because `translate` throws a type error if you attempt to pass it a template strings, when the
* possible values could be either translation keys that resolve to strings or translation keys that resolve to
* functions.
*/
getViolationTranslation(violation: TransactionViolation, translate: LocaleContextProps['translate']): string {
const {
brokenBankConnection = false,
isAdmin = false,
email,
isTransactionOlderThan7Days = false,
member,
category,
rejectedBy = '',
rejectReason = '',
formattedLimit = '',
surcharge = 0,
invoiceMarkup = 0,
maxAge = 0,
tagName,
taxName,
type,
rterType,
} = violation.data ?? {};
switch (violation.name) {
case 'allTagLevelsRequired':
return translate('violations.allTagLevelsRequired');
case 'autoReportedRejectedExpense':
return translate('violations.autoReportedRejectedExpense', {
rejectedBy,
rejectReason,
});
case 'billableExpense':
return translate('violations.billableExpense');
case 'cashExpenseWithNoReceipt':
return translate('violations.cashExpenseWithNoReceipt', {formattedLimit});
case 'categoryOutOfPolicy':
return translate('violations.categoryOutOfPolicy');
case 'conversionSurcharge':
return translate('violations.conversionSurcharge', {surcharge});
case 'customUnitOutOfPolicy':
return translate('violations.customUnitOutOfPolicy');
case 'duplicatedTransaction':
return translate('violations.duplicatedTransaction');
case 'fieldRequired':
return translate('violations.fieldRequired');
case 'futureDate':
return translate('violations.futureDate');
case 'invoiceMarkup':
return translate('violations.invoiceMarkup', {invoiceMarkup});
case 'maxAge':
return translate('violations.maxAge', {maxAge});
case 'missingCategory':
return translate('violations.missingCategory');
case 'missingComment':
return translate('violations.missingComment');
case 'missingTag':
return translate('violations.missingTag', {tagName});
case 'modifiedAmount':
return translate('violations.modifiedAmount', {type, displayPercentVariance: violation.data?.displayPercentVariance});
case 'modifiedDate':
return translate('violations.modifiedDate');
case 'nonExpensiworksExpense':
return translate('violations.nonExpensiworksExpense');
case 'overAutoApprovalLimit':
return translate('violations.overAutoApprovalLimit', {formattedLimit});
case 'overCategoryLimit':
return translate('violations.overCategoryLimit', {formattedLimit});
case 'overLimit':
return translate('violations.overLimit', {formattedLimit});
case 'overLimitAttendee':
return translate('violations.overLimitAttendee', {formattedLimit});
case 'perDayLimit':
return translate('violations.perDayLimit', {formattedLimit});
case 'receiptNotSmartScanned':
return translate('violations.receiptNotSmartScanned');
case 'receiptRequired':
return translate('violations.receiptRequired', {formattedLimit, category});
case 'rter':
return translate('violations.rter', {
brokenBankConnection,
isAdmin,
email,
isTransactionOlderThan7Days,
member,
rterType,
});
case 'smartscanFailed':
return translate('violations.smartscanFailed');
case 'someTagLevelsRequired':
return translate('violations.someTagLevelsRequired', {tagName});
case 'tagOutOfPolicy':
return translate('violations.tagOutOfPolicy', {tagName});
case 'taxAmountChanged':
return translate('violations.taxAmountChanged');
case 'taxOutOfPolicy':
return translate('violations.taxOutOfPolicy', {taxName});
case 'taxRateChanged':
return translate('violations.taxRateChanged');
case 'taxRequired':
return translate('violations.taxRequired');
case 'hold':
return translate('violations.hold');
default:
// The interpreter should never get here because the switch cases should be exhaustive.
// If typescript is showing an error on the assertion below it means the switch statement is out of
// sync with the `ViolationNames` type, and one or the other needs to be updated.
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return violation.name as never;
}
},
// We have to use regex, because Violation limit is given in a inconvenient form: "$2,000.00"
getViolationAmountLimit(violation: TransactionViolation): number {
return Number(violation.data?.formattedLimit?.replace(CONST.VIOLATION_LIMIT_REGEX, ''));
},
};
export default ViolationsUtils;