@tiledesk/tiledesk-server 2.3.1 → 2.3.4

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.
@@ -191,20 +191,24 @@ class EmailService {
191
191
  user: configEmail.user,
192
192
  pass: configEmail.pass
193
193
  },
194
+ // secureConnection: false,
195
+ // tls:{
196
+ // ciphers:'SSLv3'
197
+ // },
194
198
 
195
- // openssl genrsa -out dkim_private.pem 2048
199
+ // openssl genrsa -out dkim_private.pem 2048
196
200
  // openssl rsa -in dkim_private.pem -pubout -outform der 2>/dev/null | openssl base64 -A
197
201
  // ->
198
202
  // v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAunT2EopDAYnHwAOHd33KhlzjUXJfhmA+fK+cG85i9Pm33oyv1NoGrOynsni0PO6j7oRxxHqs6EMDOw4I/Q0C7aWn20oBomJZehTOkCV2xpuPKESiRktCe/MIZqbkRdypis4jSkFfFFkBHwgkAg5tb11E9elJap0ed/lN5/XlpGedqoypKxp+nEabgYO5mBMMNKRvbHx0eQttRYyIaNkTuMbAaqs4y3TkHOpGvZTJsvUonVMGAstSCfUmXnjF38aKpgyTausTSsxHbaxh3ieUB4ex+svnvsJ4Uh5Skklr+bxLVEHeJN55rxmV67ytLg5XCRWqdKIcJHFvSlm2YwJfcwIDAQABMacAL
199
203
  // testdkim._domainkey.tiledesk.com. 86400 IN TXT "v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAunT2EopDAYnHwAOHd33KhlzjUXJfhmA+fK+cG85i9Pm33oyv1NoGrOynsni0PO6j7oRxxHqs6EMDOw4I/Q0C7aWn20oBomJZehTOkCV2xpuPKESiRktCe/MIZqbkRdypis4jSkFfFFkBHwgkAg5tb11E9elJap0ed/lN5/XlpGedqoypKxp+nEabgYO5mBMMNKRvbHx0eQttRYyIaNkTuMbAaqs4y3TkHOpGvZTJsvUonVMGAstSCfUmXnjF38aKpgyTausTSsxHbaxh3ieUB4ex+svnvsJ4Uh5Skklr+bxLVEHeJN55rxmV67ytLg5XCRWqdKIcJHFvSlm2YwJfcwIDAQABMacAL"
200
204
 
201
- dkim: {
202
- domainName: "example.com",
203
- keySelector: "2017",
204
- privateKey: "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBg...",
205
- cacheDir: "/tmp",
206
- cacheTreshold: 100 * 1024
207
- }
205
+ // dkim: {
206
+ // domainName: "example.com",
207
+ // keySelector: "2017",
208
+ // privateKey: "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBg...",
209
+ // cacheDir: "/tmp",
210
+ // cacheTreshold: 100 * 1024
211
+ // }
208
212
  };
209
213
 
210
214
  winston.debug("getTransport transport: ",transport);
@@ -406,7 +410,9 @@ class EmailService {
406
410
  }
407
411
  }
408
412
 
409
-
413
+ // troncare nome utnete e nome progetto a max 10 caratteri
414
+ // cambiare in [Nicky:Dashboard Support] Assigned Chat
415
+ // serve per aggiornare native... fai aggiornamento
410
416
 
411
417
  let subject = `[Tiledesk ${project ? project.name : '-'}] New Assigned Chat`;
412
418
 
