@tiledesk/tiledesk-server 2.2.26 → 2.2.30

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -1,15 +1,33 @@
1
- # untagged
2
- - Operator.select returns context object that contains the temp request..TEST IT
1
+
2
+ # 2.2.29
3
+ - Added endpoint to find requests created by users and guests
4
+ - Log fix
5
+
6
+ # 2.2.28 (compatible with dasboard ver. 2.2.36)
7
+ - Operator.select returns context object that contains the temp request
3
8
  - Added Serbian language to the widget
4
9
  - Added tag field to the project_user
5
10
  - Removed default BCC from email
6
11
  - BugFix: Avoid cluster concurrent jobs in multiple nodes
12
+ - Faq template now support blank and example
13
+ - Organizzation support added
14
+ - ipFilter related to the project is now supported
15
+ - Added filter channel name for the request
16
+ - Added edit card for payment
17
+ - Fix concierge concierge bot for department selection
18
+ - Added filter to find a request by ticket_id
19
+ - Added filter to snap_lead_lead_id for request
20
+ - Added endpoint to close a request by guest
21
+
22
+
23
+ # 2.2.26 -> PROD (compatible with dasboard ver. 2.2.35)
24
+ - Tag fix for 2.2.25
7
25
 
8
26
  # 2.2.25
9
27
  - New label prechat form
10
28
  - Updated mongodb-runner from 4.8.1 to 4.8.3 to fix ssh key error
11
29
 
12
- # 2.2.24 -> PROD
30
+ # 2.2.24
13
31
  - webhook subscription can fetch temmates endpoint
14
32
  - Added hasBot and createdAt index to the request model
15
33
 
package/README.md CHANGED
@@ -90,3 +90,4 @@ To see how to upgrade tiledesk-server see [here](./docs/upgrading.md)
90
90
  # Testing
91
91
  Run unit test with `npm test` and integration test with `npm run test:int`
92
92
 
93
+
package/app.js CHANGED
@@ -96,6 +96,7 @@ var faqpub = require('./routes/faqpub');
96
96
  var labels = require('./routes/labels');
97
97
  var fetchLabels = require('./middleware/fetchLabels');
98
98
  var cacheUtil = require("./utils/cacheUtil");
99
+ var orgUtil = require("./utils/orgUtil");
99
100
  var images = require('./routes/images');
100
101
  var files = require('./routes/files');
101
102
  var campaigns = require('./routes/campaigns');
@@ -128,6 +129,10 @@ var pubModulesManager = require('./pubmodules/pubModulesManager');
128
129
  var channelManager = require('./channels/channelManager');
129
130
  channelManager.listen();
130
131
 
132
+ const ipfilter = require('express-ipfilter').IpFilter
133
+ // const IpDeniedError = require('express-ipfilter').IpDeniedError;
134
+
135
+
131
136
  var modulesManager = undefined;
