@tiledesk/tiledesk-server 2.3.1 → 2.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
 
@@ -720,6 +720,75 @@ it('createWithPriority', function (done) {
720
720
 
721
721
 
722
722
 
723
+
724
+ // mocha test/messageRoute.js --grep 'createSimpleWithFollowers'
725
+
726
+ it('createSimpleWithFollowers', function (done) {
727
+ // this.timeout(10000);
728
+
729
+ var email = "test-message-create-" + Date.now() + "@email.com";
730
+ var pwd = "pwd";
731
+
732
+ userService.signup( email ,pwd, "Test Firstname", "Test lastname").then(function(savedUser) {
733
+ projectService.createAndReturnProjectAndProjectUser("message-create", savedUser._id).then(function(savedProjectAndPU) {
734
+
735
+ var savedProject = savedProjectAndPU.project;
736
+
737
+ chai.request(server)
738
+ .post('/'+ savedProject._id + '/requests/req123-createSimpleWithFollowers/messages')
739
+ .auth(email, pwd)
740
+ .set('content-type', 'application/json')
741
+ .send({"text":"text", "followers": [savedProjectAndPU.project_user._id.toString()]})
742
+ .end(function(err, res) {
743
+ //console.log("res", res);
744
+ console.log("res.body", JSON.stringify(res.body));
745
+ res.should.have.status(200);
746
+ res.body.should.be.a('object');
747
+
748
+ expect(res.body.sender).to.equal(savedUser._id.toString());
749
+ // expect(res.body.sender).to.equal(savedProjectAndPU.project_user._id.toString());
750
+ // expect(res.body.senderFullname).to.equal("senderFullname");
751
+ expect(res.body.recipient).to.equal("req123-createSimpleWithFollowers");
752
+ expect(res.body.text).to.equal("text");
753
+ expect(res.body.id_project).to.equal(savedProject._id.toString());
754
+ expect(res.body.createdBy).to.equal(savedUser._id.toString());
755
+ expect(res.body.status).to.equal(0);
756
+
757
+ expect(res.body.request.request_id).to.equal("req123-createSimpleWithFollowers");
758
+ expect(res.body.request.requester._id).to.equal(savedProjectAndPU.project_user._id.toString());
759
+ // expect(res.body.request.requester_id).to.equal("sender");
760
+ expect(res.body.request.first_text).to.equal("text");
761
+ expect(res.body.request.id_project).to.equal(savedProject._id.toString());
762
+ expect(res.body.request.createdBy).to.equal(savedUser._id.toString());
763
+
764
+ // expect(res.body.request.messages_count).to.equal(1);
765
+
766
+ expect(res.body.request.status).to.equal(200);
767
+ expect(res.body.request.snapshot.agents.length).to.equal(1);
768
+ expect(res.body.request.participants.length).to.equal(1);
769
+ expect(res.body.request.department).to.not.equal(null);
770
+ expect(res.body.request.lead).to.not.equal(null);
771
+
772
+ expect(res.body.channel_type).to.equal("group");
773
+ expect(res.body.channel.name).to.equal("chat21");
774
+ expect(res.body.request.channel.name).to.equal("chat21");
775
+
776
+
777
+ expect(res.body.request.location).to.equal(undefined);
778
+
779
+ expect(res.body.request.followers[0]).to.equal(savedProjectAndPU.project_user._id.toString());
780
+
781
+
782
+ done();
783
+ });
784
+ });
785
+ });
786
+ });
787
+
788
+
789
+
790
+
791
+
723
792
  it('getall', function (done) {
724
793
  // this.timeout(10000);
725
794
 
@@ -686,13 +686,15 @@ describe('RequestService', function () {
686
686
  savedProject._id, "5badfe5d553d1844ad654072"),
687
687
  messageService.create("5badfe5d553d1844ad654072", "test sender", savedRequest.request_id, "hello2",
688
688
  savedProject._id, "5badfe5d553d1844ad654072")]).then(function(all) {
689
- requestService.closeRequestByRequestId(savedRequest.request_id, savedProject._id).then(function(closedRequest) {
689
+ // closeRequestByRequestId(request_id, id_project, skipStatsUpdate, notify, closed_by)
690
+ requestService.closeRequestByRequestId(savedRequest.request_id, savedProject._id, false, true, "user1").then(function(closedRequest) {
690
691
  winston.debug("resolve closedRequest", closedRequest.toObject());
691
692
  expect(closedRequest.status).to.equal(1000);
692
693
  expect(closedRequest.closed_at).to.not.equal(null);
693
694
  expect(closedRequest.transcript).to.contains("hello1");
694
695
  expect(closedRequest.transcript).to.contains("hello2");
695
696
  expect(closedRequest.snapshot.agents).to.equal(undefined);
697
+ expect(closedRequest.closed_by).to.equal("user1");
696
698
 
697
699
  done();
698
700
  }).catch(function(err){
@@ -1,11 +0,0 @@
1
- const EventEmitter = require('events');
2
-
3
- class SubscriptionEvent extends EventEmitter {}
4
- var winston = require('../config/winston');
5
-
6
-
7
- const subscriptionEvent = new SubscriptionEvent();
8
-
9
-
10
-
11
- module.exports = subscriptionEvent;