@tiledesk/tiledesk-server 2.13.18 → 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/CHANGELOG.md CHANGED
@@ -5,9 +5,13 @@
5
5
  🚀 IN PRODUCTION 🚀
6
6
  (https://www.npmjs.com/package/@tiledesk/tiledesk-server/v/2.3.77)
7
7
 
8
- # 2.13.18
8
+ # 2.13.20
9
+ - Minor bug fix on email channel
10
+
11
+ # 2.13.19
9
12
  - Improved: llm preview to support openai models
10
13
  - Updated: 2.0.30
14
+ - Fix tokens usage for llm preview
11
15
 
12
16
  # 2.13.17
13
17
  - Updated: whatsapp-connector to 1.0.7
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-server",
3
3
  "description": "The Tiledesk server module",
4
- "version": "2.13.18",
4
+ "version": "2.13.20",
5
5
  "scripts": {
6
6
  "start": "node ./bin/www",
7
7
  "pretest": "mongodb-runner start",
@@ -49,7 +49,7 @@
49
49
  "@tiledesk/tiledesk-rasa-connector": "^1.0.10",
50
50
  "@tiledesk/tiledesk-sms-connector": "^0.1.11",
51
51
  "@tiledesk/tiledesk-telegram-connector": "^0.1.14",
52
- "@tiledesk/tiledesk-tybot-connector": "^2.0.30",
52
+ "@tiledesk/tiledesk-tybot-connector": "^2.0.31",
53
53
  "@tiledesk/tiledesk-voice-twilio-connector": "^0.1.22",
54
54
  "@tiledesk/tiledesk-vxml-connector": "^0.1.78",
55
55
  "@tiledesk/tiledesk-whatsapp-connector": "1.0.7",
@@ -38,29 +38,28 @@ router.post('/', function (req, res) {
38
38
 
39
39
  router.put('/:cannedResponseid', async function (req, res) {
40
40
  winston.debug(req.body);
41
- let canned_id = req.params.cannedResponseid;
41
+ const canned_id = req.params.cannedResponseid;
42
+ const id_project = req.projectid;
42
43
  let user_role = req.projectuser.role;
43
44
 
44
45
  var update = {};
45
-
46
- if (req.body.title!=undefined) {
47
- update.title = req.body.title;
48
- }
49
- if (req.body.text!=undefined) {
50
- update.text = req.body.text;
51
- }
52
- if (req.body.attributes!=undefined) {
53
- update.attributes = req.body.attributes;
54
- }
55
46
 
56
- let canned = await CannedResponse.findById(canned_id).catch((err) => {
47
+ const allowedFields = ['title', 'text', 'attributes']
48
+
49
+ allowedFields.forEach(f => {
50
+ if (req.body[f] !== undefined) {
51
+ update[f] = req.body[f];
52
+ }
53
+ })
54
+
55
+ let canned = await CannedResponse.findOne({ _id: canned_id, id_project: id_project }).catch((err) => {
57
56
  winston.error("Error finding canned response: ", err);
58
57
  return res.status(500).send({ success: false, error: "General error: cannot find the canned response with id " + canned_id })
59
58
  })
60
59
 
61
60
  if (!canned) {
62
61
  winston.verbose("Canned response with id " + canned_id + " not found.");
63
- return res.status(404).send({ success: false, error: "Canned response with id " + canned_id + " not found." })
62
+ return res.status(404).send({ success: false, error: "Canned response not found with id " + canned_id + " for project " + id_project })
64
63
  }
65
64
 
66
65
  /**
@@ -98,17 +97,18 @@ router.put('/:cannedResponseid', async function (req, res) {
98
97
 
99
98
  router.delete('/:cannedResponseid', async function (req, res) {
100
99
  winston.debug(req.body);
101
- let canned_id = req.params.cannedResponseid;
100
+ const canned_id = req.params.cannedResponseid;
101
+ const id_project = req.projectid;
102
102
  let user_role = req.projectuser.role;
103
103
 
104
- let canned = await CannedResponse.findById(canned_id).catch((err) => {
104
+ let canned = await CannedResponse.findOne({ _id: canned_id, id_project: id_project }).catch((err) => {
105
105
  winston.error("Error finding canned response: ", err);
106
106
  return res.status(500).send({ success: false, error: "General error: cannot find the canned response with id " + canned_id })
107
107
  })
108
108
 
109
109
  if (!canned) {
110
110
  winston.verbose("Canned response with id " + canned_id + " not found.");
111
- return res.status(404).send({ success: false, error: "Canned response with id " + canned_id + " not found." })
111
+ return res.status(404).send({ success: false, error: "Canned response not found with id " + canned_id + " for project " + id_project })
112
112
  }
113
113
 
114
114
  /**
@@ -133,7 +133,7 @@ router.delete('/:cannedResponseid', async function (req, res) {
133
133
  return res.status(401).send({ success: false, error: "Unauthorized"})
134
134
  }
135
135
 
136
- CannedResponse.findByIdAndUpdate(req.params.cannedResponseid, {status: 1000}, { new: true, upsert: true }, function (err, updatedCannedResponse) {
136
+ CannedResponse.findByIdAndUpdate(canned_id, {status: 1000}, { new: true, upsert: true }, function (err, updatedCannedResponse) {
137
137
  if (err) {
138
138
  winston.error('--- > ERROR ', err);
139
139
  return res.status(500).send({ success: false, msg: 'Error updating object.' });
@@ -146,16 +146,18 @@ router.delete('/:cannedResponseid', async function (req, res) {
146
146
 
147
147
  router.delete('/:cannedResponseid/physical', async function (req, res) {
148
148
  winston.debug(req.body);
149
- let canned_id = req.params.cannedResponseid;
149
+ const canned_id = req.params.cannedResponseid;
150
+ const id_project = req.projectid;
151
+ let user_role = req.projectuser.role;
150
152
 
151
- let canned = await CannedResponse.findById(canned_id).catch((err) => {
153
+ let canned = await CannedResponse.findOne({ _id: canned_id, id_project: id_project }).catch((err) => {
152
154
  winston.error("Error finding canned response: ", err);
153
155
  return res.status(500).send({ success: false, error: "General error: cannot find the canned response with id " + canned_id })
154
156
  })
155
157
 
156
158
  if (!canned) {
157
159
  winston.verbose("Canned response with id " + canned_id + " not found.");
158
- return res.status(404).send({ success: false, error: "Canned response with id " + canned_id + " not found." })
160
+ return res.status(404).send({ success: false, error: "Canned response not found with id " + canned_id + " for project " + id_project })
159
161
  }
160
162
 
161
163
  /**
@@ -180,7 +182,7 @@ router.delete('/:cannedResponseid/physical', async function (req, res) {
180
182
  return res.status(401).send({ success: false, error: "Unauthorized"})
181
183
  }
182
184
 
183
- CannedResponse.remove({ _id: req.params.cannedResponseid }, function (err, cannedResponse) {
185
+ CannedResponse.remove({ _id: canned_id }, function (err, cannedResponse) {
184
186
  if (err) {
185
187
  winston.error('--- > ERROR ', err);
186
188
  return res.status(500).send({ success: false, msg: 'Error deleting object.' });
package/routes/faq_kb.js CHANGED
@@ -132,10 +132,11 @@ router.post('/train', roleChecker.hasRole('admin'), function (req, res) {
132
132
 
133
133
  router.post('/aitrain/', roleChecker.hasRole('admin'), async (req, res) => {
134
134
 
135
- let id_faq_kb = req.body.id_faq_kb;
135
+ const chatbot_id = req.body.id_faq_kb;
136
+ const id_project = req.projectid;
136
137
  let webhook_enabled = req.body.webhook_enabled;
137
138
 
138
- Faq_kb.findById(id_faq_kb, async (err, chatbot) => {
139
+ Faq_kb.findOne({ _id: chatbot_id, id_project: id_project}, async (err, chatbot) => {
139
140
  if (err) {
140
141
  return res.status(400).send({ success: false, error: err })
141
142
  }
@@ -145,10 +146,10 @@ router.post('/aitrain/', roleChecker.hasRole('admin'), async (req, res) => {
145
146
  if (chatbot.intentsEngine === 'tiledesk-ai') {
146
147
 
147
148
  // Option 1: emit event
148
- //faqBotEvent.emit('faq_train.train', id_faq_kb, webhook_enabled);
149
+ //faqBotEvent.emit('faq_train.train', chatbot_id, webhook_enabled);
149
150
 
150
151
  // Option 2: call service directly
151
- trainingService.train(null, id_faq_kb, webhook_enabled).then((training_result) => {
152
+ trainingService.train(null, chatbot_id, webhook_enabled).then((training_result) => {
152
153
  winston.info("training result: ", training_result);
153
154
  let response = {
154
155
  succes: true,
@@ -174,7 +175,10 @@ router.post('/askbot', roleChecker.hasRole('admin'), function (req, res) {
174
175
 
175
176
  winston.debug('ASK BOT ', req.body);
176
177
 
177
- Faq_kb.findById(req.body.id_faq_kb).exec(function (err, faq_kb) {
178
+ const chatbot_id = req.body.id_faq_kb;
179
+ const id_project = req.projectid;
180
+
181
+ Faq_kb.findOne({ _id: chatbot_id, id_project: id_project }).exec(function (err, faq_kb) {
178
182
  if (err) {
179
183
  return res.status(500).send({ success: false, msg: 'Error getting object.' });
180
184
  }
@@ -185,7 +189,7 @@ router.post('/askbot', roleChecker.hasRole('admin'), function (req, res) {
185
189
  winston.debug('faq_kb.type :' + faq_kb.type);
186
190
  if (faq_kb.type == "internal" || faq_kb.type == "tilebot") {
187
191
 
188
- var query = { "id_project": req.projectid, "id_faq_kb": req.body.id_faq_kb, "question": req.body.question };
192
+ var query = { "id_project": id_project, "id_faq_kb": chatbot_id, "question": req.body.question };
189
193
 
190
194
  Faq.find(query)
191
195
  .lean().
@@ -302,74 +306,42 @@ router.put('/:faq_kbid', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscripti
302
306
 
303
307
  winston.debug(req.body);
304
308
 
305
- var update = {};
306
- if (req.body.name != undefined) {
307
- update.name = req.body.name;
308
- }
309
- if (req.body.description != undefined) {
310
- update.description = req.body.description;
311
- }
312
- if (req.body.url != undefined) {
313
- update.url = req.body.url;
314
- }
315
- if (req.body.webhook_url != undefined) {
316
- update.webhook_url = req.body.webhook_url;
317
- }
318
- if (req.body.webhook_enabled != undefined) {
319
- update.webhook_enabled = req.body.webhook_enabled;
320
- }
321
- if (req.body.type != undefined) {
322
- update.type = req.body.type;
323
- }
324
- if (req.body.trashed != undefined) {
325
- update.trashed = req.body.trashed;
326
- }
327
- if (req.body.public != undefined) {
328
- update.public = req.body.public;
329
- }
330
- if (req.body.certified != undefined) {
331
- update.certified = req.body.certified;
332
- }
333
- if (req.body.mainCategory != undefined) {
334
- update.mainCategory = req.body.mainCategory;
335
- }
336
- if (req.body.intentsEngine != undefined) {
337
- update.intentsEngine = req.body.intentsEngine;
338
- }
339
-
340
- if (req.body.tags != undefined) {
341
- update.tags = req.body.tags;
342
- }
343
-
344
- if (req.body.trained != undefined) {
345
- update.trained = req.body.trained;
346
- }
347
-
348
- if (req.body.short_description != undefined) {
349
- update.short_description = req.body.short_description
350
- }
351
-
352
- if (req.body.title != undefined) {
353
- update.title = req.body.title
354
- }
309
+ const chatbot_id = req.params.faq_kbid;
310
+ const id_project = req.projectid;
355
311
 
356
- if (req.body.certifiedTags != undefined) {
357
- update.certifiedTags = req.body.certifiedTags
358
- }
359
-
360
- if (req.body.agents_available != undefined) {
361
- update.agents_available = req.body.agents_available
362
- }
363
-
364
- if (req.body.slug != undefined) {
365
- update.slug = req.body.slug;
366
- }
312
+ var update = {};
313
+ const allowedFields = [
314
+ 'name',
315
+ 'description',
316
+ 'url',
317
+ 'webhook_url',
318
+ 'webhook_enabled',
319
+ 'type',
320
+ 'trashed',
321
+ 'public',
322
+ 'certified',
323
+ 'mainCategory',
324
+ 'intentsEngine',
325
+ 'tags',
326
+ 'trained',
327
+ 'short_description',
328
+ 'title',
329
+ 'certifiedTags',
330
+ 'agents_available',
331
+ 'slug'
332
+ ];
333
+
334
+ allowedFields.forEach(f => {
335
+ if (req.body[f] !== undefined) {
336
+ update[f] = req.body[f];
337
+ }
338
+ });
367
339
 
368
340
  update.modified = true;
369
341
 
370
342
  winston.debug("update", update);
371
343
 
372
- Faq_kb.findByIdAndUpdate(req.params.faq_kbid, update, { new: true, upsert: true }, function (err, updatedFaq_kb) { //TODO add cache_bot_here
344
+ Faq_kb.findOneAndUpdate({ _id: chatbot_id, id_project: id_project }, update, { new: true }, function (err, updatedFaq_kb) { //TODO add cache_bot_here
373
345
  if (err) {
374
346
  if (err.code === 11000 && err.keyValue.slug) {
375
347
  return res.status(500).send({ success: false, msg: 'Error updating object.', error: 'Slug already exists: ' + err.keyValue.slug, error_code: errorCodes.CHATBOT.ERRORS.DUPLICATE_SLUG })
@@ -378,6 +350,10 @@ router.put('/:faq_kbid', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscripti
378
350
  }
379
351
  }
380
352
 
353
+ if (!updatedFaq_kb) {
354
+ return res.status(404).send({ success: false, msg: "Chatbot not found with id " + chatbot_id + " for project " + id_project });
355
+ }
356
+
381
357
  botEvent.emit('faqbot.update', updatedFaq_kb);
382
358
  res.json(updatedFaq_kb);
383
359
  });
@@ -386,6 +362,8 @@ router.put('/:faq_kbid', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscripti
386
362
  router.put('/:faq_kbid/language/:language', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscription']), (req, res) => {
387
363
 
388
364
  winston.debug("update language: ", req.params);
365
+ const chatbot_id = req.params.faq_kbid;
366
+ const id_project = req.projectid;
389
367
 
390
368
  let update = {};
391
369
  if (req.params.language != undefined) {
@@ -393,11 +371,15 @@ router.put('/:faq_kbid/language/:language', roleChecker.hasRoleOrTypes('admin',
393
371
  }
394
372
 
395
373
  winston.debug("update", update);
396
- Faq_kb.findByIdAndUpdate(req.params.faq_kbid, update, { new: true }, (err, updatedFaq_kb) => {
374
+ Faq_kb.findOneAndUpdate({ _id: chatbot_id, id_project: id_project }, update, { new: true }, (err, updatedFaq_kb) => {
397
375
  if (err) {
398
376
  return res.status(500).send({ success: false, msg: 'Error updating object.' });
399
377
  }
400
378
 
379
+ if (!updatedFaq_kb) {
380
+ return res.status(404).send({ success: false, msg: "Chatbot not found with id " + chatbot_id + " for project " + id_project });
381
+ }
382
+
401
383
  Faq.updateMany({ id_faq_kb: req.params.faq_kbid }, update, (err, result) => {
402
384
  if (err) {
403
385
  botEvent.emit('faqbot.update', updatedFaq_kb);
@@ -413,17 +395,20 @@ router.put('/:faq_kbid/language/:language', roleChecker.hasRoleOrTypes('admin',
413
395
  router.patch('/:faq_kbid/attributes', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscription']), function (req, res) { //TODO add cache_bot_here
414
396
  var data = req.body;
415
397
 
398
+ const chatbot_id = req.params.faq_kbid;
399
+ const id_project = req.projectid;
400
+
416
401
  // TODO use service method
417
- Faq_kb.findById(req.params.faq_kbid, function (err, updatedBot) {
402
+ Faq_kb.findOne({ _id: chatbot_id, id_project: id_project }, function (err, updatedBot) {
418
403
  if (err) {
419
404
  winston.error('--- > ERROR ', err);
420
405
  return res.status(500).send({ success: false, msg: 'Error updating object.' });
421
406
  }
422
407
 
423
408
  if (!updatedBot) {
424
- return res.status(404).send({ success: false, msg: 'Object not found.' });
409
+ return res.status(404).send({ success: false, msg: "Chatbot not found with id " + chatbot_id + " for project " + id_project });
425
410
  }
426
-
411
+
427
412
  if (!updatedBot.attributes) {
428
413
  winston.debug("empty attributes")
429
414
  updatedBot.attributes = {};
@@ -460,10 +445,17 @@ router.delete('/:faq_kbid', roleChecker.hasRole('admin'), function (req, res) {
460
445
 
461
446
  winston.debug(req.body);
462
447
 
463
- Faq_kb.remove({ _id: req.params.faq_kbid }, function (err, faq_kb) {
448
+ const chatbot_id = req.params.faq_kbid;
449
+ const id_project = req.projectid;
450
+
451
+ Faq_kb.findOneAndDelete({ _id: chatbot_id, id_project: id_project }, function (err, faq_kb) {
464
452
  if (err) {
465
453
  return res.status(500).send({ success: false, msg: 'Error deleting object.' });
466
454
  }
455
+
456
+ if (!faq_kb) {
457
+ return res.status(404).send({ success: false, msg: "Chatbot not found with id " + chatbot_id + " for project " + id_project });
458
+ }
467
459
  /**
468
460
  * WARNING: faq_kb is the operation result, not the faq_kb object. The event subscriber will not receive the object as expected.
469
461
  */
@@ -476,12 +468,15 @@ router.get('/:faq_kbid', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscripti
476
468
 
477
469
  winston.debug(req.query);
478
470
 
479
- Faq_kb.findById(req.params.faq_kbid, function (err, faq_kb) { //TODO add cache_bot_here
471
+ const chatbot_id = req.params.faq_kbid;
472
+ const id_project = req.projectid;
473
+
474
+ Faq_kb.findOne({ _id: chatbot_id, id_project: id_project }, function (err, faq_kb) { //TODO add cache_bot_here
480
475
  if (err) {
481
476
  return res.status(500).send({ success: false, msg: 'Error getting object.' });
482
477
  }
483
478
  if (!faq_kb) {
484
- return res.status(404).send({ success: false, msg: 'Object not found.' });
479
+ return res.status(404).send({ success: false, msg: "Chatbot not found with id " + chatbot_id + " for project " + id_project });
485
480
  }
486
481
 
487
482
  if (req.query.departmentid) {
@@ -519,8 +514,8 @@ router.get('/:faq_kbid', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscripti
519
514
 
520
515
  router.get('/:faq_kbid/published', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscription']), async function (req, res) {
521
516
 
522
- let id_project = req.projectid;
523
- let chatbot_id = req.params.faq_kbid;
517
+ const id_project = req.projectid;
518
+ const chatbot_id = req.params.faq_kbid;
524
519
 
525
520
  let published_chatbots = await faq_kb.find({ id_project: id_project, root_id: chatbot_id })
526
521
  .sort({ publishedAt: -1 })
@@ -539,12 +534,15 @@ router.get('/:faq_kbid/jwt', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscr
539
534
 
540
535
  winston.debug(req.query);
541
536
 
542
- Faq_kb.findById(req.params.faq_kbid).select("+secret").exec(function (err, faq_kb) { //TODO add cache_bot_here
537
+ const chatbot_id = req.params.faq_kbid;
538
+ const id_project = req.projectid;
539
+
540
+ Faq_kb.findOne({ _id: chatbot_id, id_project: id_project }).select("+secret").exec(function (err, faq_kb) { //TODO add cache_bot_here
543
541
  if (err) {
544
542
  return res.status(500).send({ success: false, msg: 'Error getting object.' });
545
543
  }
546
544
  if (!faq_kb) {
547
- return res.status(404).send({ success: false, msg: 'Object not found.' });
545
+ return res.status(404).send({ success: false, msg: "Chatbot not found with id " + chatbot_id + " for project " + id_project });
548
546
  }
549
547
 
550
548
  var signOptions = {
@@ -578,9 +576,11 @@ router.get('/:faq_kbid/jwt', roleChecker.hasRoleOrTypes('admin', ['bot', 'subscr
578
576
  */
579
577
  router.get('/', roleChecker.hasRoleOrTypes('agent', ['bot', 'subscription']), function (req, res) {
580
578
 
581
-
582
579
  winston.debug("req.query", req.query);
583
- winston.debug("GET FAQ-KB req projectid", req.projectid);
580
+
581
+ const id_project = req.projectid;
582
+ winston.debug("GET FAQ-KB req projectid " + id_project);
583
+
584
584
 
585
585
  let restricted_mode = false;
586
586
 
@@ -594,7 +594,7 @@ router.get('/', roleChecker.hasRoleOrTypes('agent', ['bot', 'subscription']), fu
594
594
  * if filter only for 'trashed = false',
595
595
  * the bots created before the implementation of the 'trashed' property are not returned
596
596
  */
597
- let query = { "id_project": req.projectid, "trashed": { $in: [null, false] } };
597
+ let query = { id_project: id_project, trashed: { $in: [null, false] } };
598
598
 
599
599
  if (restricted_mode === true) {
600
600
  query.agents_available = {
@@ -606,22 +606,18 @@ router.get('/', roleChecker.hasRoleOrTypes('agent', ['bot', 'subscription']), fu
606
606
  query.type = { $ne: "identity" }
607
607
  }
608
608
 
609
- let search_obj = { "$search": req.query.text };
610
-
611
609
  if (req.query.text) {
612
- if (req.query.language) {
613
- search_obj["$language"] = req.query.language;
614
- }
610
+ const search_obj = { $search: req.query.text };
611
+ if (req.query.language) search_obj.$language = req.query.language;
615
612
  query.$text = search_obj;
616
613
  }
617
614
 
618
- if (req.query.public) {
619
- query.public = req.query.public;
620
- }
621
-
622
- if (req.query.certified) {
623
- query.certified = req.query.certified;
624
- }
615
+ const allowedFilters = ["public", "certified"];
616
+ allowedFilters.forEach(f => {
617
+ if (req.query[f] !== undefined) {
618
+ query[f] = req.query[f];
619
+ }
620
+ });
625
621
 
626
622
  winston.debug("query", query);
627
623
 
@@ -664,12 +660,15 @@ router.post('/fork/:id_faq_kb', roleChecker.hasRole('admin'), async (req, res) =
664
660
  let token = req.headers.authorization;
665
661
 
666
662
  let cs = req.app.get('chatbot_service')
667
-
668
- let chatbot = await cs.getBotById(id_faq_kb, public, api_url, chatbot_templates_api_url, token, current_project_id, globals);
669
- winston.debug("chatbot: ", chatbot)
663
+ let chatbot;
664
+ try {
665
+ chatbot = await cs.getBotById(id_faq_kb, public, api_url, chatbot_templates_api_url, token, current_project_id, globals);
666
+ } catch (err) {
667
+ return res.status(500).send({ success: false, error: "Unable to get chatbot to be forked"});
668
+ }
670
669
 
671
670
  if (!chatbot) {
672
- return res.status(500).send({ success: false, message: "Unable to get chatbot" });
671
+ return res.status(500).send({ success: false, message: "Unable to get chatbot to be forked" });
673
672
  }
674
673
 
675
674
  if (!globals) {
@@ -702,8 +701,10 @@ router.post('/fork/:id_faq_kb', roleChecker.hasRole('admin'), async (req, res) =
702
701
 
703
702
  router.post('/importjson/:id_faq_kb', roleChecker.hasRole('admin'), upload.single('uploadFile'), async (req, res) => {
704
703
 
705
- let id_faq_kb = req.params.id_faq_kb;
706
- winston.debug('import on id_faq_kb: ' + id_faq_kb);
704
+ let chatbot_id = req.params.id_faq_kb;
705
+ let id_project = req.projectid;
706
+
707
+ winston.debug('import on id_faq_kb: ' + chatbot_id);
707
708
 
708
709
  winston.debug('import with option create: ' + req.query.create);
709
710
  winston.debug('import with option replace: ' + req.query.replace);
@@ -810,18 +811,18 @@ router.post('/importjson/:id_faq_kb', roleChecker.hasRole('admin'), upload.singl
810
811
  // *****************************
811
812
  else {
812
813
 
813
- if (!id_faq_kb) {
814
+ if (!chatbot_id) {
814
815
  return res.status(400).send({ success: false, message: "With replace or overwrite option a id_faq_kb must be provided" })
815
816
  }
816
817
 
817
- let chatbot = Faq_kb.findById(id_faq_kb).catch((err) => {
818
- winston.error("Error finding chatbot with id " + id_faq_kb);
819
- return res.status(404).send({ success: false, message: "Error finding chatbot with id " + id_faq_kb, error: err });
818
+ let chatbot = await Faq_kb.findOne({ _id: chatbot_id, id_project: id_project }).catch((err) => {
819
+ winston.error("Error finding chatbot with id " + chatbot_id);
820
+ return res.status(404).send({ success: false, message: "Error finding chatbot with id " + chatbot_id, error: err });
820
821
  })
821
822
 
822
823
  if (!chatbot) {
823
- winston.error("Chatbot not found with id " + id_faq_kb);
824
- return res.status(404).send({ success: false, message: "Chatbot not found with id " + id_faq_kb });
824
+ winston.error("Chatbot not found with id " + chatbot_id + " for project " + id_project);
825
+ return res.status(404).send({ success: false, message: "Chatbot not found with id " + chatbot_id + " for project " + id_project });
825
826
  }
826
827
 
827
828
  if (json.webhook_enabled) {
@@ -858,7 +859,7 @@ router.post('/importjson/:id_faq_kb', roleChecker.hasRole('admin'), upload.singl
858
859
  }
859
860
  }
860
861
 
861
- let updatedChatbot = await Faq_kb.findByIdAndUpdate(id_faq_kb, chatbot, { new: true }).catch((err) => {
862
+ let updatedChatbot = await Faq_kb.findByIdAndUpdate(chatbot._id, chatbot, { new: true }).catch((err) => {
862
863
  winston.error("Error updating chatbot");
863
864
  return res.status(400).send({ success: false, message: "Error updating chatbot", error: err })
864
865
  })
@@ -869,11 +870,11 @@ router.post('/importjson/:id_faq_kb', roleChecker.hasRole('admin'), upload.singl
869
870
  // **** REPLACE TRUE option ****
870
871
  // *****************************
871
872
  if (req.query.replace === 'true') {
872
- let result = await Faq.deleteMany({ id_faq_kb: id_faq_kb }).catch((err) => {
873
- winston.error("Unable to delete all existing faqs with id_faq_kb " + id_faq_kb);
873
+ let result = await Faq.deleteMany({ id_faq_kb: chatbot._id }).catch((err) => {
874
+ winston.error("Unable to delete all existing faqs with id_faq_kb " + chatbot._id);
874
875
  })
875
876
 
876
- winston.verbose("All faq for chatbot " + id_faq_kb + " deleted successfully")
877
+ winston.verbose("All faq for chatbot " + chatbot._id + " deleted successfully")
877
878
  winston.debug("DeleteMany faqs result ", result);
878
879
  }
879
880
 
@@ -902,7 +903,7 @@ router.post('/importjson/:id_faq_kb', roleChecker.hasRole('admin'), upload.singl
902
903
  // *******************************
903
904
  if (req.query.overwrite == "true") {
904
905
 
905
- let savingResult = await Faq.findOneAndUpdate({ id_faq_kb: id_faq_kb, intent_display_name: intent.intent_display_name }, new_faq, { new: true, upsert: true, rawResult: true }).catch((err) => {
906
+ let savingResult = await Faq.findOneAndUpdate({ id_faq_kb: chatbot._id, intent_display_name: intent.intent_display_name }, new_faq, { new: true, upsert: true, rawResult: true }).catch((err) => {
906
907
  winston.error("Unable to create faq: ", err);
907
908
  })
908
909
 
@@ -950,65 +951,69 @@ router.get('/exportjson/:id_faq_kb', roleChecker.hasRole('admin'), (req, res) =>
950
951
 
951
952
  winston.debug("exporting bot...")
952
953
 
953
- let id_faq_kb = req.params.id_faq_kb;
954
+ const chatbot_id = req.params.id_faq_kb;
955
+ const id_project = req.projectid;
954
956
 
955
- Faq_kb.findById(id_faq_kb, (err, faq_kb) => {
957
+ Faq_kb.findOne({ _id: chatbot_id, id_project: id_project }, (err, faq_kb) => {
956
958
  if (err) {
957
959
  winston.error('GET FAQ-KB ERROR ', err)
958
960
  return res.status(500).send({ success: false, msg: 'Error getting bot.' });
959
- } else {
960
- winston.debug('FAQ-KB: ', faq_kb)
961
+ }
962
+
963
+ if (!faq_kb) {
964
+ return res.status(404).send({ success: false, msg: "Chatbot not found with id " + chatbot_id + " for project " + id_project });
965
+ }
961
966
 
962
- faqService.getAll(id_faq_kb).then((faqs) => {
967
+ winston.debug('FAQ-KB: ', faq_kb)
963
968
 
964
- // delete from exclude map intent_id
965
- const intents = faqs.map(({ _id, id_project, topic, status, id_faq_kb, createdBy, createdAt, updatedAt, __v, ...keepAttrs }) => keepAttrs)
969
+ faqService.getAll(chatbot_id).then((faqs) => {
966
970
 
967
- if (!req.query.globals) {
968
- winston.verbose("Delete globals from attributes!")
969
- if (faq_kb.attributes) {
970
- delete faq_kb.attributes.globals;
971
- }
971
+ // delete from exclude map intent_id
972
+ const intents = faqs.map(({ _id, id_project, topic, status, chatbot_id, createdBy, createdAt, updatedAt, __v, ...keepAttrs }) => keepAttrs)
973
+
974
+ if (!req.query.globals) {
975
+ winston.verbose("Delete globals from attributes!")
976
+ if (faq_kb.attributes) {
977
+ delete faq_kb.attributes.globals;
972
978
  }
979
+ }
973
980
 
974
- let json = {
975
- webhook_enabled: faq_kb.webhook_enabled,
976
- webhook_url: faq_kb.webhook_url,
977
- language: faq_kb.language,
978
- name: faq_kb.name,
979
- slug: faq_kb.slug,
980
- type: faq_kb.type,
981
- subtype: faq_kb.subtype,
982
- description: faq_kb.description,
983
- attributes: faq_kb.attributes,
981
+ let json = {
982
+ webhook_enabled: faq_kb.webhook_enabled,
983
+ webhook_url: faq_kb.webhook_url,
984
+ language: faq_kb.language,
985
+ name: faq_kb.name,
986
+ slug: faq_kb.slug,
987
+ type: faq_kb.type,
988
+ subtype: faq_kb.subtype,
989
+ description: faq_kb.description,
990
+ attributes: faq_kb.attributes,
991
+ intents: intents
992
+ }
993
+
994
+ if (req.query.intentsOnly == 'true') {
995
+ let intents_obj = {
984
996
  intents: intents
985
997
  }
998
+ let intents_string = JSON.stringify(intents_obj);
999
+ res.set({ "Content-Disposition": "attachment; filename=\"intents.json\"" });
1000
+ return res.send(intents_string);
986
1001
 
987
- if (req.query.intentsOnly == 'true') {
988
- let intents_obj = {
989
- intents: intents
990
- }
991
- let intents_string = JSON.stringify(intents_obj);
992
- res.set({ "Content-Disposition": "attachment; filename=\"intents.json\"" });
993
- return res.send(intents_string);
994
-
995
- } else {
1002
+ } else {
996
1003
 
997
- // if (req.query.file == "false") {
998
- // return res.status(200).send(json);
999
- // }
1000
- let json_string = JSON.stringify(json);
1001
- res.set({ "Content-Disposition": "attachment; filename=\"bot.json\"" });
1002
- return res.send(json_string);
1003
- }
1004
+ // if (req.query.file == "false") {
1005
+ // return res.status(200).send(json);
1006
+ // }
1007
+ let json_string = JSON.stringify(json);
1008
+ res.set({ "Content-Disposition": "attachment; filename=\"bot.json\"" });
1009
+ return res.send(json_string);
1010
+ }
1004
1011
 
1005
- }).catch((err) => {
1006
- winston.error('GET FAQ ERROR: ', err)
1007
- return res.status(500).send({ success: false, msg: 'Error getting faqs.' });
1008
- })
1009
- }
1012
+ }).catch((err) => {
1013
+ winston.error('GET FAQ ERROR: ', err)
1014
+ return res.status(500).send({ success: false, msg: 'Error getting faqs.' });
1015
+ })
1010
1016
  })
1011
-
1012
1017
  })
1013
1018
 
1014
1019
  router.post('/:faq_kbid/training', roleChecker.hasRole('admin'), function (req, res) {