-
Notifications
You must be signed in to change notification settings - Fork 5
/
Utils.js
executable file
·203 lines (174 loc) · 6.68 KB
/
Utils.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* Created by Raul.Jimenez on 18/05/2016.
*/
'use strict';
var emailjs = require('./node_modules/emailjs/email');
module.exports = function Utils(config){
var that = this;
this.config = config;
this.emailsInProgress = [];
// Gets an ID and returns a human-readable-string
this.getGroupName= function(str){
if(config.organization.groups.hasOwnProperty(str)){
return config.organization.groups[str];
}else{
return null;
}
};
// Replace ${attribute_name} for its value in object f
this.parseMail = function(options){
var tmp = options.email || "";
if(options.f){
var f = options.f;
for(var property in f){
if (f.hasOwnProperty(property)) {
do{
tmp = tmp.replace('${'+property+'}', f[property]);
}while(tmp.indexOf('${'+property+'}') !== -1);
}
}
}
if(options.extent){
var extent = options.extent;
for(var extents in extent){
if(extent.hasOwnProperty(extents)){
do{
tmp = tmp.replace('${'+extents+'}', extent[extents]);
}while(tmp.indexOf('${'+extents+'}') !== -1);
}
if(extents == "spatialReference"){
var wkid = String(extent[extents]['wkid']);
tmp = tmp.replace('#{wkid}', wkid);
}
}
}
if(options.geo){
tmp = tmp.replace('#{X}', options.geo['x']);
tmp = tmp.replace('#{Y}', options.geo['y']);
}
return tmp;
};
// Extract email from a string
this.extractEmails = function(text){
return text.match(/([a-zA-Z0-9._+-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi).join(',');
};
this.processFeature = function (obj){
var userGroup, last_user_group_name, email, n, updateFeature, f, geo, extent;
f = objToCase(obj.res.features[obj.i].attributes, 'upper');
extent = obj.extents.extent;
userGroup = config.flow;
if(obj.type == 'esriGeometryPolyline'){
geo = {
x: obj.res.features[obj.i].geometry.paths[0][obj.res.features[obj.i].geometry.paths[0].length%2][0],
y:obj.res.features[obj.i].geometry.paths[0][obj.res.features[obj.i].geometry.paths[0].length%2][1]
};
}
else if(obj.type == 'esriGeometryPoint'){
geo = obj.res.features[obj.i].geometry;
}
else if(obj.type == 'esriGeometryPolygon'){
geo = obj.res.features[obj.i].centroid;
}
// Get user info
obj.service.getUserInfo({
username: f.LAST_EDITED_USER
}).then(function(res){
var hasTrigger = false;
for(var g in res.groups){
last_user_group_name = that.getGroupName(res.groups[g].id);
// Is a group with triggers
if(last_user_group_name){
if(userGroup[last_user_group_name]){
if(userGroup[last_user_group_name].hasOwnProperty(f['ESTADO'])){
console.log('\nInfo: '.yellow + 'Entity ' + f.OBJECTID + ': ' + f.LAST_EDITED_USER + ' belongs '.green +'to group ' + last_user_group_name + ' which has a trigger for the state ' + f['ESTADO']);
hasTrigger = true;
break;
}
}else{
console.log('\nInfo: '.yellow + 'Your flow has no triggers for group "' + last_user_group_name + '"');
}
}
}
// If user belongs to a group which has a trigger for current state
if(hasTrigger){
var toField = that.extractEmails(userGroup[last_user_group_name][f['ESTADO']].to);
//TODO: toFiled is not an email is the name of an attributes which should contain an username of this organization
//if (toField.indexOf('@') === -1){
// toField = getOwnerEmail('owner');
//}
if(toField !== f.LAST_EMAILED_USER && that.emailsInProgress.indexOf(f.OBJECTID) === -1){
// Avoid send emails twice because of the async. calls
that.emailsInProgress.push(f.OBJECTID);
console.log("Block: ",f.OBJECTID)
console.log("Blocked: ",that.emailsInProgress)
email = userGroup[last_user_group_name][f['ESTADO']];
email.subject = that.parseMail({email: email.subject, f: f});
var emailData = that.parseMail({
email: email.text,
f: f,
geo: geo,
extent: extent
});
email.attachment = [{ data: emailData, alternative: true}];
// send the message and get a callback with an error or details of the message that was sent
var server = emailjs.server.connect(obj.config.smtp_server);
server.send(email, function(err, message) {
if(err){
console.log("\nError:".red, err);
}else{
f.LAST_EMAILED_DATE = Date.now();
f.LAST_EMAILED_USER = toField;
updateFeature = { 'attributes': f };
console.log('\nEmail sent to:'.green, f.LAST_EMAILED_USER);
obj.service.updateFeatures({
serviceUrl: obj.feature_service,
features: [updateFeature]
}).then(function(response){
if(response.error){
console.log('\nFeature not updated:'.red, response.error.message);
}else if(response.updateResults[0].success){
console.log('\nUpdated:'.green, "true");
}else{
console.log('\nFeature not updated:'.red, JSON.stringify(response, null, 2));
}
});
} // endif
var pos = that.emailsInProgress.indexOf(f.OBJECTID);
if(pos !== -1){
try{
var timeout = config.smtp_server.timeout || 5000;
(function(pos){
setTimeout(function(pos){
that.emailsInProgress.splice(pos, 1);
console.log("Unblock: ",f.OBJECTID)
console.log("Blocked: ",that.emailsInProgress)
}, timeout, pos)
})(pos);
}catch(e){
console.log("Error:".red, e);
}
}
});
}else{
console.log('\nInfo:'.yellow + ' The notification was already sent to: ' + f.LAST_EMAILED_USER);
}
}else{
console.log('\nInfo: '.yellow + 'Entity ' + f.OBJECTID + ': ' + f.LAST_EDITED_USER + ' does not belongs '.red + 'to a group with the state: ' + f['ESTADO']);
}
});
};
}
function objToCase(obj, type){
var key, keys = Object.keys(obj);
var n = keys.length;
var newobj={}
while (n--) {
key = keys[n];
if(type === 'lower'){
newobj[key.toLowerCase()] = obj[key];
}else if (type === 'upper') {
newobj[key.toUpperCase()] = obj[key];
}
}
return newobj;
}