@tiledesk/tiledesk-server 2.4.100 → 2.4.102

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,7 @@
3
3
  var departmentService = require('../services/departmentService');
4
4
  var Request = require("../models/request");
5
5
  var Project_user = require("../models/project_user");
6
+ var Project = require("../models/project");
6
7
  var messageService = require('../services/messageService');
7
8
  const requestEvent = require('../event/requestEvent');
8
9
  const leadEvent = require('../event/leadEvent');
@@ -13,6 +14,16 @@ var cacheUtil = require("../utils/cacheUtil");
13
14
  var arrayUtil = require("../utils/arrayUtil");
14
15
  var cacheEnabler = require("../services/cacheEnabler");
15
16
  var UIDGenerator = require("../utils/UIDGenerator");
17
+ const { TdCache } = require('../utils/TdCache');
18
+ const { QuoteManager } = require('./QuoteManager');
19
+
20
+ let tdCache = new TdCache({
21
+ host: process.env.CACHE_REDIS_HOST,
22
+ port: process.env.CACHE_REDIS_PORT,
23
+ password: process.env.CACHE_REDIS_PASSWORD
24
+ });
25
+ tdCache.connect();
26
+ let qm = new QuoteManager({ tdCache: tdCache });
16
27
 
17
28
  class RequestService {
18
29
 
@@ -25,355 +36,357 @@ class RequestService {
25
36
  this.sendMessageUpdateLead();
26
37
  }
27
38
  updateSnapshotLead() {
28
- leadEvent.on('lead.update', function(lead) {
39
+ leadEvent.on('lead.update', function (lead) {
29
40
  setImmediate(() => {
30
- winston.debug("updateSnapshotLead on lead.update ", lead);
31
-
32
- Request.updateMany({lead: lead._id, id_project: lead.id_project}, {"$set": {"snapshot.lead": lead}}, function (err, updates) {
33
- if (err) {
34
- winston.error("Error updating requests updateSnapshotLead", err);
35
- return 0;
36
- }
37
- winston.verbose("updateSnapshotLead updated for " + updates.nModified + " request")
38
- requestEvent.emit('request.update.snapshot.lead',{lead:lead,updates:updates});
39
- return;
40
- });
41
- // Request.find({lead: lead._id, id_project: lead.id_project}, function(err, requests) {
41
+ winston.debug("updateSnapshotLead on lead.update ", lead);
42
42
 
43
- // if (err) {
44
- // winston.error("Error getting request by lead", err);
45
- // return 0;
46
- // }
47
- // if (!requests || (requests && requests.length==0)) {
48
- // winston.warn("No request found for lead id " +lead._id );
49
- // return 0;
50
- // }
51
-
52
- // requests.forEach(function(request) {
53
-
43
+ Request.updateMany({ lead: lead._id, id_project: lead.id_project }, { "$set": { "snapshot.lead": lead } }, function (err, updates) {
44
+ if (err) {
45
+ winston.error("Error updating requests updateSnapshotLead", err);
46
+ return 0;
47
+ }
48
+ winston.verbose("updateSnapshotLead updated for " + updates.nModified + " request")
49
+ requestEvent.emit('request.update.snapshot.lead', { lead: lead, updates: updates });
50
+ return;
51
+ });
52
+ // Request.find({lead: lead._id, id_project: lead.id_project}, function(err, requests) {
54
53
 
55
- // });
54
+ // if (err) {
55
+ // winston.error("Error getting request by lead", err);
56
+ // return 0;
57
+ // }
58
+ // if (!requests || (requests && requests.length==0)) {
59
+ // winston.warn("No request found for lead id " +lead._id );
60
+ // return 0;
61
+ // }
56
62
 
57
- // });
63
+ // requests.forEach(function(request) {
58
64
 
59
65
 
60
- });
61
- });
66
+ // });
67
+
68
+ // });
69
+
70
+
71
+ });
72
+ });
62
73
  }
63
74
 
64
75
 
