@tiledesk/tiledesk-server 2.1.41 → 2.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. package/.circleci/config.yml +54 -0
  2. package/.env.sample +1 -1
  3. package/.github/workflows/docker-community-push-latest.yml +22 -0
  4. package/.github/workflows/{docker-image-push.yml → docker-image-en-tag-push.yml} +1 -1
  5. package/.github/workflows/docker-image-tag-community-tag-push.yml +21 -0
  6. package/.github/workflows/{docker-push-latest.yml → docker-push-en-push-latest.yml} +1 -1
  7. package/CHANGELOG.md +198 -1
  8. package/Dockerfile +1 -1
  9. package/Dockerfile-en +1 -1
  10. package/README.md +5 -7
  11. package/app.js +12 -1
  12. package/channels/chat21/chat21Contact.js +34 -8
  13. package/channels/chat21/chat21Handler.js +48 -5
  14. package/channels/chat21/chat21WebHook.js +34 -5
  15. package/channels/chat21/nativeauth.js +2 -2
  16. package/config/email.js +2 -1
  17. package/config/global.js +3 -0
  18. package/config/labels/widget.json +170 -16
  19. package/event/messageEvent.js +18 -1
  20. package/middleware/passport.js +10 -4
  21. package/models/actionsConstants.js +7 -0
  22. package/models/department.js +3 -0
  23. package/models/faq.js +8 -2
  24. package/models/faq_kb.js +6 -0
  25. package/models/message.js +10 -4
  26. package/models/messageConstants.js +3 -3
  27. package/models/request.js +33 -3
  28. package/package.json +31 -28
  29. package/pubmodules/emailNotification/requestNotification.js +380 -62
  30. package/pubmodules/messageActions/messageActionsInterceptor.js +20 -7
  31. package/pubmodules/messageTransformer/index.js +1 -1
  32. package/pubmodules/messageTransformer/microLanguageAttributesTransformerInterceptor.js +67 -0
  33. package/pubmodules/pubModulesManager.js +66 -14
  34. package/pubmodules/rules/conciergeBot.js +81 -49
  35. package/routes/auth.js +34 -10
  36. package/routes/campaigns.js +117 -25
  37. package/routes/faq.js +19 -0
  38. package/routes/faq_kb.js +13 -4
  39. package/routes/faqpub.js +1 -1
  40. package/routes/images.js +1 -1
  41. package/routes/jwt.js +0 -1
  42. package/routes/logs.js +26 -0
  43. package/routes/message.js +7 -2
  44. package/routes/messagesRoot.js +73 -16
  45. package/routes/project_user.js +36 -1
  46. package/routes/request.js +88 -12
  47. package/routes/requestUtilRoot.js +30 -0
  48. package/routes/urls.js +12 -0
  49. package/routes/users.js +5 -1
  50. package/services/BotSubscriptionNotifier.js +1 -0
  51. package/services/departmentService.js +29 -5
  52. package/services/emailService.js +1103 -298
  53. package/services/faqBotHandler.js +176 -61
  54. package/services/faqBotSupport.js +181 -117
  55. package/services/faqService.js +17 -14
  56. package/services/messageService.js +57 -9
  57. package/services/modulesManager.js +86 -23
  58. package/services/requestService.js +58 -17
  59. package/template/email/assignedEmailMessage.html +205 -0
  60. package/template/email/assignedRequest.html +44 -14
  61. package/template/email/beenInvitedExistingUser.html +2 -2
  62. package/template/email/beenInvitedNewUser.html +1 -1
  63. package/template/email/newMessage.html +31 -12
  64. package/template/email/passwordChanged.html +2 -3
  65. package/template/email/pooledEmailMessage.html +208 -0
  66. package/template/email/pooledRequest.html +41 -14
  67. package/template/email/resetPassword.html +2 -3
  68. package/template/email/sendTranscript.html +1 -1
  69. package/template/email/test.html +1 -1
  70. package/template/email/ticket.html +78 -52
  71. package/template/email/ticket.txt +5 -1
  72. package/template/email/verify.html +1 -1
  73. package/test/authentication.js +76 -4
  74. package/test/authenticationJwt.js +76 -2
  75. package/test/campaignsRoute.js +226 -0
  76. package/test/faqService.js +3 -3
  77. package/test/faqkbRoute.js +3 -2
  78. package/test/messageRootRoute.js +193 -0
  79. package/test/messageRoute.js +75 -0
  80. package/test/messageService.js +5 -5
  81. package/test/requestRoute.js +27 -9
  82. package/test/requestService.js +472 -11
  83. package/test-int/bot.js +673 -8
  84. package/websocket/webSocketServer.js +7 -4
  85. package/template/email/ticket-taking.txt +0 -7
