@tiledesk/tiledesk-server 2.4.100 → 2.4.101

Sign up to get free protection for your applications and to get access to all the features.
package/routes/request.js CHANGED
@@ -34,39 +34,55 @@ const { check, validationResult } = require('express-validator');
34
34
 
35
35
 
36
36
 
37
+ router.post('/simple', [check('first_text').notEmpty()], async (req, res) => {
38
+
39
+ var startTimestamp = new Date();
40
+ const errors = validationResult(req);
41
+
42
+ if (!errors.isEmpty()) {
43
+ return res.status(422).json({ errors: errors.array() });
44
+ }
45
+
46
+ if (req.projectuser) {
47
+ winston.debug("req.projectuser", req.projectuser);
48
+ }
49
+
50
+ var project_user = req.projectuser;
51
+ })
52
+
37
53
  // undocumented, used by test
38
54
 
39
55
  // TODO make a synchronous chat21 version (with query parameter?) with request.support_group.create
40
- router.post('/',
41
- [
42
- check('first_text').notEmpty(),
43
- ],
44
- async (req, res) => {
56
+ router.post('/',
57
+ [
58
+ check('first_text').notEmpty(),
59
+ ],
60
+ async (req, res) => {
45
61
 
46
- var startTimestamp = new Date();
47
- winston.debug("request create timestamp: " + startTimestamp);
62
+ var startTimestamp = new Date();
63
+ winston.debug("request create timestamp: " + startTimestamp);
48
64
 
49
- winston.debug("req.body", req.body);
65
+ winston.debug("req.body", req.body);
50
66
 
51
- winston.debug("req.projectid: " + req.projectid);
52
- winston.debug("req.user.id: " + req.user.id);
67
+ winston.debug("req.projectid: " + req.projectid);
68
+ winston.debug("req.user.id: " + req.user.id);
53
69
 
54
70
 
55
- const errors = validationResult(req);
56
- if (!errors.isEmpty()) {
57
- return res.status(422).json({ errors: errors.array() });
58
- }
71
+ const errors = validationResult(req);
72
+ if (!errors.isEmpty()) {
73
+ return res.status(422).json({ errors: errors.array() });
74
+ }
59
75
 
60
76
  if (req.projectuser) {
61
- winston.debug("req.projectuser", req.projectuser);
77
+ winston.debug("req.projectuser", req.projectuser);
62
78
  }
63
-
79
+
64
80
  var project_user = req.projectuser;
65
81
 
66
82
  var sender = req.body.sender;
67
83
  var fullname = req.body.senderFullname || req.user.fullName;
68
84
  var email = req.body.email || req.user.email;
69
-
85
+
70
86
  let messageStatus = req.body.status || MessageConstants.CHAT_MESSAGE_STATUS.SENDING;
71
87
  winston.debug('messageStatus: ' + messageStatus);
72
88
 
@@ -76,71 +92,71 @@ async (req, res) => {
76
92
  if (sender) {
77
93
 
78
94
  var isObjectId = mongoose.Types.ObjectId.isValid(sender);
79
- winston.debug("isObjectId:"+ isObjectId);
80
-
81
- var queryProjectUser = {id_project:req.projectid, status: "active" };
82
-
95
+ winston.debug("isObjectId:" + isObjectId);
96
+
97
+ var queryProjectUser = { id_project: req.projectid, status: "active" };
98
+
83
99
  if (isObjectId) {
84
100
  queryProjectUser.id_user = sender;
85
101
  } else {
86
102
  queryProjectUser.uuid_user = sender;
87
103
  }
88
-
104
+
89
105
  winston.debug("queryProjectUser", queryProjectUser);
90
-
91
- project_user = await Project_user.findOne(queryProjectUser).populate({path:'id_user', select:{'firstname':1, 'lastname':1, 'email':1}})
106
+
107
+ project_user = await Project_user.findOne(queryProjectUser).populate({ path: 'id_user', select: { 'firstname': 1, 'lastname': 1, 'email': 1 } })
92
108
  winston.debug("project_user", project_user);
93
-
109
+
94
110
  if (!project_user) {
95
- return res.status(403).send({success: false, msg: 'Unauthorized. Project_user not found with user id : '+ sender });
111
+ return res.status(403).send({ success: false, msg: 'Unauthorized. Project_user not found with user id : ' + sender });
96
112
  }
97
113
 
98
- if ( project_user.id_user) {
114
+ if (project_user.id_user) {
99
115
  fullname = project_user.id_user.fullName;
100
- winston.debug("pu fullname: "+ fullname);
116
+ winston.debug("pu fullname: " + fullname);
101
117
  email = project_user.id_user.email;
102
- winston.debug("pu email: "+ email);
118
+ winston.debug("pu email: " + email);
103
119
  } else if (project_user.uuid_user) {
104
- var lead = await Lead.findOne({lead_id: project_user.uuid_user, id_project: req.projectid});
105
- winston.debug("lead: ",lead);
120
+ var lead = await Lead.findOne({ lead_id: project_user.uuid_user, id_project: req.projectid });
121
+ winston.debug("lead: ", lead);
106
122
  if (lead) {
107
123
  fullname = lead.fullname;
108
- winston.debug("lead fullname: "+ fullname);
124
+ winston.debug("lead fullname: " + fullname);
109
125
  email = lead.email;
110
- winston.debug("lead email: "+ email);
111
- }else {
112
- winston.warn("lead not found: " + JSON.stringify({lead_id: project_user.uuid_user, id_project: req.projectid}));
126
+ winston.debug("lead email: " + email);
127
+ } else {
128
+ winston.warn("lead not found: " + JSON.stringify({ lead_id: project_user.uuid_user, id_project: req.projectid }));
113
129
  }
114
-
130
+
115
131
  } else {
116
132
  winston.warn("pu fullname and email empty");
117
133
  }
118
-
134
+
119
135
  }
120
136
 
121
137
 
122
138
  // createIfNotExistsWithLeadId(lead_id, fullname, email, id_project, createdBy, attributes) {
123
139
  return leadService.createIfNotExistsWithLeadId(sender || req.user._id, fullname, email, req.projectid, null, req.body.attributes || req.user.attributes)
124
- .then(function(createdLead) {
140
+ .then(function (createdLead) {
125
141
 
126
142
 
127
143
 
128
- var new_request = {
129
- request_id: request_id,
144
+ var new_request = {
145
+ request_id: request_id,
130
146
  project_user_id: req.projectuser._id,
131
- lead_id: createdLead._id,
132
- id_project:req.projectid,
133
- first_text: req.body.first_text,
134
- departmentid: req.body.departmentid,
135
- sourcePage:req.body.sourcePage,
136
- language: req.body.language,
137
- userAgent:req.body.userAgent,
138
- status:null,
147
+ lead_id: createdLead._id,
148
+ id_project: req.projectid,
149
+ first_text: req.body.first_text,
150
+ departmentid: req.body.departmentid,
151
+ sourcePage: req.body.sourcePage,
152
+ language: req.body.language,
153
+ userAgent: req.body.userAgent,
154
+ status: null,
139
155
  createdBy: req.user._id,
140
- attributes: req.body.attributes,
141
- subject: req.body.subject,
142
- preflight:undefined,
143
- channel: req.body.channel,
156
+ attributes: req.body.attributes,
157
+ subject: req.body.subject,
158
+ preflight: undefined,
159
+ channel: req.body.channel,
144
160
  location: req.body.location,
145
161
  participants: req.body.participants,
146
162
  lead: createdLead, requester: project_user,
@@ -149,38 +165,38 @@ async (req, res) => {
149
165
  };
150
166
 
151
167
  return requestService.create(new_request).then(function (savedRequest) {
152
- // createWithIdAndRequester(request_id, project_user_id, lead_id, id_project, first_text, departmentid, sourcePage, language, userAgent, status, createdBy, attributes) {
153
- // return requestService.createWithIdAndRequester(request_id, req.projectuser._id, createdLead._id, req.projectid,
154
- // req.body.text, req.body.departmentid, req.body.sourcePage,
155
- // req.body.language, req.body.userAgent, null, req.user._id, req.body.attributes, req.body.subject).then(function (savedRequest) {
168
+ // createWithIdAndRequester(request_id, project_user_id, lead_id, id_project, first_text, departmentid, sourcePage, language, userAgent, status, createdBy, attributes) {
169
+ // return requestService.createWithIdAndRequester(request_id, req.projectuser._id, createdLead._id, req.projectid,
170
+ // req.body.text, req.body.departmentid, req.body.sourcePage,
171
+ // req.body.language, req.body.userAgent, null, req.user._id, req.body.attributes, req.body.subject).then(function (savedRequest) {
156
172
 
157
173
 
158
174
  // return messageService.create(sender || req.user._id, fullname, request_id, req.body.text,
159
175
  // req.projectid, req.user._id, messageStatus, req.body.attributes, req.body.type, req.body.metadata, req.body.language, undefined, req.body.channel).then(function(savedMessage){
160
-
176
+
161
177
  // create(sender, senderFullname, recipient, text, id_project, createdBy, status, attributes, type, metadata) {
162
178
  // return messageService.create(req.body.sender || req.user._id, req.body.senderFullname || req.user.fullName, request_id, req.body.text,
163
179
  // req.projectid, req.user._id, messageStatus, req.body.attributes, req.body.type, req.body.metadata).then(function(savedMessage){
164
-
165
-
166
- winston.debug('res.json(savedRequest)');
167
- var endTimestamp = new Date();
168
- winston.verbose("request create end: " + (endTimestamp - startTimestamp));
169
- return res.json(savedRequest);
170
- // });
171
- // });
172
- });
173
-
174
-
175
-
176
-
177
-
178
- }).catch(function(err) {
179
- winston.error('Error saving request.', err);
180
- return res.status(500).send({ success: false, msg: 'Error saving object.', err: err });
181
-
180
+
181
+
182
+ winston.debug('res.json(savedRequest)');
183
+ var endTimestamp = new Date();
184
+ winston.verbose("request create end: " + (endTimestamp - startTimestamp));
185
+ return res.json(savedRequest);
186
+ // });
187
+ // });
188
+ });
189
+
190
+
191
+
192
+
193
+
194
+ }).catch(function (err) {
195
+ winston.error('Error saving request.', err);
196
+ return res.status(500).send({ success: false, msg: 'Error saving object.', err: err });
197
+
198
+ });
182
199
  });
183
- });
184
200
 
185
201
 
186
202
 
@@ -192,11 +208,11 @@ router.patch('/:requestid', function (req, res) {
192
208
  // const update = _.assign({ "updatedAt": new Date() }, req.body);
193
209
  //const update = req.body;
194
210
  const update = {};
195
-
211
+
196
212
  if (req.body.lead) {
197
213
  update.lead = req.body.lead;
198
214
  }
199
-
215
+
200
216
  // TODO test it. does it work?
201
217
  if (req.body.status) {
202
218
  update.status = req.body.status;
@@ -205,7 +221,7 @@ router.patch('/:requestid', function (req, res) {
205
221
  if (req.body.tags) {
206
222
  update.tags = req.body.tags;
207
223
  }
208
-
224
+
209
225
  if (req.body.notes) {
210
226
  update.notes = req.body.notes;
211
227
  }
@@ -241,46 +257,46 @@ router.patch('/:requestid', function (req, res) {
241
257
  update.priority = req.body.priority;
242
258
  }
243
259
 
244
- if (req.body.smartAssignment!=undefined) {
260
+ if (req.body.smartAssignment != undefined) {
245
261
  update.smartAssignment = req.body.smartAssignment;
246
262
  }
247
-
248
- if (req.body.workingStatus!=undefined) {
263
+
264
+ if (req.body.workingStatus != undefined) {
249
265
  update.workingStatus = req.body.workingStatus;
250
266
  }
251
-
267
+
252
268
 
253
269
  if (req.body.channelName) {
254
270
  update["channel.name"] = req.body.channelName;
255
271
  }
256
272
 
257
273
 
258
-
259
- winston.verbose("Request patch update",update);
274
+
275
+ winston.verbose("Request patch update", update);
260
276
 
261
277
  //cacheinvalidation
262
- return Request.findOneAndUpdate({"request_id":req.params.requestid, "id_project": req.projectid}, { $set: update }, { new: true, upsert: false })
263
- .populate('lead')
264
- .populate('department')
265
- .populate('participatingBots')
266
- .populate('participatingAgents')
267
- .populate({path:'requester',populate:{path:'id_user'}})
268
- .exec( function(err, request) {
269
-
270
- if (err) {
271
- winston.error('Error patching request.', err);
272
- return res.status(500).send({ success: false, msg: 'Error updating object.' });
273
- }
278
+ return Request.findOneAndUpdate({ "request_id": req.params.requestid, "id_project": req.projectid }, { $set: update }, { new: true, upsert: false })
279
+ .populate('lead')
280
+ .populate('department')
281
+ .populate('participatingBots')
282
+ .populate('participatingAgents')
283
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
284
+ .exec(function (err, request) {
274
285
 
275
- if (!request) {
276
- return res.status(404).send({ success: false, msg: 'Request not found' });
277
- }
286
+ if (err) {
287
+ winston.error('Error patching request.', err);
288
+ return res.status(500).send({ success: false, msg: 'Error updating object.' });
289
+ }
278
290
 
279
- requestEvent.emit("request.update", request);
280
- requestEvent.emit("request.update.comment", {comment:"PATCH",request:request}); //Deprecated
281
- requestEvent.emit("request.updated", {comment:"PATCH",request:request, patch: update});
282
- return res.json(request);
283
- });
291
+ if (!request) {
292
+ return res.status(404).send({ success: false, msg: 'Request not found' });
293
+ }
294
+
295
+ requestEvent.emit("request.update", request);
296
+ requestEvent.emit("request.update.comment", { comment: "PATCH", request: request }); //Deprecated
297
+ requestEvent.emit("request.updated", { comment: "PATCH", request: request, patch: update });
298
+ return res.json(request);
299
+ });
284
300
 
285
301
  });
286
302
 
@@ -288,15 +304,15 @@ router.patch('/:requestid', function (req, res) {
288
304
  // TODO make a synchronous chat21 version (with query parameter?) with request.support_group.created
289
305
  router.put('/:requestid/close', function (req, res) {
290
306
  winston.debug(req.body);
291
-
307
+
292
308
  // closeRequestByRequestId(request_id, id_project, skipStatsUpdate, notify, closed_by)
293
309
  const closed_by = req.user.id;
294
- return requestService.closeRequestByRequestId(req.params.requestid, req.projectid, false, true, closed_by, req.body.force).then(function(closedRequest) {
310
+ return requestService.closeRequestByRequestId(req.params.requestid, req.projectid, false, true, closed_by, req.body.force).then(function (closedRequest) {
311
+
312
+ winston.verbose("request closed", closedRequest);
295
313
 
296
- winston.verbose("request closed", closedRequest);
314
+ return res.json(closedRequest);
297
315
 
298
- return res.json(closedRequest);
299
-
300
316
  });
301
317
 
302
318
 
@@ -305,12 +321,12 @@ router.put('/:requestid/close', function (req, res) {
305
321
  router.put('/:requestid/reopen', function (req, res) {
306
322
  winston.debug(req.body);
307
323
  // reopenRequestByRequestId(request_id, id_project) {
308
- return requestService.reopenRequestByRequestId(req.params.requestid, req.projectid).then(function(reopenRequest) {
324
+ return requestService.reopenRequestByRequestId(req.params.requestid, req.projectid).then(function (reopenRequest) {
309
325
 
310
326
  winston.verbose("request reopen", reopenRequest);
311
327
 
312
328
  return res.json(reopenRequest);
313
- });
329
+ });
314
330
 
315
331
 
316
332
  });
@@ -323,27 +339,27 @@ router.put('/:requestid/assignee', function (req, res) {
323
339
  });
324
340
 
325
341
  // TODO make a synchronous chat21 version (with query parameter?) with request.support_group.created
326
- router.post('/:requestid/participants',
327
- [
328
- check('member').notEmpty(),
329
- ],
330
- function (req, res) {
331
- winston.debug(req.body);
332
-
333
- const errors = validationResult(req);
334
- if (!errors.isEmpty()) {
335
- return res.status(422).json({ errors: errors.array() });
336
- }
337
-
338
- //addParticipantByRequestId(request_id, id_project, member)
339
- return requestService.addParticipantByRequestId(req.params.requestid, req.projectid, req.body.member ).then(function(updatedRequest) {
342
+ router.post('/:requestid/participants',
343
+ [
344
+ check('member').notEmpty(),
345
+ ],
346
+ function (req, res) {
347
+ winston.debug(req.body);
348
+
349
+ const errors = validationResult(req);
350
+ if (!errors.isEmpty()) {
351
+ return res.status(422).json({ errors: errors.array() });
352
+ }
353
+
354
+ //addParticipantByRequestId(request_id, id_project, member)
355
+ return requestService.addParticipantByRequestId(req.params.requestid, req.projectid, req.body.member).then(function (updatedRequest) {
340
356
 
341
357
  winston.verbose("participant added", updatedRequest);
342
358
 
343
359
  return res.json(updatedRequest);
360
+ });
361
+
344
362
  });
345
-
346
- });
347
363
 
348
364
  // TODO make a synchronous chat21 version (with query parameter?) with request.support_group.created
349
365
  /*
@@ -356,40 +372,40 @@ router.put('/:requestid/participants', function (req, res) {
356
372
  winston.debug("req.body", req.body);
357
373
 
358
374
  var participants = [];
359
- req.body.forEach(function(participant,index) {
375
+ req.body.forEach(function (participant, index) {
360
376
  participants.push(participant);
361
377
  });
362
378
  winston.debug("var participants", participants);
363
-
379
+
364
380
  //setParticipantsByRequestId(request_id, id_project, participants)
365
- return requestService.setParticipantsByRequestId(req.params.requestid, req.projectid, participants ).then(function(updatedRequest) {
381
+ return requestService.setParticipantsByRequestId(req.params.requestid, req.projectid, participants).then(function (updatedRequest) {
366
382
 
367
- winston.debug("participant set", updatedRequest);
383
+ winston.debug("participant set", updatedRequest);
368
384
 
369
- return res.json(updatedRequest);
385
+ return res.json(updatedRequest);
370
386
  });
371
-
387
+
372
388
  });
373
389
 
374
390
  // TODO make a synchronous chat21 version (with query parameter?) with request.support_group.created
375
391
  router.delete('/:requestid/participants/:participantid', function (req, res) {
376
392
  winston.debug(req.body);
377
-
378
- //removeParticipantByRequestId(request_id, id_project, member)
379
- return requestService.removeParticipantByRequestId(req.params.requestid, req.projectid, req.params.participantid ).then(function(updatedRequest) {
380
393
 
381
- winston.verbose("participant removed", updatedRequest);
394
+ //removeParticipantByRequestId(request_id, id_project, member)
395
+ return requestService.removeParticipantByRequestId(req.params.requestid, req.projectid, req.params.participantid).then(function (updatedRequest) {
382
396
 
383
- return res.json(updatedRequest);
397
+ winston.verbose("participant removed", updatedRequest);
398
+
399
+ return res.json(updatedRequest);
384
400
  });
385
-
386
-
401
+
402
+
387
403
  });
388
404
 
389
405
  // // TODO deprecated
390
406
  // router.delete('/:requestid/participants', function (req, res) {
391
407
  // winston.debug(req.body);
392
-
408
+
393
409
  // //removeParticipantByRequestId(request_id, id_project, member)
394
410
  // return requestService.removeParticipantByRequestId(req.params.requestid, req.projectid, req.body.member ).then(function(updatedRequest) {
395
411
 
@@ -397,8 +413,8 @@ router.delete('/:requestid/participants/:participantid', function (req, res) {
397
413
 
398
414
  // return res.json(updatedRequest);
399
415
  // });
400
-
401
-
416
+
417
+
402
418
  // });
403
419
 
404
420
 
@@ -408,49 +424,49 @@ router.delete('/:requestid/participants/:participantid', function (req, res) {
408
424
 
409
425
  router.put('/:requestid/assign', function (req, res) {
410
426
  winston.debug(req.body);
411
-
427
+
412
428
  // leggi la request se già assegnata o già chiusa (1000) esci
413
429
 
414
- //cacheinvalidation
415
- return Request.findOne({"request_id":req.params.requestid, "id_project": req.projectid})
416
- .exec( function(err, request) {
417
-
430
+ //cacheinvalidation
431
+ return Request.findOne({ "request_id": req.params.requestid, "id_project": req.projectid })
432
+ .exec(function (err, request) {
433
+
418
434
  if (err) {
419
435
  winston.error('Error patching request.', err);
420
436
  return res.status(500).send({ success: false, msg: 'Error updating object.' });
421
437
  }
422
-
438
+
423
439
  if (!request) {
424
440
  return res.status(404).send({ success: false, msg: 'Request not found' });
425
441
  }
426
442
 
427
- if (request.status === RequestConstants.ASSIGNED ||request.status === RequestConstants.SERVED ||request.status === RequestConstants.CLOSED ) {
443
+ if (request.status === RequestConstants.ASSIGNED || request.status === RequestConstants.SERVED || request.status === RequestConstants.CLOSED) {
428
444
  winston.info('Request already assigned');
429
445
  return res.json(request);
430
446
  }
431
- //route(request_id, departmentid, id_project) {
432
- requestService.route(req.params.requestid, req.body.departmentid, req.projectid, req.body.nobot, req.body.no_populate).then(function(updatedRequest) {
433
-
447
+ //route(request_id, departmentid, id_project) {
448
+ requestService.route(req.params.requestid, req.body.departmentid, req.projectid, req.body.nobot, req.body.no_populate).then(function (updatedRequest) {
449
+
434
450
  winston.debug("department changed", updatedRequest);
435
451
 
436
452
  return res.json(updatedRequest);
437
- }).catch(function(error) {
453
+ }).catch(function (error) {
438
454
  winston.error('Error changing the department.', error)
439
455
  return res.status(500).send({ success: false, msg: 'Error changing the department.' });
440
456
  })
441
- });
457
+ });
442
458
  });
443
459
 
444
460
  // TODO make a synchronous chat21 version (with query parameter?) with request.support_group.created
445
461
  router.put('/:requestid/departments', function (req, res) {
446
462
  winston.debug(req.body);
447
- //route(request_id, departmentid, id_project) {
448
- requestService.route(req.params.requestid, req.body.departmentid, req.projectid, req.body.nobot, req.body.no_populate).then(function(updatedRequest) {
449
-
450
- winston.debug("department changed", updatedRequest);
463
+ //route(request_id, departmentid, id_project) {
464
+ requestService.route(req.params.requestid, req.body.departmentid, req.projectid, req.body.nobot, req.body.no_populate).then(function (updatedRequest) {
451
465
 
452
- return res.json(updatedRequest);
453
- }).catch(function(error) {
466
+ winston.debug("department changed", updatedRequest);
467
+
468
+ return res.json(updatedRequest);
469
+ }).catch(function (error) {
454
470
  winston.error('Error changing the department.', error)
455
471
  return res.status(500).send({ success: false, msg: 'Error changing the department.' });
456
472
  })
@@ -459,42 +475,42 @@ router.put('/:requestid/departments', function (req, res) {
459
475
 
460
476
  router.put('/:requestid/agent', async (req, res) => {
461
477
  winston.debug(req.body);
462
- //route(request_id, departmentid, id_project) {
463
-
478
+ //route(request_id, departmentid, id_project) {
464
479
 
465
- var request = await Request.findOne({"request_id":req.params.requestid, id_project:req.projectid})
466
- .exec();
467
-
468
- if (!request) {
469
- return res.status(404).send({ success: false, msg: 'Object not found.' });
470
- }
471
480
 
472
- var departmentid = request.department;
473
- winston.debug("departmentid before: "+ departmentid);
481
+ var request = await Request.findOne({ "request_id": req.params.requestid, id_project: req.projectid })
482
+ .exec();
474
483
 
475
- if (!departmentid) {
476
- var defaultDepartment = await departmentService.getDefaultDepartment(req.projectid);
477
- winston.debug("defaultDepartment: ", defaultDepartment);
478
- departmentid = defaultDepartment.id;
479
- }
480
- winston.debug("departmentid after: "+ departmentid);
484
+ if (!request) {
485
+ return res.status(404).send({ success: false, msg: 'Object not found.' });
486
+ }
481
487
 
482
- requestService.route(req.params.requestid, departmentid, req.projectid, true, undefined).then(function(updatedRequest) {
483
-
484
- winston.debug("department changed", updatedRequest);
488
+ var departmentid = request.department;
489
+ winston.debug("departmentid before: " + departmentid);
485
490
 
486
- return res.json(updatedRequest);
487
- }).catch(function(error) {
488
- winston.error('Error changing the department.', error)
489
- return res.status(500).send({ success: false, msg: 'Error changing the department.' });
490
- })
491
+ if (!departmentid) {
492
+ var defaultDepartment = await departmentService.getDefaultDepartment(req.projectid);
493
+ winston.debug("defaultDepartment: ", defaultDepartment);
494
+ departmentid = defaultDepartment.id;
495
+ }
496
+ winston.debug("departmentid after: " + departmentid);
497
+
498
+ requestService.route(req.params.requestid, departmentid, req.projectid, true, undefined).then(function (updatedRequest) {
499
+
500
+ winston.debug("department changed", updatedRequest);
501
+
502
+ return res.json(updatedRequest);
503
+ }).catch(function (error) {
504
+ winston.error('Error changing the department.', error)
505
+ return res.status(500).send({ success: false, msg: 'Error changing the department.' });
506
+ })
491
507
 
492
508
 
493
509
  });
494
510
 
495
511
  // router.post('/:requestid/attributes', function (req, res) {
496
512
  // winston.debug(req.body);
497
-
513
+
498
514
  // //return Request.findOneAndUpdate({"request_id":req.params.requestid},{ $push: { attributes: req.body } } , { new: true, upsert: false }, function (err, updatedMessage) {
499
515
  // return Request.findOneAndUpdate({"request_id":req.params.requestid},{ $set: { attributes: req.body } } , { new: true, upsert: false }, function (err, updatedMessage) {
500
516
  // if (err) {
@@ -509,7 +525,7 @@ router.put('/:requestid/agent', async (req, res) => {
509
525
 
510
526
  // router.put('/:requestid/attributes/:attributeid', function (req, res) {
511
527
  // winston.debug(req.body);
512
-
528
+
513
529
  // return Request.findOneAndUpdate({"request_id":req.params.requestid, "attributes._id": req.params.attributeid},{ $set: { "attributes.$": req.body}} , { new: true, upsert: false }, function (err, updatedMessage) {
514
530
  // if (err) {
515
531
  // winston.error('Error patching request.', err);
@@ -523,7 +539,7 @@ router.put('/:requestid/agent', async (req, res) => {
523
539
 
524
540
  // router.delete('/:requestid/attributes/:attributeid', function (req, res) {
525
541
  // winston.debug(req.body);
526
-
542
+
527
543
 
528
544
  // return Request.findOneAndUpdate({"request_id":req.params.requestid},{ "$pull": { "attributes": { "_id": req.params.attributeid } }} , { new: true, upsert: false }, function (err, updatedMessage) {
529
545
  // if (err) {
@@ -537,19 +553,19 @@ router.put('/:requestid/agent', async (req, res) => {
537
553
  // });
538
554
 
539
555
 
540
- router.patch('/:requestid/attributes', function (req, res) {
556
+ router.patch('/:requestid/attributes', function (req, res) {
541
557
  var data = req.body;
542
558
  var id_project = req.projectid;
543
559
 
544
560
  // TODO use service method
545
561
 
546
- Request.findOne({"request_id":req.params.requestid, id_project:id_project})
547
- .populate('lead')
548
- .populate('department')
549
- .populate('participatingBots')
550
- .populate('participatingAgents')
551
- .populate({path:'requester',populate:{path:'id_user'}})
552
- .exec( function(err, request) {
562
+ Request.findOne({ "request_id": req.params.requestid, id_project: id_project })
563
+ .populate('lead')
564
+ .populate('department')
565
+ .populate('participatingBots')
566
+ .populate('participatingAgents')
567
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
568
+ .exec(function (err, request) {
553
569
  if (err) {
554
570
  return res.status(500).send({ success: false, msg: 'Error getting object.' });
555
571
  }
@@ -557,210 +573,210 @@ router.patch('/:requestid/attributes', function (req, res) {
557
573
  return res.status(404).send({ success: false, msg: 'Object not found.' });
558
574
  }
559
575
 
560
-
576
+
561
577
  if (!request.attributes) {
562
578
  winston.debug("empty attributes")
563
579
  request.attributes = {};
564
580
  }
565
581
 
566
582
  winston.debug(" req attributes", request.attributes)
567
-
568
- Object.keys(data).forEach(function(key) {
569
- var val = data[key];
570
- winston.debug("data attributes "+key+" " +val)
571
- request.attributes[key] = val;
572
- });
573
-
574
- winston.debug(" req attributes", request.attributes)
575
-
576
- // https://stackoverflow.com/questions/24054552/mongoose-not-saving-nested-object
577
- request.markModified('attributes');
578
-
579
- //cacheinvalidation
580
- request.save(function (err, savedRequest) {
581
- if (err) {
582
- winston.error("error saving request attributes",err)
583
- return res.status(500).send({ success: false, msg: 'Error getting object.' });
584
- }
585
- winston.verbose(" saved request attributes",savedRequest.toObject())
586
- requestEvent.emit("request.update", savedRequest);
587
- requestEvent.emit("request.update.comment", {comment:"ATTRIBUTES_PATCH",request:savedRequest});//Deprecated
588
- requestEvent.emit("request.updated", {comment:"ATTRIBUTES_PATCH",request:savedRequest, patch: {attributes:data}});
589
- requestEvent.emit("request.attributes.update", savedRequest);
590
- res.json(savedRequest);
591
- });
592
- });
593
-
583
+
584
+ Object.keys(data).forEach(function (key) {
585
+ var val = data[key];
586
+ winston.debug("data attributes " + key + " " + val)
587
+ request.attributes[key] = val;
588
+ });
589
+
590
+ winston.debug(" req attributes", request.attributes)
591
+
592
+ // https://stackoverflow.com/questions/24054552/mongoose-not-saving-nested-object
593
+ request.markModified('attributes');
594
+
595
+ //cacheinvalidation
596
+ request.save(function (err, savedRequest) {
597
+ if (err) {
598
+ winston.error("error saving request attributes", err)
599
+ return res.status(500).send({ success: false, msg: 'Error getting object.' });
600
+ }
601
+ winston.verbose(" saved request attributes", savedRequest.toObject())
602
+ requestEvent.emit("request.update", savedRequest);
603
+ requestEvent.emit("request.update.comment", { comment: "ATTRIBUTES_PATCH", request: savedRequest });//Deprecated
604
+ requestEvent.emit("request.updated", { comment: "ATTRIBUTES_PATCH", request: savedRequest, patch: { attributes: data } });
605
+ requestEvent.emit("request.attributes.update", savedRequest);
606
+ res.json(savedRequest);
607
+ });
608
+ });
609
+
594
610
  });
595
611
 
596
- router.post('/:requestid/notes', function (req, res) {
612
+ router.post('/:requestid/notes', function (req, res) {
597
613
  var note = {};
598
614
  note.text = req.body.text;
599
615
  // note.id_project = req.projectid;
600
616
  note.createdBy = req.user.id;
601
617
 
602
618
  //cacheinvalidation
603
- return Request.findOneAndUpdate({request_id:req.params.requestid, id_project:req.projectid},{ $push: { notes: note } } , { new: true, upsert: false })
619
+ return Request.findOneAndUpdate({ request_id: req.params.requestid, id_project: req.projectid }, { $push: { notes: note } }, { new: true, upsert: false })
604
620
  .populate('lead')
605
621
  .populate('department')
606
622
  .populate('participatingBots')
607
- .populate('participatingAgents')
608
- .populate({path:'requester',populate:{path:'id_user'}})
609
- .exec( function(err, updatedRequest) {
623
+ .populate('participatingAgents')
624
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
625
+ .exec(function (err, updatedRequest) {
610
626
 
611
- if (err) {
612
- winston.error('Error adding request note.', err);
613
- return res.status(500).send({ success: false, msg: 'Error adding request object.' });
614
- }
615
- requestEvent.emit("request.update", updatedRequest);
616
- requestEvent.emit("request.update.comment", {comment:"NOTE_ADD",request:updatedRequest});//Deprecated
617
- requestEvent.emit("request.updated", {comment:"NOTE_ADD",request:updatedRequest, patch: {notes:note}});
627
+ if (err) {
628
+ winston.error('Error adding request note.', err);
629
+ return res.status(500).send({ success: false, msg: 'Error adding request object.' });
630
+ }
631
+ requestEvent.emit("request.update", updatedRequest);
632
+ requestEvent.emit("request.update.comment", { comment: "NOTE_ADD", request: updatedRequest });//Deprecated
633
+ requestEvent.emit("request.updated", { comment: "NOTE_ADD", request: updatedRequest, patch: { notes: note } });
618
634
 
619
- return res.json(updatedRequest);
620
- });
635
+ return res.json(updatedRequest);
636
+ });
621
637
 
622
638
  });
623
639
 
624
640
 
625
- router.delete('/:requestid/notes/:noteid', function (req, res) {
626
-
627
- //cacheinvalidation
628
- return Request.findOneAndUpdate({request_id: req.params.requestid, id_project:req.projectid},{ $pull: { notes: { "_id": req.params.noteid } } } , { new: true, upsert: false })
641
+ router.delete('/:requestid/notes/:noteid', function (req, res) {
642
+
643
+ //cacheinvalidation
644
+ return Request.findOneAndUpdate({ request_id: req.params.requestid, id_project: req.projectid }, { $pull: { notes: { "_id": req.params.noteid } } }, { new: true, upsert: false })
629
645
  .populate('lead')
630
646
  .populate('department')
631
647
  .populate('participatingBots')
632
- .populate('participatingAgents')
633
- .populate({path:'requester',populate:{path:'id_user'}})
634
- .exec( function(err, updatedRequest) {
648
+ .populate('participatingAgents')
649
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
650
+ .exec(function (err, updatedRequest) {
635
651
 
636
- if (err) {
637
- winston.error('Error adding request note.', err);
638
- return res.status(500).send({ success: false, msg: 'Error adding request object.' });
639
- }
640
- requestEvent.emit("request.update", updatedRequest);
641
- requestEvent.emit("request.update.comment", {comment:"NOTE_DELETE",request:updatedRequest});//Deprecated
642
- // requestEvent.emit("request.updated", {comment:"NOTE_DELETE",request:updatedRequest, patch: {notes:req.params.noteid}});
652
+ if (err) {
653
+ winston.error('Error adding request note.', err);
654
+ return res.status(500).send({ success: false, msg: 'Error adding request object.' });
655
+ }
656
+ requestEvent.emit("request.update", updatedRequest);
657
+ requestEvent.emit("request.update.comment", { comment: "NOTE_DELETE", request: updatedRequest });//Deprecated
658
+ // requestEvent.emit("request.updated", {comment:"NOTE_DELETE",request:updatedRequest, patch: {notes:req.params.noteid}});
643
659
 
644
- return res.json(updatedRequest);
645
- });
660
+ return res.json(updatedRequest);
661
+ });
646
662
 
647
663
  });
648
664
 
649
665
 
650
666
 
651
667
  //TODO add cc
652
- router.post('/:requestid/email/send',
653
- async (req, res) => {
668
+ router.post('/:requestid/email/send',
669
+ async (req, res) => {
654
670
 
655
671
 
656
- let text = req.body.text;
657
- winston.debug("text: " + text);
672
+ let text = req.body.text;
673
+ winston.debug("text: " + text);
658
674
 
659
- let request_id = req.params.requestid;
660
- winston.debug("request_id: " + request_id);
675
+ let request_id = req.params.requestid;
676
+ winston.debug("request_id: " + request_id);
661
677
 
662
- let subject = req.body.subject;
663
- winston.info("subject: " + subject);
678
+ let subject = req.body.subject;
679
+ winston.info("subject: " + subject);
664
680
 
665
- winston.debug("req.project", req.project);
681
+ winston.debug("req.project", req.project);
666
682
 
667
- let replyto = req.body.replyto;
668
- winston.debug("replyto: " + replyto);
683
+ let replyto = req.body.replyto;
684
+ winston.debug("replyto: " + replyto);
669
685
 
670
686
 
671
- let q = Request.findOne({request_id: request_id, id_project: req.projectid})
672
- // .select("+snapshot.agents")
673
- .populate('lead')
674
- q.exec(function(err, request) {
675
- if (err) {
676
- winston.error("error getting request by id ", err);
677
- return res.status(500).send({ success: false, msg: 'Error getting object.' });
678
- }
679
- if (!request) {
680
- return res.status(404).send({ success: false, msg: 'Object not found.' });
681
- }
687
+ let q = Request.findOne({ request_id: request_id, id_project: req.projectid })
688
+ // .select("+snapshot.agents")
689
+ .populate('lead')
690
+ q.exec(function (err, request) {
691
+ if (err) {
692
+ winston.error("error getting request by id ", err);
693
+ return res.status(500).send({ success: false, msg: 'Error getting object.' });
694
+ }
695
+ if (!request) {
696
+ return res.status(404).send({ success: false, msg: 'Object not found.' });
697
+ }
682
698
 
683
699
 
684
700
 
685
- winston.info("Sending an email with text : " + text + " to request_id " + request_id);
701
+ winston.info("Sending an email with text : " + text + " to request_id " + request_id);
686
702
 
687
- if (!request.lead.email) {
688
- res.json({"no queued": true});
689
- }
703
+ if (!request.lead.email) {
704
+ res.json({ "no queued": true });
705
+ }
690
706
 
691
- let newto = request.lead.email
692
- winston.info("Sending an email newto " + newto);
707
+ let newto = request.lead.email
708
+ winston.info("Sending an email newto " + newto);
693
709
 
694
- //sendEmailDirect(to, text, project, request_id, subject, tokenQueryString, sourcePage, payload)
695
- emailService.sendEmailDirect(newto, text, req.project, request_id, subject, undefined, undefined, undefined, replyto);
710
+ //sendEmailDirect(to, text, project, request_id, subject, tokenQueryString, sourcePage, payload)
711
+ emailService.sendEmailDirect(newto, text, req.project, request_id, subject, undefined, undefined, undefined, replyto);
696
712
 
697
- res.json({"queued": true});
713
+ res.json({ "queued": true });
698
714
 
699
715
 
700
- });
716
+ });
701
717
 
702
718
 
703
719
 
704
-
705
- });
706
720
 
721
+ });
707
722
 
708
723
 
709
724
 
710
- router.post('/:requestid/followers',
711
- [
712
- check('member').notEmpty(),
713
- ],
714
- function (req, res) {
715
- winston.info("followers add", req.body);
716
-
717
- const errors = validationResult(req);
718
- if (!errors.isEmpty()) {
719
- return res.status(422).json({ errors: errors.array() });
720
- }
721
-
722
- //addParticipantByRequestId(request_id, id_project, member)
723
- return requestService.addFollowerByRequestId(req.params.requestid, req.projectid, req.body.member ).then(function(updatedRequest) {
725
+
726
+ router.post('/:requestid/followers',
727
+ [
728
+ check('member').notEmpty(),
729
+ ],
730
+ function (req, res) {
731
+ winston.info("followers add", req.body);
732
+
733
+ const errors = validationResult(req);
734
+ if (!errors.isEmpty()) {
735
+ return res.status(422).json({ errors: errors.array() });
736
+ }
737
+
738
+ //addParticipantByRequestId(request_id, id_project, member)
739
+ return requestService.addFollowerByRequestId(req.params.requestid, req.projectid, req.body.member).then(function (updatedRequest) {
724
740
 
725
741
  winston.verbose("participant added", updatedRequest);
726
742
 
727
743
  return res.json(updatedRequest);
744
+ });
745
+
728
746
  });
729
-
730
- });
731
747
 
732
748
 
733
749
  router.put('/:requestid/followers', function (req, res) {
734
750
  winston.debug("req.body", req.body);
735
751
 
736
752
  var followers = [];
737
- req.body.forEach(function(follower,index) {
753
+ req.body.forEach(function (follower, index) {
738
754
  followers.push(follower);
739
755
  });
740
756
  winston.debug("var followers", followers);
741
-
757
+
742
758
  // setFollowersByRequestId(request_id, id_project, newfollowers)
743
- return requestService.setFollowersByRequestId(req.params.requestid, req.projectid, followers ).then(function(updatedRequest) {
759
+ return requestService.setFollowersByRequestId(req.params.requestid, req.projectid, followers).then(function (updatedRequest) {
744
760
 
745
- winston.debug("followers set", updatedRequest);
761
+ winston.debug("followers set", updatedRequest);
746
762
 
747
- return res.json(updatedRequest);
763
+ return res.json(updatedRequest);
748
764
  });
749
-
765
+
750
766
  });
751
767
 
752
768
  router.delete('/:requestid/followers/:followerid', function (req, res) {
753
769
  winston.debug(req.body);
754
-
755
- //removeFollowerByRequestId(request_id, id_project, member)
756
- return requestService.removeFollowerByRequestId(req.params.requestid, req.projectid, req.params.followerid ).then(function(updatedRequest) {
757
770
 
758
- winston.verbose("follower removed", updatedRequest);
771
+ //removeFollowerByRequestId(request_id, id_project, member)
772
+ return requestService.removeFollowerByRequestId(req.params.requestid, req.projectid, req.params.followerid).then(function (updatedRequest) {
759
773
 
760
- return res.json(updatedRequest);
774
+ winston.verbose("follower removed", updatedRequest);
775
+
776
+ return res.json(updatedRequest);
761
777
  });
762
-
763
-
778
+
779
+
764
780
  });
765
781
 
766
782
 
@@ -769,21 +785,21 @@ router.delete('/:requestid/followers/:followerid', function (req, res) {
769
785
 
770
786
 
771
787
  // TODO make a synchronous chat21 version (with query parameter?) with request.support_group.created
772
- router.delete('/:requestid', function (req, res) {
773
-
788
+ router.delete('/:requestid', function (req, res) {
789
+
774
790
  var projectuser = req.projectuser;
775
791
 
776
792
 
777
- if (projectuser.role != "owner" ) {
793
+ if (projectuser.role != "owner") {
778
794
  return res.status(403).send({ success: false, msg: 'Unauthorized.' });
779
795
  }
780
796
 
781
- Message.deleteMany({recipient:req.params.requestid}, function(err) {
782
- if (err) {
783
- return res.status(500).send({success: false, msg: 'Error deleting messages.'});
784
- }
785
- winston.verbose('Messages deleted for the recipient: '+ req.params.requestid );
786
- });
797
+ Message.deleteMany({ recipient: req.params.requestid }, function (err) {
798
+ if (err) {
799
+ return res.status(500).send({ success: false, msg: 'Error deleting messages.' });
800
+ }
801
+ winston.verbose('Messages deleted for the recipient: ' + req.params.requestid);
802
+ });
787
803
 
788
804
 
789
805
  Request.remove({ request_id: req.params.requestid }, function (err, request) {
@@ -794,9 +810,9 @@ router.delete('/:requestid', function (req, res) {
794
810
 
795
811
  if (!request) {
796
812
  return res.status(404).send({ success: false, msg: 'Object not found.' });
797
- }
798
-
799
- winston.verbose('Request deleted with request_id: '+ req.params.requestid );
813
+ }
814
+
815
+ winston.verbose('Request deleted with request_id: ' + req.params.requestid);
800
816
 
801
817
  requestEvent.emit('request.delete', request);
802
818
 
@@ -807,12 +823,12 @@ router.delete('/:requestid', function (req, res) {
807
823
 
808
824
 
809
825
 
810
- router.delete('/id/:id', function (req, res) {
811
-
826
+ router.delete('/id/:id', function (req, res) {
827
+
812
828
  var projectuser = req.projectuser;
813
829
 
814
830
 
815
- if (projectuser.role != "owner" ) {
831
+ if (projectuser.role != "owner") {
816
832
  return res.status(403).send({ success: false, msg: 'Unauthorized.' });
817
833
  }
818
834
 
@@ -824,9 +840,9 @@ router.delete('/id/:id', function (req, res) {
824
840
 
825
841
  if (!request) {
826
842
  return res.status(404).send({ success: false, msg: 'Object not found.' });
827
- }
828
-
829
- winston.verbose('Request deleted with id: '+ req.params.id );
843
+ }
844
+
845
+ winston.verbose('Request deleted with id: ' + req.params.id);
830
846
 
831
847
  requestEvent.emit('request.delete', request);
832
848
 
@@ -868,21 +884,21 @@ router.get('/', function (req, res, next) {
868
884
  winston.debug('REQUEST ROUTE - SKIP PAGE ', skip);
869
885
 
870
886
 
871
- var query = { "id_project": req.projectid, "status": {$lt:1000}, preflight:false};
887
+ var query = { "id_project": req.projectid, "status": { $lt: 1000 }, preflight: false };
872
888
 
873
889
  var projectuser = req.projectuser;
874
890
 
875
891
 
876
892
  if (req.user instanceof Subscription) {
877
- //all request
893
+ //all request
878
894
  } else if (projectuser && (projectuser.role == "owner" || projectuser.role == "admin")) {
879
- //all request
880
- // per uni mostrare solo quelle priprio quindi solo participants
895
+ //all request
896
+ // per uni mostrare solo quelle priprio quindi solo participants
881
897
  if (req.query.mine) {
882
- query["$or"] = [ { "snapshot.agents.id_user": req.user.id}, {"participants": req.user.id}];
898
+ query["$or"] = [{ "snapshot.agents.id_user": req.user.id }, { "participants": req.user.id }];
883
899
  }
884
- }else {
885
- query["$or"] = [ { "snapshot.agents.id_user": req.user.id}, {"participants": req.user.id}];
900
+ } else {
901
+ query["$or"] = [{ "snapshot.agents.id_user": req.user.id }, { "participants": req.user.id }];
886
902
  }
887
903
 
888
904
  // console.log('REQUEST ROUTE - req ', req);
@@ -912,7 +928,7 @@ router.get('/', function (req, res, next) {
912
928
  if (req.query.status == 1000 || req.query.status == "1000") {
913
929
  history_search = true;
914
930
  }
915
- if (req.query.status==="all") {
931
+ if (req.query.status === "all") {
916
932
  history_search = true;
917
933
  delete query.status;
918
934
  }
@@ -930,7 +946,7 @@ router.get('/', function (req, res, next) {
930
946
  }
931
947
 
932
948
  winston.debug('req.query.hasbot', req.query.hasbot);
933
- if (req.query.hasbot!=undefined) {
949
+ if (req.query.hasbot != undefined) {
934
950
  winston.debug('req.query.hasbot', req.query.hasbot);
935
951
  query.hasBot = req.query.hasbot;
936
952
  }
@@ -966,8 +982,8 @@ router.get('/', function (req, res, next) {
966
982
  * THE SEARCH FOR DATE INTERVAL OF THE HISTORY OF REQUESTS ARE DISABLED AND
967
983
  * ARE DISPLAYED ONLY THE REQUESTS OF THE LAST 14 DAYS
968
984
  */
969
- //fixato. secondo me qui manca un parentesi tonda per gli or
970
- if ( history_search === true && req.project && req.project.profile && ((req.project.profile.type === 'free' && req.project.trialExpired === true) || (req.project.profile.type === 'payment' && req.project.isActiveSubscription === false))) {
985
+ //fixato. secondo me qui manca un parentesi tonda per gli or
986
+ if (history_search === true && req.project && req.project.profile && ((req.project.profile.type === 'free' && req.project.trialExpired === true) || (req.project.profile.type === 'payment' && req.project.isActiveSubscription === false))) {
971
987
 
972
988
 
973
989
  var startdate = moment().subtract(14, "days").format("YYYY-MM-DD");
@@ -977,7 +993,7 @@ router.get('/', function (req, res, next) {
977
993
  winston.debug('»»» REQUEST ROUTE - startdate ', startdate);
978
994
  winston.debug('»»» REQUEST ROUTE - enddate ', enddate);
979
995
 
980
- var enddatePlusOneDay= moment(new Date()).add(1, 'days').toDate()
996
+ var enddatePlusOneDay = moment(new Date()).add(1, 'days').toDate()
981
997
  winston.debug('»»» REQUEST ROUTE - enddate + 1 days: ', enddatePlusOneDay);
982
998
 
983
999
  // var enddatePlusOneDay = "2019-09-17T00:00:00.000Z"
@@ -986,12 +1002,12 @@ router.get('/', function (req, res, next) {
986
1002
  winston.debug('REQUEST ROUTE - QUERY CREATED AT ', query.createdAt);
987
1003
 
988
1004
  }
989
-
990
- /**
991
- **! *** DATE RANGE USECASE 2 ***
992
- * in the tiledesk dashboard's HISTORY PAGE
993
- * WHEN THE USER SEARCH FOR DATE INTERVAL OF THE HISTORY OF REQUESTS
994
- */
1005
+
1006
+ /**
1007
+ **! *** DATE RANGE USECASE 2 ***
1008
+ * in the tiledesk dashboard's HISTORY PAGE
1009
+ * WHEN THE USER SEARCH FOR DATE INTERVAL OF THE HISTORY OF REQUESTS
1010
+ */
995
1011
  if (req.query.start_date && req.query.end_date) {
996
1012
  winston.debug('REQUEST ROUTE - REQ QUERY start_date ', req.query.start_date);
997
1013
  winston.debug('REQUEST ROUTE - REQ QUERY end_date ', req.query.end_date);
@@ -1029,10 +1045,10 @@ router.get('/', function (req, res, next) {
1029
1045
  var range = { $gte: new Date(Date.parse(startDate)).toISOString() };
1030
1046
  if (req.query.filterRangeField) {
1031
1047
  query[req.query.filterRangeField] = range;
1032
- }else {
1048
+ } else {
1033
1049
  query.createdAt = range;
1034
1050
  }
1035
-
1051
+
1036
1052
  winston.debug('REQUEST ROUTE - QUERY CREATED AT (only for start date)', query.createdAt);
1037
1053
  }
1038
1054
  // }
@@ -1056,7 +1072,7 @@ router.get('/', function (req, res, next) {
1056
1072
  }
1057
1073
 
1058
1074
  if (req.query.snap_department_id_bot_exists) {
1059
- query["snapshot.department.id_bot"] = {"$exists": req.query.snap_department_id_bot_exists}
1075
+ query["snapshot.department.id_bot"] = { "$exists": req.query.snap_department_id_bot_exists }
1060
1076
  winston.debug('REQUEST ROUTE - QUERY snap_department_id_bot_exists', query.snap_department_id_bot_exists);
1061
1077
  }
1062
1078
 
@@ -1077,20 +1093,20 @@ router.get('/', function (req, res, next) {
1077
1093
 
1078
1094
  if (req.query.channel) {
1079
1095
  if (req.query.channel === "offline") {
1080
- query["channel.name"] = {"$in" : ["email", "form"]}
1081
- } else if (req.query.channel === "online") {
1082
- query["channel.name"] = {"$nin" : ["email", "form"]}
1096
+ query["channel.name"] = { "$in": ["email", "form"] }
1097
+ } else if (req.query.channel === "online") {
1098
+ query["channel.name"] = { "$nin": ["email", "form"] }
1083
1099
  } else {
1084
- query["channel.name"] = req.query.channel
1100
+ query["channel.name"] = req.query.channel
1085
1101
  }
1086
-
1102
+
1087
1103
  winston.debug('REQUEST ROUTE - QUERY channel', query.channel);
1088
1104
  }
1089
1105
 
1090
1106
  if (req.query.priority) {
1091
1107
  query.priority = req.query.priority;
1092
1108
  }
1093
-
1109
+
1094
1110
 
1095
1111
  var direction = -1; //-1 descending , 1 ascending
1096
1112
  if (req.query.direction) {
@@ -1113,63 +1129,63 @@ router.get('/', function (req, res, next) {
1113
1129
 
1114
1130
  var projection = undefined;
1115
1131
 
1116
- if (req.query.full_text) {
1132
+ if (req.query.full_text) {
1133
+
1134
+ if (req.query.no_textscore != "true" && req.query.no_textscore != true) {
1135
+ winston.info('fulltext projection on');
1136
+ projection = { score: { $meta: "textScore" } };
1137
+ }
1117
1138
 
1118
- if (req.query.no_textscore!= "true" && req.query.no_textscore!= true) {
1119
- winston.info('fulltext projection on');
1120
- projection = {score: { $meta: "textScore" } };
1121
- }
1122
-
1123
1139
  }
1124
1140
  // requestcachefarequi populaterequired
1125
1141
  var q1 = Request.find(query, projection).
1126
1142
  skip(skip).limit(limit);
1127
1143
 
1128
1144
 
1129
-
1130
1145
 
1131
1146
 
1132
- winston.debug('REQUEST ROUTE no_populate:' + req.query.no_populate);
1133
1147
 
1134
- if (req.query.no_populate != "true" && req.query.no_populate != true) {
1135
- winston.verbose('REQUEST ROUTE - no_polutate false ', req.headers);
1136
- q1.populate('department').
1148
+ winston.debug('REQUEST ROUTE no_populate:' + req.query.no_populate);
1149
+
1150
+ if (req.query.no_populate != "true" && req.query.no_populate != true) {
1151
+ winston.verbose('REQUEST ROUTE - no_polutate false ', req.headers);
1152
+ q1.populate('department').
1137
1153
  populate('participatingBots'). //nico già nn gli usa
1138
1154
  populate('participatingAgents'). //nico già nn gli usa
1139
1155
  populate('lead').
1140
- populate({path:'requester',populate:{path:'id_user'}}); //toglilo perche nico lo prende già da snapshot
1141
- }
1142
-
1143
- // cache(cacheUtil.defaultTTL, "requests-"+projectId).
1156
+ populate({ path: 'requester', populate: { path: 'id_user' } }); //toglilo perche nico lo prende già da snapshot
1157
+ }
1144
1158
 
1159
+ // cache(cacheUtil.defaultTTL, "requests-"+projectId).
1145
1160
 
1146
- // if (req.query.select_snapshot) {
1147
- // winston.info('select_snapshot');
1148
- // q1.select("+snapshot");
1149
- // // q1.select({ "snapshot": 1});
1150
- // }
1151
1161
 
1152
- if (req.query.full_text) {
1153
- winston.debug('fulltext sort');
1154
- if (req.query.no_textscore!= "true" && req.query.no_textscore!= true) {
1155
- q1.sort( { score: { $meta: "textScore" } } ) //https://docs.mongodb.com/manual/reference/operator/query/text/#sort-by-text-search-score
1156
- }
1157
- } else {
1158
- q1.sort(sortQuery);
1162
+ // if (req.query.select_snapshot) {
1163
+ // winston.info('select_snapshot');
1164
+ // q1.select("+snapshot");
1165
+ // // q1.select({ "snapshot": 1});
1166
+ // }
1167
+
1168
+ if (req.query.full_text) {
1169
+ winston.debug('fulltext sort');
1170
+ if (req.query.no_textscore != "true" && req.query.no_textscore != true) {
1171
+ q1.sort({ score: { $meta: "textScore" } }) //https://docs.mongodb.com/manual/reference/operator/query/text/#sort-by-text-search-score
1159
1172
  }
1160
-
1173
+ } else {
1174
+ q1.sort(sortQuery);
1175
+ }
1176
+
1161
1177
 
1162
- // winston.info('q1',q1);
1178
+ // winston.info('q1',q1);
1163
1179
 
1164
1180
 
1165
- q1.exec();
1181
+ q1.exec();
1166
1182
 
1167
- // TODO if ?onlycount=true do not perform find query but only
1168
- // set q1 to undefined; to skip query
1183
+ // TODO if ?onlycount=true do not perform find query but only
1184
+ // set q1 to undefined; to skip query
1169
1185
 
1170
- var q2 = Request.countDocuments(query).exec();
1186
+ var q2 = Request.countDocuments(query).exec();
1171
1187
 
1172
- if (req.query.no_count && req.query.no_count =="true") {
1188
+ if (req.query.no_count && req.query.no_count == "true") {
1173
1189
  winston.info('REQUEST ROUTE - no_count ');
1174
1190
  q2 = 0;
1175
1191
  }
@@ -1179,7 +1195,7 @@ router.get('/', function (req, res, next) {
1179
1195
  q2
1180
1196
  ];
1181
1197
 
1182
- Promise.all(promises).then(function(results) {
1198
+ Promise.all(promises).then(function (results) {
1183
1199
  var objectToReturn = {
1184
1200
  perPage: limit,
1185
1201
  count: results[1],
@@ -1189,15 +1205,15 @@ router.get('/', function (req, res, next) {
1189
1205
  winston.debug('REQUEST ROUTE - objectToReturn ', objectToReturn);
1190
1206
 
1191
1207
  const endExecTime = new Date();
1192
- winston.verbose('REQUEST ROUTE - exec time: ' + (endExecTime-startExecTime));
1208
+ winston.verbose('REQUEST ROUTE - exec time: ' + (endExecTime - startExecTime));
1193
1209
 
1194
1210
  return res.json(objectToReturn);
1195
1211
 
1196
- }).catch(function(err){
1212
+ }).catch(function (err) {
1197
1213
  winston.error('REQUEST ROUTE - REQUEST FIND ERR ', err);
1198
1214
  return res.status(500).send({ success: false, msg: 'Error getting requests.', err: err });
1199
1215
  });
1200
-
1216
+
1201
1217
 
1202
1218
  });
1203
1219
 
@@ -1229,7 +1245,7 @@ router.get('/csv', function (req, res, next) {
1229
1245
 
1230
1246
  if (req.query.full_text) {
1231
1247
  winston.debug('req.query.fulltext', req.query.full_text);
1232
- query.$text = {"$search": req.query.full_text};
1248
+ query.$text = { "$search": req.query.full_text };
1233
1249
  }
1234
1250
 
1235
1251
  if (req.query.status) {
@@ -1247,9 +1263,9 @@ router.get('/csv', function (req, res, next) {
1247
1263
  winston.debug('req.query.participant', req.query.participant);
1248
1264
  query.participants = req.query.participant;
1249
1265
  }
1250
-
1266
+
1251
1267
  winston.debug('req.query.hasbot', req.query.hasbot);
1252
- if (req.query.hasbot!=undefined) {
1268
+ if (req.query.hasbot != undefined) {
1253
1269
  winston.debug('req.query.hasbot', req.query.hasbot);
1254
1270
  query.hasBot = req.query.hasbot;
1255
1271
  }
@@ -1299,16 +1315,16 @@ router.get('/csv', function (req, res, next) {
1299
1315
  var direction = 1; //-1 descending , 1 ascending
1300
1316
  if (req.query.direction) {
1301
1317
  direction = req.query.direction;
1302
- }
1303
- winston.debug("direction",direction);
1318
+ }
1319
+ winston.debug("direction", direction);
1304
1320
 
1305
1321
  var sortField = "createdAt";
1306
1322
  if (req.query.sort) {
1307
1323
  sortField = req.query.sort;
1308
- }
1309
- winston.debug("sortField",sortField);
1324
+ }
1325
+ winston.debug("sortField", sortField);
1310
1326
 
1311
- var sortQuery={};
1327
+ var sortQuery = {};
1312
1328
  sortQuery[sortField] = direction;
1313
1329
 
1314
1330
  winston.debug("sort query", sortQuery);
@@ -1321,124 +1337,124 @@ router.get('/csv', function (req, res, next) {
1321
1337
  // aggiungi filtro per data marco
1322
1338
 
1323
1339
  winston.debug('REQUEST ROUTE - REQUEST FIND ', query)
1324
- return Request.find(query, '-transcript -status -__v').
1340
+ return Request.find(query, '-transcript -status -__v').
1325
1341
  skip(skip).limit(limit).
1326
- //populate('department', {'_id':-1, 'name':1}).
1327
- populate('department').
1328
- populate('lead').
1329
- // populate('participatingBots').
1330
- // populate('participatingAgents').
1331
- lean().
1332
- // populate({
1333
- // path: 'department',
1334
- // //select: { '_id': -1,'name':1}
1335
- // select: {'name':1}
1336
- // }).
1337
- sort(sortQuery).
1338
- exec(function (err, requests) {
1339
- if (err) {
1340
- winston.error('REQUEST ROUTE - REQUEST FIND ERR ', err)
1341
- return res.status(500).send({ success: false, msg: 'Error getting csv requests.',err:err });
1342
+ //populate('department', {'_id':-1, 'name':1}).
1343
+ populate('department').
1344
+ populate('lead').
1345
+ // populate('participatingBots').
1346
+ // populate('participatingAgents').
1347
+ lean().
1348
+ // populate({
1349
+ // path: 'department',
1350
+ // //select: { '_id': -1,'name':1}
1351
+ // select: {'name':1}
1352
+ // }).
1353
+ sort(sortQuery).
1354
+ exec(function (err, requests) {
1355
+ if (err) {
1356
+ winston.error('REQUEST ROUTE - REQUEST FIND ERR ', err)
1357
+ return res.status(500).send({ success: false, msg: 'Error getting csv requests.', err: err });
1358
+ }
1359
+
1360
+
1361
+ requests.forEach(function (element) {
1362
+
1363
+ var channel_name = "";
1364
+ if (element.channel && element.channel.name) {
1365
+ channel_name = element.channel.name;
1342
1366
  }
1343
-
1367
+ delete element.channel;
1368
+ element.channel_name = channel_name;
1344
1369
 
1345
- requests.forEach(function(element) {
1370
+ var department_name = "";
1371
+ if (element.department && element.department.name) {
1372
+ department_name = element.department.name;
1373
+ }
1374
+ delete element.department;
1375
+ element.department_name = department_name;
1346
1376
 
1347
- var channel_name = "";
1348
- if (element.channel && element.channel.name) {
1349
- channel_name = element.channel.name;
1350
- }
1351
- delete element.channel;
1352
- element.channel_name = channel_name;
1377
+ var lead_fullname = "";
1378
+ if (element.lead && element.lead.fullname) {
1379
+ lead_fullname = element.lead.fullname
1380
+ }
1381
+ element.lead_fullname = lead_fullname;
1353
1382
 
1354
- var department_name = "";
1355
- if (element.department && element.department.name) {
1356
- department_name = element.department.name;
1357
- }
1358
- delete element.department;
1359
- element.department_name = department_name;
1360
1383
 
1361
- var lead_fullname = "";
1362
- if (element.lead && element.lead.fullname) {
1363
- lead_fullname = element.lead.fullname
1364
- }
1365
- element.lead_fullname = lead_fullname;
1384
+ var lead_email = "";
1385
+ if (element.lead && element.lead.email) {
1386
+ lead_email = element.lead.email
1387
+ }
1366
1388
 
1389
+ element.lead_email = lead_email;
1367
1390
 
1368
- var lead_email = "";
1369
- if (element.lead && element.lead.email) {
1370
- lead_email = element.lead.email
1371
- }
1372
-
1373
- element.lead_email = lead_email;
1391
+ var tags = [];
1392
+ var tagsString = "";
1393
+ if (element.tags && element.tags.length > 0) {
1374
1394
 
1375
- var tags = [];
1376
- var tagsString = "";
1377
- if (element.tags && element.tags.length>0) {
1378
-
1379
- element.tags.forEach(function(tag) {
1380
- // tags = tags + tag.tag + ", ";
1381
- tags.push(tag.tag);
1382
- });
1383
- }
1384
- tagsString = tags.join(", ")
1395
+ element.tags.forEach(function (tag) {
1396
+ // tags = tags + tag.tag + ", ";
1397
+ tags.push(tag.tag);
1398
+ });
1399
+ }
1400
+ tagsString = tags.join(", ")
1385
1401
 
1386
- winston.debug('tagsString ' +tagsString)
1387
-
1388
- element.tags = tagsString;
1402
+ winston.debug('tagsString ' + tagsString)
1389
1403
 
1404
+ element.tags = tagsString;
1390
1405
 
1391
- // var participatingAgents = "";
1392
- // if (element.participatingAgents && element.participatingAgents.length>0) {
1393
- // element.participatingAgents.forEach(function(agent) {
1394
- // participatingAgents = participatingAgents + ", " + agent;
1395
- // });
1396
- // }
1397
- // // da terminare e testare. potrebbe essere troppo lenta la query per tanti record
1398
- // element.participatingAgents = participatingAgents;
1399
-
1400
1406
 
1407
+ // var participatingAgents = "";
1408
+ // if (element.participatingAgents && element.participatingAgents.length>0) {
1409
+ // element.participatingAgents.forEach(function(agent) {
1410
+ // participatingAgents = participatingAgents + ", " + agent;
1411
+ // });
1412
+ // }
1413
+ // // da terminare e testare. potrebbe essere troppo lenta la query per tanti record
1414
+ // element.participatingAgents = participatingAgents;
1401
1415
 
1402
- delete element.lead;
1403
1416
 
1404
- delete element.attributes;
1405
1417
 
1406
- delete element.notes;
1418
+ delete element.lead;
1407
1419
 
1408
- // delete element.tags;
1420
+ delete element.attributes;
1409
1421
 
1410
- delete element.channelOutbound;
1422
+ delete element.notes;
1411
1423
 
1412
- delete element.location;
1424
+ // delete element.tags;
1413
1425
 
1414
- delete element.snapshot;
1415
-
1426
+ delete element.channelOutbound;
1416
1427
 
1417
- // TODO print also lead. use a library to flattize
1418
- });
1428
+ delete element.location;
1419
1429
 
1420
- winston.debug('REQUEST ROUTE - REQUEST AS CSV', requests);
1430
+ delete element.snapshot;
1431
+
1432
+
1433
+ // TODO print also lead. use a library to flattize
1434
+ });
1435
+
1436
+ winston.debug('REQUEST ROUTE - REQUEST AS CSV', requests);
1437
+
1438
+ return res.csv(requests, true);
1439
+ });
1440
+
1441
+ // });
1421
1442
 
1422
- return res.csv(requests, true);
1423
- });
1424
-
1425
- // });
1426
-
1427
1443
  });
1428
1444
 
1429
1445
  router.get('/:requestid', function (req, res) {
1430
1446
 
1431
1447
  var requestid = req.params.requestid;
1432
- winston.debug("get request by id: "+requestid);
1448
+ winston.debug("get request by id: " + requestid);
1449
+
1433
1450
 
1434
-
1435
- let q = Request.findOne({request_id: requestid, id_project: req.projectid})
1436
- // .select("+snapshot.agents")
1437
- .populate('lead')
1438
- .populate('department')
1439
- .populate('participatingBots')
1440
- .populate('participatingAgents')
1441
- .populate({path:'requester',populate:{path:'id_user'}});
1451
+ let q = Request.findOne({ request_id: requestid, id_project: req.projectid })
1452
+ // .select("+snapshot.agents")
1453
+ .populate('lead')
1454
+ .populate('department')
1455
+ .populate('participatingBots')
1456
+ .populate('participatingAgents')
1457
+ .populate({ path: 'requester', populate: { path: 'id_user' } });
1442
1458
 
1443
1459
  // if (cacheEnabler.request) { cache disabled beacuse cacheoose don't support .populate without lean. here cache is not important
1444
1460
  // q.cache(cacheUtil.defaultTTL, req.projectid+":requests:request_id:"+requestid) //request_cache
@@ -1446,7 +1462,7 @@ router.get('/:requestid', function (req, res) {
1446
1462
  // }
1447
1463
  //
1448
1464
  // .populate({path:'requester',populate:{path:'id_user', select:{'firstname':1, 'lastname':1}}})
1449
- q.exec(function(err, request) {
1465
+ q.exec(function (err, request) {
1450
1466
  if (err) {
1451
1467
  winston.error("error getting request by id ", err);
1452
1468
  return res.status(500).send({ success: false, msg: 'Error getting object.' });