132
137
  try {
133
138
  modulesManager = require('./services/modulesManager');
@@ -233,7 +238,10 @@ if (process.env.ROUTELOGGER_ENABLED==="true") {
233
238
 
234
239
  var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
235
240
  winston.debug("fullUrl:"+ fullUrl);
241
+ winston.debug(" req.get('host'):"+ req.get('host'));
242
+
236
243
  winston.debug("req.get('origin'):" + req.get('origin'));
244
+ winston.debug("req.get('referer'):" + req.get('referer'));
237
245
 
238
246
  var routerLogger = new RouterLogger({
239
247
  origin: req.get('origin'),
@@ -291,7 +299,7 @@ var projectSetter = function (req, res, next) {
291
299
  if (err) {
292
300
  winston.warn("Problem getting project with id: " + projectid + " req.originalUrl: " + req.originalUrl);
293
301
  }
294
-
302
+
295
303
  winston.debug("projectSetter project:" + project);
296
304
  if (!project) {
297
305
  winston.warn("ProjectSetter project not found with id: " + projectid);
@@ -311,6 +319,53 @@ var projectSetter = function (req, res, next) {
311
319
  }
312
320
 
313
321
 
322
+ function customDetection (req) {
323
+ const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
324
+ winston.info("standard ip: "+ip); // ip address of the user
325
+ return ip;
326
+ }
327
+
328
+
329
+ var projectIpFilter = function (req, res, next) {
330
+ // var projectIpFilter = function (err, req, res, next) {
331
+
332
+ // var ip = require('ip');
333
+ // winston.info("projectIpFilter ip2: " + ip.address() );
334
+
335
+
336
+ const nextIp = function(err) {
337
+ winston.info("projectIpFilter next",err);
338
+
339
+ if (err && err.name === "IpDeniedError") {
340
+ winston.info("IpDeniedError");
341
+ return res.status(401).json({ err: "error project ip filter" });
342
+ // next(err)
343
+ }
344
+
345
+ next();
346
+
347
+ }
348
+
349
+
350
+ if (!req.project) {
351
+ return next();
352
+ }
353
+
354
+ var projectIpFilterEnabled = req.project.ipFilterEnabled;
355
+ winston.debug("project projectIpFilterEnabled: " +projectIpFilterEnabled)
356
+
357
+ var projectIpFilter = req.project.ipFilter
358
+ winston.debug("project ipFilter: " + projectIpFilter)
359
+
360
+ if (projectIpFilterEnabled === true && projectIpFilter && projectIpFilter.length > 0) {
361
+ var ip = ipfilter(projectIpFilter, { detectIp: customDetection, mode: 'allow' })
362
+ // var ip = ipfilter(projectIpFilter, { mode: 'allow' })
363
+ ip(req, res, nextIp);
364
+ } else {
365
+ next();
366
+ }
367
+
368
+ }
314
369
 
315
370
 
316
371
 
@@ -322,6 +377,7 @@ var projectSetter = function (req, res, next) {
322
377
  // app.post('/oauth/token', oauth2.token);
323
378
 
324
379
 
380
+ // const ips = ['::1'];
325
381
 
326
382
  app.use('/auth', auth);
327
383
  app.use('/testauth', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken], authtest);
@@ -355,7 +411,7 @@ if (modulesManager) {
355
411
  }
356
412
 
357
413
 
358
- app.use('/:projectid/', [projectIdSetter, projectSetter]);
414
+ app.use('/:projectid/', [projectIdSetter, projectSetter, projectIpFilter]);
359
415
 
360
416
 
361
417
  app.use('/:projectid/authtestWithRoleCheck', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken], authtestWithRoleCheck);
@@ -447,8 +503,22 @@ app.use(function (err, req, res, next) {
447
503
  });*/
448
504
 
449
505
 
506
+
507
+
508
+
509
+
510
+
511
+
512
+
450
513
  // error handler
451
514
  app.use((err, req, res, next) => {
515
+
516
+ winston.info("err.name", err.name)
517
+ if (err.name === "IpDeniedError") {
518
+ winston.info("IpDeniedError");
519
+ return res.status(401).json({ err: "error ip filter" });
520
+ }
521
+
452
522
  winston.error("General error", err);
453
523
  return res.status(500).json({ err: "error" });
454
524
  });
package/bin/www CHANGED
@@ -32,7 +32,10 @@ webSocketServer.init(httpServer);
32
32
  * Listen on provided port, on all network interfaces.
33
33
  */
34
34
 
35
- var listener = httpServer.listen(port,function(){
35
+ // use ipv4 or ipv6 https://stackoverflow.com/questions/50855419/get-only-ipv4-ips-via-nodejs-express
36
+ // var listener = httpServer.listen(port,'0.0.0.0', function(){
37
+
38
+ var listener = httpServer.listen(port, function(){
36
39
  console.log('Listening tiledesk-server ver:'+version+' on port ' + listener.address().port); //Listening on port 8888
37
40
  });
38
41
 
package/config/global.js CHANGED
@@ -1,3 +1,5 @@
1
1
  module.exports = {
2
2
  'apiUrl':'http://localhost:3000',
3
+ 'organizationBaseUrl' : 'org.local',
4
+ 'organizationEnabled' : false
3
5
  };
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,
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.26",
4
+ "version": "2.2.30",
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",
@@ -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)
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.debug("orgUtil.ORGANIZATION_ENABLED: " + orgUtil.ORGANIZATION_ENABLED);
386
+ if (orgUtil.ORGANIZATION_ENABLED == true ) {
387
+
388
+ // winston.info("project_users", project_users);
389
+ winston.debug("project_users.length:"+ project_users.length);
390
+
391
+ let org = orgUtil.getOrg(req);
392
+ winston.debug("org:"+ org);
393
+
394
+ if (org!=undefined) {
395
+ winston.debug("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.debug("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.debug("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);
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;
@@ -0,0 +1,74 @@
1
+
2
+
3
+ 'use strict';
4
+ var global = require('../config/global');
5
+ var email = require('../config/email');
6
+ var winston = require('../config/winston');
7
+
8
+
9
+
10
+
11
+ class OrgUtil {
12
+
13
+ constructor() {
14
+ this.ORGANIZATION_ENABLED = process.env.ORGANIZATION_ENABLED || global.organizationEnabled;
15
+ if (this.ORGANIZATION_ENABLED===true || this.ORGANIZATION_ENABLED === "true") {
16
+ this.ORGANIZATION_ENABLED = true;
17
+ }else {
18
+ this.ORGANIZATION_ENABLED = false;
19
+ }
20
+
21
+ winston.info("Organization enabled: "+ this.ORGANIZATION_ENABLED );
22
+
23
+ this.ORGANIZATION_BASE_URL = process.env.ORGANIZATION_BASE_URL || global.organizationBaseUrl;
24
+ winston.info("Organization base url: "+ this.ORGANIZATION_BASE_URL );
25
+
26
+ }
27
+
28
+ getOrg(req) {
29
+
30
+ // if (this.ORGANIZATION_ENABLED===false) {
31
+ // return undefined;
32
+ // }
33
+
34
+
35
+ // let host = req.get('host');
36
+ // winston.info("host: "+ host );
37
+
38
+ let origin = req.get('origin');
39
+ winston.info("origin: "+ origin );
40
+
41
+ // winston.info("email: " + email.baseUrl);
42
+ winston.info("this.ORGANIZATION_BASE_URL: " + this.ORGANIZATION_BASE_URL);
43
+ // global.organizationBaseUrl
44
+ // if (host !=email.baseUrl ) {
45
+ if (origin && origin.indexOf(this.ORGANIZATION_BASE_URL)>-1) {
46
+ // if (origin!=this.ORGANIZATION_BASE_URL) {
47
+ // winston.info("host found: "+ host );
48
+ // return host;
49
+ winston.info("origin found: "+ origin );
50
+ return origin;
51
+ }
52
+ winston.info("origin not found: "+ origin );
53
+
54
+ // winston.info("host not found: "+ host );
55
+ // if (host.indexOf("localhost1")>-1) {
56
+ // console.log("host found: "+ host );
57
+ // return "localhost";
58
+ // }
59
+ return undefined;
60
+
61
+ }
62
+
63
+
64
+
65
+
66
+
67
+
68
+ }
69
+
70
+
71
+ var orgUtil = new OrgUtil();
72
+
73
+
74
+ module.exports = orgUtil;