-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathExpenseRule.jsx
74 lines (69 loc) · 1.99 KB
/
ExpenseRule.jsx
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
export default class ExpenseRule {
/**
* Creates a new instance of this class.
*
* @param {Array} ruleArray
*/
constructor(ruleArray) {
ruleArray.forEach((value, key) => {
this[key] = value;
});
}
/**
* Returns the applyWhen array associated with the passed field
* i.e. field == category returns [field: 'category', condition: 'matches', value: 'car']
*
* @param {string} field
*
* @return {Object}
*/
getApplyWhenByField(field) {
return this.applyWhen.find((conditions) => conditions.field === field) || {};
}
/**
* Get the externalID saved deep in the tax field
*
* @returns {String}
*/
getExternalTaxID() {
return this.tax.field_id_TAX.externalID || '';
}
/**
* Checks if the passed expense matches this expense rule
*
* @param {SExpense3} expense
* @returns {boolean}
*/
isMatch(expense) {
let isMatch = true;
this.applyWhen.forEach((conditions) => {
switch (conditions.field) {
case 'category':
if (!this.checkCondition(conditions.condition, conditions.value, expense.getCategory())) {
isMatch = false;
}
break;
default:
break;
}
});
return isMatch;
}
/**
* Checks the passed value against the actual based on what the condition (matches, greater, less than, etc.)
*
* @param {string} condition
* @param {Mixed} ruleValue
* @param {Mixed} transactionValue
* @returns {boolean}
*/
checkCondition(condition, ruleValue, transactionValue) {
// Add more condition types (Greater than, Less than, Contains) below
switch (condition) {
case 'matches':
return ruleValue === transactionValue;
default:
return false;
}
}
}