@tiledesk/tiledesk-server 2.2.24 → 2.2.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/models/project.js CHANGED
@@ -101,6 +101,16 @@ var ProjectSchema = new Schema({
101
101
  return [new Channel({ name: 'chat21' })];
102
102
  }
103
103
  },
104
+ organization: {
105
+ type: String,
106
+ },
107
+ ipFilterEnabled:{
108
+ type: Boolean,
109
+ default: false
110
+ },
111
+ ipFilter: [{
112
+ type: String
113
+ }],
104
114
  // defaultLanguage: {
105
115
  // type: String,
106
116
  // required: true,
@@ -5,6 +5,7 @@ var Schema = mongoose.Schema;
5
5
  var winston = require('../config/winston');
6
6
  var RoleConstants = require('./roleConstants');
7
7
  var PresenceSchema = require('./presence');
8
+ var TagSchema = require("../models/tag");
8
9
 
9
10
 
10
11
 
@@ -58,6 +59,7 @@ var PresenceSchema = require('./presence');
58
59
  settings: {
59
60
  type: Object,
60
61
  },
62
+ tags: [TagSchema],
61
63
  createdBy: {
62
64
  type: String,
63
65
  required: true
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-server",
3
3
  "description": "The Tiledesk server module",
4
- "version": "2.2.24",
4
+ "version": "2.2.29",
5
5
  "scripts": {
6
6
  "start": "node ./bin/www",
7
7
  "pretest": "mongodb-runner start",
@@ -38,7 +38,7 @@
38
38
  "@tiledesk-ent/tiledesk-server-groups": "^1.1.2",
39
39
  "@tiledesk-ent/tiledesk-server-jwthistory": "^1.1.9",
40
40
  "@tiledesk-ent/tiledesk-server-mt": "^1.1.7",
41
- "@tiledesk-ent/tiledesk-server-payments": "^1.1.5",
41
+ "@tiledesk-ent/tiledesk-server-payments": "^1.1.6",
42
42
  "@tiledesk-ent/tiledesk-server-queue": "^1.1.11",
43
43
  "@tiledesk-ent/tiledesk-server-request-history": "^1.1.5",
44
44
  "@tiledesk-ent/tiledesk-server-resthook": "^1.1.53",
@@ -61,6 +61,7 @@
61
61
  "email-templates": "^8.0.8",
62
62
  "eventemitter2": "^6.4.4",
63
63
  "express": "~4.17.1",
64
+ "express-ipfilter": "^1.2.0",
64
65
  "express-session": "^1.17.2",
65
66
  "express-validator": "^6.12.1",
66
67
  "fast-csv": "^4.3.6",
@@ -109,7 +110,7 @@
109
110
  "chai-string": "^1.5.0",
110
111
  "loadtest": "^5.1.2",
111
112
  "mocha": "^8.4.0",
112
- "mongodb-runner": "4.8.1",
113
+ "mongodb-runner": "4.8.3",
113
114
  "nodemon": "^2.0.12",
114
115
  "sinon": "^9.2.4",
115
116
  "sinon-mongoose": "^2.3.0"
@@ -70,8 +70,11 @@ devi mandare un messaggio welcome tu altrimenti il bot inserito successivamente
70
70
  //TESTA QUESTO
71
71
 
72
72
  winston.debug("message.request.status: "+message.request.status);
73
- winston.debug("message.request.department.id_bot: "+message.request.department.id_bot);
74
- if (message.request.status === 50 && message.request.department.id_bot == undefined) {
73
+
74
+ winston.debug("message.request.hasBot: "+message.request.hasBot);
75
+ // winston.info("message.request.department.id_bot: "+message.request.department.id_bot);
76
+ if (message.request.status === 50 && message.request.hasBot === false) {
77
+ // if (message.request.status === 50 && message.request.department.id_bot == undefined) {
75
78
  //apply only if the status is temp and no bot is available. with agent you must reroute to assign temp request to an agent
76
79
  winston.debug("rerouting");
77
80
  // reroute(request_id, id_project, nobot)
@@ -48,9 +48,15 @@ scheduleUnresponsiveRequests() {
48
48
  // }
49
49
 
50
50
  //https://crontab.guru/examples.html
51
- var s= schedule.scheduleJob(this.cronExp, function(fireDate){
52
- winston.debug('CloseBotUnresponsiveRequestTask scheduleUnresponsiveRequests job was supposed to run at ' + fireDate + ', but actually ran at ' + new Date());
53
- that.findUnresponsiveRequests();
51
+ var s= schedule.scheduleJob(this.cronExp, function(fireDate){ //TODO aggiungi un bias random
52
+
53
+ let timeInMs = Math.random() * (1000); // avoid cluster concurrent jobs in multiple nodes between 0 and 1sec
54
+ winston.info('timeInMs => '+ timeInMs);
55
+
56
+ setTimeout(function () {
57
+ winston.debug('CloseBotUnresponsiveRequestTask scheduleUnresponsiveRequests job was supposed to run at ' + fireDate + ', but actually ran at ' + new Date());
58
+ that.findUnresponsiveRequests();
59
+ },timeInMs );
54
60
  });
55
61
  }
56
62
 
@@ -65,6 +71,8 @@ findUnresponsiveRequests() {
65
71
  query.id_project = JSON.parse(this.queryProject);
66
72
  }
67
73
 
74
+
75
+
68
76
  winston.debug("CloseBotUnresponsiveRequestTask query",query);
69
77
 
70
78
  Request.find(query)
@@ -84,6 +92,7 @@ findUnresponsiveRequests() {
84
92
  winston.debug("CloseBotUnresponsiveRequestTask: found unresponsive requests ", requests);
85
93
 
86
94
  requests.forEach(request => {
95
+
87
96
  winston.debug("********unresponsive request ", request);
88
97
 
89
98
  return requestService.closeRequestByRequestId(request.request_id, request.id_project, false, false).then(function(updatedStatusRequest) {
package/routes/faq_kb.js CHANGED
@@ -10,9 +10,8 @@ var httpUtil = require("../utils/httpUtil");
10
10
 
11
11
  router.post('/', function (req, res) {
12
12
  winston.info('create BOT ', req.body);
13
- // create(name, url, projectid, user_id, type, description) {
14
- faqService.create(req.body.name, req.body.url, req.projectid, req.user.id, req.body.type, req.body.description, undefined, undefined,
15
- req.body.language).then(function(savedFaq_kb) {
13
+ // create(name, url, projectid, user_id, type, description, webhook_url, webhook_enabled, language, template)
14
+ faqService.create(req.body.name, req.body.url, req.projectid, req.user.id, req.body.type, req.body.description, undefined, undefined, req.body.language, req.body.template).then(function(savedFaq_kb) {
16
15
  res.json(savedFaq_kb);
17
16
  });
18
17
 
package/routes/labels.js CHANGED
@@ -27,6 +27,8 @@ router.get('/default', function (req, res) {
27
27
  });
28
28
  // curl -v -X POST -H 'Content-Type:application/json' -d '{"lang":"IT"}' http://localhost:3000/123/labels/default/clone
29
29
 
30
+ // curl -v -X POST -H 'Content-Type:application/json' -u andrea.leo@f21.it:123456 -d '{"lang":"IT"}' http://localhost:3001/624c78e27b91d2a2ab49543a/labels/default/clone
31
+
30
32
  router.post('/default/clone',
31
33
  [
32
34
  passport.authenticate(['basic', 'jwt'], { session: false }),
package/routes/project.js CHANGED
@@ -15,6 +15,7 @@ require('../middleware/passport')(passport);
15
15
  var validtoken = require('../middleware/valid-token')
16
16
  var RoleConstants = require("../models/roleConstants");
17
17
  var cacheUtil = require('../utils/cacheUtil');
18
+ var orgUtil = require("../utils/orgUtil");
18
19
 
19
20
  router.put('/:projectid', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRole('admin')], function (req, res) {
20
21
  winston.debug('UPDATE PROJECT REQ BODY ', req.body);
@@ -151,6 +152,15 @@ router.put('/:projectid', [passport.authenticate(['basic', 'jwt'], { session: fa
151
152
  if (req.body.channels!=undefined) {
152
153
  update.channels = req.body.channels;
153
154
  }
155
+
156
+ if (req.body.ipFilterEnabled!=undefined) {
157
+ update.ipFilterEnabled = req.body.ipFilterEnabled;
158
+ }
159
+
160
+ if (req.body.ipFilter!=undefined) {
161
+ update.ipFilter = req.body.ipFilter;
162
+ }
163
+
154
164
 
155
165
  // if (req.body.defaultLanguage!=undefined) {
156
166
  // update.defaultLanguage = req.body.defaultLanguage;
@@ -291,6 +301,13 @@ router.patch('/:projectid', [passport.authenticate(['basic', 'jwt'], { session:
291
301
  update.channels = req.body.channels;
292
302
  }
293
303
 
304
+ if (req.body.ipFilterEnabled!=undefined) {
305
+ update.ipFilterEnabled = req.body.ipFilterEnabled;
306
+ }
307
+
308
+ if (req.body.ipFilter!=undefined) {
309
+ update.ipFilter = req.body.ipFilter;
310
+ }
294
311
 
295
312
  // if (req.body.defaultLanguage!=undefined) {
296
313
  // update.defaultLanguage = req.body.defaultLanguage;
@@ -362,6 +379,61 @@ router.get('/', [passport.authenticate(['basic', 'jwt'], { session: false }), va
362
379
  return res.status(500).send({ success: false, msg: 'Error getting object.' });
363
380
  }
364
381
 
382
+
383
+
384
+ //organization: if third sub domain iterate and put only project with organization==subdomain otherwise remove projects with org
385
+ winston.info("orgUtil.ORGANIZATION_ENABLED: " + orgUtil.ORGANIZATION_ENABLED);
386
+ if (orgUtil.ORGANIZATION_ENABLED == true ) {
387
+
388
+ // winston.info("project_users", project_users);
389
+ winston.info("project_users.length:"+ project_users.length);
390
+
391
+ let org = orgUtil.getOrg(req);
392
+ winston.info("org:"+ org);
393
+
394
+ if (org!=undefined) {
395
+ winston.info("org!=undefined");
396
+
397
+ var project_users = project_users.filter(function (projectUser) {
398
+ if (projectUser.id_project.organization && projectUser.id_project.organization === org ) {
399
+ winston.info("keep");
400
+ return true;
401
+ }
402
+ });
403
+
404
+ /* for (var i = 0; i < project_users.length; i++) {
405
+ winston.info("project_users[i].id_project.organization: " + project_users[i].id_project.organization);
406
+ if (project_users[i].id_project.organization && project_users[i].id_project.organization === org ) {
407
+ //keep
408
+ winston.info("keep");
409
+ } else {
410
+ // project_users.splice(i, 1); // 2nd parameter means remove one item only
411
+ winston.info("splice");
412
+ }
413
+ }*/
414
+ } else {
415
+
416
+ var project_users = project_users.filter(function (projectUser) {
417
+ if (projectUser.id_project.organization == undefined ) {
418
+ // winston.info("keep");
419
+ return true;
420
+ }
421
+ });
422
+
423
+
424
+ /*
425
+ for (var i = 0; i < project_users.length; i++) {
426
+ winston.info("project_users[i].id_project.organization: " + project_users[i].id_project.organization);
427
+ if (project_users[i].id_project.organization) {
428
+ project_users.splice(i, 1); // 2nd parameter means remove one item only
429
+ }
430
+ }*/
431
+ }
432
+ } else {
433
+ winston.info("no")
434
+ }
435
+
436
+
365
437
  project_users.sort((a, b) => (a.id_project && b.id_project && a.id_project.updatedAt > b.id_project.updatedAt) ? 1 : -1)
366
438
  project_users.reverse();
367
439
  res.json(project_users);
@@ -196,7 +196,8 @@ router.post('/', [passport.authenticate(['basic', 'jwt'], { session: false }), v
196
196
  // role: RoleConstants.USER,
197
197
  // - Create project_user endpoint by agent (Ticketing) now is with Guest Role
198
198
  role: RoleConstants.GUEST,
199
- user_available: false,
199
+ user_available: false,
200
+ tags: req.body.tags,
200
201
  createdBy: req.user.id,
201
202
  updatedBy: req.user.id
202
203
  });
@@ -234,6 +235,10 @@ router.put('/', [passport.authenticate(['basic', 'jwt'], { session: false }), va
234
235
  if (req.body["settings.email.notification.conversation.pooled"]!=undefined) {
235
236
  update["settings.email.notification.conversation.pooled"] = req.body["settings.email.notification.conversation.pooled"];
236
237
  }
238
+ if (req.body.tags!=undefined) {
239
+ update.tags = req.body.tags;
240
+ }
241
+
237
242
 
238
243
 
239
244
  Project_user.findByIdAndUpdate(req.projectuser.id, update, { new: true, upsert: true }, function (err, updatedProject_user) {
@@ -289,7 +294,9 @@ router.put('/:project_userid', [passport.authenticate(['basic', 'jwt'], { sessio
289
294
  update["settings.email.notification.conversation.pooled"] = req.body["settings.email.notification.conversation.pooled"];
290
295
  }
291
296
 
292
-
297
+ if (req.body.tags!=undefined) {
298
+ update.tags = req.body.tags;
299
+ }
293
300
 
294
301
  winston.debug("project_userid update", update);
295
302
 
package/routes/request.js CHANGED
@@ -693,6 +693,10 @@ router.get('/', function (req, res, next) {
693
693
  query.location = req.query.location;
694
694
  }
695
695
 
696
+ if (req.query.ticket_id) {
697
+ query.ticket_id = req.query.ticket_id;
698
+ }
699
+
696
700
  // if (req.query.request_id) {
697
701
  // console.log('req.query.request_id', req.query.request_id);
698
702
  // query.request_id = req.query.request_id;
@@ -798,6 +802,17 @@ router.get('/', function (req, res, next) {
798
802
  winston.debug('REQUEST ROUTE - QUERY snap_department_id_bot_exists', query.snap_department_id_bot_exists);
799
803
  }
800
804
 
805
+ if (req.query.snap_lead_lead_id) {
806
+ query["snapshot.lead.lead_id"] = req.query.snap_lead_lead_id;
807
+ winston.debug('REQUEST ROUTE - QUERY snap_lead_lead_id', query.snap_lead_lead_id);
808
+ }
809
+
810
+ if (req.query.channel) {
811
+ query["channel.name"] = req.query.channel
812
+ winston.debug('REQUEST ROUTE - QUERY channel', query.channel);
813
+ }
814
+
815
+
801
816
  var direction = -1; //-1 descending , 1 ascending
802
817
  if (req.query.direction) {
803
818
  direction = req.query.direction;
@@ -4,6 +4,8 @@ var Request = require("../models/request");
4
4
  var winston = require('../config/winston');
5
5
  const requestEvent = require('../event/requestEvent');
6
6
  const { check, validationResult } = require('express-validator');
7
+ var requestService = require('../services/requestService');
8
+ var mongoose = require('mongoose');
7
9
 
8
10
 
9
11
  router.patch('/:requestid/rating', function (req, res) {
@@ -54,5 +56,329 @@ router.patch('/:requestid/rating', function (req, res) {
54
56
  });
55
57
 
56
58
 
59
+ router.put('/:requestid/closeg', function (req, res) {
60
+ winston.debug(req.body);
61
+
62
+ // closeRequestByRequestId(request_id, id_project)
63
+ return requestService.closeRequestByRequestId(req.params.requestid, req.projectid).then(function(closedRequest) {
64
+
65
+ winston.verbose("request closed", closedRequest);
66
+
67
+ return res.json(closedRequest);
68
+
69
+ });
70
+
71
+
72
+ });
73
+
74
+
75
+
76
+
77
+ router.get('/me', function (req, res, next) {
78
+
79
+ winston.debug("req projectid", req.projectid);
80
+ winston.debug("req.query.sort", req.query.sort);
81
+ winston.debug('REQUEST ROUTE - QUERY ', req.query)
82
+
83
+ const DEFAULT_LIMIT = 40;
84
+
85
+ var limit = DEFAULT_LIMIT; // Number of request per page
86
+
87
+ if (req.query.limit) {
88
+ limit = parseInt(req.query.limit);
89
+ }
90
+ if (limit > 100) {
91
+ limit = DEFAULT_LIMIT;
92
+ }
93
+
94
+
95
+ var page = 0;
96
+
97
+ if (req.query.page) {
98
+ page = req.query.page;
99
+ }
100
+
101
+ var skip = page * limit;
102
+ winston.debug('REQUEST ROUTE - SKIP PAGE ', skip);
103
+
104
+
105
+ var user_id = req.user._id;
106
+ winston.debug('REQUEST ROUTE - user_id: '+user_id);
107
+
108
+ var isObjectId = mongoose.Types.ObjectId.isValid(user_id);
109
+ winston.debug("isObjectId:"+ isObjectId);
110
+
111
+
112
+ var query = { "id_project": req.projectid, "status": {$lt:1000}, preflight:false};
113
+
114
+
115
+ if (isObjectId) {
116
+ query["snapshot.requester.id_user"] = user_id;
117
+ // query.id_user = mongoose.Types.ObjectId(contact_id);
118
+ } else {
119
+ query["snapshot.requester.uuid_user"] = user_id;
120
+ }
121
+
122
+
123
+ // $or:[{"snapshot.requester.id_user": user_id}, {"snapshot.requester.uuid_user": user_id}]};
124
+
125
+ winston.debug('REQUEST ROUTE - query ', query);
126
+
127
+
128
+ if (req.query.dept_id) {
129
+ query.department = req.query.dept_id;
130
+ winston.debug('REQUEST ROUTE - QUERY DEPT ID', query.department);
131
+ }
132
+
133
+ if (req.query.full_text) {
134
+ winston.debug('req.query.fulltext', req.query.full_text);
135
+ query.$text = { "$search": req.query.full_text };
136
+ }
137
+
138
+ var history_search = false;
139
+
140
+ if (req.query.status) {
141
+ winston.debug('req.query.status', req.query.status);
142
+ query.status = req.query.status;
143
+
144
+ if (req.query.status == 1000 || req.query.status == "1000") {
145
+ history_search = true;
146
+ }
147
+ if (req.query.status==="all") {
148
+ history_search = true;
149
+ delete query.status;
150
+ }
151
+ }
152
+
153
+ // if (req.query.lead) {
154
+ // winston.debug('req.query.lead', req.query.lead);
155
+ // query.lead = req.query.lead;
156
+ // }
157
+
158
+ // USERS & BOTS
159
+ if (req.query.participant) {
160
+ winston.debug('req.query.participant', req.query.participant);
161
+ query.participants = req.query.participant;
162
+ }
163
+
164
+ winston.debug('req.query.hasbot', req.query.hasbot);
165
+ if (req.query.hasbot!=undefined) {
166
+ winston.debug('req.query.hasbot', req.query.hasbot);
167
+ query.hasBot = req.query.hasbot;
168
+ }
169
+
170
+ // if (req.query.waiting_time_exists) { //non basta aggiungi anche che nn è null
171
+ // query.waiting_time = {"$exists": req.query.waiting_time_exists} //{$ne:null}
172
+ // winston.debug('REQUEST ROUTE - QUERY waiting_time_exists', query.waiting_time_exists);
173
+ // }
174
+
175
+
176
+ if (req.query.tags) {
177
+ winston.debug('req.query.tags', req.query.tags);
178
+ query["tags.tag"] = req.query.tags;
179
+ }
180
+
181
+ if (req.query.location) {
182
+ query.location = req.query.location;
183
+ }
184
+
185
+ if (req.query.ticket_id) {
186
+ query.ticket_id = req.query.ticket_id;
187
+ }
188
+
189
+ // if (req.query.request_id) {
190
+ // console.log('req.query.request_id', req.query.request_id);
191
+ // query.request_id = req.query.request_id;
192
+ // }
193
+
194
+ /**
195
+ **! *** DATE RANGE USECASE 1 ***
196
+ * in the tiledesk dashboard's HISTORY PAGE
197
+ * WHEN THE TRIAL IS EXIPIRED OR THE SUBSCIPTION IS NOT ACTIVE
198
+ * THE SEARCH FOR DATE INTERVAL OF THE HISTORY OF REQUESTS ARE DISABLED AND
199
+ * ARE DISPLAYED ONLY THE REQUESTS OF THE LAST 14 DAYS
200
+ */
201
+ 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)) {
202
+
203
+
204
+ var startdate = moment().subtract(14, "days").format("YYYY-MM-DD");
205
+
206
+ var enddate = moment().format("YYYY-MM-DD");
207
+
208
+ winston.debug('»»» REQUEST ROUTE - startdate ', startdate);
209
+ winston.debug('»»» REQUEST ROUTE - enddate ', enddate);
210
+
211
+ var enddatePlusOneDay= moment(new Date()).add(1, 'days').toDate()
212
+ winston.debug('»»» REQUEST ROUTE - enddate + 1 days: ', enddatePlusOneDay);
213
+
214
+ // var enddatePlusOneDay = "2019-09-17T00:00:00.000Z"
215
+
216
+ query.createdAt = { $gte: new Date(Date.parse(startdate)).toISOString(), $lte: new Date(enddatePlusOneDay).toISOString() }
217
+ winston.debug('REQUEST ROUTE - QUERY CREATED AT ', query.createdAt);
218
+
219
+ }
220
+
221
+ /**
222
+ **! *** DATE RANGE USECASE 2 ***
223
+ * in the tiledesk dashboard's HISTORY PAGE
224
+ * WHEN THE USER SEARCH FOR DATE INTERVAL OF THE HISTORY OF REQUESTS
225
+ */
226
+ if (req.query.start_date && req.query.end_date) {
227
+ winston.debug('REQUEST ROUTE - REQ QUERY start_date ', req.query.start_date);
228
+ winston.debug('REQUEST ROUTE - REQ QUERY end_date ', req.query.end_date);
229
+
230
+ /**
231
+ * USING TIMESTAMP in MS */
232
+ // var formattedStartDate = new Date(+req.query.start_date);
233
+ // var formattedEndDate = new Date(+req.query.end_date);
234
+ // query.createdAt = { $gte: formattedStartDate, $lte: formattedEndDate }
235
+
236
+ /**
237
+ * USING MOMENT */
238
+ var startDate = moment(req.query.start_date, 'DD/MM/YYYY').format('YYYY-MM-DD');
239
+ var endDate = moment(req.query.end_date, 'DD/MM/YYYY').format('YYYY-MM-DD');
240
+
241
+ winston.debug('REQUEST ROUTE - REQ QUERY FORMATTED START DATE ', startDate);
242
+ winston.debug('REQUEST ROUTE - REQ QUERY FORMATTED END DATE ', endDate);
243
+
244
+ // ADD ONE DAY TO THE END DAY
245
+ var date = new Date(endDate);
246
+ var newdate = new Date(date);
247
+ var endDate_plusOneDay = newdate.setDate(newdate.getDate() + 1);
248
+ winston.debug('REQUEST ROUTE - REQ QUERY FORMATTED END DATE + 1 DAY ', endDate_plusOneDay);
249
+ // var endDate_plusOneDay = moment('2018-09-03').add(1, 'd')
250
+ // var endDate_plusOneDay = endDate.add(1).day();
251
+ // var toDate = new Date(Date.parse(endDate_plusOneDay)).toISOString()
252
+
253
+ query.createdAt = { $gte: new Date(Date.parse(startDate)).toISOString(), $lte: new Date(endDate_plusOneDay).toISOString() }
254
+ winston.debug('REQUEST ROUTE - QUERY CREATED AT ', query.createdAt);
255
+
256
+ } else if (req.query.start_date && !req.query.end_date) {
257
+ winston.debug('REQUEST ROUTE - REQ QUERY END DATE IS EMPTY (so search only for start date)');
258
+ var startDate = moment(req.query.start_date, 'DD/MM/YYYY').format('YYYY-MM-DD');
259
+
260
+ var range = { $gte: new Date(Date.parse(startDate)).toISOString() };
261
+ if (req.query.filterRangeField) {
262
+ query[req.query.filterRangeField] = range;
263
+ }else {
264
+ query.createdAt = range;
265
+ }
266
+
267
+ winston.debug('REQUEST ROUTE - QUERY CREATED AT (only for start date)', query.createdAt);
268
+ }
269
+ // }
270
+
271
+
272
+
273
+ if (req.query.snap_department_routing) {
274
+ query["snapshot.department.routing"] = req.query.snap_department_routing;
275
+ winston.debug('REQUEST ROUTE - QUERY snap_department_routing', query.snap_department_routing);
276
+ }
277
+
278
+ if (req.query.snap_department_default) {
279
+ query["snapshot.department.default"] = req.query.snap_department_default;
280
+ winston.debug('REQUEST ROUTE - QUERY snap_department_default', query.snap_department_default);
281
+ }
282
+
283
+
284
+ if (req.query.snap_department_id_bot) {
285
+ query["snapshot.department.id_bot"] = req.query.snap_department_id_bot;
286
+ winston.debug('REQUEST ROUTE - QUERY snap_department_id_bot', query.snap_department_id_bot);
287
+ }
288
+
289
+ if (req.query.snap_department_id_bot_exists) {
290
+ query["snapshot.department.id_bot"] = {"$exists": req.query.snap_department_id_bot_exists}
291
+ winston.debug('REQUEST ROUTE - QUERY snap_department_id_bot_exists', query.snap_department_id_bot_exists);
292
+ }
293
+
294
+ // if (req.query.snap_lead_lead_id) {
295
+ // query["snapshot.lead.lead_id"] = req.query.snap_lead_lead_id;
296
+ // winston.debug('REQUEST ROUTE - QUERY snap_lead_lead_id', query.snap_lead_lead_id);
297
+ // }
298
+
299
+ if (req.query.channel) {
300
+ query["channel.name"] = req.query.channel
301
+ winston.debug('REQUEST ROUTE - QUERY channel', query.channel);
302
+ }
303
+
304
+
305
+ var direction = -1; //-1 descending , 1 ascending
306
+ if (req.query.direction) {
307
+ direction = req.query.direction;
308
+ }
309
+ winston.debug("direction", direction);
310
+
311
+ var sortField = "createdAt";
312
+ if (req.query.sort) {
313
+ sortField = req.query.sort;
314
+ }
315
+ winston.debug("sortField", sortField);
316
+
317
+ var sortQuery = {};
318
+ sortQuery[sortField] = direction;
319
+
320
+ winston.debug("sort query", sortQuery);
321
+
322
+ winston.verbose('REQUEST ROUTE - REQUEST FIND ', query);
323
+
324
+ // requestcachefarequi populaterequired
325
+ var q1 = Request.find(query).
326
+ skip(skip).limit(limit);
327
+
328
+
329
+ winston.debug('REQUEST ROUTE no_populate:' + req.query.no_populate);
330
+
331
+ if (req.query.no_populate != "true" && req.query.no_populate != true) {
332
+ winston.verbose('REQUEST ROUTE - no_polutate false ', req.headers);
333
+ q1.populate('department').
334
+ populate('participatingBots'). //nico già nn gli usa
335
+ populate('participatingAgents'). //nico già nn gli usa
336
+ populate('lead').
337
+ populate({path:'requester',populate:{path:'id_user'}}); //toglilo perche nico lo prende già da snapshot
338
+ }
339
+
340
+ // cache(cacheUtil.defaultTTL, "requests-"+projectId).
341
+
342
+
343
+ // if (req.query.select_snapshot) {
344
+ // winston.info('select_snapshot');
345
+ // q1.select("+snapshot");
346
+ // // q1.select({ "snapshot": 1});
347
+ // }
348
+
349
+ q1.sort(sortQuery);
350
+
351
+ // winston.info('q1',q1);
352
+
353
+
354
+ q1.exec();
355
+
356
+ // TODO if ?onlycount=true do not perform find query but only
357
+ // set q1 to undefined; to skip query
358
+
359
+ var q2 = Request.countDocuments(query).exec();
360
+
361
+ var promises = [
362
+ q1,
363
+ q2
364
+ ];
365
+
366
+ Promise.all(promises).then(function(results) {
367
+ var objectToReturn = {
368
+ perPage: limit,
369
+ count: results[1],
370
+ requests: results[0]
371
+ };
372
+ winston.debug('REQUEST ROUTE - objectToReturn ', objectToReturn);
373
+ return res.json(objectToReturn);
374
+
375
+ }).catch(function(err){
376
+ winston.error('REQUEST ROUTE - REQUEST FIND ERR ', err);
377
+ return res.status(500).send({ success: false, msg: 'Error getting requests.', err: err });
378
+ });
379
+
380
+
381
+ });
382
+
57
383
 
58
384
  module.exports = router;
package/routes/widget.js CHANGED
@@ -116,7 +116,7 @@ router.get('/', function(req, res, next) {
116
116
 
117
117
  // TOOD add labels
118
118
  Promise.all([
119
- Project.findOne({_id: req.projectid, status: 100}).select('-settings') //--profile todo trialExpired attento user_available cambia spesso quindi magari leggerlo dal db ogni volta
119
+ Project.findOne({_id: req.projectid, status: 100}).select('-settings')
120
120
  //@DISABLED_CACHE .cache(cacheUtil.queryTTL, "projects:query:id:status:100:"+req.projectid+":select:-settings")
121
121
  ,
122
122
  availableUsers()