@tiledesk/tiledesk-server 2.13.19 → 2.13.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/routes/message.js CHANGED
@@ -54,9 +54,6 @@ async (req, res) => {
54
54
  // return res.status(422).json({ errors: errors.array() });
55
55
  // }
56
56
 
57
-
58
-
59
-
60
57
  // TODO se sei agent non puoi cambiare sender
61
58
  // verificare validazione invio immagine senza caption
62
59
  var project_user = req.projectuser;
@@ -67,272 +64,253 @@ async (req, res) => {
67
64
  let messageStatus = req.body.status || MessageConstants.CHAT_MESSAGE_STATUS.SENDING;
68
65
  winston.debug('messageStatus: ' + messageStatus);
69
66
 
70
- let q = Request.findOne({request_id: req.params.request_id, id_project: req.projectid});
71
- if (cacheEnabler.request) {
72
- q.cache(cacheUtil.defaultTTL, req.projectid+":requests:request_id:"+req.params.request_id+":simple") //request_cache
73
- winston.debug('request cache enabled');
74
- }
75
- // cacherequest // requestcachefarequi nocachepopulatereqired
76
- return q.exec(async(err, request) => {
77
- // .populate('lead')
78
- // .populate('department')
79
- // .populate('participatingBots')
80
- // .populate('participatingAgents')
81
- // .populate({path:'requester',populate:{path:'id_user'}})
82
-
83
-
84
- if (err) {
85
- winston.log({
86
- level: 'error',
87
- message: 'Error getting the request: '+ JSON.stringify(err) + " " + JSON.stringify(req.body) ,
88
- label: req.projectid
89
- });
90
- // winston.error('Error getting the request.', err);
91
- return res.status(500).send({success: false, msg: 'Error getting the request.', err:err});
92
- }
67
+ let q = Request.findOne({request_id: req.params.request_id, id_project: req.projectid});
68
+ if (cacheEnabler.request) {
69
+ q.cache(cacheUtil.defaultTTL, req.projectid+":requests:request_id:"+req.params.request_id+":simple") //request_cache
70
+ winston.debug('request cache enabled');
71
+ }
72
+ // cacherequest // requestcachefarequi nocachepopulatereqired
73
+ return q.exec(async(err, request) => {
74
+ // .populate('lead')
75
+ // .populate('department')
76
+ // .populate('participatingBots')
77
+ // .populate('participatingAgents')
78
+ // .populate({path:'requester',populate:{path:'id_user'}})
79
+
80
+ if (err) {
81
+ winston.log({
82
+ level: 'error',
83
+ message: 'Error getting the request: '+ JSON.stringify(err) + " " + JSON.stringify(req.body) ,
84
+ label: req.projectid
85
+ });
86
+ // winston.error('Error getting the request.', err);
87
+ return res.status(500).send({success: false, msg: 'Error getting the request.', err:err});
88
+ }
93
89
 
94
- if (!request) { //the request doen't exists create it
90
+ if (!request) { //the request doen't exists create it
95
91
 
96
- winston.debug("request not exists", request);
92
+ winston.debug("request not exists", request);
97
93
 
98
- if (project_user) {
99
- winston.debug("project_user", project_user);
100
- }
101
-
102
-
103
- // sponz: 4/01/23 disable it
104
- if (!req.body.text && (!req.body.type || req.body.type=="text") ) {
105
- return res.status(422).json({ errors: ["text field is required"] });
106
- }
94
+ if (project_user) {
95
+ winston.debug("project_user", project_user);
96
+ }
97
+
98
+
99
+ // sponz: 4/01/23 disable it
100
+ if (!req.body.text && (!req.body.type || req.body.type=="text") ) {
101
+ return res.status(422).json({ errors: ["text field is required"] });
102
+ }
107
103
 
108
- if (sender) {
104
+ if (sender) {
109
105
 
110
- var isObjectId = mongoose.Types.ObjectId.isValid(sender);
111
- winston.debug("isObjectId:"+ isObjectId);
112
-
113
- var queryProjectUser = {id_project:req.projectid, status: "active" };
114
-
115
- if (isObjectId) {
116
- queryProjectUser.id_user = sender;
117
- } else {
118
- queryProjectUser.uuid_user = sender;
119
- }
120
-
121
- winston.debug("queryProjectUser", queryProjectUser);
122
-
123
- project_user = await Project_user.findOne(queryProjectUser).populate({path:'id_user', select:{'firstname':1, 'lastname':1, 'email':1}})
124
- winston.debug("project_user", project_user);
106
+ var isObjectId = mongoose.Types.ObjectId.isValid(sender);
107
+ winston.debug("isObjectId:"+ isObjectId);
108
+
109
+ var queryProjectUser = {id_project:req.projectid, status: "active" };
110
+
111
+ if (isObjectId) {
112
+ queryProjectUser.id_user = sender;
113
+ } else {
114
+ queryProjectUser.uuid_user = sender;
115
+ }
116
+
117
+ winston.debug("queryProjectUser", queryProjectUser);
125
118
 
126
- if (!project_user) {
127
- return res.status(403).send({success: false, msg: 'Unauthorized. Project_user not found with user id : '+ sender });
128
- }
129
-
130
- if ( project_user.id_user) {
131
- fullname = project_user.id_user.fullName;
132
- winston.debug("pu fullname: "+ fullname);
133
- email = project_user.id_user.email;
134
- winston.debug("pu email: "+ email);
135
- } else if (project_user.uuid_user) {
136
- var lead = await Lead.findOne({lead_id: project_user.uuid_user, id_project: req.projectid});
137
- winston.debug("lead: ",lead);
138
- if (lead) {
139
- fullname = lead.fullname;
140
- winston.debug("lead fullname: "+ fullname);
141
- email = lead.email;
142
- winston.debug("lead email: "+ email);
143
- }else {
144
- winston.warn("lead not found: " + JSON.stringify({lead_id: project_user.uuid_user, id_project: req.projectid}));
145
- }
146
-
147
- } else {
148
- winston.warn("pu fullname and email empty");
149
- }
150
-
119
+ project_user = await Project_user.findOne(queryProjectUser).populate({path:'id_user', select:{'firstname':1, 'lastname':1, 'email':1}})
120
+ winston.debug("project_user", project_user);
121
+
122
+ if (!project_user) {
123
+ return res.status(403).send({success: false, msg: 'Unauthorized. Project_user not found with user id : '+ sender });
124
+ }
125
+
126
+ if ( project_user.id_user) {
127
+ fullname = project_user.id_user.fullName;
128
+ winston.debug("pu fullname: "+ fullname);
129
+ email = project_user.id_user.email;
130
+ winston.debug("pu email: "+ email);
131
+ } else if (project_user.uuid_user) {
132
+ var lead = await Lead.findOne({lead_id: project_user.uuid_user, id_project: req.projectid});
133
+ winston.debug("lead: ",lead);
134
+ if (lead) {
135
+ fullname = lead.fullname;
136
+ winston.debug("lead fullname: "+ fullname);
137
+ email = lead.email;
138
+ winston.debug("lead email: "+ email);
139
+ }else {
140
+ winston.warn("lead not found: " + JSON.stringify({lead_id: project_user.uuid_user, id_project: req.projectid}));
151
141
  }
142
+
143
+ } else {
144
+ winston.warn("pu fullname and email empty");
145
+ }
146
+
147
+ }
152
148
 
153
- // prende fullname e email da quello loggato
149
+ // prende fullname e email da quello loggato
154
150
 
155
- // createIfNotExistsWithLeadId(lead_id, fullname, email, id_project, createdBy, attributes) {
156
- return leadService.createIfNotExistsWithLeadId(sender || req.user._id, fullname, email, req.projectid, null, req.body.attributes || req.user.attributes)
157
- .then(function(createdLead) {
151
+ // createIfNotExistsWithLeadId(lead_id, fullname, email, id_project, createdBy, attributes) {
152
+ return leadService.createIfNotExistsWithLeadId(sender || req.user._id, fullname, email, req.projectid, null, req.body.attributes || req.user.attributes)
153
+ .then(function(createdLead) {
158
154
 
159
-
160
155
 
161
- var new_request = {
162
- request_id: req.params.request_id,
163
- project_user_id: project_user._id,
164
- lead_id: createdLead._id,
165
- id_project:req.projectid,
166
- first_text: req.body.text,
167
- departmentid: req.body.departmentid,
168
- sourcePage:req.body.sourcePage,
169
- language: req.body.language,
170
- userAgent:req.body.userAgent,
171
- status:req.body.request_status,
172
- createdBy: req.user._id,
173
- attributes: req.body.attributes,
174
- subject: req.body.subject,
175
- preflight:req.body.preflight,
176
- channel: req.body.channel,
177
- location: req.body.location,
178
- participants: req.body.participants,
179
- lead: createdLead,
180
- requester: project_user,
181
- priority: req.body.priority,
182
- followers: req.body.followers,
183
- proactive: true
184
- };
185
-
186
- return requestService.create(new_request).then(function (savedRequest) {
187
-
188
-
189
- if (!savedRequest) {
190
- return res.status(403).send({ success: false, message: "Requests quota exceeded"})
191
- }
192
- winston.debug("returning savedRequest to", savedRequest.toJSON());
156
+
157
+ var new_request = {
158
+ request_id: req.params.request_id,
159
+ project_user_id: project_user._id,
160
+ lead_id: createdLead._id,
161
+ id_project:req.projectid,
162
+ first_text: req.body.text,
163
+ departmentid: req.body.departmentid,
164
+ sourcePage:req.body.sourcePage,
165
+ language: req.body.language,
166
+ userAgent:req.body.userAgent,
167
+ status:req.body.request_status,
168
+ createdBy: req.user._id,
169
+ attributes: req.body.attributes,
170
+ subject: req.body.subject,
171
+ preflight:req.body.preflight,
172
+ channel: req.body.channel,
173
+ location: req.body.location,
174
+ participants: req.body.participants,
175
+ lead: createdLead,
176
+ requester: project_user,
177
+ priority: req.body.priority,
178
+ followers: req.body.followers,
179
+ proactive: true
180
+ };
181
+
182
+ return requestService.create(new_request).then(function (savedRequest) {
183
+
184
+
185
+ if (!savedRequest) {
186
+ return res.status(403).send({ success: false, message: "Requests quota exceeded"})
187
+ }
188
+ winston.debug("returning savedRequest to", savedRequest.toJSON());
193
189
 
194
- // createWithIdAndRequester(request_id, project_user_id, lead_id, id_project, first_text, departmentid, sourcePage, language, userAgent, status,
195
- // createdBy, attributes, subject, preflight, channel, location) {
190
+ // createWithIdAndRequester(request_id, project_user_id, lead_id, id_project, first_text, departmentid, sourcePage, language, userAgent, status,
191
+ // createdBy, attributes, subject, preflight, channel, location) {
196
192
 
197
- // return requestService.createWithIdAndRequester(req.params.request_id, req.projectuser._id, createdLead._id, req.projectid,
198
- // req.body.text, req.body.departmentid, req.body.sourcePage,
199
- // req.body.language, req.body.userAgent, null, req.user._id, req.body.attributes, req.body.subject, undefined, req.body.channel, req.body.location ).then(function (savedRequest) {
193
+ // return requestService.createWithIdAndRequester(req.params.request_id, req.projectuser._id, createdLead._id, req.projectid,
194
+ // req.body.text, req.body.departmentid, req.body.sourcePage,
195
+ // req.body.language, req.body.userAgent, null, req.user._id, req.body.attributes, req.body.subject, undefined, req.body.channel, req.body.location ).then(function (savedRequest) {
200
196
 
201
197
 
202
198
 
203
199
 
204
- // create(sender, senderFullname, recipient, text, id_project, createdBy, status, attributes, type, metadata, language, channel_type, channel) {
205
- return messageService.create(sender || req.user._id, fullname, req.params.request_id, req.body.text,
206
- req.projectid, req.user._id, messageStatus, req.body.attributes, req.body.type, req.body.metadata, req.body.language, undefined, req.body.channel).then(function (savedMessage) {
200
+ // create(sender, senderFullname, recipient, text, id_project, createdBy, status, attributes, type, metadata, language, channel_type, channel) {
201
+ return messageService.create(sender || req.user._id, fullname, req.params.request_id, req.body.text,
202
+ req.projectid, req.user._id, messageStatus, req.body.attributes, req.body.type, req.body.metadata, req.body.language, undefined, req.body.channel).then(function (savedMessage) {
207
203
 
208
- // return requestService.incrementMessagesCountByRequestId(savedRequest.request_id, savedRequest.id_project).then(function(savedRequestWithIncrement) {
204
+ // return requestService.incrementMessagesCountByRequestId(savedRequest.request_id, savedRequest.id_project).then(function(savedRequestWithIncrement) {
209
205
 
210
- let message = savedMessage.toJSON();
206
+ let message = savedMessage.toJSON();
211
207
 
212
- winston.debug("returning message to", message);
208
+ winston.debug("returning message to", message);
213
209
 
214
- winston.debug("returning savedRequest2210 to", savedRequest.toJSON());
210
+ winston.debug("returning savedRequest2210 to", savedRequest.toJSON());
215
211
 
216
212
 
217
- savedRequest //bug
218
- // Request.findById(savedRequest.id)
219
- .populate('lead')
220
- .populate('department')
221
- .populate('participatingBots')
222
- .populate('participatingAgents')
223
- // .populate('followers')
224
- .populate({ path: 'requester', populate: { path: 'id_user' } })
225
- // .exec(function (err, savedRequestPopulated){
226
- .execPopulate(function (err, savedRequestPopulated) { //bug with execPopulate request.attributes are invalid (NOT real data). but this bug is related to chat21 listener changes by reference. i think populate suffer from this problem bacause it it the same obect passed by reference
213
+ savedRequest //bug
214
+ // Request.findById(savedRequest.id)
215
+ .populate('lead')
216
+ .populate('department')
217
+ .populate('participatingBots')
218
+ .populate('participatingAgents')
219
+ // .populate('followers')
220
+ .populate({ path: 'requester', populate: { path: 'id_user' } })
221
+ // .exec(function (err, savedRequestPopulated){
222
+ .execPopulate(function (err, savedRequestPopulated) { //bug with execPopulate request.attributes are invalid (NOT real data). but this bug is related to chat21 listener changes by reference. i think populate suffer from this problem bacause it it the same obect passed by reference
227
223
 
228
- if (err) {
229
- return winston.error("Error gettting savedRequestPopulated for send Message", err);
230
- }
224
+ if (err) {
225
+ return winston.error("Error gettting savedRequestPopulated for send Message", err);
226
+ }
231
227
 
232
- winston.debug("returning savedRequest221 to", savedRequest.toJSON());
228
+ winston.debug("returning savedRequest221 to", savedRequest.toJSON());
233
229
 
234
230
 
235
- winston.debug("savedRequestPopulated", savedRequestPopulated.toJSON());
231
+ winston.debug("savedRequestPopulated", savedRequestPopulated.toJSON());
236
232
 
237
- winston.debug("returning savedRequest22 to", savedRequest.toJSON());
233
+ winston.debug("returning savedRequest22 to", savedRequest.toJSON());
238
234
 
239
235
 
240
- message.request = savedRequestPopulated;
241
- winston.debug("returning2 message to", message);
236
+ message.request = savedRequestPopulated;
237
+ winston.debug("returning2 message to", message);
242
238
 
243
239
 
244
- return res.json(message);
245
- });
240
+ return res.json(message);
246
241
  });
247
- }).catch(function (err) { //pubblica questo
248
- winston.error('Error creating request: ' + JSON.stringify(err));
249
- return res.status(500).send({ success: false, msg: 'Error creating request', err: err });
250
242
  });
243
+ }).catch(function (err) { //pubblica questo
244
+ winston.error('Error creating request: ' + JSON.stringify(err));
245
+ return res.status(500).send({ success: false, msg: 'Error creating request', err: err });
246
+ });
251
247
 
252
- });
253
-
254
-
255
-
256
- } else {
257
-
258
-
259
-
260
- winston.debug("request exists", request.toObject());
261
-
262
-
263
- // create(sender, senderFullname, recipient, text, id_project, createdBy, status, attributes, type, metadata, language, channel_type, channel) {
264
- return messageService.create(sender || req.user._id, fullname, req.params.request_id, req.body.text,
265
- request.id_project, null, messageStatus, req.body.attributes, req.body.type, req.body.metadata, req.body.language, undefined, req.body.channel).then(function(savedMessage){
266
-
267
- // TOOD update also request attributes and sourcePage
268
- // return requestService.incrementMessagesCountByRequestId(request.request_id, request.id_project).then(function(savedRequest) {
269
- // console.log("savedRequest.participants.indexOf(message.sender)", savedRequest.participants.indexOf(message.sender));
270
-
271
- if (request.participants && request.participants.indexOf(sender) > -1) { //update waiitng time if write an agent (member of participants)
272
- winston.debug("updateWaitingTimeByRequestId");
273
- return requestService.updateWaitingTimeByRequestId(request.request_id, request.id_project, true).then(function(upRequest) {
274
- let message = savedMessage.toJSON();
275
- message.request = upRequest;
276
-
277
-
278
- return res.json(message);
279
- });
280
- }else {
281
- let message = savedMessage.toJSON();
282
-
283
- winston.debug("getting request for response");
284
-
285
- let q =
286
- Request.findOne({request_id: request.request_id, id_project: request.id_project})
287
- // request
288
- .populate('lead')
289
- .populate('department')
290
- .populate('participatingBots')
291
- .populate('participatingAgents')
292
- // .populate('followers')
293
- .populate({path:'requester',populate:{path:'id_user'}});
294
-
295
-
296
- if (cacheEnabler.request) {
297
- q.cache(cacheUtil.defaultTTL, request.id_project+":requests:request_id:"+request.request_id)
298
- winston.debug('request cache enabled for messages');
299
- }
300
-
301
- q.exec(function (err, requestPopulated){
302
- // q.execPopulate(function (err, requestPopulated){
303
-
304
- if (err) {
305
- return winston.error("Error gettting savedRequestPopulated for send Message", err);
306
- }
248
+ });
307
249
 
308
- message.request = requestPopulated;
309
-
310
- return res.json(message);
311
-
312
- });
313
- }
314
- // });
315
- }).catch(function(err){
316
- winston.log({
317
- level: 'error',
318
- message: 'Error creating message endpoint: '+ JSON.stringify(err) + " " + JSON.stringify(req.body) ,
319
- label: req.projectid
320
- });
321
- winston.error("Error creating message", err);
322
- return res.status(500).send({success: false, msg: 'Error creating message', err:err });
323
- });
324
250
 
325
251
 
252
+ } else {
326
253
 
254
+ winston.debug("request exists", request.toObject());
255
+ if (request.channel?.name === 'form') {
256
+ if (!sender && request.participantsAgents?.[0] !== req.user.id) {
257
+ return res.status(403).send({ success: false, message: "Error creating message", err: "You don't belong the conversation" });
327
258
  }
259
+ }
328
260
 
261
+ return messageService.create(sender || req.user._id, fullname, req.params.request_id, req.body.text,
262
+ request.id_project, null, messageStatus, req.body.attributes, req.body.type, req.body.metadata,
263
+ req.body.language, undefined, req.body.channel).then(function(savedMessage){
329
264
 
265
+ // TOOD update also request attributes and sourcePage
266
+ // return requestService.incrementMessagesCountByRequestId(request.request_id, request.id_project).then(function(savedRequest) {
267
+ // console.log("savedRequest.participants.indexOf(message.sender)", savedRequest.participants.indexOf(message.sender));
268
+
269
+ if (request.participants && request.participants.indexOf(sender) > -1) { //update waiitng time if write an agent (member of participants)
270
+ winston.debug("updateWaitingTimeByRequestId");
271
+ return requestService.updateWaitingTimeByRequestId(request.request_id, request.id_project, true).then(function(upRequest) {
272
+ let message = savedMessage.toJSON();
273
+ message.request = upRequest;
274
+ return res.json(message);
275
+ });
330
276
 
331
- });
332
-
333
-
334
-
335
-
277
+ } else {
278
+
279
+ let message = savedMessage.toJSON();
280
+ winston.debug("getting request for response");
281
+
282
+ let q = Request.findOne({request_id: request.request_id, id_project: request.id_project})
283
+ .populate('lead')
284
+ .populate('department')
285
+ .populate('participatingBots')
286
+ .populate('participatingAgents')
287
+ .populate({path:'requester',populate:{path:'id_user'}});
288
+
289
+ if (cacheEnabler.request) {
290
+ q.cache(cacheUtil.defaultTTL, request.id_project+":requests:request_id:"+request.request_id)
291
+ winston.debug('request cache enabled for messages');
292
+ }
293
+
294
+ q.exec(function (err, requestPopulated) {
295
+
296
+ if (err) {
297
+ return winston.error("Error gettting savedRequestPopulated for send Message", err);
298
+ }
299
+ message.request = requestPopulated;
300
+ return res.json(message);
301
+ });
302
+ }
303
+ }).catch(function(err) {
304
+ winston.log({
305
+ level: 'error',
306
+ message: 'Error creating message endpoint: '+ JSON.stringify(err) + " " + JSON.stringify(req.body) ,
307
+ label: req.projectid
308
+ });
309
+ winston.error("Error creating message", err);
310
+ return res.status(500).send({success: false, msg: 'Error creating message', err:err });
311
+ });
312
+ }
313
+ });
336
314
  });
337
315
 
338
316
 
@@ -22,7 +22,7 @@ class ChatbotService {
22
22
  return resbody.data;
23
23
  }).catch((err) => {
24
24
  winston.error("(ChatbotService) fork error " + err);
25
- return err;
25
+ throw err;
26
26
  })
27
27
 
28
28
  }
@@ -47,7 +47,7 @@ class ChatbotService {
47
47
  return chatbot;
48
48
  }).catch((err) => {
49
49
  winston.error('(ChatbotService) FAQ_KB EXPORTJSON ERROR ' + err);
50
- return err;
50
+ throw err;
51
51
  })
52
52
 
53
53
  // public bot
@@ -65,7 +65,7 @@ class ChatbotService {
65
65
  return chatbot
66
66
  }).catch((err) => {
67
67
  winston.error('(ChatbotService) FAQ_KB CHATBOT TEMPLATES ERROR ' + err);
68
- return err;
68
+ throw err;
69
69
  })
70
70
  }
71
71
 
@@ -88,7 +88,7 @@ class ChatbotService {
88
88
  return resbody.data;
89
89
  }).catch((err) => {
90
90
  winston.error("(ChatbotService) CREATE NEW CHATBOT ERROR " + err);
91
- return err;
91
+ throw err;
92
92
  })