@@ -20,9 +20,14 @@ var mongoose = require('mongoose');
20
20
  var jwt = require('jsonwebtoken');
21
21
  const uuidv4 = require('uuid/v4');
22
22
  var config = require('../../config/database');
23
+ var configGlobal = require('../../config/global');
23
24
 
24
25
  var widgetConfig = require('../../config/widget');
25
26
  var widgetTestLocation = process.env.WIDGET_TEST_LOCATION || widgetConfig.testLocation;
27
+ let configSecret = process.env.GLOBAL_SECRET || config.secret;
28
+
29
+ let apiUrl = process.env.API_URL || configGlobal.apiUrl;
30
+ winston.debug('********* RequestNotification apiUrl: ' + apiUrl);
26
31
 
27
32
  class RequestNotification {
28
33
 
@@ -31,7 +36,15 @@ listen() {
31
36
  var that = this;
32
37
 
33
38
 
34
- messageEvent.on("message.create", function(message) {
39
+
40
+ var messageCreateKey = 'message.create';
41
+ if (messageEvent.queueEnabled) {
42
+ messageCreateKey = 'message.create.queue';
43
+ }
44
+ winston.debug('RequestNotification messageCreateKey: ' + messageCreateKey);
45
+
46
+
47
+ messageEvent.on(messageCreateKey, function(message) {
35
48
 
36
49
  setImmediate(() => {
37
50
  winston.debug("sendUserEmail", message);
@@ -39,21 +52,38 @@ listen() {
39
52
  if (message.attributes && message.attributes.subtype==='info') {
40
53
  return winston.debug("not sending sendUserEmail for attributes.subtype info messages");
41
54
  }
55
+
56
+
42
57
  if (message.request && (message.request.channel.name===ChannelConstants.EMAIL || message.request.channel.name===ChannelConstants.FORM)) {
43
58
 
44
59
  if (message.sender != message.request.lead.lead_id) {
45
- winston.verbose("sending sendEmailChannelEmail for EMAIL or FORM channel");
46
- return that.sendEmailChannelEmail(message.id_project, message);
60
+ winston.verbose("sending sendToUserEmailChannelEmail for EMAIL or FORM channel");
61
+ return that.sendToUserEmailChannelEmail(message.id_project, message);
47
62
  } else {
48
- winston.verbose("Not sending sendEmailChannelEmail for agent messages");
63
+
64
+ if (message.text != message.request.first_text) {
65
+ winston.verbose("sending sendToAgentEmailChannelEmail for EMAIL or FORM channel");
66
+ return that.sendToAgentEmailChannelEmail(message.id_project, message);
67
+ } else {
68
+ winston.debug("sending sendToAgentEmailChannelEmail for EMAIL or FORM channel disabled for first text message")
69
+ }
70
+
49
71
  }
50
72
 
51
73
  } else {
52
- if (process.env.DISABLE_SEND_OFFLINE_EMAIL==="true" ||process.env.DISABLE_SEND_OFFLINE_EMAIL===true ) {
74
+ winston.debug("sendUserEmail chat channel");
75
+ // controlla se sta funzionando
76
+ if (process.env.DISABLE_SEND_OFFLINE_EMAIL === "true" || process.env.DISABLE_SEND_OFFLINE_EMAIL === true ) {
53
77
  return winston.debug("DISABLE_SEND_OFFLINE_EMAIL disabled");
54
78
  }
55
79
  // mandare email se ultimo messaggio > X MINUTI configurato in Notification . potresti usare request.updated_at ?
56
- return that.sendUserEmail(message.id_project, message);
80
+ if (message.request && message.request.lead && message.sender != message.request.lead.lead_id) {
81
+ winston.debug("sendUserEmail", message);
82
+
83
+ // send an email only if offline and has an email
84
+ return that.sendUserEmail(message.id_project, message);
85
+ }
86
+
57
87
  }
58
88
 
59
89
 
@@ -61,14 +91,22 @@ listen() {
61
91
  });
62
92
  });
63
93
 
64
- requestEvent.on("request.create", function(request) {
94
+ var requestCreateKey = 'request.create';
95
+ if (requestEvent.queueEnabled) {
96
+ requestCreateKey = 'request.create.queue';
97
+ }
98
+ winston.debug('RequestNotification requestCreateKey: ' + requestCreateKey);
65
99
 
100
+ requestEvent.on(requestCreateKey, function(request) {
101
+ // winston.info('quiiiiiiiiiiiii');
66
102
  setImmediate(() => {
67
103
 
104
+ /*
68
105
  if (request && (request.channel.name===ChannelConstants.EMAIL || request.channel.name===ChannelConstants.FORM )) {
69
106
  winston.verbose("sending sendEmailChannelTakingNotification for EMAIL or FORM channel");
70
107
  that.sendEmailChannelTakingNotification(request.id_project, request)
71
108
  }
109
+ */
72
110
 
73
111
  that.sendAgentEmail(request.id_project, request);
74
112
 
@@ -76,7 +114,14 @@ listen() {
76
114
  });
77
115
 
78
116
 
79
- requestEvent.on("request.participants.update", function(data) {
117
+ var requestParticipantsUpdateKey = 'request.participants.update';
118
+ // this is not queued
119
+ // if (requestEvent.queueEnabled) {
120
+ // requestParticipantsUpdateKey = 'request.participants.update.queue';
121
+ // }
122
+ winston.debug('RequestNotification requestParticipantsUpdateKey: ' + requestParticipantsUpdateKey);
123
+
124
+ requestEvent.on(requestParticipantsUpdateKey, function(data) {
80
125
 
81
126
  winston.debug("requestEvent request.participants.update");
82
127
 
@@ -103,7 +148,13 @@ listen() {
103
148
 
104
149
  // TODO Send email also for addAgent and reassign. Alessio request for pooled only?
105
150
 
106
- requestEvent.on("request.close.extended", function(data) {
151
+ var requestCloseExtendedKey = 'request.close.extended';
152
+ // this is not queued
153
+ // if (requestEvent.queueEnabled) {
154
+ // requestCloseExtendedKey = 'request.close.extended.queue';
155
+ // }
156
+ winston.debug('RequestNotification requestCloseExtendedKey: ' + requestCloseExtendedKey);
157
+ requestEvent.on(requestCloseExtendedKey, function(data) {
107
158
  setImmediate(() => {
108
159
  var request = data.request;
109
160
  var notify = data.notify;
@@ -136,7 +187,7 @@ listen() {
136
187
  if (project_users && project_users.length>0) {
137
188
  project_users.forEach(project_user => {
138
189
  if (project_user.id_user && project_user.id_user.email) {
139
- return that.sendTranscriptByEmail(project_user.id_user.email, request_id, id_project);
190
+ return that.sendTranscriptByEmail(project_user.id_user.email, request_id, id_project, project);
140
191
  } else {
141
192
  }
142
193
  });
@@ -149,7 +200,7 @@ listen() {
149
200
  return Lead.findById(request.requester_id, function(err, lead){
150
201
  //if (lead && lead.email) {
151
202
  if (lead && lead.email) {
152
- return that.sendTranscriptByEmail(lead.email, request_id, id_project);
203
+ return that.sendTranscriptByEmail(lead.email, request_id, id_project, project);
153
204
  }
154
205
 
155
206
  });
@@ -168,7 +219,7 @@ listen() {
168
219
  }
169
220
 
170
221
 
171
- sendEmailChannelEmail(projectid, message) {
222
+ sendToUserEmailChannelEmail(projectid, message) {
172
223
  try {
173
224
 
174
225
  if (!message.request) {
@@ -179,14 +230,13 @@ sendEmailChannelEmail(projectid, message) {
179
230
  return winston.debug("The lead object is undefined or has empty email");
180
231
  }
181
232
 
182
- Project.findOne({_id: projectid, status: 100}, function(err, project){
233
+ Project.findOne({_id: projectid, status: 100}).select("+settings").exec(function(err, project){
183
234
  if (err) {
184
235
  return winston.error(err);
185
236
  }
186
237
 
187
238
  if (!project) {
188
- // console.warn("Project not found", req.projectid);
189
- return console.warn("Project not found", projectid);
239
+ return winston.warn("Project not found", projectid);
190
240
  }
191
241
 
192
242
  // if (project.settings && project.settings.email && project.settings.email.notification && project.settings.email.notification.conversation && project.settings.email.notification.conversation.offline && project.settings.email.notification.conversation.offline.blocked == true ) {
@@ -197,8 +247,61 @@ sendEmailChannelEmail(projectid, message) {
197
247
  // if (project.settings && project.settings.email && project.settings.email.notification && project.settings.email.notification.conversation && project.settings.email.notification.conversation.offline && project.settings.email.notification.conversation.offline.enabled == false ) {
198
248
  // return winston.info("RequestNotification offline email notification for the project with id : " + projectid + " for the offline conversation is disabled");
199
249
  // }
200
- emailService.sendEmailChannelNotification(message.request.lead.email, message, project);
201
250
 
251
+ let lead = message.request.lead;
252
+ winston.debug("sending channel emaol email to lead ", lead);
253
+
254
+
255
+ winston.debug("sending user email to "+ lead.email);
256
+
257
+ var signOptions = {
258
+ issuer: 'https://tiledesk.com',
259
+ subject: 'userexternal',
260
+ audience: 'https://tiledesk.com',
261
+ jwtid: uuidv4()
262
+ };
263
+
264
+
265
+ var recipient = lead.lead_id;
266
+ winston.debug("recipient:"+ recipient);
267
+
268
+ let userEmail = {_id: recipient, firstname: lead.fullname, lastname: lead.fullname, email: lead.email, attributes: lead.attributes};
269
+ winston.debug("userEmail ",userEmail);
270
+
271
+
272
+ var token = jwt.sign(userEmail, configSecret, signOptions);
273
+ winston.debug("token "+token);
274
+
275
+ var sourcePage = widgetTestLocation + "?tiledesk_projectid="
276
+ + projectid + "&project_name="+encodeURIComponent(project.name)
277
+
278
+ if (message.request.sourcePage) {
279
+ sourcePage = message.request.sourcePage;
280
+ }
281
+
282
+ if (sourcePage.indexOf("?")===-1) {
283
+ sourcePage = sourcePage + "?";
284
+ }
285
+
286
+ sourcePage = sourcePage
287
+ + "&tiledesk_recipientId="+message.request.request_id
288
+ + "&tiledesk_isOpen=true";
289
+
290
+
291
+ sourcePage = apiUrl + "/urls/redirect?path=" + encodeURIComponent(sourcePage)
292
+ winston.debug("sourcePage "+sourcePage);
293
+
294
+
295
+ var tokenQueryString;
296
+ if(sourcePage && sourcePage.indexOf('?')>-1) { //controllo superfluo visto che lo metto prima? ma lascio comunque per indipendenza
297
+ tokenQueryString = encodeURIComponent("&tiledesk_jwt=JWT "+token)
298
+ }else {
299
+ tokenQueryString = encodeURIComponent("?tiledesk_jwt=JWT "+token);
300
+ }
301
+ winston.debug("tokenQueryString: "+tokenQueryString);
302
+
303
+ emailService.sendEmailChannelNotification(message.request.lead.email, message, project, tokenQueryString, sourcePage);
304
+
202
305
 
203
306
  });
204
307
 
@@ -210,7 +313,166 @@ sendEmailChannelEmail(projectid, message) {
210
313
 
211
314
 
212
315
 
316
+ sendToAgentEmailChannelEmail(projectid, message) {
317
+ let savedRequest = message.request;
318
+ // send email
319
+ try {
320
+
321
+
322
+ Project.findOne({_id: projectid, status: 100}).select("+settings").exec(async function(err, project){
323
+ if (err) {
324
+ return winston.error(err);
325
+ }
326
+
327
+ if (!project) {
328
+ return winston.warn("Project not found", projectid);
329
+ } else {
330
+
331
+ winston.debug("project", project);
332
+
333
+ if (project.settings && project.settings.email && project.settings.email.notification && project.settings.email.notification.conversation && project.settings.email.notification.conversation.blocked == true ) {
334
+ return winston.verbose("RequestNotification email notification for the project with id : " + projectid + " for all the conversations is blocked");
335
+ }
336
+
337
+ winston.debug("savedRequest", savedRequest);
338
+
339
+ // TODO fare il controllo anche sul dipartimento con modalità assigned o pooled
340
+ if (savedRequest.status==RequestConstants.UNASSIGNED) { //POOLED
341
+
342
+ if (project.settings && project.settings.email && project.settings.email.notification && project.settings.email.notification.conversation && project.settings.email.notification.conversation.ticket && project.settings.email.notification.conversation.ticket.pooled == false ) {
343
+ return winston.info("RequestNotification email notification for the project with id : " + projectid + " for the pooled conversation ticket is disabled");
344
+ }
345
+
346
+ if (!savedRequest.snapshot) {
347
+ return winston.warn("RequestNotification savedRequest.snapshot is null :(. You are closing an old request?");
348
+ }
349
+
350
+
351
+
352
+
353
+ var snapshotAgents = await Request.findById(savedRequest.id).select({"snapshot":1}).exec();
354
+
355
+ winston.debug('snapshotAgents',snapshotAgents);
356
+
357
+
358
+ // winston.info("savedRequest.snapshot.agents", savedRequest.snapshot.agents);
359
+ // agents è selected false quindi nn va sicuro
360
+ if (!snapshotAgents.snapshot.agents) {
361
+ return winston.warn("RequestNotification snapshotAgents.snapshot.agents is null :(. You are closing an old request?", savedRequest);
362
+ }
363
+
364
+ // var allAgents = savedRequest.agents;
365
+ var allAgents = snapshotAgents.snapshot.agents;
366
+ // winston.debug("allAgents", allAgents);
367
+
368
+ allAgents.forEach(project_user => {
369
+ // winston.debug("project_user", project_user); //DON'T UNCOMMENT THIS. OTHERWISE this.agents.filter of models/request.js:availableAgentsCount has .filter not found.
370
+
371
+
372
+ var userid = project_user.id_user;
373
+
374
+ if (project_user.settings && project_user.settings.email && project_user.settings.email.notification && project_user.settings.email.notification.conversation && project_user.settings.email.notification.conversation.ticket && project_user.settings.email.notification.conversation.ticket.pooled == false ) {
375
+ return winston.verbose("RequestNotification email notification for the user with id " + userid+ " the pooled conversation ticket is disabled");
376
+ }
377
+
378
+ User.findOne({_id: userid , status: 100})
379
+ .cache(cacheUtil.defaultTTL, "users:id:"+userid)
380
+ .exec(function (err, user) {
381
+ if (err) {
382
+ // winston.debug(err);
383
+ }
384
+ if (!user) {
385
+ winston.warn("User not found", userid);
386
+ } else {
387
+ winston.verbose("Sending sendNewPooledMessageNotification to user with email: "+ user.email);
388
+ if (user.emailverified) {
389
+ emailService.sendNewPooledMessageEmailNotification(user.email, savedRequest, project, message);
390
+ }else {
391
+ winston.verbose("User email not verified", user.email);
392
+ }
393
+ }
394
+ });
395
+
396
+
397
+ });
398
+
399
+ }
400
+
401
+ // TODO fare il controllo anche sul dipartimento con modalità assigned o pooled
402
+ else if (savedRequest.status==RequestConstants.ASSIGNED) { //ASSIGNED
403
+
404
+ if (project.settings && project.settings.email && project.settings.email.notification && project.settings.email.notification.conversation && project.settings.email.notification.conversation.ticket && project.settings.email.notification.conversation.ticket.assigned == false ) {
405
+ return winston.verbose("RequestNotification email notification for the project with id : " + projectid + " for the assigned conversation ticket is disabled");
406
+ }
407
+
408
+
409
+ var assignedId = savedRequest.participants[0];
410
+
411
+ // winston.info("assignedId1:"+ assignedId);
412
+
413
+ // if (!assignedId) {
414
+ // console.log("attention90", savedRequest);
415
+ // }
416
+
417
+
418
+
419
+ Project_user.findOne( { id_user:assignedId, id_project: projectid, status: "active"}) //attento in 2.1.14.2
420
+ .exec(function (err, project_user) {
421
+
422
+ winston.debug("project_user notification", project_user);
423
+ if (project_user && project_user.settings && project_user.settings.email && project_user.settings.email.notification && project_user.settings.email.notification.conversation && project_user.settings.email.notification.conversation.ticket && project_user.settings.email.notification.conversation.ticket.assigned && project_user.settings.email.notification.conversation.ticket.assigned.toyou == false ) {
424
+ return winston.info("RequestNotification email notification for the user with id : " + assignedId + " for the pooled conversation ticket is disabled");
425
+ }
426
+
427
+ // botprefix
428
+ if (assignedId.startsWith("bot_")) {
429
+ return ;
430
+ }
431
+
432
+ User.findOne({_id: assignedId, status: 100})
433
+ .cache(cacheUtil.defaultTTL, "users:id:"+assignedId)
434
+ .exec(function (err, user) {
435
+ if (err) {
436
+ winston.error("Error sending email to " + savedRequest.participants[0], err);
437
+ }
438
+ if (!user) {
439
+ winston.warn("User not found", savedRequest.participants[0]);
440
+ } else {
441
+ winston.verbose("Sending sendNewAssignedAgentMessageEmailNotification to user with email: "+ user.email);
442
+ // if (user.emailverified) { enable it? send anyway to improve engagment for new account
443
+ // attento cambia
444
+ emailService.sendNewAssignedAgentMessageEmailNotification(user.email, savedRequest, project, message);
445
+ // }
446
+ }
447
+ });
448
+
449
+ });
450
+
451
+ }
452
+
453
+
454
+
455
+ else {
456
+ return winston.debug("Other states");
457
+ }
458
+
459
+
460
+
461
+ }
462
+
463
+ });
464
+
465
+ } catch (e) {
466
+ winston.warn("Error sending email", {error:e, projectid:projectid, message: message, savedRequest:savedRequest}); //it's better to view error email at this stage
467
+ }
468
+ //end send email
469
+
470
+ }
471
+
472
+
473
+
213
474
 
475
+ //unused
214
476
  sendEmailChannelTakingNotification(projectid, request) {
215
477
  try {
216
478
 
@@ -219,14 +481,13 @@ sendEmailChannelTakingNotification(projectid, request) {
219
481
  return winston.debug("The lead object is undefined or has empty email");
220
482
  }
221
483
 
222
- Project.findOne({_id: projectid, status: 100}, function(err, project){
484
+ Project.findOne({_id: projectid, status: 100}).select("+settings").exec(function(err, project){
223
485
  if (err) {
224
486
  return winston.error(err);
225
487
  }
226
488
 
227
489
  if (!project) {
228
- // console.warn("Project not found", req.projectid);
229
- return console.warn("Project not found", projectid);
490
+ return winston.warn("Project not found", projectid);
230
491
  }
231
492
 
232
493
  // if (project.settings && project.settings.email && project.settings.email.notification && project.settings.email.notification.conversation && project.settings.email.notification.conversation.offline && project.settings.email.notification.conversation.offline.blocked == true ) {
@@ -237,8 +498,8 @@ sendEmailChannelTakingNotification(projectid, request) {
237
498
  // if (project.settings && project.settings.email && project.settings.email.notification && project.settings.email.notification.conversation && project.settings.email.notification.conversation.offline && project.settings.email.notification.conversation.offline.enabled == false ) {
238
499
  // return winston.info("RequestNotification offline email notification for the project with id : " + projectid + " for the offline conversation is disabled");
239
500
  // }
240
- emailService.sendEmailChannelTakingNotification(request.lead.email, request, project);
241
-
501
+ emailService.sendEmailChannelTakingNotification(request.lead.email, request, project);
502
+
242
503
 
243
504
  });
244
505
 
@@ -262,14 +523,13 @@ sendUserEmail(projectid, message) {
262
523
  return winston.debug("The lead object is undefined or has empty email");
263
524
  }
264
525
 
265
- Project.findOne({_id: projectid, status: 100}, function(err, project){
526
+ Project.findOne({_id: projectid, status: 100}).select("+settings").exec(function(err, project){
266
527
  if (err) {
267
528
  return winston.error(err);
268
529
  }
269
530
 
270
531
  if (!project) {
271
- // console.warn("Project not found", req.projectid);
272
- return console.warn("Project not found", projectid);
532
+ return winston.warn("Project not found", projectid);
273
533
  }
274
534
 
275
535
  if (project.settings && project.settings.email && project.settings.email.notification && project.settings.email.notification.conversation && project.settings.email.notification.conversation.offline && project.settings.email.notification.conversation.offline.blocked == true ) {
@@ -313,9 +573,9 @@ sendUserEmail(projectid, message) {
313
573
 
314
574
  //send email to lead
315
575
  return Lead.findOne({lead_id: recipient}, function(err, lead){
316
- winston.debug("lead", lead);
576
+ winston.debug("lead", lead); //TODO lead is already present in request.lead
317
577
  if (lead && lead.email) {
318
- winston.info("sending user email to "+ lead.email);
578
+ winston.debug("sending user email to "+ lead.email);
319
579
 
320
580
  var signOptions = {
321
581
  issuer: 'https://tiledesk.com',
@@ -325,29 +585,40 @@ sendUserEmail(projectid, message) {
325
585
  };
326
586
 
327
587
  let userAnonym = {_id: recipient, firstname: lead.fullname, lastname: lead.fullname, email: lead.email, attributes: lead.attributes};
328
- winston.info("userAnonym ",userAnonym);
588
+ winston.debug("userAnonym ",userAnonym);
329
589
 
330
590
 
331
- var token = jwt.sign(userAnonym, config.secret, signOptions);
332
- winston.info("token "+token);
333
-
334
- var sourcePage = widgetTestLocation;
591
+ var token = jwt.sign(userAnonym, configSecret, signOptions);
592
+ winston.debug("token "+token);
335
593
 
594
+ var sourcePage = widgetTestLocation + "?tiledesk_projectid="
595
+ + projectid + "&project_name="+encodeURIComponent(project.name)
336
596
 
337
597
  if (message.request.sourcePage) {
338
- sourcePage = message.request.sourcePage;
598
+ sourcePage = message.request.sourcePage;
339
599
  }
340
600
 
341
- winston.info("sourcePage "+sourcePage);
601
+ if (sourcePage && sourcePage.indexOf("?")===-1) {
602
+ sourcePage = sourcePage + "?";
603
+ }
604
+
605
+ sourcePage = sourcePage
606
+ + "&tiledesk_recipientId="+message.request.request_id
607
+ + "&tiledesk_isOpen=true";
608
+
609
+ sourcePage = apiUrl + "/urls/redirect?path=" + encodeURIComponent(sourcePage)
610
+
611
+ winston.debug("sourcePage "+sourcePage);
342
612
 
343
613
  var tokenQueryString;
344
- if(sourcePage && sourcePage.indexOf('?')>-1) {
345
- tokenQueryString = "&tiledesk_customToken=JWT "+token
614
+ if(sourcePage && sourcePage.indexOf('?')>-1) { //controllo superfluo visto che lo metto prima? ma lascio comunque per indipendenza
615
+ tokenQueryString = encodeURIComponent("&tiledesk_jwt=JWT "+token)
346
616
  }else {
347
- tokenQueryString = "?tiledesk_customToken=JWT "+token
617
+ tokenQueryString = encodeURIComponent("?tiledesk_jwt=JWT "+token);
348
618
  }
349
-
350
- emailService.sendNewMessageNotification(lead.email, message, project, tokenQueryString);
619
+ winston.debug("tokenQueryString: "+tokenQueryString);
620
+
621
+ emailService.sendNewMessageNotification(lead.email, message, project, tokenQueryString, sourcePage);
351
622
  }
352
623
 
353
624
  });
@@ -365,19 +636,17 @@ sendUserEmail(projectid, message) {
365
636
  }
366
637
 
367
638
  sendAgentEmail(projectid, savedRequest) {
368
- // console.log("savedRequest23", savedRequest);
369
639
  // send email
370
640
  try {
371
641
 
372
642
 
373
- Project.findOne({_id: projectid, status: 100}, function(err, project){
643
+ Project.findOne({_id: projectid, status: 100}).select("+settings").exec( async function(err, project){
374
644
  if (err) {
375
645
  return winston.error(err);
376
646
  }
377
647
 
378
648
  if (!project) {
379
- // console.warn("Project not found", req.projectid);
380
- return console.warn("Project not found", projectid);
649
+ return winston.warn("Project not found", projectid);
381
650
  } else {
382
651
 
383
652
  winston.debug("project", project);
@@ -386,7 +655,7 @@ sendAgentEmail(projectid, savedRequest) {
386
655
  return winston.verbose("RequestNotification email notification for the project with id : " + projectid + " for all the conversations is blocked");
387
656
  }
388
657
 
389
- winston.debug("savedRequest", savedRequest);
658
+ winston.debug("savedRequest: " + JSON.stringify(savedRequest));
390
659
 
391
660
  // TODO fare il controllo anche sul dipartimento con modalità assigned o pooled
392
661
  if (savedRequest.status==RequestConstants.UNASSIGNED) { //POOLED
@@ -394,18 +663,40 @@ sendAgentEmail(projectid, savedRequest) {
394
663
  if (project.settings && project.settings.email && project.settings.email.notification && project.settings.email.notification.conversation && project.settings.email.notification.conversation.pooled == false ) {
395
664
  return winston.info("RequestNotification email notification for the project with id : " + projectid + " for the pooled conversation is disabled");
396
665
  }
397
-
398
666
  if (!savedRequest.snapshot) {
399
667
  return winston.warn("RequestNotification savedRequest.snapshot is null :(. You are closing an old request?");
400
668
  }
401
- if (!savedRequest.snapshot.agents) {
402
- return winston.warn("RequestNotification savedRequest.snapshot.agents is null :(. You are closing an old request?", savedRequest);
669
+
670
+
671
+
672
+ var snapshotAgents = savedRequest; //riassegno varibile cosi nn cambio righe successive
673
+
674
+
675
+
676
+
677
+ // winston.info("savedRequest.snapshot.agents", savedRequest.snapshot.agents);
678
+ // agents è selected false quindi nn va sicuro
679
+ if (!snapshotAgents.snapshot.agents) {
680
+ //return winston.warn("RequestNotification snapshotAgents.snapshot.agents is null :(. You are closing an old request?", savedRequest);
681
+
682
+ // agents già c'è in quanto viene creato con departmentService.getOperator nella request.create ma nn c'è per request.participants.update
683
+ snapshotAgents = await Request.findById(savedRequest.id).select({"snapshot":1}).exec();
684
+ winston.debug('load snapshotAgents with Request.findById ');
685
+ }
686
+ winston.debug('snapshotAgents', snapshotAgents);
687
+
688
+ if (!snapshotAgents.snapshot.agents) {
689
+ return winston.warn("RequestNotification snapshotAgents.snapshot.agents is null :(. You are closing an old request?", savedRequest);
403
690
  }
691
+
404
692
  // var allAgents = savedRequest.agents;
405
- var allAgents = savedRequest.snapshot.agents;
406
- // winston.debug("allAgents", allAgents);
693
+ var allAgents = snapshotAgents.snapshot.agents;
694
+
695
+ // // var allAgents = savedRequest.agents;
696
+ // var allAgents = savedRequest.snapshot.agents;
697
+ // // winston.debug("allAgents", allAgents);
407
698
 
408
- allAgents.forEach(project_user => {
699
+ allAgents.forEach(project_user => {
409
700
  // winston.debug("project_user", project_user); //DON'T UNCOMMENT THIS. OTHERWISE this.agents.filter of models/request.js:availableAgentsCount has .filter not found.
410
701
 
411
702
 
@@ -424,7 +715,7 @@ sendAgentEmail(projectid, savedRequest) {
424
715
  if (!user) {
425
716
  winston.warn("User not found", userid);
426
717
  } else {
427
- winston.debug("Sending sendNewPooledRequestNotification to user with email: "+ user.email);
718
+ winston.verbose("Sending sendNewPooledRequestNotification to user with email: "+ user.email);
428
719
  if (user.emailverified) {
429
720
  emailService.sendNewPooledRequestNotification(user.email, savedRequest, project);
430
721
  }else {
@@ -456,19 +747,27 @@ sendAgentEmail(projectid, savedRequest) {
456
747
 
457
748
 
458
749
 
459
- Project_user.findOne( { id_user:assignedId, id_project: projectid, status: "active"}) //attento in 2.1.14.2
750
+ Project_user.findOne( { id_user:assignedId, id_project: projectid, status: "active"})
460
751
  .exec(function (err, project_user) {
461
752
 
753
+ // botprefix
754
+ if (assignedId.startsWith("bot_")) {
755
+ return ;
756
+ }
757
+
758
+ if (err) {
759
+ return winston.error("RequestNotification email notification error getting project_user", err);
760
+ }
462
761
  winston.debug("project_user notification", project_user);
463
762
  if (project_user && project_user.settings && project_user.settings.email && project_user.settings.email.notification && project_user.settings.email.notification.conversation && project_user.settings.email.notification.conversation.assigned && project_user.settings.email.notification.conversation.assigned.toyou == false ) {
464
763
  return winston.info("RequestNotification email notification for the user with id : " + assignedId + " for the pooled conversation is disabled");
465
764
  }
466
765
 
467
- // botprefix
468
- if (assignedId.startsWith("bot_")) {
469
- return ;
470
- }
766
+
471
767
 
768
+ if (!project_user) {
769
+ return winston.warn("RequestNotification email notification for the user with id : " + assignedId + " not found project_user");
770
+ }
472
771
  User.findOne({_id: assignedId, status: 100})
473
772
  .cache(cacheUtil.defaultTTL, "users:id:"+assignedId)
474
773
  .exec(function (err, user) {
@@ -476,11 +775,30 @@ sendAgentEmail(projectid, savedRequest) {
476
775
  winston.error("Error sending email to " + savedRequest.participants[0], err);
477
776
  }
478
777
  if (!user) {
479
- console.warn("User not found", savedRequest.participants[0]);
778
+ winston.warn("User not found", savedRequest.participants[0]);
480
779
  } else {
481
- winston.debug("Sending sendNewAssignedRequestNotification to user with email", user.email);
482
- // if (user.emailverified) { enable it? send anyway to improve engagment for new account
483
- emailService.sendNewAssignedRequestNotification(user.email, savedRequest, project);
780
+ winston.verbose("Sending sendNewAssignedRequestNotification to user with email", user.email);
781
+ // if (user.emailverified) { enable it? send anyway to improve engagment for new account
782
+
783
+
784
+ // var signOptions = {
785
+ // issuer: 'https://tiledesk.com',
786
+ // subject: 'user',
787
+ // audience: 'https://tiledesk.com',
788
+ // jwtid: uuidv4()
789
+ // };
790
+
791
+ // let userObject = {_id: user._id, firstname: user.firstname, lastname: user.lastname, email: user.email, attributes: user.attributes};
792
+ // winston.debug("userObject ",userObject);
793
+
794
+
795
+ // var agentToken = jwt.sign(userObject, configSecret, signOptions);
796
+ // winston.debug("agentToken "+agentToken);
797
+
798
+
799
+
800
+
801
+ emailService.sendNewAssignedRequestNotification(user.email, savedRequest, project);
484
802
  // }
485
803
  }
486
804
  });
@@ -513,7 +831,7 @@ sendAgentEmail(projectid, savedRequest) {
513
831
 
514
832
 
515
833
 
516
- sendTranscriptByEmail(sendTo, request_id, id_project) {
834
+ sendTranscriptByEmail(sendTo, request_id, id_project, project) {
517
835
  return new Promise(function (resolve, reject) {
518
836
  return Request.findOne({request_id: request_id, id_project: id_project})
519
837
  .populate('department')
@@ -543,8 +861,8 @@ sendAgentEmail(projectid, savedRequest) {
543
861
 
544
862
 
545
863
 
546
- emailService.sendRequestTranscript(sendTo, messages, request);
547
- winston.debug("sendTranscriptByEmail sent");
864
+ emailService.sendRequestTranscript(sendTo, messages, request, project);
865
+ winston.verbose("sendTranscriptByEmail sent");
548
866
  return resolve({sendTo: sendTo, messages: messages, request: request});
549
867
 
550
868