-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
186 lines (162 loc) · 6.82 KB
/
index.js
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
///<reference path='typings/business-rules-engine/business-rules-engine.d.ts'/>
///<reference path='typings/node/node.d.ts'/>
///<reference path='typings/i18n-2/i18n-2.d.ts'/>
///<reference path='typings/underscore/underscore.d.ts'/>
///<reference path='typings/moment/moment.d.ts'/>
///<reference path='typings/q/q.d.ts'/>
///<reference path='node_modules/br-vacation-approval/business-rules.d.ts'/>
var _ = require('underscore');
var Q = require('q');
var moment = require('moment-range');
var Validation = require('business-rules-engine');
var VacationApproval = require('br-vacation-approval');
var Utils = require("business-rules-engine/commonjs/Utils");
var i18n = require('i18n-2');
var en = require('business-rules-engine/commonjs/i18n/messages_en.js');
var cz = require('business-rules-engine/commonjs/i18n/messages_cz.js');
var de = require('business-rules-engine/commonjs/i18n/messages_de.js');
/**
* @name Custom async property validator example
* @description
* Return true for valid BranchOfBusiness, otherwise return false.
*
* To create a async custom validator you have to implement IAsyncPropertyValidator interface.
* Async custom validator must have property isAsync set to true;
*/
var FakeVacationDeputyService = (function () {
function FakeVacationDeputyService() {
}
/**
* It checks first deputy in the vacation request with list of all approved vacations that they are not in conflict.
* @param an {any} vacation request to check
* @returns {boolean} return true for valid value, otherwise false
*/
FakeVacationDeputyService.prototype.isAcceptable = function (data) {
var deferred = Q.defer();
setTimeout(function () {
//check if there is something to validate -> check required data for validation
var namesAreValid = data.Deputy1 !== undefined && data.Deputy1.FirstName !== undefined && data.Deputy1.LastName !== undefined;
var datesAreValid = _.isDate(data.Duration.From) && _.isDate(data.Duration.To);
if (!namesAreValid || !datesAreValid) {
//nothing to validate
deferred.resolve(true);
return;
}
//fetch items form somewhere - eg. db
var items = [
{ "approvedDays": [moment(), moment().add('days', 1).startOf('days')], "fullName": "John Smith" },
{ "approvedDays": [moment().add('days', 1).startOf('days'), moment().add('days', 2).startOf('days')], "fullName": "Paul Neuman" }
];
//find out range
var durationRange = moment().range(data.Duration.From, data.Duration.To);
//validation
var hasSomeConflicts = _.some(items, function (item) {
return (item.fullName == (data.Deputy1.FirstName + " " + data.Deputy1.LastName) && _.some(item.approvedDays, function (approvedDay) {
return durationRange.contains(approvedDay.startOf('days'));
}));
});
deferred.resolve(!hasSomeConflicts);
}, 1000);
return deferred.promise;
};
return FakeVacationDeputyService;
})();
//prepeare localization
var local = new i18n({
locales: ['en', 'cz', 'de'],
directory: 'node_modules/br-vacation-approval/i18n',
extension: '.json' });
_.extend(local.locales['en'], en.ValidationMessages);
_.extend(local.locales['cz'], cz.ValidationMessages);
_.extend(local.locales['de'], de.ValidationMessages);
//friendly-displayed errors
var displayErrors = function (node, indent) {
if (indent == 0) {
console.log(Array(50).join("--"));
console.log("-- " + local.__("Errors"));
console.log(Array(50).join("--"));
}
if (!node.HasErrors) {
console.log(local.__("NoErrors"));
return;
}
if (node.Children.length == 0) {
//stopped recursion
var msg = local.__(node.Name) + ":";
_.each(node["Errors"], function (childError, key) {
if (childError.HasError) {
//display validation failure - process translateArgs
var failure = childError.TranslateArgs;
if (failure == undefined) {
msg += childError.ErrorMessage;
} else {
//custom messages
if (failure["CustomMessage"] == undefined) {
//call standard translation
msg += failure == undefined ? failure.TranslateId : Utils.StringFce.format(local.__(failure.TranslateId), failure.MessageArgs);
} else {
//call custom messages function - pass translation config and message args
msg += failure == undefined ? failure.TranslateId : failure["CustomMessage"](local.__(failure.TranslateId), failure.MessageArgs);
}
}
msg += " ";
}
});
if (node.HasErrors)
console.log(Array(indent + 1).join("--") + msg);
} else {
//log name
console.log(Array(++indent).join("--") + local.__(node.Name) + ":");
for (var i = 0, len = node.Children.length; i < len; i++) {
if (node.Children[i].HasErrors)
displayErrors(node.Children[i], indent);
}
}
};
var execFce = function (lang) {
//set default culture
local.setLocale(lang);
//create test data
var data = {};
//business rules for vacation approval
var businessRules = new VacationApproval.BusinessRules(data, new FakeVacationDeputyService());
//fill some fields
data.Employee = {
FirstName: "John",
LastName: "Smith toooooooooooooooooooooooooo long"
};
data.Duration = {
From: moment(new Date()).add({ days: -1 }).toDate(),
To: moment(new Date()).add({ days: -10 }).toDate()
};
//execute validation
var promise = businessRules.Validate();
return promise.then(function (result) {
//verify results
displayErrors(businessRules.Errors, 0);
}, function (reason) {
console.log(reason);
}).then(function () {
//fill additional fields
data.Employee.LastName = "Smith";
data.Deputy1.FirstName = "John";
data.Deputy1.LastName = "Smith";
data.Deputy1.Email = "jsmith@gmail.com";
data.Duration.From = moment(new Date()).add({ days: 1 }).toDate();
data.Duration.To = moment(new Date()).add({ days: 3 }).toDate();
//execute validation
var promise = businessRules.Validate();
return promise.then(function (result) {
//verify results
displayErrors(businessRules.Errors, 0);
}, function (reason) {
console.log(reason);
});
});
};
execFce('en').then(function () {
return execFce('de');
}).then(function () {
return execFce('cz');
});
//# sourceMappingURL=index.js.map