@@ -958,11 +964,12 @@ class EmailService {
958
964
 
959
965
  let inReplyTo;
960
966
  let references;
967
+ winston.debug("message.request.attributes", message.request.attributes);
961
968
  if (message.request.attributes) {
962
969
  if (message.request.attributes.email_messageId) {
963
970
  inReplyTo = message.request.attributes.email_messageId;
964
971
  }
965
- if (message.attributes.email_references) {
972
+ if (message.request.attributes.email_references) {
966
973
  references = message.request.attributes.email_references;
967
974
  }
968
975
  }
@@ -1176,6 +1183,177 @@ class EmailService {
1176
1183
  }
1177
1184
 
1178
1185
 
1186
+
1187
+
1188
+
1189
+
1190
+
1191
+
1192
+
1193
+
1194
+
1195
+ async sendFollowerNotification(to, message, project) {
1196
+
1197
+ var that = this;
1198
+
1199
+
1200
+ if (project.toJSON) {
1201
+ project = project.toJSON();
1202
+ }
1203
+
1204
+ var html = await this.readTemplate('newMessageFollower.html', project.settings);
1205
+
1206
+
1207
+ var envTemplate = process.env.EMAIL_FOLLOWER_HTML_TEMPLATE;
1208
+ winston.debug("envTemplate: " + envTemplate);
1209
+
1210
+ if (envTemplate) {
1211
+ html = envTemplate;
1212
+ }
1213
+
1214
+ winston.debug("html: " + html);
1215
+
1216
+ var template = handlebars.compile(html);
1217
+
1218
+ var baseScope = JSON.parse(JSON.stringify(that));
1219
+ delete baseScope.pass;
1220
+
1221
+
1222
+ let msgText = message.text;//.replace(/[\n\r]/g, '<br>');
1223
+ msgText = encode(msgText);
1224
+ if (this.markdown) {
1225
+ msgText = marked(msgText);
1226
+ }
1227
+
1228
+ winston.debug("msgText: " + msgText);
1229
+ winston.debug("baseScope: " + JSON.stringify(baseScope));
1230
+
1231
+
1232
+ var replacements = {
1233
+ message: message,
1234
+ project: project,
1235
+ msgText: msgText,
1236
+ baseScope: baseScope
1237
+ };
1238
+
1239
+ var html = template(replacements);
1240
+ winston.debug("html: " + html);
1241
+
1242
+ const fs = require('fs');
1243
+ fs.writeFileSync('tem1111.html', html);
1244
+
1245
+
1246
+
1247
+ let messageId = message._id + "@" + MESSAGE_ID_DOMAIN;
1248
+
1249
+ let replyTo;
1250
+ if (this.replyEnabled) {
1251
+ replyTo = message.request.request_id + this.inboundDomainDomainWithAt;
1252
+ }
1253
+
1254
+ let headers;
1255
+ if (message.request) {
1256
+
1257
+ messageId = message.request.request_id + "+" + messageId;
1258
+
1259
+ if (message.request.attributes && message.request.attributes.email_replyTo) {
1260
+ replyTo = message.request.attributes.email_replyTo;
1261
+ }
1262
+
1263
+ headers = {"X-TILEDESK-PROJECT-ID": project._id, "X-TILEDESK-REQUEST-ID": message.request.request_id, "X-TILEDESK-TICKET-ID":message.request.ticket_id };
1264
+
1265
+ winston.verbose("messageId: " + messageId);
1266
+ winston.verbose("replyTo: " + replyTo);
1267
+ winston.verbose("email headers", headers);
1268
+ }
1269
+
1270
+
1271
+ let inReplyTo;
1272
+ let references;
1273
+ let cc;
1274
+ let ccString;
1275
+
1276
+ if (message.request && message.request.attributes) {
1277
+ winston.debug("email message.request.attributes: ", message.request.attributes);
1278
+
1279
+ if (message.request.attributes.email_messageId) {
1280
+ inReplyTo = message.request.attributes.email_messageId;
1281
+ }
1282
+ if (message.request.attributes.email_references) {
1283
+ references = message.request.attributes.email_references;
1284
+ }
1285
+
1286
+ if (message.request.attributes.email_cc) {
1287
+ cc = message.request.attributes.email_cc;
1288
+ }
1289
+ winston.debug("email message.request.attributes.email_ccStr: "+ message.request.attributes.email_ccStr);
1290
+ if (message.request.attributes.email_ccStr!=undefined) {
1291
+ ccString = message.request.attributes.email_ccStr;
1292
+ winston.debug("email set ccString");
1293
+ }
1294
+ }
1295
+ winston.verbose("email inReplyTo: "+ inReplyTo);
1296
+ winston.verbose("email references: "+ references);
1297
+ winston.verbose("email cc: ", cc);
1298
+ winston.verbose("email ccString: "+ ccString);
1299
+
1300
+ let from;
1301
+ let configEmail;
1302
+ if (project && project.settings && project.settings.email) {
1303
+ if (project.settings.email.config) {
1304
+ configEmail = project.settings.email.config;
1305
+ winston.verbose("custom email configEmail setting found: ", configEmail);
1306
+ }
1307
+ if (project.settings.email.from) {
1308
+ from = project.settings.email.from;
1309
+ winston.verbose("custom from email setting found: "+ from);
1310
+ }
1311
+ }
1312
+
1313
+
1314
+
1315
+
1316
+ that.send({
1317
+ messageId: messageId,
1318
+ // sender: message.senderFullname, //must be an email
1319
+ from:from,
1320
+ to:to,
1321
+ cc: ccString,
1322
+ replyTo: replyTo,
1323
+ inReplyTo: inReplyTo,
1324
+ references: references,
1325
+ // subject:`${message.request ? message.request.subject : '-'}`,
1326
+ subject:`${message.request ? message.request.ticket_id : '-'}`, //gmail uses subject
1327
+ text:html,
1328
+ html:html,
1329
+ config:configEmail,
1330
+ headers:headers
1331
+ });
1332
+
1333
+ // // messageId = "notification" + messageId;
1334
+
1335
+ // // that.send({
1336
+ // // messageId: messageId,
1337
+ // // // sender: message.senderFullname, //must be an email
1338
+ // // to: that.bcc,
1339
+ // // replyTo: replyTo,
1340
+ // // inReplyTo: inReplyTo,
1341
+ // // references: references,
1342
+ // // // subject: `${message.request ? message.request.subject : '-'} - notification`,
1343
+ // // subject: `${message.request ? message.request.subject : '-'} - notification`,
1344
+ // // text:html,
1345
+ // // html:html,
1346
+ // // headers:headers
1347
+ // // });
1348
+
1349
+
1350
+ }
1351
+
1352
+
1353
+
1354
+
1355
+
1356
+
1179
1357
  /*
1180
1358
  sendEmailChannelTakingNotification(to, request, project, tokenQueryString) {
1181
1359
 
@@ -1505,6 +1683,6 @@ async sendRequestTranscript(to, messages, request, project) {
1505
1683
 
1506
1684
  var emailService = new EmailService();
1507
1685
 
1508
- // emailService.sendTest("al@f21.it");
1686
+ // emailService.sendTest("asd.");
1509
1687
 
1510
1688
  module.exports = emailService;
@@ -128,6 +128,8 @@ class LeadService {
128
128
 
129
129
 
130
130
  leadEvent.emit('lead.update', updatedLead);
131
+ leadEvent.emit('lead.email.update', updatedLead);
132
+ leadEvent.emit('lead.fullname.update', updatedLead);
131
133
  return resolve(updatedLead);
132
134
  });
133
135
  });
@@ -21,6 +21,7 @@ class RequestService {
21
21
 
22
22
  listen() {
23
23
  this.updateSnapshotLead();
24
+ this.sendMessageUpdateLead();
24
25
  }
25
26
  updateSnapshotLead() {
26
27
  leadEvent.on('lead.update', function(lead) {
@@ -58,6 +59,62 @@ class RequestService {
58
59
  });
59
60
  });
60
61
  }
62
+
63
+
64
+ sendMessageUpdateLead() {
65
+ leadEvent.on('lead.fullname.update', function(lead) {
66
+ // leadEvent.on('lead.update', function(lead) {
67
+
68
+ setImmediate(() => {
69
+ winston.debug("sendMessageUpdateLead on lead.update ", lead);
70
+
71
+ Request.find({lead: lead._id, id_project: lead.id_project}, function(err, requests) {
72
+
73
+ if (err) {
74
+ winston.error("Error getting sendMessageUpdateLead request by lead", err);
75
+ return 0;
76
+ }
77
+ if (!requests || (requests && requests.length==0)) {
78
+ winston.warn("sendMessageUpdateLead No request found for lead id " +lead._id );
79
+ return 0;
80
+ }
81
+
82
+ // winston.info("sendMessageUpdateLead requests ", requests);
83
+
84
+ requests.forEach(function(request) {
85
+
86
+ winston.debug("sendMessageUpdateLead request ", request);
87
+
88
+ // send(sender, senderFullname, recipient, text, id_project, createdBy, attributes, type, metadata, language)
89
+ messageService.send(
90
+ 'system',
91
+ 'Bot',
92
+ // lead.fullname,
93
+ request.request_id,
94
+ "Lead updated",
95
+ request.id_project,
96
+ 'system',
97
+ {
98
+ subtype:"info/support",
99
+ "updateconversation" : false,
100
+ messagelabel: {key: "LEAD_UPDATED"},
101
+ updateUserEmail: lead.email,
102
+ updateUserFullname: lead.fullname
103
+ },
104
+ undefined,
105
+ request.language
106
+
107
+ );
108
+
109
+ });
110
+
111
+ });
112
+
113
+ });
114
+ });
115
+ }
116
+
117
+
61
118
  getAvailableAgentsCount(agents) {
62
119
 
63
120
  var project_users_available = agents.filter(function (projectUser) {
@@ -142,6 +199,7 @@ class RequestService {
142
199
  request.snapshot = {}
143
200
  }
144
201
 
202
+
145
203
  request.snapshot.department = result.department;
146
204
  request.snapshot.agents = result.agents;
147
205
  request.snapshot.availableAgentsCount = that.getAvailableAgentsCount(result.agents);
@@ -400,6 +458,10 @@ class RequestService {
400
458
  var notes = request.notes;
401
459
  var priority = request.priority;
402
460
 
461
+ var auto_close = request.auto_close;
462
+
463
+ var followers = request.followers;
464
+
403
465
  if (!departmentid) {
404
466
  departmentid ='default';
405
467
  }
@@ -421,7 +483,7 @@ class RequestService {
421
483
  first_text:first_text, departmentid:departmentid, sourcePage:sourcePage, language:language, userAgent:userAgent, status:status,
422
484
  createdBy:createdBy, attributes:attributes, subject:subject, preflight: preflight, channel: channel, location: location,
423
485
  participants:participants, tags: tags, notes:notes,
424
- priority: priority}};
486
+ priority: priority, auto_close: auto_close, followers: followers}};
425
487
 
426
488
  winston.debug("context",context);
427
489
 
@@ -505,6 +567,7 @@ class RequestService {
505
567
  snapshot.department = result.department;
506
568
  }
507
569
 
570
+ // console.log("result.agents",result.agents);
508
571
  snapshot.agents = agents;
509
572
  snapshot.availableAgentsCount = that.getAvailableAgentsCount(agents);
510
573
 
@@ -549,7 +612,9 @@ class RequestService {
549
612
  snapshot: snapshot,
550
613
  tags: tags,
551
614
  notes: notes,
552
- priority: priority
615
+ priority: priority,
616
+ auto_close: auto_close,
617
+ followers: followers
553
618
  });
554
619
 
555
620
 
@@ -842,14 +907,14 @@ class RequestService {
842
907
 
843
908
  }
844
909
 
845
- setClosedAtByRequestId(request_id, id_project, closed_at) {
910
+ setClosedAtByRequestId(request_id, id_project, closed_at, closed_by) {
846
911
 
847
912
  return new Promise(function (resolve, reject) {
848
913
  // winston.debug("request_id", request_id);
849
914
  // winston.debug("newstatus", newstatus);
850
915
 
851
916
  return Request
852
- .findOneAndUpdate({request_id: request_id, id_project: id_project}, {closed_at: closed_at}, {new: true, upsert:false})
917
+ .findOneAndUpdate({request_id: request_id, id_project: id_project}, {closed_at: closed_at, closed_by: closed_by}, {new: true, upsert:false})
853
918
  .populate('lead')
854
919
  .populate('department')
855
920
  .populate('participatingBots')
@@ -934,7 +999,7 @@ class RequestService {
934
999
  }
935
1000
 
936
1001
 
937
- closeRequestByRequestId(request_id, id_project, skipStatsUpdate, notify) {
1002
+ closeRequestByRequestId(request_id, id_project, skipStatsUpdate, notify, closed_by) {
938
1003
 
939
1004
  var that = this;
940
1005
  return new Promise(function (resolve, reject) {
@@ -991,7 +1056,8 @@ class RequestService {
991
1056
  return resolve(updatedRequest);
992
1057
  }
993
1058
 
994
- return that.setClosedAtByRequestId(request_id, id_project, new Date().getTime()).then(function(updatedRequest) {
1059
+ // setClosedAtByRequestId(request_id, id_project, closed_at, closed_by)
1060
+ return that.setClosedAtByRequestId(request_id, id_project, new Date().getTime(), closed_by).then(function(updatedRequest) {
995
1061
 
996
1062
  winston.verbose("Request closed with id: " + updatedRequest.id);
997
1063
  winston.debug("Request closed ", updatedRequest);
@@ -1728,6 +1794,298 @@ class RequestService {
1728
1794
 
1729
1795
 
1730
1796
 
1797
+
1798
+
1799
+
1800
+
1801
+ addFollowerByRequestId(request_id, id_project, member) {
1802
+ winston.debug("request_id: " + request_id);
1803
+ winston.debug("id_project: " + id_project);
1804
+ winston.debug("addFollowerByRequestId member: " + member);
1805
+
1806
+
1807
+
1808
+ //TODO control if member is a valid project_user of the project
1809
+ // validate member is string
1810
+ return new Promise(function (resolve, reject) {
1811
+
1812
+ if (member==undefined) {
1813
+ var err = "addFollowerByRequestId error, member field is null";
1814
+ winston.error(err);
1815
+ return reject(err);
1816
+ }
1817
+
1818
+ return Request
1819
+ .findOne({request_id: request_id, id_project: id_project})
1820
+ // qui cache
1821
+ .exec( function(err, request) {
1822
+ if (err){
1823
+ winston.error("Error adding follower ", err);
1824
+ return reject(err);
1825
+ }
1826
+ if (!request) {
1827
+ winston.error('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1828
+ return reject('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1829
+ }
1830
+
1831
+ winston.debug("assigned_operator here1");
1832
+
1833
+ // return Request.findById(id).then(function (request) {
1834
+ if (request.followers.indexOf(member)==-1){
1835
+ request.followers.push(member);
1836
+
1837
+ request.save(function(err, savedRequest) {
1838
+ if (err) {
1839
+ winston.error(err);
1840
+ return reject(err);
1841
+ }
1842
+
1843
+ winston.debug("saved", savedRequest);
1844
+
1845
+ return savedRequest
1846
+ .populate('lead')
1847
+ .populate('department')
1848
+ .populate('participatingBots')
1849
+ .populate('participatingAgents')
1850
+ // .populate('followers')
1851
+ .populate({path:'requester',populate:{path:'id_user'}})
1852
+ .execPopulate( function(err, requestComplete) {
1853
+
1854
+ if (err) {
1855
+ winston.error("Error getting addFollowerByRequestId", err);
1856
+ return reject(err);
1857
+ }
1858
+
1859
+
1860
+ winston.debug("populated", requestComplete);
1861
+
1862
+ requestEvent.emit('request.update', requestComplete);
1863
+ requestEvent.emit("request.update.comment", {comment:"FOLLOWER_ADD",request:requestComplete});//Deprecated
1864
+ requestEvent.emit("request.updated", {comment:"FOLLOWER_ADD",request:requestComplete, patch: {member:member}});
1865
+ requestEvent.emit('request.followers.join', {member:member, request: requestComplete});
1866
+
1867
+ return resolve(requestComplete);
1868
+ });
1869
+ });
1870
+ // qui assignetat
1871
+ } else {
1872
+ winston.debug('Request member '+ member+ ' already added for request_id '+ request_id + ' and id_project '+ id_project);
1873
+ return request
1874
+ .populate('lead')
1875
+ .populate('department')
1876
+ .populate('participatingBots')
1877
+ .populate('participatingAgents')
1878
+ // .populate('followers')
1879
+ .populate({path:'requester',populate:{path:'id_user'}})
1880
+ .execPopulate( function(err, requestComplete) {
1881
+ return resolve(requestComplete);
1882
+ });
1883
+ }
1884
+
1885
+ });
1886
+ });
1887
+ }
1888
+
1889
+
1890
+
1891
+
1892
+
1893
+ setFollowersByRequestId(request_id, id_project, newfollowers) {
1894
+
1895
+ //TODO validate participants
1896
+ // validate if array of string newparticipants
1897
+ return new Promise(function (resolve, reject) {
1898
+
1899
+ var isArray = Array.isArray(newfollowers);
1900
+
1901
+ if(isArray==false) {
1902
+ winston.error('setFollowersByRequestId error newfollowers is not an array for request_id '+ request_id + ' and id_project '+ id_project);
1903
+ return reject('setFollowersByRequestId error newfollowers is not an array for request_id '+ request_id + ' and id_project '+ id_project);
1904
+ }
1905
+
1906
+ return Request
1907
+
1908
+ .findOne({request_id: request_id, id_project: id_project})
1909
+ // qui cache ok
1910
+ .exec( function(err, request) {
1911
+ if (err) {
1912
+ winston.error("Error setFollowersByRequestId", err);
1913
+ return reject(err);
1914
+ }
1915
+ if (!request) {
1916
+ winston.error('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1917
+ return reject('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
1918
+ }
1919
+ var oldfollowers = request.followers;
1920
+ winston.debug('oldParticipants', oldfollowers);
1921
+ winston.debug('newparticipants', newfollowers);
1922
+
1923
+ if (requestUtil.arraysEqual(oldfollowers, newfollowers)){
1924
+ //if (oldParticipants === newparticipants) {
1925
+ winston.verbose('Request members '+ oldfollowers+ ' already equal to ' + newfollowers + ' for request_id '+ request_id + ' and id_project '+ id_project);
1926
+ return request
1927
+ .populate('lead')
1928
+ .populate('department')
1929
+ .populate('participatingBots')
1930
+ .populate('participatingAgents')
1931
+ .populate({path:'requester',populate:{path:'id_user'}})
1932
+ .execPopulate( function(err, requestComplete) {
1933
+ return resolve(requestComplete);
1934
+ });
1935
+
1936
+ }
1937
+
1938
+ request.followers = newfollowers;
1939
+
1940
+ //cacheinvalidation
1941
+ return request.save(function(err, updatedRequest) {
1942
+ // dopo save non aggiorna participating
1943
+ if (err) {
1944
+ winston.error("Error setFollowersByRequestId", err);
1945
+ return reject(err);
1946
+ }
1947
+
1948
+ return updatedRequest
1949
+ .populate('lead')
1950
+ .populate('department')
1951
+ .populate('participatingBots')
1952
+ .populate('participatingAgents')
1953
+ .populate({path:'requester',populate:{path:'id_user'}})
1954
+ .execPopulate( function(err, requestComplete) {
1955
+
1956
+
1957
+ if (err) {
1958
+ winston.error("Error getting setFollowersByRequestId", err);
1959
+ return reject(err);
1960
+ }
1961
+
1962
+ winston.debug("oldfollowers ", oldfollowers);
1963
+
1964
+ requestEvent.emit('request.update', requestComplete);
1965
+ requestEvent.emit("request.update.comment", {comment:"FOLLOWERS_SET",request:requestComplete});//Deprecated
1966
+ requestEvent.emit("request.updated", {comment:"FOLLOWERS_SET",request:requestComplete, patch: {}});
1967
+
1968
+ // requestEvent.emit('request.followers.update', {beforeRequest:request,
1969
+ // removedParticipants:removedParticipants,
1970
+ // addedParticipants:addedParticipants,
1971
+ // request:requestComplete});
1972
+
1973
+ return resolve(requestComplete);
1974
+ });
1975
+ });
1976
+
1977
+ });
1978
+
1979
+
1980
+ });
1981
+ }
1982
+
1983
+
1984
+
1985
+
1986
+
1987
+
1988
+ removeFollowerByRequestId(request_id, id_project, member) {
1989
+ winston.debug("request_id", request_id);
1990
+ winston.debug("id_project", id_project);
1991
+ winston.debug("member", member);
1992
+
1993
+ return new Promise(function (resolve, reject) {
1994
+
1995
+
1996
+
1997
+ if (member==undefined) {
1998
+ var err = "removeFollowerByRequestId error, member field is null";
1999
+ winston.error(err);
2000
+ return reject(err);
2001
+ }
2002
+
2003
+
2004
+ return Request
2005
+ .findOne({request_id: request_id, id_project: id_project})
2006
+ // .populate('participatingAgents') //for abandoned_by_project_users
2007
+ // qui cache
2008
+ .exec( async (err, request) => {
2009
+
2010
+ if (err){
2011
+ winston.error("Error removing follower ", err);
2012
+ return reject(err);
2013
+ }
2014
+
2015
+ if (!request) {
2016
+ winston.error('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
2017
+ return reject('Request not found for request_id '+ request_id + ' and id_project '+ id_project);
2018
+ }
2019
+
2020
+ var index = request.followers.indexOf(member);
2021
+ winston.debug("index", index);
2022
+
2023
+ if (index > -1) {
2024
+ request.followers.splice(index, 1);
2025
+ // winston.debug(" request.participants", request.participants);
2026
+
2027
+
2028
+ // winston.debug(" request", request);
2029
+ //cacheinvalidation
2030
+ return request.save(function(err, savedRequest) {
2031
+ if (err){
2032
+ winston.error("Error saving removed follower ", err);
2033
+ return reject(err);
2034
+ }
2035
+
2036
+ return savedRequest
2037
+ .populate('lead')
2038
+ .populate('department')
2039
+ .populate('participatingBots')
2040
+ .populate('participatingAgents')
2041
+ // .populate('followers')
2042
+ .populate({path:'requester',populate:{path:'id_user'}})
2043
+ .execPopulate( function(err, requestComplete) {
2044
+
2045
+ if (err){
2046
+ winston.error("Error getting removed follower ", err);
2047
+ return reject(err);
2048
+ }
2049
+
2050
+
2051
+ requestEvent.emit('request.update', requestComplete);
2052
+ requestEvent.emit("request.update.comment", {comment:"FOLLOWER_REMOVE",request:requestComplete});//Deprecated
2053
+ requestEvent.emit("request.updated", {comment:"FOLLOWER_REMOVE",request:requestComplete, patch: {member:member}});
2054
+ requestEvent.emit('request.followers.leave', {member:member, request: requestComplete});
2055
+
2056
+
2057
+ return resolve(requestComplete);
2058
+
2059
+ });
2060
+ });
2061
+
2062
+
2063
+ }else {
2064
+ winston.verbose('Request member '+ member+ ' already not found for request_id '+ request_id + ' and id_project '+ id_project);
2065
+
2066
+ return request
2067
+ .populate('lead')
2068
+ .populate('department')
2069
+ .populate('participatingBots')
2070
+ .populate('participatingAgents')
2071
+ // .populate('followers')
2072
+ .populate({path:'requester',populate:{path:'id_user'}})
2073
+ .execPopulate( function(err, requestComplete) {
2074
+ return resolve(requestComplete);
2075
+ });
2076
+ }
2077
+
2078
+ });
2079
+ });
2080
+ }
2081
+
2082
+
2083
+
2084
+
2085
+
2086
+
2087
+
2088
+
1731
2089
  }
1732
2090
 
1733
2091