93
93
 
94
94
  }
@@ -110,7 +110,7 @@ class ChatbotService {
110
110
  return resbody.data;
111
111
  }).catch((err) => {
112
112
  winston.error("(ChatbotService) IMPORT FAQS ERROR " + err);
113
- return err;
113
+ throw err;
114
114
  })
115
115
  }
116
116
 
@@ -1959,6 +1959,23 @@ class RequestService {
1959
1959
  winston.error('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
1960
1960
  return reject('Request not found for request_id ' + request_id + ' and id_project ' + id_project);
1961
1961
  }
1962
+
1963
+ if (request.channel?.name === 'form' || request.channel?.name === 'email') {
1964
+ if (Array.isArray(request.participantsAgents)) {
1965
+ if (request.participantsAgents.length === 1) {
1966
+ winston.error('Cannot add participants: participantsAgents already has one element for request_id ' + request_id + ' and id_project ' + id_project);
1967
+ return reject('Cannot add participants: only one participant allowed for this request');
1968
+ } else if (request.participantsAgents.length === 0) {
1969
+ if (Array.isArray(newparticipants) && newparticipants.length === 1) {
1970
+ // ok, allow to add one participant
1971
+ } else {
1972
+ winston.error('Can only add one participant for request_id ' + request_id + ' and id_project ' + id_project);
1973
+ return reject('Can only add one participant for this request');
1974
+ }
1975
+ }
1976
+ }
1977
+ }
1978
+
1962
1979
  var oldParticipants = request.participants;
1963
1980
  winston.debug('oldParticipants', oldParticipants);
1964
1981
  winston.debug('newparticipants', newparticipants);
@@ -673,6 +673,72 @@ describe('FaqKBRoute', () => {
673
673
 
674
674
  })
675
675
 
676
+ it('fork-chatbot-private-not-permitted', (done) => {
677
+ var email_user1 = "user1-signup-" + Date.now() + "@email.com";
678
+ var email_user2 = "user2-signup-" + (Date.now() + 1) + "@email.com";
679
+ var pwd = "pwd";
680
+
681
+ userService.signup(email_user1, pwd, "User1 Firstname", "User1 lastname").then(function (user1) {
682
+ userService.signup(email_user2, pwd, "User2 Firstname", "User2 lastname").then(function (user2) {
683
+ projectService.create("project1", user1._id).then(function (currentProject) {
684
+ projectService.create("project2", user2._id).then(function (landingProject) {
685
+
686
+ class chatbot_service {
687
+ async getBotById(id, published, api_url, chatbot_templates_api_url, token, project_id) {
688
+ return new Promise((resolve, reject) => {
689
+ reject({ success: false, msg: "Chatbot not found" })
690
+ })
691
+ }
692
+
693
+ async createBot(api_url, token, chatbot, project_id) {
694
+ return chatbot_mock.empty_chatbot_mock
695
+ }
696
+
697
+ async importFaqs(api_url, id_faq_kb, token, chatbot, project_id) {
698
+ return chatbot_mock.import_faqs_res_mock
699
+ }
700
+ }
701
+
702
+ server.set('chatbot_service', new chatbot_service());
703
+
704
+ chai.request(server)
705
+ .post('/' + currentProject._id + '/faq_kb')
706
+ .auth(email_user1, pwd)
707
+ .send({ "name": "chatbot1", type: "tilebot", language: 'en', public: "false", template: "blank" })
708
+ .end((err, res) => {
709
+ if (err) { console.error("err: ", err); }
710
+ if (log) { console.log("res.body", res.body); }
711
+
712
+ res.should.have.status(200);
713
+ res.body.should.be.a('object');
714
+ expect(res.body.name).to.equal("chatbot1");
715
+ expect(res.body.language).to.equal("en");
716
+ expect(res.body.public).to.equal(false);
717
+
718
+ const chatbot_id = res.body._id;
719
+
720
+ chai.request(server)
721
+ .post('/' + landingProject._id + '/faq_kb/fork/' + chatbot_id + "?public=false&projectid=" + landingProject._id)
722
+ .auth(email_user2, pwd)
723
+ .end((err, res) => {
724
+
725
+ if (err) { console.error("err: ", err); }
726
+ if (log) { console.log("res.body: ", res.body); }
727
+
728
+ res.should.have.status(500);
729
+ res.body.should.be.a('object');
730
+ expect(res.body.success).to.equal(false);
731
+ expect(res.body.error).to.equal("Unable to get chatbot to be forked");
732
+ done();
733
+ })
734
+
735
+ });
736
+ });
737
+ });
738
+ });
739
+ });
740
+ });
741
+
676
742
  it('fork-chatbot-public', (done) => {
677
743
  var email_user1 = "user1-signup-" + Date.now() + "@email.com";
678
744
  var email_user2 = "user2-signup-" + Date.now() + "@email.com";