65
76
  sendMessageUpdateLead() {
66
- leadEvent.on('lead.fullname.email.update', function(lead) {
77
+ leadEvent.on('lead.fullname.email.update', function (lead) {
67
78
  winston.debug("lead.fullname.email.update ");
68
79
  // leadEvent.on('lead.update', function(lead) {
69
-
80
+
70
81
  setImmediate(() => {
71
- winston.debug("sendMessageUpdateLead on lead.update ", lead);
72
-
73
- Request.find({lead: lead._id, id_project: lead.id_project}, function(err, requests) {
82
+ winston.debug("sendMessageUpdateLead on lead.update ", lead);
74
83
 
75
- if (err) {
76
- winston.error("Error getting sendMessageUpdateLead request by lead", err);
77
- return 0;
78
- }
79
- if (!requests || (requests && requests.length==0)) {
80
- winston.warn("sendMessageUpdateLead No request found for lead id " +lead._id );
81
- return 0;
82
- }
83
-
84
- // winston.info("sendMessageUpdateLead requests ", requests);
85
-
86
- requests.forEach(function(request) {
87
-
88
- winston.debug("sendMessageUpdateLead request ", request);
89
-
90
- // send(sender, senderFullname, recipient, text, id_project, createdBy, attributes, type, metadata, language)
91
- messageService.send(
92
- 'system',
93
- 'Bot',
94
- // lead.fullname,
95
- request.request_id,
96
- "Lead updated",
97
- request.id_project,
98
- 'system',
99
- {
100
- subtype:"info/support",
101
- "updateconversation" : false,
102
- messagelabel: {key: "LEAD_UPDATED"},
103
- updateUserEmail: lead.email,
104
- updateUserFullname: lead.fullname
105
- },
106
- undefined,
107
- request.language
108
-
109
- );
84
+ Request.find({ lead: lead._id, id_project: lead.id_project }, function (err, requests) {
110
85
 
111
- });
86
+ if (err) {
87
+ winston.error("Error getting sendMessageUpdateLead request by lead", err);
88
+ return 0;
89
+ }
90
+ if (!requests || (requests && requests.length == 0)) {
91
+ winston.warn("sendMessageUpdateLead No request found for lead id " + lead._id);
92
+ return 0;
93
+ }
94
+
95
+ // winston.info("sendMessageUpdateLead requests ", requests);
96
+
97
+ requests.forEach(function (request) {
98
+
99
+ winston.debug("sendMessageUpdateLead request ", request);
100
+
101
+ // send(sender, senderFullname, recipient, text, id_project, createdBy, attributes, type, metadata, language)
102
+ messageService.send(
103
+ 'system',
104
+ 'Bot',
105
+ // lead.fullname,
106
+ request.request_id,
107
+ "Lead updated",
108
+ request.id_project,
109
+ 'system',
110
+ {
111
+ subtype: "info/support",
112
+ "updateconversation": false,
113
+ messagelabel: { key: "LEAD_UPDATED" },
114
+ updateUserEmail: lead.email,
115
+ updateUserFullname: lead.fullname
116
+ },
117
+ undefined,
118
+ request.language
119
+
120
+ );
121
+
122
+ });
112
123
 
113
- });
114
-
115
124
  });
125
+
126
+ });
116
127
  });
117
128
  }
118
129
 
119
130
 
120
131
  getAvailableAgentsCount(agents) {
121
-
122
- var project_users_available = agents.filter(function (projectUser) {
123
- if (projectUser.user_available == true) {
124
- return true;
125
- }
126
- });
127
- winston.debug('++ AVAILABLE PROJECT USERS count ', project_users_available)
128
-
129
- if (project_users_available && project_users_available.length>0){
130
- return project_users_available.length;
131
- }else {
132
- return 0;
132
+
133
+ var project_users_available = agents.filter(function (projectUser) {
134
+ if (projectUser.user_available == true) {
135
+ return true;
133
136
  }
137
+ });
138
+ winston.debug('++ AVAILABLE PROJECT USERS count ', project_users_available)
139
+
140
+ if (project_users_available && project_users_available.length > 0) {
141
+ return project_users_available.length;
142
+ } else {
143
+ return 0;
144
+ }
145
+
134
146
 
135
-
136
147
  }
137
148
 
138
- //change create with this
139
- routeInternal (request, departmentid, id_project, nobot) {
140
- var that = this;
149
+ //change create with this
150
+ routeInternal(request, departmentid, id_project, nobot) {
151
+ var that = this;
141
152
 
142
153
  return new Promise(function (resolve, reject) {
143
154
 
144
- var context = {request: request};
155
+ var context = { request: request };
145
156
 
146
- // getOperators(departmentid, projectid, nobot, disableWebHookCall, context)
147
- return departmentService.getOperators(departmentid, id_project, nobot, undefined, context).then(function (result) {
157
+ // getOperators(departmentid, projectid, nobot, disableWebHookCall, context)
158
+ return departmentService.getOperators(departmentid, id_project, nobot, undefined, context).then(function (result) {
148
159
 
149
- // winston.debug("getOperators", result);
160
+ // winston.debug("getOperators", result);
150
161
 
151
- var assigned_at = undefined;
152
-
153
- var status = RequestConstants.UNASSIGNED;
154
- var assigned_operator_id;
155
- var participants = [];
156
- var participantsAgents = [];
157
- var participantsBots = [];
158
- var hasBot = false;
162
+ var assigned_at = undefined;
159
163
 
160
- if (result.operators && result.operators.length>0) {
161
- assigned_operator_id = result.operators[0].id_user;
164
+ var status = RequestConstants.UNASSIGNED;
165
+ var assigned_operator_id;
166
+ var participants = [];
167
+ var participantsAgents = [];
168
+ var participantsBots = [];
169
+ var hasBot = false;
162
170
 
163
- status = RequestConstants.ASSIGNED;
171
+ if (result.operators && result.operators.length > 0) {
172
+ assigned_operator_id = result.operators[0].id_user;
164
173
 
165
- var assigned_operator_idString = assigned_operator_id.toString();
166
- participants.push(assigned_operator_idString);
174
+ status = RequestConstants.ASSIGNED;
167
175
 
168
- // botprefix
169
- if (assigned_operator_idString.startsWith("bot_")) {
170
- hasBot = true;
171
-
172
- // botprefix
173
- var assigned_operator_idStringBot = assigned_operator_idString.replace("bot_","");
174
- participantsBots.push(assigned_operator_idStringBot);
175
- }else {
176
- participantsAgents.push(assigned_operator_idString);
177
- hasBot = false; //??
178
- }
176
+ var assigned_operator_idString = assigned_operator_id.toString();
177
+ participants.push(assigned_operator_idString);
178
+
179
+ // botprefix
180
+ if (assigned_operator_idString.startsWith("bot_")) {
181
+ hasBot = true;
179
182
 
180
- assigned_at = Date.now();
183
+ // botprefix
184
+ var assigned_operator_idStringBot = assigned_operator_idString.replace("bot_", "");
185
+ participantsBots.push(assigned_operator_idStringBot);
186
+ } else {
187
+ participantsAgents.push(assigned_operator_idString);
188
+ hasBot = false; //??
181
189
  }
182
- winston.debug("routeInternal assigned_operator_id: "+ assigned_operator_id);
183
- winston.debug("routeInternal status: "+ status);
184
190
 
191
+ assigned_at = Date.now();
192
+ }
193
+ winston.debug("routeInternal assigned_operator_id: " + assigned_operator_id);
194
+ winston.debug("routeInternal status: " + status);
185
195
 
186
196
 
187
- // cosi modifica la request originale forse devi fare il clone?????
188
- request.status = status;
189
197
 
190
- request.participants = participants;
191
- request.participantsAgents = participantsAgents;
192
- request.participantsBots = participantsBots;
193
- request.hasBot = hasBot;
198
+ // cosi modifica la request originale forse devi fare il clone?????
199
+ request.status = status;
194
200
 
195
- request.department = result.department._id;
196
- // request.agents = result.agents;
197
- request.assigned_at = assigned_at;
198
- request.waiting_time = undefined //reset waiting_time on reroute
199
-
200
- if (!request.snapshot) { //if used other methods than .create
201
- request.snapshot = {}
202
- }
201
+ request.participants = participants;
202
+ request.participantsAgents = participantsAgents;
203
+ request.participantsBots = participantsBots;
204
+ request.hasBot = hasBot;
203
205
 
204
-
205
- request.snapshot.department = result.department;
206
- request.snapshot.agents = result.agents;
207
- request.snapshot.availableAgentsCount = that.getAvailableAgentsCount(result.agents);
206
+ request.department = result.department._id;
207
+ // request.agents = result.agents;
208
+ request.assigned_at = assigned_at;
209
+ request.waiting_time = undefined //reset waiting_time on reroute
208
210
 
209
- return resolve(request);
210
-
211
-
212
- }).catch(function(err){
213
- return reject(err);
214
- });
211
+ if (!request.snapshot) { //if used other methods than .create
212
+ request.snapshot = {}
213
+ }
214
+
215
+
216
+ request.snapshot.department = result.department;
217
+ request.snapshot.agents = result.agents;
218
+ request.snapshot.availableAgentsCount = that.getAvailableAgentsCount(result.agents);
219
+
220
+ return resolve(request);
221
+
222
+
223
+ }).catch(function (err) {
224
+ return reject(err);
225
+ });
215
226
 
216
227
 
217
228
  });
218
229
  }
219
-
230
+
220
231
  // TODO changePreflightByRequestId se un agente entra in request freflight true disabilitare add agente e reassing ma mettere un bottone removePreflight???
221
232
  // usalo no_populate
222
233
  route(request_id, departmentid, id_project, nobot, no_populate) {
223
- var that = this;
234
+ var that = this;
224
235
 
225
- return new Promise(function (resolve, reject) {
226
- winston.debug("request_id:" + request_id);
227
- winston.debug("departmentid:" + departmentid);
228
- winston.debug("id_project:" + id_project);
229
- winston.debug("nobot:"+ nobot);
236
+ return new Promise(function (resolve, reject) {
237
+ winston.debug("request_id:" + request_id);
238
+ winston.debug("departmentid:" + departmentid);
239
+ winston.debug("id_project:" + id_project);
240
+ winston.debug("nobot:" + nobot);
230
241
 
231
- let q= Request
232
- .findOne({request_id: request_id, id_project: id_project});
242
+ let q = Request
243
+ .findOne({ request_id: request_id, id_project: id_project });
233
244
 
234
- // if (cacheEnabler.request) { //(node:60837) UnhandledPromiseRejectionWarning: VersionError: No matching document found for id "633efe246a6cc0eda5732684" version 0 modifiedPaths "status, participants, participantsAgents, department, assigned_at, snapshot, snapshot.department, snapshot.department.updatedAt, snapshot.agents"
235
- // q.cache(cacheUtil.defaultTTL, id_project+":requests:request_id:"+request_id+":simple") //request_cache
236
- // winston.debug('request cache enabled');
237
- // }
238
- return q.exec( function(err, request) {
245
+ // if (cacheEnabler.request) { //(node:60837) UnhandledPromiseRejectionWarning: VersionError: No matching document found for id "633efe246a6cc0eda5732684" version 0 modifiedPaths "status, participants, participantsAgents, department, assigned_at, snapshot, snapshot.department, snapshot.department.updatedAt, snapshot.agents"
246
+ // q.cache(cacheUtil.defaultTTL, id_project+":requests:request_id:"+request_id+":simple") //request_cache
247
+ // winston.debug('request cache enabled');
248
+ // }
249
+ return q.exec(function (err, request) {
239
250
 
240
- if (err) {
241
- winston.error(err);
242
- return reject(err);
243
- }
244
-
245
- winston.debug('request return',request);
251
+ if (err) {
252
+ winston.error(err);
253
+ return reject(err);
254
+ }
246
255
 
247
- // cambia var in let
256
+ winston.debug('request return', request);
248
257
 
249
- //it is important to clone here
250
- var requestBeforeRoute = Object.assign({}, request.toObject());
251
- winston.debug("requestBeforeRoute",requestBeforeRoute);
258
+ // cambia var in let
252
259
 
253
- var beforeParticipants = requestBeforeRoute.participants;
254
- winston.debug("beforeParticipants: ", beforeParticipants);
260
+ //it is important to clone here
261
+ var requestBeforeRoute = Object.assign({}, request.toObject());
262
+ winston.debug("requestBeforeRoute", requestBeforeRoute);
255
263
 
256
- return that.routeInternal(request, departmentid, id_project, nobot ).then(function(routedRequest){
264
+ var beforeParticipants = requestBeforeRoute.participants;
265
+ winston.debug("beforeParticipants: ", beforeParticipants);
257
266
 
258
- winston.debug("after routeInternal",routedRequest);
259
- // winston.info("requestBeforeRoute.participants " +requestBeforeRoute.request_id , requestBeforeRoute.participants);
260
- // console.log("routedRequest.participants " +routedRequest.request_id , routedRequest.participants);
261
- winston.debug("requestBeforeRoute.status:" + requestBeforeRoute.status);
262
- winston.debug("routedRequest.status:" + routedRequest.status);
267
+ return that.routeInternal(request, departmentid, id_project, nobot).then(function (routedRequest) {
263
268
 
264
- let beforeDepartmentId;
265
- if (requestBeforeRoute.department) { //requestBeforeRoute.department can be empty for internal ticket
266
- beforeDepartmentId = requestBeforeRoute.department.toString();
267
- winston.debug("beforeDepartmentId:"+ beforeDepartmentId);
268
- }
269
-
270
- let afterDepartmentId;
271
- if (routedRequest.department) {
272
- afterDepartmentId = routedRequest.department.toString();
273
- winston.debug("afterDepartmentId:"+ afterDepartmentId);
274
- }
275
-
276
-
277
- if (requestBeforeRoute.status === routedRequest.status &&
278
- beforeDepartmentId === afterDepartmentId &&
279
- requestUtil.arraysEqual(beforeParticipants, routedRequest.participants)) {
269
+ winston.debug("after routeInternal", routedRequest);
270
+ // winston.info("requestBeforeRoute.participants " +requestBeforeRoute.request_id , requestBeforeRoute.participants);
271
+ // console.log("routedRequest.participants " +routedRequest.request_id , routedRequest.participants);
272
+ winston.debug("requestBeforeRoute.status:" + requestBeforeRoute.status);
273
+ winston.debug("routedRequest.status:" + routedRequest.status);
280
274
 
281
- winston.verbose("Request " +request.request_id + " contains already the same participants at the same request status. Routed to the same participants");
275
+ let beforeDepartmentId;
276
+ if (requestBeforeRoute.department) { //requestBeforeRoute.department can be empty for internal ticket
277
+ beforeDepartmentId = requestBeforeRoute.department.toString();
278
+ winston.debug("beforeDepartmentId:" + beforeDepartmentId);
279
+ }
282
280
 
283
- if (no_populate==="true" || no_populate===true) {
284
- winston.debug("no_populate is true");
285
- return resolve(request);
286
- }
281
+ let afterDepartmentId;
282
+ if (routedRequest.department) {
283
+ afterDepartmentId = routedRequest.department.toString();
284
+ winston.debug("afterDepartmentId:" + afterDepartmentId);
285
+ }
286
+
287
+
288
+ if (requestBeforeRoute.status === routedRequest.status &&
289
+ beforeDepartmentId === afterDepartmentId &&
290
+ requestUtil.arraysEqual(beforeParticipants, routedRequest.participants)) {
291
+
292
+ winston.verbose("Request " + request.request_id + " contains already the same participants at the same request status. Routed to the same participants");
293
+
294
+ if (no_populate === "true" || no_populate === true) {
295
+ winston.debug("no_populate is true");
296
+ return resolve(request);
297
+ }
287
298
 
288
- return request
289
- .populate('lead')
299
+ return request
300
+ .populate('lead')
290
301
  .populate('department')
291
302
  .populate('participatingBots')
292
- .populate('participatingAgents')
293
- .populate({path:'requester',populate:{path:'id_user'}})
294
- .execPopulate( function(err, requestComplete) {
295
- winston.debug("requestComplete",requestComplete);
296
-
303
+ .populate('participatingAgents')
304
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
305
+ .execPopulate(function (err, requestComplete) {
306
+ winston.debug("requestComplete", requestComplete);
307
+
297
308
  return resolve(requestComplete);
298
309
  });
310
+ }
311
+
312
+ //cacheinvalidation
313
+ return routedRequest.save(function (err, savedRequest) {
314
+ // https://stackoverflow.com/questions/54792749/mongoose-versionerror-no-matching-document-found-for-id-when-document-is-being
315
+ //return routedRequest.update(function(err, savedRequest) {
316
+ if (err) {
317
+ winston.error('Error saving the request. ', { err: err, routedRequest: routedRequest });
318
+ return reject(err);
299
319
  }
300
320
 
301
- //cacheinvalidation
302
- return routedRequest.save(function(err, savedRequest) {
303
- // https://stackoverflow.com/questions/54792749/mongoose-versionerror-no-matching-document-found-for-id-when-document-is-being
304
- //return routedRequest.update(function(err, savedRequest) {
305
- if (err) {
306
- winston.error('Error saving the request. ', {err:err, routedRequest:routedRequest});
307
- return reject(err);
308
- }
309
-
310
- winston.debug("after save savedRequest", savedRequest);
321
+ winston.debug("after save savedRequest", savedRequest);
311
322
 
312
- return savedRequest
323
+ return savedRequest
313
324
  .populate('lead')
314
325
  .populate('department')
315
326
  .populate('participatingBots')
316
- .populate('participatingAgents')
317
- .populate({path:'requester',populate:{path:'id_user'}})
318
- .execPopulate( function(err, requestComplete) {
319
-
320
- // return Request //to populate correctly i must re-exec the query
321
- // .findById(savedRequest.id)
322
- // .populate('lead')
323
- // .populate('department')
324
- // .populate('participatingBots')
325
- // .populate('participatingAgents')
326
- // .populate({path:'requester',populate:{path:'id_user'}})
327
- // .exec( function(err, requestComplete) {
328
-
329
-
330
- if (err) {
331
- winston.error('Error populating the request.',err);
332
- return reject(err);
333
- }
327
+ .populate('participatingAgents')
328
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
329
+ .execPopulate(function (err, requestComplete) {
330
+
331
+ // return Request //to populate correctly i must re-exec the query
332
+ // .findById(savedRequest.id)
333
+ // .populate('lead')
334
+ // .populate('department')
335
+ // .populate('participatingBots')
336
+ // .populate('participatingAgents')
337
+ // .populate({path:'requester',populate:{path:'id_user'}})
338
+ // .exec( function(err, requestComplete) {
339
+
340
+
341
+ if (err) {
342
+ winston.error('Error populating the request.', err);
343
+ return reject(err);
344
+ }
334
345
 
335
- winston.verbose("Request routed",requestComplete.toObject());
336
-
346
+ winston.verbose("Request routed", requestComplete.toObject());
337
347
 
338
- var oldParticipants = beforeParticipants;
339
- winston.debug("oldParticipants ", oldParticipants);
340
348
 
341
- let newParticipants = requestComplete.participants;
342
- winston.debug("newParticipants ", newParticipants);
349
+ var oldParticipants = beforeParticipants;
350
+ winston.debug("oldParticipants ", oldParticipants);
343
351
 
344
- var removedParticipants = oldParticipants.filter(d => !newParticipants.includes(d));
345
- winston.debug("removedParticipants ", removedParticipants);
352
+ let newParticipants = requestComplete.participants;
353
+ winston.debug("newParticipants ", newParticipants);
346
354
 
347
- var addedParticipants = newParticipants.filter(d => !oldParticipants.includes(d));
348
- winston.debug("addedParticipants ", addedParticipants);
355
+ var removedParticipants = oldParticipants.filter(d => !newParticipants.includes(d));
356
+ winston.debug("removedParticipants ", removedParticipants);
349
357
 
350
-
351
- requestEvent.emit('request.update',requestComplete);
352
- requestEvent.emit("request.update.comment", {comment:"REROUTE",request:requestComplete});//Deprecated
353
- requestEvent.emit("request.updated", {comment:"REROUTE",request:requestComplete, patch:{removedParticipants: removedParticipants, addedParticipants:addedParticipants}});
358
+ var addedParticipants = newParticipants.filter(d => !oldParticipants.includes(d));
359
+ winston.debug("addedParticipants ", addedParticipants);
354
360
 
355
- requestEvent.emit('request.participants.update', {beforeRequest:request,
356
- removedParticipants:removedParticipants,
357
- addedParticipants:addedParticipants,
358
- request:requestComplete});
359
361
 
360
- requestEvent.emit('request.department.update',requestComplete); //se req ha bot manda messaggio \welcome
362
+ requestEvent.emit('request.update', requestComplete);
363
+ requestEvent.emit("request.update.comment", { comment: "REROUTE", request: requestComplete });//Deprecated
364
+ requestEvent.emit("request.updated", { comment: "REROUTE", request: requestComplete, patch: { removedParticipants: removedParticipants, addedParticipants: addedParticipants } });
361
365
 
362
- winston.debug("here end");
366
+ requestEvent.emit('request.participants.update', {
367
+ beforeRequest: request,
368
+ removedParticipants: removedParticipants,
369
+ addedParticipants: addedParticipants,
370
+ request: requestComplete
371
+ });
363
372
 
364
- return resolve(requestComplete);
365
- });
373
+ requestEvent.emit('request.department.update', requestComplete); //se req ha bot manda messaggio \welcome
366
374
 
375
+ winston.debug("here end");
376
+
377
+ return resolve(requestComplete);
378
+ });
367
379
 
368
-
369
- });
370
380
 
371
- }).catch(function(err) {
372
- return reject(err);
373
- });
374
381
 
375
-
376
382
  });
383
+
384
+ }).catch(function (err) {
385
+ return reject(err);
386
+ });
387
+
388
+
389
+ });
377
390
  });
378
391
  }
379
392
 
@@ -384,413 +397,717 @@ class RequestService {
384
397
  return new Promise(function (resolve, reject) {
385
398
  // winston.debug("request_id", request_id);
386
399
  // winston.debug("newstatus", newstatus);
387
-
388
- let q = Request
389
- .findOne({request_id: request_id, id_project: id_project});
390
400
 
391
- if (cacheEnabler.request) {
392
- q.cache(cacheUtil.defaultTTL, id_project+":requests:request_id:"+request_id+":simple") //request_cache
393
- winston.debug('request cache enabled');
401
+ let q = Request
402
+ .findOne({ request_id: request_id, id_project: id_project });
403
+
404
+ if (cacheEnabler.request) {
405
+ q.cache(cacheUtil.defaultTTL, id_project + ":requests:request_id:" + request_id + ":simple") //request_cache
406
+ winston.debug('request cache enabled');
407
+ }
408
+
409
+ return q.exec(function (err, request) {
410
+
411
+ if (err) {
412
+ winston.error(err);
413
+ return reject(err);
394
414
  }
395
415
 
396
- return q.exec( function(err, request) {
397
-
398
- if (err) {
399
- winston.error(err);
400
- return reject(err);
401
- }
416
+ winston.debug('request cache simple 3', request);
417
+
418
+ winston.debug("here reroute1 ");
402
419
 
403
- winston.debug('request cache simple 3', request);
420
+ // Cannot read property 'toString' of undefined at /usr/src/app/services/requestService.js:404:61 at /usr/src/app/node_modules/cachegoose/out/extend-query.js:44:13 at Command.cb [as callback] (/usr/src/app/node_modules/cacheman-redis/node/index.js:142:9) at normal_reply (/usr/src/app/node_modules/redis/index.js:655:21) at RedisClient.return_reply (/usr/src/app/node_modules/redis/index.js:753:9) at JavascriptRedisParser.returnReply (/usr/src/app/node_modules/redis/index.js:138:18) at JavascriptRedisParser.execute (/usr/src/app/node_modules/redis-parser/lib/parser.js:544:14) at Socket.<anonymous> (/usr/src/app/node_modules/redis/index.js:219:27) at Socket.emit (events.js:314:20) at addChunk (_stream_readable.js:297:12) at readableAddChunk (_stream_readable.js:272:9) at Socket.Readable.push (_stream_readable.js:213:10) at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {"date":"Tue Mar 21 2023 18:45:47 GMT+0000 (Coordinated Unive
421
+ if (request.department === undefined) {
422
+ winston.error("Request with request_id " + request_id + " has empty department. So I can't reroute");
423
+ return reject("Request with request_id " + request_id + " has empty department. So I can't reroute");
424
+ }
404
425
 
405
- winston.debug("here reroute1 ");
406
426
 
407
- // Cannot read property 'toString' of undefined at /usr/src/app/services/requestService.js:404:61 at /usr/src/app/node_modules/cachegoose/out/extend-query.js:44:13 at Command.cb [as callback] (/usr/src/app/node_modules/cacheman-redis/node/index.js:142:9) at normal_reply (/usr/src/app/node_modules/redis/index.js:655:21) at RedisClient.return_reply (/usr/src/app/node_modules/redis/index.js:753:9) at JavascriptRedisParser.returnReply (/usr/src/app/node_modules/redis/index.js:138:18) at JavascriptRedisParser.execute (/usr/src/app/node_modules/redis-parser/lib/parser.js:544:14) at Socket.<anonymous> (/usr/src/app/node_modules/redis/index.js:219:27) at Socket.emit (events.js:314:20) at addChunk (_stream_readable.js:297:12) at readableAddChunk (_stream_readable.js:272:9) at Socket.Readable.push (_stream_readable.js:213:10) at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {"date":"Tue Mar 21 2023 18:45:47 GMT+0000 (Coordinated Unive
408
- if (request.department === undefined) {
409
- winston.error("Request with request_id " + request_id + " has empty department. So I can't reroute");
410
- return reject("Request with request_id " + request_id + " has empty department. So I can't reroute");
411
- }
427
+ return that.route(request_id, request.department.toString(), id_project, nobot).then(function (routedRequest) {
412
428
 
413
-
414
- return that.route(request_id, request.department.toString(), id_project, nobot).then(function(routedRequest){
429
+ var endDate = new Date();
430
+ winston.verbose("Performance Request reroute in millis: " + endDate - startDate);
415
431
 
416
- var endDate = new Date();
417
- winston.verbose("Performance Request reroute in millis: " + endDate-startDate);
432
+ return resolve(routedRequest);
433
+ }).catch(function (err) {
434
+ return reject(err);
435
+ });
436
+
437
+ });
438
+ });
439
+ }
418
440
 
419
- return resolve(routedRequest);
420
- }).catch(function(err) {
421
- return reject(err);
422
- });
423
-
424
- });
425
- });
426
- }
427
-
428
441
 
429
442
 
430
443
  createWithRequester(project_user_id, lead_id, id_project, first_text, departmentid, sourcePage, language, userAgent, status, createdBy, attributes, subject, preflight) {
431
444
 
432
- var request_id = 'support-group-'+ id_project + "-" + UIDGenerator.generate();
433
- winston.debug("request_id: "+request_id);
434
-
445
+ var request_id = 'support-group-' + id_project + "-" + UIDGenerator.generate();
446
+ winston.debug("request_id: " + request_id);
447
+
435
448
  return this.createWithIdAndRequester(request_id, project_user_id, lead_id, id_project, first_text, departmentid, sourcePage, language, userAgent, status, createdBy, attributes, subject, preflight);
436
449
  }
437
450
 
438
451
  createWithIdAndRequester(request_id, project_user_id, lead_id, id_project, first_text, departmentid, sourcePage, language, userAgent, status, createdBy, attributes, subject, preflight, channel, location) {
439
-
452
+
440
453
  var request = {
441
- request_id:request_id, project_user_id:project_user_id, lead_id:lead_id, id_project:id_project,first_text:first_text,
442
- departmentid:departmentid, sourcePage:sourcePage, language:language, userAgent:userAgent, status:status, createdBy:createdBy,
443
- attributes:attributes, subject:subject, preflight:preflight, channel:channel, location:location
444
- };
454
+ request_id: request_id, project_user_id: project_user_id, lead_id: lead_id, id_project: id_project, first_text: first_text,
455
+ departmentid: departmentid, sourcePage: sourcePage, language: language, userAgent: userAgent, status: status, createdBy: createdBy,
456
+ attributes: attributes, subject: subject, preflight: preflight, channel: channel, location: location
457
+ };
445
458
 
446
459
  return this.create(request);
447
- };
460
+ };
448
461
 
449
- create(request) {
462
+ async create(request) {
450
463
 
451
464
  var startDate = new Date();
452
465
 
453
- var request_id = request.request_id;
454
- var project_user_id = request.project_user_id;
455
- var lead_id = request.lead_id;
456
- var id_project = request.id_project;
466
+ if (!request.createdAt) {
467
+ request.createdAt = new Date();
468
+ }
457
469
 
458
- var first_text = request.first_text;
459
- //removed for ticket
460
- // // lascia che sia nico a fare il replace...certo tu devi fare il test che tutto sia ok quindi dopo demo
461
- // var first_text;
462
- // if (request.first_text) { //first_text can be empty for type image
463
- // first_text = request.first_text.replace(/[\n\r]+/g, ' '); //replace new line with space
464
- // }
465
-
466
- var departmentid = request.departmentid;
467
- var sourcePage = request.sourcePage;
468
- var language = request.language;
469
- var userAgent = request.userAgent;
470
- var status = request.status;
471
- var createdBy = request.createdBy;
472
- var attributes = request.attributes;
473
- var subject = request.subject;
474
- var preflight = request.preflight;
475
- var channel = request.channel;
476
- var location = request.location;
477
- var participants = request.participants || [];
478
470
 
479
- var tags = request.tags;
480
- var notes = request.notes;
481
- var priority = request.priority;
471
+ var request_id = request.request_id;
472
+ var project_user_id = request.project_user_id;
473
+ var lead_id = request.lead_id;
474
+ var id_project = request.id_project;
482
475
 
483
- var auto_close = request.auto_close;
476
+ var first_text = request.first_text;
484
477
 
485
- var followers = request.followers;
478
+ //removed for ticket
479
+ // // lascia che sia nico a fare il replace...certo tu devi fare il test che tutto sia ok quindi dopo demo
480
+ // var first_text;
481
+ // if (request.first_text) { //first_text can be empty for type image
482
+ // first_text = request.first_text.replace(/[\n\r]+/g, ' '); //replace new line with space
483
+ // }
486
484
 
487
- if (!departmentid) {
488
- departmentid ='default';
489
- }
485
+ var departmentid = request.departmentid;
486
+ var sourcePage = request.sourcePage;
487
+ var language = request.language;
488
+ var userAgent = request.userAgent;
489
+ var status = request.status;
490
+ var createdBy = request.createdBy;
491
+ var attributes = request.attributes;
492
+ var subject = request.subject;
493
+ var preflight = request.preflight;
494
+ var channel = request.channel;
495
+ var location = request.location;
496
+ var participants = request.participants || [];
497
+
498
+ var tags = request.tags;
499
+ var notes = request.notes;
500
+ var priority = request.priority;
501
+
502
+ var auto_close = request.auto_close;
503
+
504
+ var followers = request.followers;
505
+ let createdAt = request.createdAt;
506
+
507
+ if (!departmentid) {
508
+ departmentid = 'default';
509
+ }
510
+
511
+ if (!createdBy) {
512
+ if (project_user_id) {
513
+ createdBy = project_user_id;
514
+ } else {
515
+ createdBy = "system";
516
+ }
490
517
 
491
- if (!createdBy) {
492
- if (project_user_id) {
493
- createdBy = project_user_id;
494
- }else {
495
- createdBy = "system";
496
518
  }
497
-
498
- }
499
-
500
- var that = this;
501
519
 
502
- return new Promise(async (resolve, reject) => {
520
+ var that = this;
503
521
 
504
- var context = {request: {request_id:request_id, project_user_id:project_user_id, lead_id:lead_id, id_project:id_project,
505
- first_text:first_text, departmentid:departmentid, sourcePage:sourcePage, language:language, userAgent:userAgent, status:status,
506
- createdBy:createdBy, attributes:attributes, subject:subject, preflight: preflight, channel: channel, location: location,
507
- participants:participants, tags: tags, notes:notes,
508
- priority: priority, auto_close: auto_close, followers: followers}};
522
+ return new Promise(async (resolve, reject) => {
509
523
 
510
- winston.debug("context",context);
524
+ let q = Project.findOne({ _id: request.id_project, status: 100 });
525
+ if (cacheEnabler.project) {
526
+ q.cache(cacheUtil.longTTL, "projects:id:" + request.id_project) //project_cache
527
+ winston.debug('project cache enabled for /project detail');
528
+ }
529
+ q.exec(async function (err, p) {
530
+ if (err) {
531
+ winston.error('Error getting project ', err);
532
+ }
533
+ if (!p) {
534
+ winston.warn('Project not found ');
535
+ }
536
+
537
+
538
+ let payload = {
539
+ project: p,
540
+ request: request
541
+ }
542
+
543
+ let available = await qm.checkQuote(p, request, 'requests');
544
+ if (available === false) {
545
+ winston.info("Requests limits reached for project " + p._id)
546
+ return false;
547
+ }
548
+
511
549
 
512
- var participantsAgents = [];
513
- var participantsBots = [];
514
- var hasBot = false;
550
+ var context = {
551
+ request: {
552
+ request_id: request_id, project_user_id: project_user_id, lead_id: lead_id, id_project: id_project,
553
+ first_text: first_text, departmentid: departmentid, sourcePage: sourcePage, language: language, userAgent: userAgent, status: status,
554
+ createdBy: createdBy, attributes: attributes, subject: subject, preflight: preflight, channel: channel, location: location,
555
+ participants: participants, tags: tags, notes: notes,
556
+ priority: priority, auto_close: auto_close, followers: followers
557
+ }
558
+ };
515
559
 
516
- var dep_id = undefined;
560
+ winston.debug("context", context);
517
561
 
518
- var assigned_at = undefined;
519
-
520
- var agents = [];
562
+ var participantsAgents = [];
563
+ var participantsBots = [];
564
+ var hasBot = false;
521
565
 
522
- var snapshot = {};
566
+ var dep_id = undefined;
523
567
 
524
- try {
525
- // getOperators(departmentid, projectid, nobot, disableWebHookCall, context) {
526
- var result = await departmentService.getOperators(departmentid, id_project, false, undefined, context);
527
- // console.log("************* after get operator: "+new Date().toISOString());
568
+ var assigned_at = undefined;
528
569
 
529
- winston.debug("getOperators", result);
530
- } catch(err) {
531
- return reject(err);
532
- }
533
-
534
-
535
-
536
- agents = result.agents;
570
+ var agents = [];
537
571
 
538
- if (status == 50) {
539
- // skip assignment
540
- if (participants.length == 0 ) {
541
- dep_id = result.department._id;
542
- }
543
- } else {
544
-
545
- if (participants.length == 0 ) {
546
- if (result.operators && result.operators.length>0) {
547
- participants.push(result.operators[0].id_user.toString());
548
- }
549
- // for preflight it is important to save agents in req for trigger. try to optimize it
550
- dep_id = result.department._id;
572
+ var snapshot = {};
551
573
 
552
- }
574
+ try {
575
+ // getOperators(departmentid, projectid, nobot, disableWebHookCall, context) {
576
+ var result = await departmentService.getOperators(departmentid, id_project, false, undefined, context);
577
+ // console.log("************* after get operator: "+new Date().toISOString());
553
578
 
554
- if (participants.length > 0) {
555
-
556
- status = RequestConstants.ASSIGNED;
557
-
558
- // botprefix
559
- if (participants[0].startsWith("bot_")) {
560
-
561
- hasBot = true;
562
- winston.debug("hasBot:"+hasBot);
563
-
564
- // botprefix
565
- var assigned_operator_idStringBot = participants[0].replace("bot_","");
566
- winston.debug("assigned_operator_idStringBot:"+assigned_operator_idStringBot);
567
-
568
- participantsBots.push(assigned_operator_idStringBot);
569
-
570
- } else {
571
-
572
- participantsAgents.push(participants[0]);
573
-
574
- }
575
-
576
- assigned_at = Date.now();
579
+ winston.debug("getOperators", result);
580
+ } catch (err) {
581
+ return reject(err);
582
+ }
577
583
 
578
- } else {
579
584
 
580
- status = RequestConstants.UNASSIGNED;
581
585
 
582
- }
586
+ agents = result.agents;
583
587
 
588
+ if (status == 50) {
589
+ // skip assignment
590
+ if (participants.length == 0) {
591
+ dep_id = result.department._id;
584
592
  }
593
+ } else {
585
594
 
586
-
587
-
595
+ if (participants.length == 0) {
596
+ if (result.operators && result.operators.length > 0) {
597
+ participants.push(result.operators[0].id_user.toString());
598
+ }
599
+ // for preflight it is important to save agents in req for trigger. try to optimize it
600
+ dep_id = result.department._id;
588
601
 
589
- if (dep_id) {
590
- snapshot.department = result.department;
591
602
  }
592
-
593
- // console.log("result.agents",result.agents);
594
- snapshot.agents = agents;
595
- snapshot.availableAgentsCount = that.getAvailableAgentsCount(agents);
596
603
 
597
- if (request.requester) { //.toObject()????
598
- snapshot.requester = request.requester;
599
- }
600
- if (request.lead) {
601
- snapshot.lead = request.lead;
602
- }
603
-
604
- // winston.debug("assigned_operator_id", assigned_operator_id);
605
- // winston.debug("req status", status);
606
-
607
- var newRequest = new Request({
608
- request_id: request_id,
609
- requester: project_user_id,
610
- lead: lead_id,
611
- first_text: first_text,
612
- subject: subject,
613
- status: status,
614
- participants: participants,
615
- participantsAgents:participantsAgents,
616
- participantsBots: participantsBots,
617
- hasBot: hasBot,
618
- department: dep_id,
619
- // agents: agents,
620
-
621
- //others
622
- sourcePage: sourcePage,
623
- language: language,
624
- userAgent: userAgent,
625
- assigned_at : assigned_at,
626
-
627
- attributes: attributes,
628
- //standard
629
- id_project: id_project,
630
- createdBy: createdBy,
631
- updatedBy: createdBy,
632
- preflight: preflight,
633
- channel: channel,
634
- location: location,
635
- snapshot: snapshot,
636
- tags: tags,
637
- notes: notes,
638
- priority: priority,
639
- auto_close: auto_close,
640
- followers: followers
641
- });
604
+ if (participants.length > 0) {
642
605
 
643
- winston.debug('newRequest.',newRequest);
606
+ status = RequestConstants.ASSIGNED;
644
607
 
645
-
646
- //cacheinvalidation
647
- return newRequest.save(function(err, savedRequest) {
648
- if (err) {
649
- winston.error('RequestService error for method createWithIdAndRequester for newRequest' + JSON.stringify(newRequest), err);
650
- return reject(err);
651
- }
652
-
653
-
654
- winston.verbose("Request created",savedRequest.toObject());
655
-
656
- var endDate = new Date();
657
- winston.verbose("Performance Request created in millis: " + endDate-startDate);
658
-
659
- requestEvent.emit('request.create.simple', savedRequest);
660
-
661
- return resolve(savedRequest);
662
-
663
- });
664
- // }).catch(function(err){
665
- // return reject(err);
666
- // });
608
+ // botprefix
609
+ if (participants[0].startsWith("bot_")) {
667
610
 
611
+ hasBot = true;
612
+ winston.debug("hasBot:" + hasBot);
668
613
 
669
- });
670
- }
614
+ // botprefix
615
+ var assigned_operator_idStringBot = participants[0].replace("bot_", "");
616
+ winston.debug("assigned_operator_idStringBot:" + assigned_operator_idStringBot);
671
617
 
618
+ participantsBots.push(assigned_operator_idStringBot);
672
619
 
620
+ } else {
673
621
 
622
+ participantsAgents.push(participants[0]);
674
623
 
624
+ }
675
625
 
676
- //DEPRECATED. USED ONLY IN SAME TESTS
677
- createWithId(request_id, requester_id, id_project, first_text, departmentid, sourcePage, language, userAgent, status, createdBy, attributes) {
626
+ assigned_at = Date.now();
678
627
 
679
- // winston.debug("request_id", request_id);
628
+ } else {
680
629
 
630
+ status = RequestConstants.UNASSIGNED;
681
631
 
682
- if (!departmentid) {
683
- departmentid ='default';
684
- }
632
+ }
685
633
 
686
- if (!createdBy) {
687
- createdBy = requester_id;
688
- }
689
-
690
- var that = this;
634
+ }
691
635
 
692
- return new Promise(function (resolve, reject) {
693
636
 
694
- var context = {request: {request_id:request_id, requester_id:requester_id, id_project:id_project,
695
- first_text:first_text, departmentid:departmentid, sourcePage:sourcePage, language:language, userAgent:userAgent, status:status,
696
- createdBy:createdBy, attributes:attributes}};
697
637
 
698
- // getOperators(departmentid, projectid, nobot, disableWebHookCall, context)
699
638
 
700
- return departmentService.getOperators(departmentid, id_project, false, undefined, context).then(function (result) {
639
+ if (dep_id) {
640
+ snapshot.department = result.department;
641
+ }
701
642
 
702
- // winston.debug("getOperators", result);
643
+ // console.log("result.agents",result.agents);
644
+ snapshot.agents = agents;
645
+ snapshot.availableAgentsCount = that.getAvailableAgentsCount(agents);
703
646
 
704
- var status = RequestConstants.UNASSIGNED;
705
- var assigned_operator_id;
706
- var participants = [];
707
- var participantsAgents = [];
708
- var participantsBots = [];
709
- var hasBot = false;
647
+ if (request.requester) { //.toObject()????
648
+ snapshot.requester = request.requester;
649
+ }
650
+ if (request.lead) {
651
+ snapshot.lead = request.lead;
652
+ }
710
653
 
711
- var assigned_at = undefined;
712
- if (result.operators && result.operators.length>0) {
713
- assigned_operator_id = result.operators[0].id_user;
714
- status = RequestConstants.ASSIGNED;
654
+ // winston.debug("assigned_operator_id", assigned_operator_id);
655
+ // winston.debug("req status", status);
656
+
657
+ var newRequest = new Request({
658
+ request_id: request_id,
659
+ requester: project_user_id,
660
+ lead: lead_id,
661
+ first_text: first_text,
662
+ subject: subject,
663
+ status: status,
664
+ participants: participants,
665
+ participantsAgents: participantsAgents,
666
+ participantsBots: participantsBots,
667
+ hasBot: hasBot,
668
+ department: dep_id,
669
+ // agents: agents,
670
+
671
+ //others
672
+ sourcePage: sourcePage,
673
+ language: language,
674
+ userAgent: userAgent,
675
+ assigned_at: assigned_at,
676
+
677
+ attributes: attributes,
678
+ //standard
679
+ id_project: id_project,
680
+ createdBy: createdBy,
681
+ updatedBy: createdBy,
682
+ preflight: preflight,
683
+ channel: channel,
684
+ location: location,
685
+ snapshot: snapshot,
686
+ tags: tags,
687
+ notes: notes,
688
+ priority: priority,
689
+ auto_close: auto_close,
690
+ followers: followers,
691
+ createdAt: createdAt
692
+ });
715
693
 
716
- var assigned_operator_idString = assigned_operator_id.toString();
717
- participants.push(assigned_operator_idString);
694
+ winston.debug('newRequest.', newRequest);
718
695
 
719
- // botprefix
720
- if (assigned_operator_idString.startsWith("bot_")) {
721
- hasBot = true;
722
696
 
723
- // botprefix
724
- var assigned_operator_idStringBot = assigned_operator_idString.replace("bot_","");
725
- winston.debug("assigned_operator_idStringBot:"+assigned_operator_idStringBot);
726
- participantsBots.push(assigned_operator_idStringBot);
697
+ //cacheinvalidation
698
+ return newRequest.save( async function (err, savedRequest) {
727
699
 
728
- }else {
729
- participantsAgents.push(assigned_operator_idString);
730
- }
731
- assigned_at = Date.now();
700
+ if (err) {
701
+ winston.error('RequestService error for method createWithIdAndRequester for newRequest' + JSON.stringify(newRequest), err);
702
+ return reject(err);
732
703
  }
733
- // winston.debug("assigned_operator_id", assigned_operator_id);
734
- // winston.debug("status", status);
735
-
736
- var newRequest = new Request({
737
- request_id: request_id,
738
- requester_id: requester_id,
739
- first_text: first_text,
740
- status: status,
741
- participants: participants,
742
- participantsAgents:participantsAgents,
743
- participantsBots: participantsBots,
744
- hasBot:hasBot,
745
- department: result.department._id,
746
- agents: result.agents,
747
- //availableAgents: result.available_agents,
748
-
749
- // assigned_operator_id: result.assigned_operator_id,
750
-
751
- //others
752
- sourcePage: sourcePage,
753
- language: language,
754
- userAgent: userAgent,
755
- assigned_at:assigned_at,
756
- attributes: attributes,
757
- //standard
758
- id_project: id_project,
759
- createdBy: createdBy,
760
- updatedBy: createdBy
761
- });
762
-
763
704
 
764
- // winston.debug('newRequest.',newRequest);
765
705
 
706
+ winston.debug("Request created", savedRequest.toObject());
766
707
 
767
-
768
- //cacheinvalidation
769
- return newRequest.save(function(err, savedRequest) {
770
- if (err) {
771
- winston.error('RequestService error for method createWithId for newRequest' + JSON.stringify(newRequest), err);
772
- return reject(err);
773
- }
774
-
775
-
776
- winston.verbose("Request created",savedRequest.toObject());
777
-
778
-
779
- requestEvent.emit('request.create.simple',savedRequest);
780
-
781
- return resolve(savedRequest);
782
-
783
- });
784
- }).catch(function(err){
785
- return reject(err);
786
- });
708
+ var endDate = new Date();
709
+ winston.verbose("Performance Request created in millis: " + endDate - startDate);
787
710
 
711
+ requestEvent.emit('request.create.simple', savedRequest);
712
+ requestEvent.emit('request.create.quote', payload);;
788
713
 
789
- });
714
+ return resolve(savedRequest);
715
+
716
+ });
717
+ // }).catch(function(err){
718
+ // return reject(err);
719
+ // });
790
720
 
721
+ })
722
+ });
791
723
 
724
+ }
725
+
726
+
727
+ async _create(request) {
728
+
729
+ var startDate = new Date();
730
+
731
+ if (!request.createdAt) {
732
+ request.createdAt = new Date();
733
+ }
734
+
792
735
 
793
-
736
+ var request_id = request.request_id;
737
+ var project_user_id = request.project_user_id;
738
+ var lead_id = request.lead_id;
739
+ var id_project = request.id_project;
740
+
741
+ var first_text = request.first_text;
742
+
743
+ //removed for ticket
744
+ // // lascia che sia nico a fare il replace...certo tu devi fare il test che tutto sia ok quindi dopo demo
745
+ // var first_text;
746
+ // if (request.first_text) { //first_text can be empty for type image
747
+ // first_text = request.first_text.replace(/[\n\r]+/g, ' '); //replace new line with space
748
+ // }
749
+
750
+ var departmentid = request.departmentid;
751
+ var sourcePage = request.sourcePage;
752
+ var language = request.language;
753
+ var userAgent = request.userAgent;
754
+ var status = request.status;
755
+ var createdBy = request.createdBy;
756
+ var attributes = request.attributes;
757
+ var subject = request.subject;
758
+ var preflight = request.preflight;
759
+ var channel = request.channel;
760
+ var location = request.location;
761
+ var participants = request.participants || [];
762
+
763
+ var tags = request.tags;
764
+ var notes = request.notes;
765
+ var priority = request.priority;
766
+
767
+ var auto_close = request.auto_close;
768
+
769
+ var followers = request.followers;
770
+ let createdAt = request.createdAt;
771
+
772
+ if (!departmentid) {
773
+ departmentid = 'default';
774
+ }
775
+
776
+ if (!createdBy) {
777
+ if (project_user_id) {
778
+ createdBy = project_user_id;
779
+ } else {
780
+ createdBy = "system";
781
+ }
782
+
783
+ }
784
+
785
+ var that = this;
786
+
787
+ return new Promise(async (resolve, reject) => {
788
+
789
+ var context = {
790
+ request: {
791
+ request_id: request_id, project_user_id: project_user_id, lead_id: lead_id, id_project: id_project,
792
+ first_text: first_text, departmentid: departmentid, sourcePage: sourcePage, language: language, userAgent: userAgent, status: status,
793
+ createdBy: createdBy, attributes: attributes, subject: subject, preflight: preflight, channel: channel, location: location,
794
+ participants: participants, tags: tags, notes: notes,
795
+ priority: priority, auto_close: auto_close, followers: followers
796
+ }
797
+ };
798
+
799
+ winston.debug("context", context);
800
+
801
+ var participantsAgents = [];
802
+ var participantsBots = [];
803
+ var hasBot = false;
804
+
805
+ var dep_id = undefined;
806
+
807
+ var assigned_at = undefined;
808
+
809
+ var agents = [];
810
+
811
+ var snapshot = {};
812
+
813
+ try {
814
+ // getOperators(departmentid, projectid, nobot, disableWebHookCall, context) {
815
+ var result = await departmentService.getOperators(departmentid, id_project, false, undefined, context);
816
+ // console.log("************* after get operator: "+new Date().toISOString());
817
+
818
+ winston.debug("getOperators", result);
819
+ } catch (err) {
820
+ return reject(err);
821
+ }
822
+
823
+
824
+
825
+ agents = result.agents;
826
+
827
+ if (status == 50) {
828
+ // skip assignment
829
+ if (participants.length == 0) {
830
+ dep_id = result.department._id;
831
+ }
832
+ } else {
833
+
834
+ if (participants.length == 0) {
835
+ if (result.operators && result.operators.length > 0) {
836
+ participants.push(result.operators[0].id_user.toString());
837
+ }
838
+ // for preflight it is important to save agents in req for trigger. try to optimize it
839
+ dep_id = result.department._id;
840
+
841
+ }
842
+
843
+ if (participants.length > 0) {
844
+
845
+ status = RequestConstants.ASSIGNED;
846
+
847
+ // botprefix
848
+ if (participants[0].startsWith("bot_")) {
849
+
850
+ hasBot = true;
851
+ winston.debug("hasBot:" + hasBot);
852
+
853
+ // botprefix
854
+ var assigned_operator_idStringBot = participants[0].replace("bot_", "");
855
+ winston.debug("assigned_operator_idStringBot:" + assigned_operator_idStringBot);
856
+
857
+ participantsBots.push(assigned_operator_idStringBot);
858
+
859
+ } else {
860
+
861
+ participantsAgents.push(participants[0]);
862
+
863
+ }
864
+
865
+ assigned_at = Date.now();
866
+
867
+ } else {
868
+
869
+ status = RequestConstants.UNASSIGNED;
870
+
871
+ }
872
+
873
+ }
874
+
875
+
876
+
877
+
878
+ if (dep_id) {
879
+ snapshot.department = result.department;
880
+ }
881
+
882
+ // console.log("result.agents",result.agents);
883
+ snapshot.agents = agents;
884
+ snapshot.availableAgentsCount = that.getAvailableAgentsCount(agents);
885
+
886
+ if (request.requester) { //.toObject()????
887
+ snapshot.requester = request.requester;
888
+ }
889
+ if (request.lead) {
890
+ snapshot.lead = request.lead;
891
+ }
892
+
893
+ // winston.debug("assigned_operator_id", assigned_operator_id);
894
+ // winston.debug("req status", status);
895
+
896
+ var newRequest = new Request({
897
+ request_id: request_id,
898
+ requester: project_user_id,
899
+ lead: lead_id,
900
+ first_text: first_text,
901
+ subject: subject,
902
+ status: status,
903
+ participants: participants,
904
+ participantsAgents: participantsAgents,
905
+ participantsBots: participantsBots,
906
+ hasBot: hasBot,
907
+ department: dep_id,
908
+ // agents: agents,
909
+
910
+ //others
911
+ sourcePage: sourcePage,
912
+ language: language,
913
+ userAgent: userAgent,
914
+ assigned_at: assigned_at,
915
+
916
+ attributes: attributes,
917
+ //standard
918
+ id_project: id_project,
919
+ createdBy: createdBy,
920
+ updatedBy: createdBy,
921
+ preflight: preflight,
922
+ channel: channel,
923
+ location: location,
924
+ snapshot: snapshot,
925
+ tags: tags,
926
+ notes: notes,
927
+ priority: priority,
928
+ auto_close: auto_close,
929
+ followers: followers,
930
+ createdAt: createdAt
931
+ });
932
+
933
+ winston.debug('newRequest.', newRequest);
934
+
935
+
936
+ //cacheinvalidation
937
+ return newRequest.save(function (err, savedRequest) {
938
+
939
+ if (err) {
940
+ winston.error('RequestService error for method createWithIdAndRequester for newRequest' + JSON.stringify(newRequest), err);
941
+ return reject(err);
942
+ }
943
+
944
+
945
+ winston.debug("Request created", savedRequest.toObject());
946
+
947
+ var endDate = new Date();
948
+ winston.verbose("Performance Request created in millis: " + endDate - startDate);
949
+
950
+ requestEvent.emit('request.create.simple', savedRequest);
951
+
952
+ let q = Project.findOne({ _id: request.id_project, status: 100 });
953
+ if (cacheEnabler.project) {
954
+ q.cache(cacheUtil.longTTL, "projects:id:" + request.id_project) //project_cache
955
+ winston.debug('project cache enabled for /project detail');
956
+ }
957
+ q.exec(async function (err, p) {
958
+ if (err) {
959
+ winston.error('Error getting project ', err);
960
+ }
961
+ if (!p) {
962
+ winston.warn('Project not found ');
963
+ }
964
+ //TODO REMOVE settings from project
965
+ let payload = {
966
+ project: p,
967
+ request: request
968
+ }
969
+
970
+ requestEvent.emit('request.create.quote', payload);;
971
+
972
+ });
973
+
974
+ return resolve(savedRequest);
975
+
976
+ });
977
+ // }).catch(function(err){
978
+ // return reject(err);
979
+ // });
980
+
981
+
982
+ });
983
+ }
984
+
985
+
986
+
987
+
988
+
989
+ //DEPRECATED. USED ONLY IN SAME TESTS
990
+ createWithId(request_id, requester_id, id_project, first_text, departmentid, sourcePage, language, userAgent, status, createdBy, attributes) {
991
+
992
+ // winston.debug("request_id", request_id);
993
+
994
+
995
+ if (!departmentid) {
996
+ departmentid = 'default';
997
+ }
998
+
999
+ if (!createdBy) {
1000
+ createdBy = requester_id;
1001
+ }
1002
+
1003
+ var that = this;
1004
+
1005
+ return new Promise(function (resolve, reject) {
1006
+
1007
+ var context = {
1008
+ request: {
1009
+ request_id: request_id, requester_id: requester_id, id_project: id_project,
1010
+ first_text: first_text, departmentid: departmentid, sourcePage: sourcePage, language: language, userAgent: userAgent, status: status,
1011
+ createdBy: createdBy, attributes: attributes
1012
+ }
1013
+ };
1014
+
1015
+ // getOperators(departmentid, projectid, nobot, disableWebHookCall, context)
1016
+
1017
+ return departmentService.getOperators(departmentid, id_project, false, undefined, context).then(function (result) {
1018
+
1019
+ // winston.debug("getOperators", result);
1020
+
1021
+ var status = RequestConstants.UNASSIGNED;
1022
+ var assigned_operator_id;
1023
+ var participants = [];
1024
+ var participantsAgents = [];
1025
+ var participantsBots = [];
1026
+ var hasBot = false;
1027
+
1028
+ var assigned_at = undefined;
1029
+ if (result.operators && result.operators.length > 0) {
1030
+ assigned_operator_id = result.operators[0].id_user;
1031
+ status = RequestConstants.ASSIGNED;
1032
+
1033
+ var assigned_operator_idString = assigned_operator_id.toString();
1034
+ participants.push(assigned_operator_idString);
1035
+
1036
+ // botprefix
1037
+ if (assigned_operator_idString.startsWith("bot_")) {
1038
+ hasBot = true;
1039
+
1040
+ // botprefix
1041
+ var assigned_operator_idStringBot = assigned_operator_idString.replace("bot_", "");
1042
+ winston.debug("assigned_operator_idStringBot:" + assigned_operator_idStringBot);
1043
+ participantsBots.push(assigned_operator_idStringBot);
1044
+
1045
+ } else {
1046
+ participantsAgents.push(assigned_operator_idString);
1047
+ }
1048
+ assigned_at = Date.now();
1049
+ }
1050
+ // winston.debug("assigned_operator_id", assigned_operator_id);
1051
+ // winston.debug("status", status);
1052
+
1053
+ var newRequest = new Request({
1054
+ request_id: request_id,
1055
+ requester_id: requester_id,
1056
+ first_text: first_text,
1057
+ status: status,
1058
+ participants: participants,
1059
+ participantsAgents: participantsAgents,
1060
+ participantsBots: participantsBots,
1061
+ hasBot: hasBot,
1062
+ department: result.department._id,
1063
+ agents: result.agents,
1064
+ //availableAgents: result.available_agents,
1065
+
1066
+ // assigned_operator_id: result.assigned_operator_id,
1067
+
1068
+ //others
1069
+ sourcePage: sourcePage,
1070
+ language: language,
1071
+ userAgent: userAgent,
1072
+ assigned_at: assigned_at,
1073
+ attributes: attributes,
1074
+ //standard
1075
+ id_project: id_project,
1076
+ createdBy: createdBy,
1077
+ updatedBy: createdBy
1078
+ });
1079
+
1080
+
1081
+ // winston.debug('newRequest.',newRequest);
1082
+
1083
+
1084
+
1085
+ //cacheinvalidation
1086
+ return newRequest.save(function (err, savedRequest) {
1087
+ if (err) {
1088
+ winston.error('RequestService error for method createWithId for newRequest' + JSON.stringify(newRequest), err);
1089
+ return reject(err);
1090
+ }
1091
+
1092
+
1093
+ winston.verbose("Request created", savedRequest.toObject());
1094
+
1095
+
1096
+ requestEvent.emit('request.create.simple', savedRequest);
1097
+
1098
+ return resolve(savedRequest);
1099
+
1100
+ });
1101
+ }).catch(function (err) {
1102
+ return reject(err);
1103
+ });
1104
+
1105
+
1106
+ });
1107
+
1108
+
1109
+
1110
+
794
1111
 
795
1112
  }
796
1113
 
@@ -798,31 +1115,31 @@ class RequestService {
798
1115
  changeStatusByRequestId(request_id, id_project, newstatus) {
799
1116
 
800
1117
  return new Promise(function (resolve, reject) {
801
- // winston.debug("request_id", request_id);
802
- // winston.debug("newstatus", newstatus);
1118
+ // winston.debug("request_id", request_id);
1119
+ // winston.debug("newstatus", newstatus);
803
1120
 
804
- //TODO CHECK IF ALREADY CLOSED
805
- return Request
806
- .findOneAndUpdate({request_id: request_id, id_project: id_project}, {status: newstatus}, {new: true, upsert:false})
1121
+ //TODO CHECK IF ALREADY CLOSED
1122
+ return Request
1123
+ .findOneAndUpdate({ request_id: request_id, id_project: id_project }, { status: newstatus }, { new: true, upsert: false })
807
1124
  .populate('lead')
808
1125
  .populate('department')
809
1126
  .populate('participatingBots')
810
- .populate('participatingAgents')
811
- .populate({path:'requester',populate:{path:'id_user'}})
812
- .exec( function(err, updatedRequest) {
1127
+ .populate('participatingAgents')
1128
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
1129
+ .exec(function (err, updatedRequest) {
813
1130
 
814
- if (err) {
815
- winston.error(err);
816
- return reject(err);
817
- }
1131
+ if (err) {
1132
+ winston.error(err);
1133
+ return reject(err);
1134
+ }
818
1135
 
819
- requestEvent.emit('request.update',updatedRequest); //deprecated
820
- requestEvent.emit("request.update.comment", {comment:"STATUS_CHANGE",request:updatedRequest});//Deprecated
821
- requestEvent.emit("request.updated", {comment:"STATUS_CHANGE",request:updatedRequest, patch:{status: newstatus}});
822
- //TODO emit request.clone or reopen also
1136
+ requestEvent.emit('request.update', updatedRequest); //deprecated
1137
+ requestEvent.emit("request.update.comment", { comment: "STATUS_CHANGE", request: updatedRequest });//Deprecated
1138
+ requestEvent.emit("request.updated", { comment: "STATUS_CHANGE", request: updatedRequest, patch: { status: newstatus } });
1139
+ //TODO emit request.clone or reopen also
823
1140
 
824
- return resolve(updatedRequest);
825
- });
1141
+ return resolve(updatedRequest);
1142
+ });
826
1143
  });
827
1144
 
828
1145
  }
@@ -832,38 +1149,38 @@ class RequestService {
832
1149
 
833
1150
  return new Promise(function (resolve, reject) {
834
1151
  winston.debug("changeFirstTextAndPreflightByRequestId", request_id);
835
- // winston.debug("request_id", request_id);
836
- // winston.debug("newstatus", newstatus);
1152
+ // winston.debug("request_id", request_id);
1153
+ // winston.debug("newstatus", newstatus);
837
1154
 
838
- if (!first_text) {
839
- winston.error(err);
840
- return reject({err:" Error changing first text. The field first_text is empty"});
841
- }
842
-
843
- return Request
844
- .findOneAndUpdate({request_id: request_id, id_project: id_project}, {first_text: first_text, preflight: preflight}, {new: true, upsert:false})
1155
+ if (!first_text) {
1156
+ winston.error(err);
1157
+ return reject({ err: " Error changing first text. The field first_text is empty" });
1158
+ }
1159
+
1160
+ return Request
1161
+ .findOneAndUpdate({ request_id: request_id, id_project: id_project }, { first_text: first_text, preflight: preflight }, { new: true, upsert: false })
845
1162
  .populate('lead')
846
1163
  .populate('department')
847
1164
  .populate('participatingBots')
848
- .populate('participatingAgents')
849
- .populate({path:'requester',populate:{path:'id_user'}})
850
- .exec( function(err, updatedRequest) {
1165
+ .populate('participatingAgents')
1166
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
1167
+ .exec(function (err, updatedRequest) {
851
1168
 
852
- if (err) {
853
- winston.error(err);
854
- return reject(err);
855
- }
1169
+ if (err) {
1170
+ winston.error(err);
1171
+ return reject(err);
1172
+ }
856
1173
 
857
-
858
- requestEvent.emit('request.update.preflight',updatedRequest); //archive to audit log
859
- requestEvent.emit('request.update',updatedRequest);
860
- requestEvent.emit("request.update.comment", {comment:"FIRSTTEXT_PREFLIGHT_CHANGE",request:updatedRequest});//Deprecated
861
- requestEvent.emit("request.updated", {comment:"FIRSTTEXT_PREFLIGHT_CHANGE",request:updatedRequest, patch: {first_text: first_text, preflight: preflight}});
862
1174
 
863
- //TODO emit request.clone or reopen also
1175
+ requestEvent.emit('request.update.preflight', updatedRequest); //archive to audit log
1176
+ requestEvent.emit('request.update', updatedRequest);
1177
+ requestEvent.emit("request.update.comment", { comment: "FIRSTTEXT_PREFLIGHT_CHANGE", request: updatedRequest });//Deprecated
1178
+ requestEvent.emit("request.updated", { comment: "FIRSTTEXT_PREFLIGHT_CHANGE", request: updatedRequest, patch: { first_text: first_text, preflight: preflight } });
864
1179
 
865
- return resolve(updatedRequest);
866
- });
1180
+ //TODO emit request.clone or reopen also
1181
+
1182
+ return resolve(updatedRequest);
1183
+ });
867
1184
  });
868
1185
 
869
1186
  }
@@ -871,30 +1188,30 @@ class RequestService {
871
1188
  changeFirstTextByRequestId(request_id, id_project, first_text) {
872
1189
 
873
1190
  return new Promise(function (resolve, reject) {
874
- // winston.debug("request_id", request_id);
875
- // winston.debug("newstatus", newstatus);
1191
+ // winston.debug("request_id", request_id);
1192
+ // winston.debug("newstatus", newstatus);
876
1193
 
877
- return Request
878
- .findOneAndUpdate({request_id: request_id, id_project: id_project}, {first_text: first_text}, {new: true, upsert:false})
1194
+ return Request
1195
+ .findOneAndUpdate({ request_id: request_id, id_project: id_project }, { first_text: first_text }, { new: true, upsert: false })
879
1196
  .populate('lead')
880
1197
  .populate('department')
881
1198
  .populate('participatingBots')
882
- .populate('participatingAgents')
883
- .populate({path:'requester',populate:{path:'id_user'}})
884
- .exec( function(err, updatedRequest) {
1199
+ .populate('participatingAgents')
1200
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
1201
+ .exec(function (err, updatedRequest) {
885
1202
 
886
- if (err) {
887
- winston.error(err);
888
- return reject(err);
889
- }
890
- requestEvent.emit('request.update',updatedRequest);
891
- requestEvent.emit("request.update.comment", {comment:"FIRSTTEXT_CHANGE",request:updatedRequest});//Deprecated
892
- requestEvent.emit("request.updated", {comment:"FIRSTTEXT_CHANGE",request:updatedRequest, patch: {first_text: first_text}});
1203
+ if (err) {
1204
+ winston.error(err);
1205
+ return reject(err);
1206
+ }
1207
+ requestEvent.emit('request.update', updatedRequest);
1208
+ requestEvent.emit("request.update.comment", { comment: "FIRSTTEXT_CHANGE", request: updatedRequest });//Deprecated
1209
+ requestEvent.emit("request.updated", { comment: "FIRSTTEXT_CHANGE", request: updatedRequest, patch: { first_text: first_text } });
893
1210
 
894
- //TODO emit request.clone or reopen also
1211
+ //TODO emit request.clone or reopen also
895
1212
 
896
- return resolve(updatedRequest);
897
- });
1213
+ return resolve(updatedRequest);
1214
+ });
898
1215
  });
899
1216
 
900
1217
  }
@@ -904,28 +1221,28 @@ class RequestService {
904
1221
  changePreflightByRequestId(request_id, id_project, preflight) {
905
1222
 
906
1223
  return new Promise(function (resolve, reject) {
907
- // winston.debug("request_id", request_id);
908
- // winston.debug("newstatus", newstatus);
1224
+ // winston.debug("request_id", request_id);
1225
+ // winston.debug("newstatus", newstatus);
909
1226
 
910
- return Request
911
- .findOneAndUpdate({request_id: request_id, id_project: id_project}, {preflight: preflight}, {new: true, upsert:false})
1227
+ return Request
1228
+ .findOneAndUpdate({ request_id: request_id, id_project: id_project }, { preflight: preflight }, { new: true, upsert: false })
912
1229
  .populate('lead')
913
1230
  .populate('department')
914
1231
  .populate('participatingBots')
915
- .populate('participatingAgents')
916
- .populate({path:'requester',populate:{path:'id_user'}})
917
- .exec( function(err, updatedRequest) {
1232
+ .populate('participatingAgents')
1233
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
1234
+ .exec(function (err, updatedRequest) {
918
1235
 
919
- if (err) {
920
- winston.error(err);
921
- return reject(err);
922
- }
923
- requestEvent.emit('request.update',updatedRequest);
924
- requestEvent.emit("request.update.comment", {comment:"PREFLIGHT_CHANGE",request:updatedRequest});//Deprecated
925
- requestEvent.emit("request.updated", {comment:"PREFLIGHT_CHANGE",request:updatedRequest, patch: {preflight: preflight}});
1236
+ if (err) {
1237
+ winston.error(err);
1238
+ return reject(err);
1239
+ }
1240
+ requestEvent.emit('request.update', updatedRequest);
1241
+ requestEvent.emit("request.update.comment", { comment: "PREFLIGHT_CHANGE", request: updatedRequest });//Deprecated
1242
+ requestEvent.emit("request.updated", { comment: "PREFLIGHT_CHANGE", request: updatedRequest, patch: { preflight: preflight } });
926
1243
 
927
- return resolve(updatedRequest);
928
- });
1244
+ return resolve(updatedRequest);
1245
+ });
929
1246
  });
930
1247
 
931
1248
  }
@@ -933,76 +1250,76 @@ class RequestService {
933
1250
  setClosedAtByRequestId(request_id, id_project, closed_at, closed_by) {
934
1251
 
935
1252
  return new Promise(function (resolve, reject) {
936
- // winston.debug("request_id", request_id);
937
- // winston.debug("newstatus", newstatus);
1253
+ // winston.debug("request_id", request_id);
1254
+ // winston.debug("newstatus", newstatus);
938
1255
 
939
- return Request
940
- .findOneAndUpdate({request_id: request_id, id_project: id_project}, {closed_at: closed_at, closed_by: closed_by}, {new: true, upsert:false})
1256
+ return Request
1257
+ .findOneAndUpdate({ request_id: request_id, id_project: id_project }, { closed_at: closed_at, closed_by: closed_by }, { new: true, upsert: false })
941
1258
  .populate('lead')
942
1259
  .populate('department')
943
1260
  .populate('participatingBots')
944
- .populate('participatingAgents')
945
- .populate({path:'requester',populate:{path:'id_user'}})
946
- .exec( function(err, updatedRequest) {
947
- if (err) {
948
- winston.error(err);
949
- return reject(err);
950
- }
1261
+ .populate('participatingAgents')
1262
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
1263
+ .exec(function (err, updatedRequest) {
1264
+ if (err) {
1265
+ winston.error(err);
1266
+ return reject(err);
1267
+ }
951
1268
 
952
- // winston.debug("updatedRequest", updatedRequest);
953
- return resolve(updatedRequest);
954
- });
1269
+ // winston.debug("updatedRequest", updatedRequest);
1270
+ return resolve(updatedRequest);
1271
+ });
1272
+ });
1273
+
1274
+ }
1275
+
1276
+
1277
+
1278
+ // unused
1279
+ incrementMessagesCountByRequestId(request_id, id_project) {
1280
+
1281
+ return new Promise(function (resolve, reject) {
1282
+ // winston.debug("request_id", request_id);
1283
+ // winston.debug("newstatus", newstatus);
1284
+
1285
+ return Request
1286
+ .findOneAndUpdate({ request_id: request_id, id_project: id_project }, { $inc: { 'messages.messages_count': 1 } }, { new: true, upsert: false }, function (err, updatedRequest) {
1287
+ if (err) {
1288
+ winston.error(err);
1289
+ return reject(err);
1290
+ }
1291
+ winston.debug("Message count +1");
1292
+ return resolve(updatedRequest);
1293
+ });
955
1294
  });
956
1295
 
957
1296
  }
958
1297
 
959
-
960
-
961
- // unused
962
- incrementMessagesCountByRequestId(request_id, id_project) {
963
-
964
- return new Promise(function (resolve, reject) {
965
- // winston.debug("request_id", request_id);
966
- // winston.debug("newstatus", newstatus);
967
-
968
- return Request
969
- .findOneAndUpdate({request_id: request_id, id_project: id_project}, {$inc : {'messages.messages_count' : 1}}, {new: true, upsert:false}, function(err, updatedRequest) {
970
- if (err) {
971
- winston.error(err);
972
- return reject(err);
973
- }
974
- winston.debug("Message count +1");
975
- return resolve(updatedRequest);
976
- });
977
- });
978
-
979
- }
980
-
981
1298
  updateWaitingTimeByRequestId(request_id, id_project, enable_populate) {
982
1299
 
983
1300
  return new Promise(function (resolve, reject) {
984
- // winston.debug("request_id", request_id);
985
- // winston.debug("newstatus", newstatus);
1301
+ // winston.debug("request_id", request_id);
1302
+ // winston.debug("newstatus", newstatus);
986
1303
 
987
- let q = Request
988
- .findOne({request_id: request_id, id_project: id_project});
1304
+ let q = Request
1305
+ .findOne({ request_id: request_id, id_project: id_project });
989
1306
 
990
- if (enable_populate==true) {
1307
+ if (enable_populate == true) {
991
1308
  winston.debug("updateWaitingTimeByRequestId enable_populate");
992
1309
 
993
1310
  q.populate('lead')
994
- .populate('department')
995
- .populate('participatingBots')
996
- .populate('participatingAgents')
997
- .populate({path:'requester',populate:{path:'id_user'}});
1311
+ .populate('department')
1312
+ .populate('participatingBots')
1313
+ .populate('participatingAgents')
1314
+ .populate({ path: 'requester', populate: { path: 'id_user' } });
998
1315
  }
999
-
1316
+
1000
1317
 
1001
1318
  // if (cacheEnabler.request) { //attention this cache is not usable bacause cacheoose don't support populate without .lean.. so if cached populated field is not returned with cacheoose, updateWaitingTime is only used in chat21webhook but i thik it is important for messages route
1002
1319
  // q.cache(cacheUtil.defaultTTL, id_project+":requests:request_id:"+request_id) //request_cache
1003
1320
  // winston.debug('request cache enabled');
1004
1321
  // }
1005
- q.exec(function(err, request) {
1322
+ q.exec(function (err, request) {
1006
1323
  if (err) {
1007
1324
  winston.error(err);
1008
1325
  return reject(err);
@@ -1012,18 +1329,18 @@ class RequestService {
1012
1329
  var now = Date.now();
1013
1330
  var waitingTime = now - request.createdAt;
1014
1331
  // winston.debug("waitingTime", waitingTime);
1015
-
1016
-
1332
+
1333
+
1017
1334
  request.waiting_time = waitingTime;
1018
1335
  // TODO REENABLE SERVED
1019
1336
  // request.status = RequestConstants.SERVED;
1020
1337
  request.first_response_at = now;
1021
1338
 
1022
- // winston.debug(" request", request);
1023
- winston.debug("Request waitingTime setted");
1024
- //cacheinvalidation
1339
+ // winston.debug(" request", request);
1340
+ winston.debug("Request waitingTime setted");
1341
+ //cacheinvalidation
1025
1342
  // return resolve(request.save());
1026
- request.save(function(err, savedRequest) {
1343
+ request.save(function (err, savedRequest) {
1027
1344
  if (err) {
1028
1345
  return reject(err);
1029
1346
  }
@@ -1034,10 +1351,10 @@ class RequestService {
1034
1351
 
1035
1352
 
1036
1353
 
1037
- }else {
1354
+ } else {
1038
1355
  return resolve(request);
1039
1356
  }
1040
-
1357
+
1041
1358
  });
1042
1359
 
1043
1360
  });
@@ -1049,56 +1366,56 @@ class RequestService {
1049
1366
 
1050
1367
  var that = this;
1051
1368
  return new Promise(function (resolve, reject) {
1052
- // winston.debug("request_id", request_id);
1053
-
1054
- if (force==undefined) {
1055
- winston.debug("force is undefined ");
1056
- force = false;
1057
- }
1058
- // else {
1059
- // winston.info("force is: " + force);
1060
- // }
1061
-
1062
- return Request
1063
- .findOne({request_id: request_id, id_project: id_project})
1064
-
1065
- .populate('lead')
1066
- .populate('department')
1067
- .populate('participatingBots')
1068
- .populate('participatingAgents')
1069
- .populate({path:'requester',populate:{path:'id_user'}})
1070
- .exec( function(err, request) {
1071
-
1072
-
1073
- if (err){
1074
- winston.error("Error getting closing request ", err);
1075
- return reject(err);
1076
- }
1077
- if (!request) {
1078
- winston.error("Request not found for request_id "+ request_id + " and id_project " + id_project);
1079
- return reject({"success":false, msg:"Request not found for request_id "+ request_id + " and id_project " + id_project});
1080
- }
1081
- if (force == false && request.status == RequestConstants.CLOSED) {
1082
- // qui1000
1083
- // if (request.statusObj.closed) {
1084
- winston.debug("Request already closed for request_id "+ request_id + " and id_project " + id_project);
1085
- return resolve(request);
1086
- }
1369
+ // winston.debug("request_id", request_id);
1087
1370
 
1088
- winston.debug("sono qui");
1371
+ if (force == undefined) {
1372
+ winston.debug("force is undefined ");
1373
+ force = false;
1374
+ }
1375
+ // else {
1376
+ // winston.info("force is: " + force);
1377
+ // }
1378
+
1379
+ return Request
1380
+ .findOne({ request_id: request_id, id_project: id_project })
1381
+
1382
+ .populate('lead')
1383
+ .populate('department')
1384
+ .populate('participatingBots')
1385
+ .populate('participatingAgents')
1386
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
1387
+ .exec(function (err, request) {
1388
+
1389
+
1390
+ if (err) {
1391
+ winston.error("Error getting closing request ", err);
1392
+ return reject(err);
1393
+ }
1394
+ if (!request) {
1395
+ winston.error("Request not found for request_id " + request_id + " and id_project " + id_project);
1396
+ return reject({ "success": false, msg: "Request not found for request_id " + request_id + " and id_project " + id_project });
1397
+ }
1398
+ if (force == false && request.status == RequestConstants.CLOSED) {
1399
+ // qui1000
1400
+ // if (request.statusObj.closed) {
1401
+ winston.debug("Request already closed for request_id " + request_id + " and id_project " + id_project);
1402
+ return resolve(request);
1403
+ }
1404
+
1405
+ winston.debug("sono qui");
1406
+
1407
+ // un utente può chiudere se appartiene a participatingAgents oppure meglio agents del progetto?
1408
+
1409
+
1410
+ return that.changeStatusByRequestId(request_id, id_project, 1000).then(function (updatedRequest) {
1411
+ // qui1000
1412
+ // return that.changeStatusByRequestId(request_id, id_project, {closed:true}).then(function(updatedRequest) {
1089
1413
 
1090
- // un utente può chiudere se appartiene a participatingAgents oppure meglio agents del progetto?
1091
-
1092
-
1093
- return that.changeStatusByRequestId(request_id, id_project, 1000).then(function(updatedRequest) {
1094
- // qui1000
1095
- // return that.changeStatusByRequestId(request_id, id_project, {closed:true}).then(function(updatedRequest) {
1096
-
1097
1414
 
1098
1415
  // winston.debug("updatedRequest", updatedRequest);
1099
- return messageService.getTranscriptByRequestId(request_id, id_project).then(function(transcript) {
1100
- // winston.debug("transcript", transcript);
1101
- return that.updateTrascriptByRequestId(request_id, id_project, transcript).then(function(updatedRequest) {
1416
+ return messageService.getTranscriptByRequestId(request_id, id_project).then(function (transcript) {
1417
+ // winston.debug("transcript", transcript);
1418
+ return that.updateTrascriptByRequestId(request_id, id_project, transcript).then(function (updatedRequest) {
1102
1419
 
1103
1420
 
1104
1421
  if (skipStatsUpdate) {
@@ -1107,18 +1424,18 @@ class RequestService {
1107
1424
  winston.debug("Request closed ", updatedRequest);
1108
1425
  //TODO ?? requestEvent.emit('request.update', updatedRequest);
1109
1426
  requestEvent.emit('request.close', updatedRequest);
1110
- requestEvent.emit('request.close.extended', {request: updatedRequest, notify: notify});
1427
+ requestEvent.emit('request.close.extended', { request: updatedRequest, notify: notify });
1111
1428
  return resolve(updatedRequest);
1112
1429
  }
1113
-
1430
+
1114
1431
  // setClosedAtByRequestId(request_id, id_project, closed_at, closed_by)
1115
- return that.setClosedAtByRequestId(request_id, id_project, new Date().getTime(), closed_by).then(function(updatedRequest) {
1116
-
1117
- winston.verbose("Request closed with id: " + updatedRequest.id);
1118
- winston.debug("Request closed ", updatedRequest);
1119
- //TODO ?? requestEvent.emit('request.update', updatedRequest);
1120
- requestEvent.emit('request.close', updatedRequest);
1121
- requestEvent.emit('request.close.extended', {request: updatedRequest, notify: notify});
1432
+ return that.setClosedAtByRequestId(request_id, id_project, new Date().getTime(), closed_by).then(function (updatedRequest) {
1433
+
1434
+ winston.verbose("Request closed with id: " + updatedRequest.id);
1435
+ winston.debug("Request closed ", updatedRequest);
1436
+ //TODO ?? requestEvent.emit('request.update', updatedRequest);
1437
+ requestEvent.emit('request.close', updatedRequest);
1438
+ requestEvent.emit('request.close.extended', { request: updatedRequest, notify: notify });
1122
1439
 
1123
1440
  return resolve(updatedRequest);
1124
1441
  });
@@ -1127,9 +1444,9 @@ class RequestService {
1127
1444
 
1128
1445
  });
1129
1446
  });
1130
- }).catch(function(err) {
1447
+ }).catch(function (err) {
1131
1448
  winston.error(err);
1132
- return reject(err);
1449
+ return reject(err);
1133
1450
  });
1134
1451
  });
1135
1452
  });
@@ -1141,55 +1458,55 @@ class RequestService {
1141
1458
 
1142
1459
  var that = this;
1143
1460
  return new Promise(function (resolve, reject) {
1144
- // winston.debug("request_id", request_id);
1145
-
1146
- return Request
1147
- .findOne({request_id: request_id, id_project: id_project})
1148
-
1461
+ // winston.debug("request_id", request_id);
1462
+
1463
+ return Request
1464
+ .findOne({ request_id: request_id, id_project: id_project })
1465
+
1149
1466
  .populate('lead')
1150
1467
  .populate('department')
1151
1468
  .populate('participatingBots')
1152
- .populate('participatingAgents')
1153
- .populate({path:'requester',populate:{path:'id_user'}})
1154
- .exec( function(err, request) {
1469
+ .populate('participatingAgents')
1470
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
1471
+ .exec(function (err, request) {
1155
1472
 
1156
-
1157
- if (err){
1473
+
1474
+ if (err) {
1158
1475
  winston.error("Error getting reopened request ", err);
1159
1476
  return reject(err);
1160
1477
  }
1161
1478
  if (!request) {
1162
- winston.error("Request not found for request_id "+ request_id + " and id_project " + id_project);
1163
- return reject({"success":false, msg:"Request not found for request_id "+ request_id + " and id_project " + id_project});
1479
+ winston.error("Request not found for request_id " + request_id + " and id_project " + id_project);
1480
+ return reject({ "success": false, msg: "Request not found for request_id " + request_id + " and id_project " + id_project });
1164
1481
  }
1165
1482
 
1166
- if (request.status == RequestConstants.ASSIGNED || request.status == RequestConstants.UNASSIGNED
1483
+ if (request.status == RequestConstants.ASSIGNED || request.status == RequestConstants.UNASSIGNED
1167
1484
  // TODO REENABLE SERVED
1168
1485
  // || request.status == RequestConstants.SERVED
1169
- ) {
1170
- winston.debug("request already open");
1486
+ ) {
1487
+ winston.debug("request already open");
1171
1488
  return resolve(request);
1172
1489
  }
1173
1490
 
1174
- if (request.participants.length>0) {
1175
- request.status = RequestConstants.ASSIGNED;
1491
+ if (request.participants.length > 0) {
1492
+ request.status = RequestConstants.ASSIGNED;
1176
1493
  // assigned_at?
1177
1494
  } else {
1178
- request.status = RequestConstants.UNASSIGNED;
1495
+ request.status = RequestConstants.UNASSIGNED;
1179
1496
  }
1180
1497
  // TODO REENABLE SERVED
1181
1498
  // attento served qui????forse no
1182
1499
 
1183
- //cacheinvalidation
1184
- request.save(function(err, savedRequest) {
1500
+ //cacheinvalidation
1501
+ request.save(function (err, savedRequest) {
1185
1502
  if (err) {
1186
1503
  winston.error("Error saving reopened the request", err);
1187
- return reject(err);
1188
- }
1189
-
1504
+ return reject(err);
1505
+ }
1506
+
1190
1507
  requestEvent.emit('request.update', savedRequest);
1191
- requestEvent.emit("request.update.comment", {comment:"REOPEN",request:savedRequest});//Deprecated
1192
- requestEvent.emit("request.updated", {comment:"REOPEN",request:savedRequest, patch: {status: savedRequest.status}});
1508
+ requestEvent.emit("request.update.comment", { comment: "REOPEN", request: savedRequest });//Deprecated
1509
+ requestEvent.emit("request.updated", { comment: "REOPEN", request: savedRequest, patch: { status: savedRequest.status } });
1193
1510
 
1194
1511
  requestEvent.emit('request.reopen', savedRequest);
1195
1512
 
@@ -1197,18 +1514,18 @@ class RequestService {
1197
1514
 
1198
1515
  // TODO allora neanche qui participatingAgent è ok?
1199
1516
 
1200
-
1517
+
1201
1518
  return resolve(savedRequest);
1202
-
1519
+
1203
1520
  });
1204
1521
 
1205
-
1522
+
1206
1523
 
1207
1524
  })
1208
- // .catch(function(err) {
1209
- // winston.error("Error reopening the request", err);
1210
- // return reject(err);
1211
- // });
1525
+ // .catch(function(err) {
1526
+ // winston.error("Error reopening the request", err);
1527
+ // return reject(err);
1528
+ // });
1212
1529
  });
1213
1530
 
1214
1531
  }
@@ -1219,22 +1536,22 @@ class RequestService {
1219
1536
  // winston.debug("request_id", request_id);
1220
1537
  // winston.debug("transcript", transcript);
1221
1538
 
1222
- //cacheinvalidation
1223
- return Request.findOneAndUpdate({request_id: request_id, id_project: id_project}, {transcript: transcript}, {new: true, upsert:false}, function(err, updatedRequest) {
1224
- if (err) {
1225
- winston.error(err);
1226
- return reject(err);
1227
- }
1228
- // winston.debug("updatedRequest", updatedRequest);
1229
- return resolve(updatedRequest);
1230
- });
1539
+ //cacheinvalidation
1540
+ return Request.findOneAndUpdate({ request_id: request_id, id_project: id_project }, { transcript: transcript }, { new: true, upsert: false }, function (err, updatedRequest) {
1541
+ if (err) {
1542
+ winston.error(err);
1543
+ return reject(err);
1544
+ }
1545
+ // winston.debug("updatedRequest", updatedRequest);
1546
+ return resolve(updatedRequest);
1547
+ });
1231
1548
  });
1232
1549
 
1233
1550
  }
1234
1551
 
1235
1552
  findByRequestId(request_id, id_project) {
1236
1553
  return new Promise(function (resolve, reject) {
1237
- return Request.findOne({request_id: request_id, id_project: id_project}, function(err, request) {
1554
+ return Request.findOne({ request_id: request_id, id_project: id_project }, function (err, request) {
1238
1555
  if (err) {
1239
1556
  return reject(err);
1240
1557
  }
@@ -1247,141 +1564,143 @@ class RequestService {
1247
1564
 
1248
1565
 
1249
1566
  setParticipantsByRequestId(request_id, id_project, newparticipants) {
1250
-
1567
+
1251
1568
  //TODO validate participants
1252
1569
  // validate if array of string newparticipants
1253
1570
  return new Promise(function (resolve, reject) {
1254
-
1571
+
1255
1572
  var isArray = Array.isArray(newparticipants);
1256
1573
 
1257
- if(isArray==false) {
1258
- winston.error('setParticipantsByRequestId error newparticipants is not an array for request_id '+ request_id + ' and id_project '+ id_project);
1259
- return reject('setParticipantsByRequestId error newparticipants is not an array for request_id '+ request_id + ' and id_project '+ id_project);
1574
+ if (isArray == false) {
1575
+ winston.error('setParticipantsByRequestId error newparticipants is not an array for request_id ' + request_id + ' and id_project ' + id_project);
1576
+ return reject('setParticipantsByRequestId error newparticipants is not an array for request_id ' + request_id + ' and id_project ' + id_project);
1260
1577
  }
1261
1578
 
1262
1579
  return Request
1263
-
1264
- .findOne({request_id: request_id, id_project: id_project})
1265
- // qui cache ok
1266
- .exec( function(err, request) {
1267
- if (err) {
1268
- winston.error("Error setParticipantsByRequestId", err);
1269
- return reject(err);
1270
- }
1271
- if (!request) {
1272
- winston.error('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1273
- return reject('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1274
- }
1275
- var oldParticipants = request.participants;
1276
- winston.debug('oldParticipants', oldParticipants);
1277
- winston.debug('newparticipants', newparticipants);
1278
-
1279
- if (requestUtil.arraysEqual(oldParticipants, newparticipants)){
1280
- //if (oldParticipants === newparticipants) {
1281
- winston.verbose('Request members '+ oldParticipants+ ' already equal to ' + newparticipants + ' for request_id '+ request_id + ' and id_project '+ id_project);
1282
- return request
1283
- .populate('lead')
1284
- .populate('department')
1285
- .populate('participatingBots')
1286
- .populate('participatingAgents')
1287
- .populate({path:'requester',populate:{path:'id_user'}})
1288
- .execPopulate( function(err, requestComplete) {
1289
- return resolve(requestComplete);
1290
- });
1291
1580
 
1292
- }
1581
+ .findOne({ request_id: request_id, id_project: id_project })
1582
+ // qui cache ok
1583
+ .exec(function (err, request) {
1584
+ if (err) {
1585
+ winston.error("Error setParticipantsByRequestId", err);
1586
+ return reject(err);
1587
+ }
1588
+ if (!request) {
1589
+ winston.error('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
1590
+ return reject('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
1591
+ }
1592
+ var oldParticipants = request.participants;
1593
+ winston.debug('oldParticipants', oldParticipants);
1594
+ winston.debug('newparticipants', newparticipants);
1595
+
1596
+ if (requestUtil.arraysEqual(oldParticipants, newparticipants)) {
1597
+ //if (oldParticipants === newparticipants) {
1598
+ winston.verbose('Request members ' + oldParticipants + ' already equal to ' + newparticipants + ' for request_id ' + request_id + ' and id_project ' + id_project);
1599
+ return request
1600
+ .populate('lead')
1601
+ .populate('department')
1602
+ .populate('participatingBots')
1603
+ .populate('participatingAgents')
1604
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
1605
+ .execPopulate(function (err, requestComplete) {
1606
+ return resolve(requestComplete);
1607
+ });
1293
1608
 
1294
- request.participants = newparticipants;
1609
+ }
1295
1610
 
1296
- var newparticipantsAgents = [];
1297
- var newparticipantsBots = [];
1611
+ request.participants = newparticipants;
1298
1612
 
1613
+ var newparticipantsAgents = [];
1614
+ var newparticipantsBots = [];
1299
1615
 
1300
- if (newparticipants && newparticipants.length>0) {
1301
- var hasBot = false;
1302
- newparticipants.forEach(newparticipant => {
1303
- // botprefix
1304
- if (newparticipant.startsWith("bot_")) {
1305
- hasBot = true;
1306
- // botprefix
1307
- var assigned_operator_idStringBot = newparticipant.replace("bot_","");
1308
- winston.debug("assigned_operator_idStringBot:"+assigned_operator_idStringBot);
1309
- newparticipantsBots.push(assigned_operator_idStringBot);
1310
-
1311
- }else {
1312
- newparticipantsAgents.push(newparticipant);
1313
- }
1314
- });
1315
- request.hasBot = hasBot;
1316
- }
1317
1616
 
1318
- request.participantsAgents = newparticipantsAgents;
1319
- request.participantsBots = newparticipantsBots;
1320
-
1321
-
1617
+ if (newparticipants && newparticipants.length > 0) {
1618
+ var hasBot = false;
1619
+ newparticipants.forEach(newparticipant => {
1620
+ // botprefix
1621
+ if (newparticipant.startsWith("bot_")) {
1622
+ hasBot = true;
1623
+ // botprefix
1624
+ var assigned_operator_idStringBot = newparticipant.replace("bot_", "");
1625
+ winston.debug("assigned_operator_idStringBot:" + assigned_operator_idStringBot);
1626
+ newparticipantsBots.push(assigned_operator_idStringBot);
1322
1627
 
1628
+ } else {
1629
+ newparticipantsAgents.push(newparticipant);
1630
+ }
1631
+ });
1632
+ request.hasBot = hasBot;
1633
+ }
1323
1634
 
1635
+ request.participantsAgents = newparticipantsAgents;
1636
+ request.participantsBots = newparticipantsBots;
1324
1637
 
1325
- if (request.participants.length>0) {
1326
- request.status = RequestConstants.ASSIGNED;
1327
- // assigned_at?
1328
- } else {
1329
- request.status = RequestConstants.UNASSIGNED;
1330
- }
1331
-
1332
- request.waiting_time = undefined //reset waiting_time on reroute ????
1333
- // TODO REENABLE SERVED
1334
- // qui potrebbe essere che la richiesta era served con i vecchi agenti e poi facendo setParticipants si riporta ad assigned o unassigned perdendo l'informazione di served
1335
1638
 
1336
- //cacheinvalidation
1337
- return request.save(function(err, updatedRequest) {
1338
- // dopo save non aggiorna participating
1339
- if (err) {
1340
- winston.error("Error setParticipantsByRequestId", err);
1341
- return reject(err);
1639
+
1640
+
1641
+
1642
+ if (request.participants.length > 0) {
1643
+ request.status = RequestConstants.ASSIGNED;
1644
+ // assigned_at?
1645
+ } else {
1646
+ request.status = RequestConstants.UNASSIGNED;
1342
1647
  }
1343
-
1344
- return updatedRequest
1345
- .populate('lead')
1346
- .populate('department')
1347
- .populate('participatingBots')
1348
- .populate('participatingAgents')
1349
- .populate({path:'requester',populate:{path:'id_user'}})
1350
- .execPopulate( function(err, requestComplete) {
1351
1648
 
1649
+ request.waiting_time = undefined //reset waiting_time on reroute ????
1650
+ // TODO REENABLE SERVED
1651
+ // qui potrebbe essere che la richiesta era served con i vecchi agenti e poi facendo setParticipants si riporta ad assigned o unassigned perdendo l'informazione di served
1352
1652
 
1653
+ //cacheinvalidation
1654
+ return request.save(function (err, updatedRequest) {
1655
+ // dopo save non aggiorna participating
1353
1656
  if (err) {
1354
- winston.error("Error getting setParticipantsByRequestId", err);
1657
+ winston.error("Error setParticipantsByRequestId", err);
1355
1658
  return reject(err);
1356
1659
  }
1357
1660
 
1358
- winston.debug("oldParticipants ", oldParticipants);
1661
+ return updatedRequest
1662
+ .populate('lead')
1663
+ .populate('department')
1664
+ .populate('participatingBots')
1665
+ .populate('participatingAgents')
1666
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
1667
+ .execPopulate(function (err, requestComplete) {
1668
+
1669
+
1670
+ if (err) {
1671
+ winston.error("Error getting setParticipantsByRequestId", err);
1672
+ return reject(err);
1673
+ }
1674
+
1675
+ winston.debug("oldParticipants ", oldParticipants);
1359
1676
 
1360
- let newParticipants = requestComplete.participants;
1361
- winston.debug("newParticipants ", newParticipants);
1677
+ let newParticipants = requestComplete.participants;
1678
+ winston.debug("newParticipants ", newParticipants);
1362
1679
 
1363
- var removedParticipants = oldParticipants.filter(d => !newParticipants.includes(d));
1364
- winston.debug("removedParticipants ", removedParticipants);
1680
+ var removedParticipants = oldParticipants.filter(d => !newParticipants.includes(d));
1681
+ winston.debug("removedParticipants ", removedParticipants);
1365
1682
 
1366
- var addedParticipants = newParticipants.filter(d => !oldParticipants.includes(d));
1367
- winston.debug("addedParticipants ", addedParticipants);
1683
+ var addedParticipants = newParticipants.filter(d => !oldParticipants.includes(d));
1684
+ winston.debug("addedParticipants ", addedParticipants);
1368
1685
 
1369
1686
 
1370
- requestEvent.emit('request.update', requestComplete);
1371
- requestEvent.emit("request.update.comment", {comment:"PARTICIPANTS_SET",request:requestComplete});//Deprecated
1372
- requestEvent.emit("request.updated", {comment:"PARTICIPANTS_SET",request:requestComplete, patch: {removedParticipants:removedParticipants, addedParticipants:addedParticipants}});
1687
+ requestEvent.emit('request.update', requestComplete);
1688
+ requestEvent.emit("request.update.comment", { comment: "PARTICIPANTS_SET", request: requestComplete });//Deprecated
1689
+ requestEvent.emit("request.updated", { comment: "PARTICIPANTS_SET", request: requestComplete, patch: { removedParticipants: removedParticipants, addedParticipants: addedParticipants } });
1690
+
1691
+ requestEvent.emit('request.participants.update', {
1692
+ beforeRequest: request,
1693
+ removedParticipants: removedParticipants,
1694
+ addedParticipants: addedParticipants,
1695
+ request: requestComplete
1696
+ });
1373
1697
 
1374
- requestEvent.emit('request.participants.update', {beforeRequest:request,
1375
- removedParticipants:removedParticipants,
1376
- addedParticipants:addedParticipants,
1377
- request:requestComplete});
1698
+ // TODO allora neanche qui participatingAgent è ok?
1699
+ return resolve(requestComplete);
1700
+ });
1701
+ });
1378
1702
 
1379
- // TODO allora neanche qui participatingAgent è ok?
1380
- return resolve(requestComplete);
1381
- });
1382
1703
  });
1383
-
1384
- });
1385
1704
 
1386
1705
 
1387
1706
  });
@@ -1392,108 +1711,108 @@ class RequestService {
1392
1711
  winston.debug("id_project: " + id_project);
1393
1712
  winston.debug("addParticipantByRequestId member: " + member);
1394
1713
 
1395
-
1396
1714
 
1397
- //TODO control if member is a valid project_user of the project
1398
- // validate member is string
1715
+
1716
+ //TODO control if member is a valid project_user of the project
1717
+ // validate member is string
1399
1718
  return new Promise(function (resolve, reject) {
1400
1719
 
1401
- if (member==undefined) {
1720
+ if (member == undefined) {
1402
1721
  var err = "addParticipantByRequestId error, member field is null";
1403
1722
  winston.error(err);
1404
1723
  return reject(err);
1405
1724
  }
1406
1725
 
1407
- return Request
1408
- .findOne({request_id: request_id, id_project: id_project})
1409
- // qui cache
1410
- .exec( function(err, request) {
1411
- if (err){
1412
- winston.error("Error adding participant ", err);
1413
- return reject(err);
1414
- }
1415
- if (!request) {
1416
- winston.error('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1417
- return reject('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1418
- }
1726
+ return Request
1727
+ .findOne({ request_id: request_id, id_project: id_project })
1728
+ // qui cache
1729
+ .exec(function (err, request) {
1730
+ if (err) {
1731
+ winston.error("Error adding participant ", err);
1732
+ return reject(err);
1733
+ }
1734
+ if (!request) {
1735
+ winston.error('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
1736
+ return reject('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
1737
+ }
1419
1738
 
1420
- winston.debug("assigned_operator here1");
1739
+ winston.debug("assigned_operator here1");
1421
1740
 
1422
- // return Request.findById(id).then(function (request) {
1423
- if (request.participants.indexOf(member)==-1){
1424
- request.participants.push(member);
1741
+ // return Request.findById(id).then(function (request) {
1742
+ if (request.participants.indexOf(member) == -1) {
1743
+ request.participants.push(member);
1425
1744
 
1426
- // botprefix
1427
- if (member.startsWith("bot_")) {
1428
- request.hasBot = true;
1745
+ // botprefix
1746
+ if (member.startsWith("bot_")) {
1747
+ request.hasBot = true;
1429
1748
 
1430
- // botprefix
1431
- var assigned_operator_idStringBot = member.replace("bot_","");
1432
- winston.debug("assigned_operator_idStringBot:"+assigned_operator_idStringBot);
1433
- request.participantsBots.push(assigned_operator_idStringBot);
1434
- }else {
1435
- request.participantsAgents.push(member);
1436
- request.hasBot = false; //???
1437
- }
1438
-
1439
-
1440
- if (request.participants.length>0) {
1441
- request.status = RequestConstants.ASSIGNED;
1442
- var assigned_at = Date.now();
1443
- request.assigned_at = assigned_at;
1444
- } else {
1445
- request.status = RequestConstants.UNASSIGNED;
1446
- }
1447
- // check error here
1448
- //cacheinvalidation
1449
- request.save(function(err, savedRequest) {
1450
- if (err) {
1451
- winston.error(err);
1452
- return reject(err);
1749
+ // botprefix
1750
+ var assigned_operator_idStringBot = member.replace("bot_", "");
1751
+ winston.debug("assigned_operator_idStringBot:" + assigned_operator_idStringBot);
1752
+ request.participantsBots.push(assigned_operator_idStringBot);
1753
+ } else {
1754
+ request.participantsAgents.push(member);
1755
+ request.hasBot = false; //???
1453
1756
  }
1454
1757
 
1455
- winston.debug("saved", savedRequest);
1456
-
1457
- return savedRequest
1458
- .populate('lead')
1459
- .populate('department')
1460
- .populate('participatingBots')
1461
- .populate('participatingAgents')
1462
- .populate({path:'requester',populate:{path:'id_user'}})
1463
- .execPopulate( function(err, requestComplete) {
1464
1758
 
1759
+ if (request.participants.length > 0) {
1760
+ request.status = RequestConstants.ASSIGNED;
1761
+ var assigned_at = Date.now();
1762
+ request.assigned_at = assigned_at;
1763
+ } else {
1764
+ request.status = RequestConstants.UNASSIGNED;
1765
+ }
1766
+ // check error here
1767
+ //cacheinvalidation
1768
+ request.save(function (err, savedRequest) {
1465
1769
  if (err) {
1466
- winston.error("Error getting addParticipantByRequestId", err);
1770
+ winston.error(err);
1467
1771
  return reject(err);
1468
1772
  }
1469
1773
 
1774
+ winston.debug("saved", savedRequest);
1775
+
1776
+ return savedRequest
1777
+ .populate('lead')
1778
+ .populate('department')
1779
+ .populate('participatingBots')
1780
+ .populate('participatingAgents')
1781
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
1782
+ .execPopulate(function (err, requestComplete) {
1783
+
1784
+ if (err) {
1785
+ winston.error("Error getting addParticipantByRequestId", err);
1786
+ return reject(err);
1787
+ }
1788
+
1789
+
1790
+ winston.debug("populated", requestComplete);
1791
+
1792
+ requestEvent.emit('request.update', requestComplete);
1793
+ requestEvent.emit("request.update.comment", { comment: "PARTICIPANT_ADD", request: requestComplete });//Deprecated
1794
+ requestEvent.emit("request.updated", { comment: "PARTICIPANT_ADD", request: requestComplete, patch: { member: member } });
1795
+ requestEvent.emit('request.participants.join', { member: member, request: requestComplete });
1796
+
1797
+ return resolve(requestComplete);
1798
+ });
1799
+ });
1800
+ // qui assignetat
1801
+ } else {
1802
+ winston.debug('Request member ' + member + ' already added for request_id ' + request_id + ' and id_project ' + id_project);
1803
+ return request
1804
+ .populate('lead')
1805
+ .populate('department')
1806
+ .populate('participatingBots')
1807
+ .populate('participatingAgents')
1808
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
1809
+ .execPopulate(function (err, requestComplete) {
1810
+ return resolve(requestComplete);
1811
+ });
1812
+ }
1470
1813
 
1471
- winston.debug("populated", requestComplete);
1472
-
1473
- requestEvent.emit('request.update', requestComplete);
1474
- requestEvent.emit("request.update.comment", {comment:"PARTICIPANT_ADD",request:requestComplete});//Deprecated
1475
- requestEvent.emit("request.updated", {comment:"PARTICIPANT_ADD",request:requestComplete, patch: {member:member}});
1476
- requestEvent.emit('request.participants.join', {member:member, request: requestComplete});
1477
-
1478
- return resolve(requestComplete);
1479
- });
1480
1814
  });
1481
- // qui assignetat
1482
- } else {
1483
- winston.debug('Request member '+ member+ ' already added for request_id '+ request_id + ' and id_project '+ id_project);
1484
- return request
1485
- .populate('lead')
1486
- .populate('department')
1487
- .populate('participatingBots')
1488
- .populate('participatingAgents')
1489
- .populate({path:'requester',populate:{path:'id_user'}})
1490
- .execPopulate( function(err, requestComplete) {
1491
- return resolve(requestComplete);
1492
- });
1493
- }
1494
-
1495
- });
1496
- });
1815
+ });
1497
1816
  }
1498
1817
 
1499
1818
  removeParticipantByRequestId(request_id, id_project, member) {
@@ -1501,151 +1820,151 @@ class RequestService {
1501
1820
  winston.debug("id_project", id_project);
1502
1821
  winston.debug("member", member);
1503
1822
 
1504
- return new Promise(function (resolve, reject) {
1823
+ return new Promise(function (resolve, reject) {
1505
1824
 
1506
1825
 
1507
1826
 
1508
- if (member==undefined) {
1827
+ if (member == undefined) {
1509
1828
  var err = "removeParticipantByRequestId error, member field is null";
1510
1829
  winston.error(err);
1511
1830
  return reject(err);
1512
1831
  }
1513
1832
 
1514
-
1515
- return Request
1516
- .findOne({request_id: request_id, id_project: id_project})
1833
+
1834
+ return Request
1835
+ .findOne({ request_id: request_id, id_project: id_project })
1517
1836
  // .populate('participatingAgents') //for abandoned_by_project_users
1518
1837
  // qui cache
1519
- .exec( async (err, request) => {
1520
-
1521
- if (err){
1522
- winston.error("Error removing participant ", err);
1523
- return reject(err);
1524
- }
1838
+ .exec(async (err, request) => {
1525
1839
 
1526
- if (!request) {
1527
- winston.error('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1528
- return reject('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1529
- }
1840
+ if (err) {
1841
+ winston.error("Error removing participant ", err);
1842
+ return reject(err);
1843
+ }
1530
1844
 
1531
- var index = request.participants.indexOf(member);
1532
- // winston.debug("index", index);
1845
+ if (!request) {
1846
+ winston.error('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
1847
+ return reject('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
1848
+ }
1533
1849
 
1534
- if (index > -1) {
1535
- request.participants.splice(index, 1);
1536
- // winston.debug(" request.participants", request.participants);
1850
+ var index = request.participants.indexOf(member);
1851
+ // winston.debug("index", index);
1852
+
1853
+ if (index > -1) {
1854
+ request.participants.splice(index, 1);
1855
+ // winston.debug(" request.participants", request.participants);
1537
1856
 
1538
- // botprefix
1539
- if (member.startsWith("bot_")) {
1540
- request.hasBot = false;
1541
1857
  // botprefix
1542
- var assigned_operator_idStringBot = member.replace("bot_","");
1543
- winston.debug("assigned_operator_idStringBot:"+assigned_operator_idStringBot);
1544
-
1545
- var indexParticipantsBots = request.participantsBots.indexOf(assigned_operator_idStringBot);
1546
- request.participantsBots.splice(indexParticipantsBots, 1);
1547
- }else {
1548
- var indexParticipantsAgents = request.participantsAgents.indexOf(member);
1549
- request.participantsAgents.splice(indexParticipantsAgents, 1);
1550
-
1551
-
1552
-
1553
- try {
1554
-
1555
- //request.attributes.abandoned_by_project_users start TODO move to routing-queue
1556
- if (!request.attributes) {
1557
- winston.debug("removeParticipantByRequestId request.attributes is empty. creating it");
1558
- request.attributes = {};
1559
- }
1560
- if (!request.attributes.abandoned_by_project_users) {
1561
- winston.debug("removeParticipantByRequestId request.attributes.abandoned_by_project_users is empty. creating it");
1562
- request.attributes.abandoned_by_project_users = {}
1563
- }
1858
+ if (member.startsWith("bot_")) {
1859
+ request.hasBot = false;
1860
+ // botprefix
1861
+ var assigned_operator_idStringBot = member.replace("bot_", "");
1862
+ winston.debug("assigned_operator_idStringBot:" + assigned_operator_idStringBot);
1564
1863
 
1565
- /*
1566
- winston.info("request.participatingAgents",request.participatingAgents);
1567
- var pu = request.participatingAgents.find(projectUser => {
1568
- console.log(projectUser);
1569
- projectUser.id_user.toString() === member
1570
- });
1571
- winston.verbose("pu",pu);
1572
- */
1864
+ var indexParticipantsBots = request.participantsBots.indexOf(assigned_operator_idStringBot);
1865
+ request.participantsBots.splice(indexParticipantsBots, 1);
1866
+ } else {
1867
+ var indexParticipantsAgents = request.participantsAgents.indexOf(member);
1868
+ request.participantsAgents.splice(indexParticipantsAgents, 1);
1573
1869
 
1574
- var pu = await Project_user.findOne({ id_user: member, id_project:id_project }).exec();
1575
- winston.debug("pu",pu);
1576
1870
 
1577
- request.attributes.abandoned_by_project_users[pu._id] = new Date().getTime();
1578
- winston.debug("removeParticipantByRequestId request.attributes.abandoned_by_project_users", request.attributes.abandoned_by_project_users);
1579
- //request.attributes.abandoned_by_project_users end
1580
1871
 
1581
- }catch(e) {
1582
- winston.error("Error getting removeParticipantByRequestId pu",e);
1583
- }
1584
-
1872
+ try {
1585
1873
 
1586
- }
1874
+ //request.attributes.abandoned_by_project_users start TODO move to routing-queue
1875
+ if (!request.attributes) {
1876
+ winston.debug("removeParticipantByRequestId request.attributes is empty. creating it");
1877
+ request.attributes = {};
1878
+ }
1879
+ if (!request.attributes.abandoned_by_project_users) {
1880
+ winston.debug("removeParticipantByRequestId request.attributes.abandoned_by_project_users is empty. creating it");
1881
+ request.attributes.abandoned_by_project_users = {}
1882
+ }
1883
+
1884
+ /*
1885
+ winston.info("request.participatingAgents",request.participatingAgents);
1886
+ var pu = request.participatingAgents.find(projectUser => {
1887
+ console.log(projectUser);
1888
+ projectUser.id_user.toString() === member
1889
+ });
1890
+ winston.verbose("pu",pu);
1891
+ */
1892
+
1893
+ var pu = await Project_user.findOne({ id_user: member, id_project: id_project }).exec();
1894
+ winston.debug("pu", pu);
1895
+
1896
+ request.attributes.abandoned_by_project_users[pu._id] = new Date().getTime();
1897
+ winston.debug("removeParticipantByRequestId request.attributes.abandoned_by_project_users", request.attributes.abandoned_by_project_users);
1898
+ //request.attributes.abandoned_by_project_users end
1899
+
1900
+ } catch (e) {
1901
+ winston.error("Error getting removeParticipantByRequestId pu", e);
1902
+ }
1587
1903
 
1588
1904
 
1589
- if (request.status!= RequestConstants.CLOSED) {//don't change the status to 100 or 200 for closed request to resolve this bug. if the agent leave the group and after close the request the status became 100, but if the request is closed the state (1000) must not be changed
1590
- // qui1000 ????
1591
- if (request.participants.length>0) {
1592
- request.status = RequestConstants.ASSIGNED;
1593
- // assignet_at?
1594
- } else {
1595
- request.status = RequestConstants.UNASSIGNED;
1596
- }
1597
- }
1598
-
1599
- request.markModified('attributes');
1600
- // winston.debug(" request", request);
1601
- //cacheinvalidation
1602
- return request.save(function(err, savedRequest) {
1603
- if (err){
1604
- winston.error("Error saving removed participant ", err);
1605
- return reject(err);
1606
1905
  }
1607
-
1608
- return savedRequest
1609
- .populate('lead')
1610
- .populate('department')
1611
- .populate('participatingBots')
1612
- .populate('participatingAgents')
1613
- .populate({path:'requester',populate:{path:'id_user'}})
1614
- .execPopulate( function(err, requestComplete) {
1615
-
1616
- if (err){
1617
- winston.error("Error getting removed participant ", err);
1618
- return reject(err);
1906
+
1907
+
1908
+ if (request.status != RequestConstants.CLOSED) {//don't change the status to 100 or 200 for closed request to resolve this bug. if the agent leave the group and after close the request the status became 100, but if the request is closed the state (1000) must not be changed
1909
+ // qui1000 ????
1910
+ if (request.participants.length > 0) {
1911
+ request.status = RequestConstants.ASSIGNED;
1912
+ // assignet_at?
1913
+ } else {
1914
+ request.status = RequestConstants.UNASSIGNED;
1915
+ }
1619
1916
  }
1620
1917
 
1621
-
1622
- requestEvent.emit('request.update', requestComplete);
1623
- requestEvent.emit("request.update.comment", {comment:"PARTICIPANT_REMOVE",request:requestComplete});//Deprecated
1624
- requestEvent.emit("request.updated", {comment:"PARTICIPANT_REMOVE",request:requestComplete, patch: {member:member}});
1625
- requestEvent.emit('request.participants.leave', {member:member, request: requestComplete});
1626
-
1918
+ request.markModified('attributes');
1919
+ // winston.debug(" request", request);
1920
+ //cacheinvalidation
1921
+ return request.save(function (err, savedRequest) {
1922
+ if (err) {
1923
+ winston.error("Error saving removed participant ", err);
1924
+ return reject(err);
1925
+ }
1926
+
1927
+ return savedRequest
1928
+ .populate('lead')
1929
+ .populate('department')
1930
+ .populate('participatingBots')
1931
+ .populate('participatingAgents')
1932
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
1933
+ .execPopulate(function (err, requestComplete) {
1934
+
1935
+ if (err) {
1936
+ winston.error("Error getting removed participant ", err);
1937
+ return reject(err);
1938
+ }
1939
+
1940
+
1941
+ requestEvent.emit('request.update', requestComplete);
1942
+ requestEvent.emit("request.update.comment", { comment: "PARTICIPANT_REMOVE", request: requestComplete });//Deprecated
1943
+ requestEvent.emit("request.updated", { comment: "PARTICIPANT_REMOVE", request: requestComplete, patch: { member: member } });
1944
+ requestEvent.emit('request.participants.leave', { member: member, request: requestComplete });
1945
+
1627
1946
 
1628
- return resolve(requestComplete);
1947
+ return resolve(requestComplete);
1629
1948
 
1630
- });
1631
- });
1949
+ });
1950
+ });
1632
1951
 
1633
1952
 
1634
- }else {
1635
- winston.verbose('Request member '+ member+ ' already not found for request_id '+ request_id + ' and id_project '+ id_project);
1953
+ } else {
1954
+ winston.verbose('Request member ' + member + ' already not found for request_id ' + request_id + ' and id_project ' + id_project);
1636
1955
 
1637
- return request
1638
- .populate('lead')
1639
- .populate('department')
1640
- .populate('participatingBots')
1641
- .populate('participatingAgents')
1642
- .populate({path:'requester',populate:{path:'id_user'}})
1643
- .execPopulate( function(err, requestComplete) {
1644
- return resolve(requestComplete);
1645
- });
1646
- }
1647
-
1648
- });
1956
+ return request
1957
+ .populate('lead')
1958
+ .populate('department')
1959
+ .populate('participatingBots')
1960
+ .populate('participatingAgents')
1961
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
1962
+ .execPopulate(function (err, requestComplete) {
1963
+ return resolve(requestComplete);
1964
+ });
1965
+ }
1966
+
1967
+ });
1649
1968
  });
1650
1969
  }
1651
1970
 
@@ -1653,57 +1972,57 @@ class RequestService {
1653
1972
 
1654
1973
  updateAttributesByRequestId(request_id, id_project, attributes) {
1655
1974
  var data = attributes;
1656
-
1657
- Request.findOne({"request_id":request_id, id_project:id_project})
1658
- .populate('lead')
1659
- .populate('department')
1660
- .populate('participatingBots')
1661
- .populate('participatingAgents')
1662
- .populate({path:'requester',populate:{path:'id_user'}})
1663
- .exec( function(err, request) {
1975
+
1976
+ Request.findOne({ "request_id": request_id, id_project: id_project })
1977
+ .populate('lead')
1978
+ .populate('department')
1979
+ .populate('participatingBots')
1980
+ .populate('participatingAgents')
1981
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
1982
+ .exec(function (err, request) {
1664
1983
  if (err) {
1665
1984
  return reject(err);
1666
1985
  }
1667
1986
  if (!request) {
1668
- return reject('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1987
+ return reject('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
1669
1988
  }
1670
-
1671
-
1989
+
1990
+
1672
1991
  if (!request.attributes) {
1673
1992
  winston.debug("empty attributes")
1674
1993
  request.attributes = {};
1675
1994
  }
1676
-
1995
+
1677
1996
  winston.debug(" req attributes", request.attributes)
1678
-
1679
- Object.keys(data).forEach(function(key) {
1680
- var val = data[key];
1681
- winston.debug("data attributes "+key+" " +val)
1682
- request.attributes[key] = val;
1683
- });
1684
-
1685
- winston.debug(" req attributes", request.attributes)
1686
-
1687
- // https://stackoverflow.com/questions/24054552/mongoose-not-saving-nested-object
1688
- request.markModified('attributes');
1689
-
1690
- //cacheinvalidation
1691
- return request.save(function (err, savedRequest) {
1692
- if (err) {
1693
- winston.error("error saving request attributes",err)
1694
- return reject({msg:"Error saving request attributes",err:err});
1695
- }
1696
- winston.verbose(" saved request attributes",savedRequest.toObject())
1697
- requestEvent.emit("request.update", savedRequest);
1698
- requestEvent.emit("request.update.comment", {comment:"ATTRIBUTES_UPDATE",request:savedRequest});//Deprecated
1699
- requestEvent.emit("request.updated", {comment:"ATTRIBUTES_UPDATE",request:savedRequest, patch: {attributes:data}});
1700
1997
 
1701
- requestEvent.emit("request.attributes.update", savedRequest);
1702
- // allora neanche qui participatingAgent è ok?
1703
- return resolve(savedRequest);
1704
- });
1705
- });
1706
-
1998
+ Object.keys(data).forEach(function (key) {
1999
+ var val = data[key];
2000
+ winston.debug("data attributes " + key + " " + val)
2001
+ request.attributes[key] = val;
2002
+ });
2003
+
2004
+ winston.debug(" req attributes", request.attributes)
2005
+
2006
+ // https://stackoverflow.com/questions/24054552/mongoose-not-saving-nested-object
2007
+ request.markModified('attributes');
2008
+
2009
+ //cacheinvalidation
2010
+ return request.save(function (err, savedRequest) {
2011
+ if (err) {
2012
+ winston.error("error saving request attributes", err)
2013
+ return reject({ msg: "Error saving request attributes", err: err });
2014
+ }
2015
+ winston.verbose(" saved request attributes", savedRequest.toObject())
2016
+ requestEvent.emit("request.update", savedRequest);
2017
+ requestEvent.emit("request.update.comment", { comment: "ATTRIBUTES_UPDATE", request: savedRequest });//Deprecated
2018
+ requestEvent.emit("request.updated", { comment: "ATTRIBUTES_UPDATE", request: savedRequest, patch: { attributes: data } });
2019
+
2020
+ requestEvent.emit("request.attributes.update", savedRequest);
2021
+ // allora neanche qui participatingAgent è ok?
2022
+ return resolve(savedRequest);
2023
+ });
2024
+ });
2025
+
1707
2026
  }
1708
2027
 
1709
2028
 
@@ -1718,59 +2037,59 @@ class RequestService {
1718
2037
 
1719
2038
  return new Promise(function (resolve, reject) {
1720
2039
 
1721
- if (tag==undefined) {
2040
+ if (tag == undefined) {
1722
2041
  var err = "addTagByRequestId error, tag field is null";
1723
2042
  winston.error(err);
1724
2043
  return reject(err);
1725
2044
  }
1726
-
1727
2045
 
1728
- return Request
1729
- .findOne({request_id: request_id, id_project: id_project})
1730
- .populate('lead')
2046
+
2047
+ return Request
2048
+ .findOne({ request_id: request_id, id_project: id_project })
2049
+ .populate('lead')
1731
2050
  .populate('department')
1732
2051
  .populate('participatingBots')
1733
- .populate('participatingAgents')
1734
- .populate({path:'requester',populate:{path:'id_user'}})
1735
- .exec( function(err, request) {
1736
- if (err){
1737
- winston.error("Error adding tag ", err);
1738
- return reject(err);
1739
- }
1740
- if (!request) {
1741
- winston.error('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1742
- return reject('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1743
- }
2052
+ .populate('participatingAgents')
2053
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
2054
+ .exec(function (err, request) {
2055
+ if (err) {
2056
+ winston.error("Error adding tag ", err);
2057
+ return reject(err);
2058
+ }
2059
+ if (!request) {
2060
+ winston.error('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
2061
+ return reject('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
2062
+ }
1744
2063
 
1745
2064
 
1746
- // return Request.findById(id).then(function (request) {
1747
- if (request.tags.indexOf(tag)==-1){
1748
- request.tags.push(tag);
1749
- // check error here
2065
+ // return Request.findById(id).then(function (request) {
2066
+ if (request.tags.indexOf(tag) == -1) {
2067
+ request.tags.push(tag);
2068
+ // check error here
1750
2069
 
1751
- //cacheinvalidation
1752
- request.save(function(err, savedRequest) {
1753
- if (err) {
1754
- winston.error(err);
1755
- return reject(err);
1756
- }
1757
-
1758
- requestEvent.emit('request.update', savedRequest);
1759
- requestEvent.emit("request.update.comment", {comment:"TAG_ADD",request:savedRequest}); //Deprecated
1760
- requestEvent.emit("request.updated", {comment:"TAG_ADD",request:savedRequest, patch: {tags:tag}});
2070
+ //cacheinvalidation
2071
+ request.save(function (err, savedRequest) {
2072
+ if (err) {
2073
+ winston.error(err);
2074
+ return reject(err);
2075
+ }
1761
2076
 
1762
-
1763
- // allora neanche qui participatingAgent è ok?
1764
- return resolve(savedRequest);
1765
- });
2077
+ requestEvent.emit('request.update', savedRequest);
2078
+ requestEvent.emit("request.update.comment", { comment: "TAG_ADD", request: savedRequest }); //Deprecated
2079
+ requestEvent.emit("request.updated", { comment: "TAG_ADD", request: savedRequest, patch: { tags: tag } });
1766
2080
 
1767
- // qui assignetat
1768
- } else {
1769
- winston.debug('Request tag '+ tag+ ' already added for request_id '+ request_id + ' and id_project '+ id_project);
1770
- return resolve(request);
1771
- }
1772
- });
1773
- });
2081
+
2082
+ // allora neanche qui participatingAgent è ok?
2083
+ return resolve(savedRequest);
2084
+ });
2085
+
2086
+ // qui assignetat
2087
+ } else {
2088
+ winston.debug('Request tag ' + tag + ' already added for request_id ' + request_id + ' and id_project ' + id_project);
2089
+ return resolve(request);
2090
+ }
2091
+ });
2092
+ });
1774
2093
  }
1775
2094
 
1776
2095
 
@@ -1783,66 +2102,66 @@ class RequestService {
1783
2102
 
1784
2103
 
1785
2104
 
1786
- if (tag==undefined) {
2105
+ if (tag == undefined) {
1787
2106
  var err = "removeTagByRequestId error, tag field is null";
1788
2107
  winston.error(err);
1789
2108
  return reject(err);
1790
2109
  }
1791
2110
 
1792
-
1793
- return Request
1794
- .findOne({request_id: request_id, id_project: id_project})
2111
+
2112
+ return Request
2113
+ .findOne({ request_id: request_id, id_project: id_project })
1795
2114
  .populate('lead')
1796
2115
  .populate('department')
1797
2116
  .populate('participatingBots')
1798
- .populate('participatingAgents')
1799
- .populate({path:'requester',populate:{path:'id_user'}})
1800
- .exec( function(err, request) {
1801
-
1802
- if (err){
1803
- winston.error("Error removing tag ", err);
1804
- return reject(err);
1805
- }
2117
+ .populate('participatingAgents')
2118
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
2119
+ .exec(function (err, request) {
1806
2120
 
1807
- if (!request) {
1808
- winston.error('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1809
- return reject('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1810
- }
2121
+ if (err) {
2122
+ winston.error("Error removing tag ", err);
2123
+ return reject(err);
2124
+ }
2125
+
2126
+ if (!request) {
2127
+ winston.error('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
2128
+ return reject('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
2129
+ }
1811
2130
 
1812
- // var index = request.tags.indexOf(tag);
1813
- var index = request.tags.findIndex(t => t.tag === tag);
2131
+ // var index = request.tags.indexOf(tag);
2132
+ var index = request.tags.findIndex(t => t.tag === tag);
1814
2133
 
1815
- winston.debug("index", index);
2134
+ winston.debug("index", index);
1816
2135
 
1817
- if (index > -1) {
1818
- request.tags.splice(index, 1);
1819
- // winston.debug(" request.participants", request.participants);
1820
-
2136
+ if (index > -1) {
2137
+ request.tags.splice(index, 1);
2138
+ // winston.debug(" request.participants", request.participants);
1821
2139
 
1822
- //cacheinvalidation
1823
- request.save(function(err, savedRequest) {
1824
2140
 
1825
- if (!err) {
1826
- requestEvent.emit('request.update', savedRequest);
1827
- requestEvent.emit("request.update.comment", {comment:"TAG_REMOVE",request:savedRequest});//Deprecated
1828
- requestEvent.emit("request.updated", {comment:"TAG_REMOVE",request:savedRequest, patch: {tags:tag}});
2141
+ //cacheinvalidation
2142
+ request.save(function (err, savedRequest) {
1829
2143
 
1830
- }
2144
+ if (!err) {
2145
+ requestEvent.emit('request.update', savedRequest);
2146
+ requestEvent.emit("request.update.comment", { comment: "TAG_REMOVE", request: savedRequest });//Deprecated
2147
+ requestEvent.emit("request.updated", { comment: "TAG_REMOVE", request: savedRequest, patch: { tags: tag } });
1831
2148
 
1832
- // allora neanche qui participatingAgent è ok?
2149
+ }
1833
2150
 
1834
- return resolve(savedRequest);
2151
+ // allora neanche qui participatingAgent è ok?
1835
2152
 
1836
- });
2153
+ return resolve(savedRequest);
1837
2154
 
2155
+ });
1838
2156
 
1839
- }else {
1840
- winston.info('Request tag '+ tag+ ' already not found for request_id '+ request_id + ' and id_project '+ id_project);
1841
- return resolve(request);
1842
- }
1843
-
1844
-
1845
- });
2157
+
2158
+ } else {
2159
+ winston.info('Request tag ' + tag + ' already not found for request_id ' + request_id + ' and id_project ' + id_project);
2160
+ return resolve(request);
2161
+ }
2162
+
2163
+
2164
+ });
1846
2165
  });
1847
2166
  }
1848
2167
 
@@ -1858,87 +2177,87 @@ class RequestService {
1858
2177
  winston.debug("id_project: " + id_project);
1859
2178
  winston.debug("addFollowerByRequestId member: " + member);
1860
2179
 
1861
-
1862
2180
 
1863
- //TODO control if member is a valid project_user of the project
1864
- // validate member is string
2181
+
2182
+ //TODO control if member is a valid project_user of the project
2183
+ // validate member is string
1865
2184
  return new Promise(function (resolve, reject) {
1866
2185
 
1867
- if (member==undefined) {
2186
+ if (member == undefined) {
1868
2187
  var err = "addFollowerByRequestId error, member field is null";
1869
2188
  winston.error(err);
1870
2189
  return reject(err);
1871
2190
  }
1872
2191
 
1873
- return Request
1874
- .findOne({request_id: request_id, id_project: id_project})
1875
- // qui cache
1876
- .exec( function(err, request) {
1877
- if (err){
1878
- winston.error("Error adding follower ", err);
1879
- return reject(err);
1880
- }
1881
- if (!request) {
1882
- winston.error('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1883
- return reject('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1884
- }
1885
-
1886
- winston.debug("assigned_operator here1");
1887
-
1888
- // return Request.findById(id).then(function (request) {
1889
- if (request.followers.indexOf(member)==-1){
1890
- request.followers.push(member);
1891
-
1892
- request.save(function(err, savedRequest) {
1893
- if (err) {
1894
- winston.error(err);
1895
- return reject(err);
1896
- }
2192
+ return Request
2193
+ .findOne({ request_id: request_id, id_project: id_project })
2194
+ // qui cache
2195
+ .exec(function (err, request) {
2196
+ if (err) {
2197
+ winston.error("Error adding follower ", err);
2198
+ return reject(err);
2199
+ }
2200
+ if (!request) {
2201
+ winston.error('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
2202
+ return reject('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
2203
+ }
1897
2204
 
1898
- winston.debug("saved", savedRequest);
2205
+ winston.debug("assigned_operator here1");
1899
2206
 
1900
- return savedRequest
1901
- .populate('lead')
1902
- .populate('department')
1903
- .populate('participatingBots')
1904
- .populate('participatingAgents')
1905
- // .populate('followers')
1906
- .populate({path:'requester',populate:{path:'id_user'}})
1907
- .execPopulate( function(err, requestComplete) {
2207
+ // return Request.findById(id).then(function (request) {
2208
+ if (request.followers.indexOf(member) == -1) {
2209
+ request.followers.push(member);
1908
2210
 
2211
+ request.save(function (err, savedRequest) {
1909
2212
  if (err) {
1910
- winston.error("Error getting addFollowerByRequestId", err);
2213
+ winston.error(err);
1911
2214
  return reject(err);
1912
2215
  }
1913
2216
 
2217
+ winston.debug("saved", savedRequest);
2218
+
2219
+ return savedRequest
2220
+ .populate('lead')
2221
+ .populate('department')
2222
+ .populate('participatingBots')
2223
+ .populate('participatingAgents')
2224
+ // .populate('followers')
2225
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
2226
+ .execPopulate(function (err, requestComplete) {
2227
+
2228
+ if (err) {
2229
+ winston.error("Error getting addFollowerByRequestId", err);
2230
+ return reject(err);
2231
+ }
2232
+
2233
+
2234
+ winston.debug("populated", requestComplete);
2235
+
2236
+ requestEvent.emit('request.update', requestComplete);
2237
+ requestEvent.emit("request.update.comment", { comment: "FOLLOWER_ADD", request: requestComplete });//Deprecated
2238
+ requestEvent.emit("request.updated", { comment: "FOLLOWER_ADD", request: requestComplete, patch: { member: member } });
2239
+ requestEvent.emit('request.followers.join', { member: member, request: requestComplete });
2240
+
2241
+ return resolve(requestComplete);
2242
+ });
2243
+ });
2244
+ // qui assignetat
2245
+ } else {
2246
+ winston.debug('Request member ' + member + ' already added for request_id ' + request_id + ' and id_project ' + id_project);
2247
+ return request
2248
+ .populate('lead')
2249
+ .populate('department')
2250
+ .populate('participatingBots')
2251
+ .populate('participatingAgents')
2252
+ // .populate('followers')
2253
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
2254
+ .execPopulate(function (err, requestComplete) {
2255
+ return resolve(requestComplete);
2256
+ });
2257
+ }
1914
2258
 
1915
- winston.debug("populated", requestComplete);
1916
-
1917
- requestEvent.emit('request.update', requestComplete);
1918
- requestEvent.emit("request.update.comment", {comment:"FOLLOWER_ADD",request:requestComplete});//Deprecated
1919
- requestEvent.emit("request.updated", {comment:"FOLLOWER_ADD",request:requestComplete, patch: {member:member}});
1920
- requestEvent.emit('request.followers.join', {member:member, request: requestComplete});
1921
-
1922
- return resolve(requestComplete);
1923
- });
1924
2259
  });
1925
- // qui assignetat
1926
- } else {
1927
- winston.debug('Request member '+ member+ ' already added for request_id '+ request_id + ' and id_project '+ id_project);
1928
- return request
1929
- .populate('lead')
1930
- .populate('department')
1931
- .populate('participatingBots')
1932
- .populate('participatingAgents')
1933
- // .populate('followers')
1934
- .populate({path:'requester',populate:{path:'id_user'}})
1935
- .execPopulate( function(err, requestComplete) {
1936
- return resolve(requestComplete);
1937
- });
1938
- }
1939
-
1940
- });
1941
- });
2260
+ });
1942
2261
  }
1943
2262
 
1944
2263
 
@@ -1946,90 +2265,90 @@ class RequestService {
1946
2265
 
1947
2266
 
1948
2267
  setFollowersByRequestId(request_id, id_project, newfollowers) {
1949
-
2268
+
1950
2269
  //TODO validate participants
1951
2270
  // validate if array of string newparticipants
1952
2271
  return new Promise(function (resolve, reject) {
1953
-
2272
+
1954
2273
  var isArray = Array.isArray(newfollowers);
1955
2274
 
1956
- if(isArray==false) {
1957
- winston.error('setFollowersByRequestId error newfollowers is not an array for request_id '+ request_id + ' and id_project '+ id_project);
1958
- return reject('setFollowersByRequestId error newfollowers is not an array for request_id '+ request_id + ' and id_project '+ id_project);
2275
+ if (isArray == false) {
2276
+ winston.error('setFollowersByRequestId error newfollowers is not an array for request_id ' + request_id + ' and id_project ' + id_project);
2277
+ return reject('setFollowersByRequestId error newfollowers is not an array for request_id ' + request_id + ' and id_project ' + id_project);
1959
2278
  }
1960
2279
 
1961
2280
  return Request
1962
-
1963
- .findOne({request_id: request_id, id_project: id_project})
1964
- // qui cache ok
1965
- .exec( function(err, request) {
1966
- if (err) {
1967
- winston.error("Error setFollowersByRequestId", err);
1968
- return reject(err);
1969
- }
1970
- if (!request) {
1971
- winston.error('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1972
- return reject('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1973
- }
1974
- var oldfollowers = request.followers;
1975
- winston.debug('oldParticipants', oldfollowers);
1976
- winston.debug('newparticipants', newfollowers);
1977
-
1978
- if (requestUtil.arraysEqual(oldfollowers, newfollowers)){
1979
- //if (oldParticipants === newparticipants) {
1980
- winston.verbose('Request members '+ oldfollowers+ ' already equal to ' + newfollowers + ' for request_id '+ request_id + ' and id_project '+ id_project);
1981
- return request
1982
- .populate('lead')
1983
- .populate('department')
1984
- .populate('participatingBots')
1985
- .populate('participatingAgents')
1986
- .populate({path:'requester',populate:{path:'id_user'}})
1987
- .execPopulate( function(err, requestComplete) {
1988
- return resolve(requestComplete);
1989
- });
1990
-
1991
- }
1992
2281
 
1993
- request.followers = newfollowers;
1994
-
1995
- //cacheinvalidation
1996
- return request.save(function(err, updatedRequest) {
1997
- // dopo save non aggiorna participating
2282
+ .findOne({ request_id: request_id, id_project: id_project })
2283
+ // qui cache ok
2284
+ .exec(function (err, request) {
1998
2285
  if (err) {
1999
2286
  winston.error("Error setFollowersByRequestId", err);
2000
2287
  return reject(err);
2001
2288
  }
2002
-
2003
- return updatedRequest
2004
- .populate('lead')
2005
- .populate('department')
2006
- .populate('participatingBots')
2007
- .populate('participatingAgents')
2008
- .populate({path:'requester',populate:{path:'id_user'}})
2009
- .execPopulate( function(err, requestComplete) {
2289
+ if (!request) {
2290
+ winston.error('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
2291
+ return reject('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
2292
+ }
2293
+ var oldfollowers = request.followers;
2294
+ winston.debug('oldParticipants', oldfollowers);
2295
+ winston.debug('newparticipants', newfollowers);
2296
+
2297
+ if (requestUtil.arraysEqual(oldfollowers, newfollowers)) {
2298
+ //if (oldParticipants === newparticipants) {
2299
+ winston.verbose('Request members ' + oldfollowers + ' already equal to ' + newfollowers + ' for request_id ' + request_id + ' and id_project ' + id_project);
2300
+ return request
2301
+ .populate('lead')
2302
+ .populate('department')
2303
+ .populate('participatingBots')
2304
+ .populate('participatingAgents')
2305
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
2306
+ .execPopulate(function (err, requestComplete) {
2307
+ return resolve(requestComplete);
2308
+ });
2010
2309
 
2310
+ }
2311
+
2312
+ request.followers = newfollowers;
2011
2313
 
2314
+ //cacheinvalidation
2315
+ return request.save(function (err, updatedRequest) {
2316
+ // dopo save non aggiorna participating
2012
2317
  if (err) {
2013
- winston.error("Error getting setFollowersByRequestId", err);
2318
+ winston.error("Error setFollowersByRequestId", err);
2014
2319
  return reject(err);
2015
2320
  }
2016
2321
 
2017
- winston.debug("oldfollowers ", oldfollowers);
2322
+ return updatedRequest
2323
+ .populate('lead')
2324
+ .populate('department')
2325
+ .populate('participatingBots')
2326
+ .populate('participatingAgents')
2327
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
2328
+ .execPopulate(function (err, requestComplete) {
2329
+
2330
+
2331
+ if (err) {
2332
+ winston.error("Error getting setFollowersByRequestId", err);
2333
+ return reject(err);
2334
+ }
2335
+
2336
+ winston.debug("oldfollowers ", oldfollowers);
2018
2337
 
2019
- requestEvent.emit('request.update', requestComplete);
2020
- requestEvent.emit("request.update.comment", {comment:"FOLLOWERS_SET",request:requestComplete});//Deprecated
2021
- requestEvent.emit("request.updated", {comment:"FOLLOWERS_SET",request:requestComplete, patch: {}});
2338
+ requestEvent.emit('request.update', requestComplete);
2339
+ requestEvent.emit("request.update.comment", { comment: "FOLLOWERS_SET", request: requestComplete });//Deprecated
2340
+ requestEvent.emit("request.updated", { comment: "FOLLOWERS_SET", request: requestComplete, patch: {} });
2022
2341
 
2023
- // requestEvent.emit('request.followers.update', {beforeRequest:request,
2024
- // removedParticipants:removedParticipants,
2025
- // addedParticipants:addedParticipants,
2026
- // request:requestComplete});
2342
+ // requestEvent.emit('request.followers.update', {beforeRequest:request,
2343
+ // removedParticipants:removedParticipants,
2344
+ // addedParticipants:addedParticipants,
2345
+ // request:requestComplete});
2346
+
2347
+ return resolve(requestComplete);
2348
+ });
2349
+ });
2027
2350
 
2028
- return resolve(requestComplete);
2029
- });
2030
2351
  });
2031
-
2032
- });
2033
2352
 
2034
2353
 
2035
2354
  });
@@ -2045,92 +2364,92 @@ class RequestService {
2045
2364
  winston.debug("id_project", id_project);
2046
2365
  winston.debug("member", member);
2047
2366
 
2048
- return new Promise(function (resolve, reject) {
2367
+ return new Promise(function (resolve, reject) {
2049
2368
 
2050
2369
 
2051
2370
 
2052
- if (member==undefined) {
2371
+ if (member == undefined) {
2053
2372
  var err = "removeFollowerByRequestId error, member field is null";
2054
2373
  winston.error(err);
2055
2374
  return reject(err);
2056
2375
  }
2057
2376
 
2058
-
2059
- return Request
2060
- .findOne({request_id: request_id, id_project: id_project})
2377
+
2378
+ return Request
2379
+ .findOne({ request_id: request_id, id_project: id_project })
2061
2380
  // .populate('participatingAgents') //for abandoned_by_project_users
2062
2381
  // qui cache
2063
- .exec( async (err, request) => {
2064
-
2065
- if (err){
2066
- winston.error("Error removing follower ", err);
2067
- return reject(err);
2068
- }
2382
+ .exec(async (err, request) => {
2069
2383
 
2070
- if (!request) {
2071
- winston.error('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
2072
- return reject('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
2073
- }
2384
+ if (err) {
2385
+ winston.error("Error removing follower ", err);
2386
+ return reject(err);
2387
+ }
2388
+
2389
+ if (!request) {
2390
+ winston.error('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
2391
+ return reject('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
2392
+ }
2074
2393
 
2075
- var index = request.followers.indexOf(member);
2076
- winston.debug("index", index);
2394
+ var index = request.followers.indexOf(member);
2395
+ winston.debug("index", index);
2077
2396
 
2078
- if (index > -1) {
2079
- request.followers.splice(index, 1);
2080
- // winston.debug(" request.participants", request.participants);
2081
-
2397
+ if (index > -1) {
2398
+ request.followers.splice(index, 1);
2399
+ // winston.debug(" request.participants", request.participants);
2082
2400
 
2083
- // winston.debug(" request", request);
2084
- //cacheinvalidation
2085
- return request.save(function(err, savedRequest) {
2086
- if (err){
2087
- winston.error("Error saving removed follower ", err);
2088
- return reject(err);
2089
- }
2090
-
2091
- return savedRequest
2092
- .populate('lead')
2093
- .populate('department')
2094
- .populate('participatingBots')
2095
- .populate('participatingAgents')
2096
- // .populate('followers')
2097
- .populate({path:'requester',populate:{path:'id_user'}})
2098
- .execPopulate( function(err, requestComplete) {
2099
-
2100
- if (err){
2101
- winston.error("Error getting removed follower ", err);
2102
- return reject(err);
2103
- }
2104
2401
 
2105
-
2106
- requestEvent.emit('request.update', requestComplete);
2107
- requestEvent.emit("request.update.comment", {comment:"FOLLOWER_REMOVE",request:requestComplete});//Deprecated
2108
- requestEvent.emit("request.updated", {comment:"FOLLOWER_REMOVE",request:requestComplete, patch: {member:member}});
2109
- requestEvent.emit('request.followers.leave', {member:member, request: requestComplete});
2110
-
2402
+ // winston.debug(" request", request);
2403
+ //cacheinvalidation
2404
+ return request.save(function (err, savedRequest) {
2405
+ if (err) {
2406
+ winston.error("Error saving removed follower ", err);
2407
+ return reject(err);
2408
+ }
2111
2409
 
2112
- return resolve(requestComplete);
2410
+ return savedRequest
2411
+ .populate('lead')
2412
+ .populate('department')
2413
+ .populate('participatingBots')
2414
+ .populate('participatingAgents')
2415
+ // .populate('followers')
2416
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
2417
+ .execPopulate(function (err, requestComplete) {
2113
2418
 
2114
- });
2115
- });
2419
+ if (err) {
2420
+ winston.error("Error getting removed follower ", err);
2421
+ return reject(err);
2422
+ }
2116
2423
 
2117
2424
 
2118
- }else {
2119
- winston.verbose('Request member '+ member+ ' already not found for request_id '+ request_id + ' and id_project '+ id_project);
2425
+ requestEvent.emit('request.update', requestComplete);
2426
+ requestEvent.emit("request.update.comment", { comment: "FOLLOWER_REMOVE", request: requestComplete });//Deprecated
2427
+ requestEvent.emit("request.updated", { comment: "FOLLOWER_REMOVE", request: requestComplete, patch: { member: member } });
2428
+ requestEvent.emit('request.followers.leave', { member: member, request: requestComplete });
2120
2429
 
2121
- return request
2122
- .populate('lead')
2123
- .populate('department')
2124
- .populate('participatingBots')
2125
- .populate('participatingAgents')
2126
- // .populate('followers')
2127
- .populate({path:'requester',populate:{path:'id_user'}})
2128
- .execPopulate( function(err, requestComplete) {
2129
- return resolve(requestComplete);
2130
- });
2131
- }
2132
-
2133
- });
2430
+
2431
+ return resolve(requestComplete);
2432
+
2433
+ });
2434
+ });
2435
+
2436
+
2437
+ } else {
2438
+ winston.verbose('Request member ' + member + ' already not found for request_id ' + request_id + ' and id_project ' + id_project);
2439
+
2440
+ return request
2441
+ .populate('lead')
2442
+ .populate('department')
2443
+ .populate('participatingBots')
2444
+ .populate('participatingAgents')
2445
+ // .populate('followers')
2446
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
2447
+ .execPopulate(function (err, requestComplete) {
2448
+ return resolve(requestComplete);
2449
+ });
2450
+ }
2451
+
2452
+ });
2134
2453
  });
2135
2454
  }
2136
2455