@tiledesk/tiledesk-server 2.10.3 → 2.10.5

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/faq_kb.js CHANGED
@@ -16,13 +16,14 @@ const faq = require('../models/faq');
16
16
  var jwt = require('jsonwebtoken');
17
17
  const uuidv4 = require('uuid/v4');
18
18
  const trainingService = require('../services/trainingService');
19
+ var roleChecker = require('../middleware/has-role');
20
+ const roleConstants = require('../models/roleConstants');
19
21
 
20
22
  let chatbot_templates_api_url = process.env.CHATBOT_TEMPLATES_API_URL
21
23
 
22
- router.post('/', async function (req, res) {
24
+ router.post('/', roleChecker.hasRole('admin'), async function (req, res) {
23
25
  winston.debug('create BOT ', req.body);
24
- //create(name, url, projectid, user_id, type, description, webhook_url, webhook_enabled, language, template)
25
- //faqService.create(req.body.name, req.body.url, req.projectid, req.user.id, req.body.type, req.body.description, undefined, undefined, req.body.language, req.body.template, req.body.mainCategory, req.body.intentsEngine).then(function (savedFaq_kb) {
26
+
26
27
  let quoteManager = req.app.get('quote_manager');
27
28
  let limits = await quoteManager.getPlanLimits(req.project);
28
29
  let chatbots_limit = limits.chatbots;
@@ -32,8 +33,8 @@ router.post('/', async function (req, res) {
32
33
  winston.debug("chatbots_count for project " + req.projectid + ": " + chatbots_count);
33
34
 
34
35
  if (chatbots_count >= chatbots_limit) {
35
- //return res.status(403).send({ success: false, error: "Maximum number of chatbots reached for the current plan", plan_limit: chatbots_limit })
36
36
  winston.info("Chatbots limit reached for project " + req.projectid + ". Block currently disabled.");
37
+ //return res.status(403).send({ success: false, error: "Maximum number of chatbots reached for the current plan", plan_limit: chatbots_limit })
37
38
  }
38
39
 
39
40
  faqService.create(req.body.name, req.body.url, req.projectid, req.user.id, req.body.type, req.body.description, req.body.webhook_url, req.body.webhook_enabled, req.body.language, req.body.template, req.body.mainCategory, req.body.intentsEngine, req.body.attributes).then(function (savedFaq_kb) {
@@ -42,8 +43,7 @@ router.post('/', async function (req, res) {
42
43
 
43
44
  });
44
45
 
45
-
46
- router.post('/train', function (req, res) {
46
+ router.post('/train', roleChecker.hasRole('admin'), function (req, res) {
47
47
 
48
48
  winston.info('train BOT ', req.body);
49
49
 
@@ -59,15 +59,12 @@ router.post('/train', function (req, res) {
59
59
  winston.debug('faq_kb.type :' + faq_kb.type);
60
60
  if (faq_kb.type == "internal" && faq_kb.url) {
61
61
 
62
-
63
-
64
62
  var train = {
65
63
  language: faq_kb.language,
66
64
  nlu: []
67
65
  };
68
66
  winston.info("train", train);
69
67
 
70
-
71
68
  var query = { "id_project": req.projectid, "id_faq_kb": req.body.id_faq_kb };
72
69
 
73
70
  Faq.find(query)
@@ -103,9 +100,8 @@ router.post('/train', function (req, res) {
103
100
  winston.error("error training", e);
104
101
  }
105
102
 
106
-
107
103
  return res.json({ train: train, httpResponse: trainHttp });
108
- // return res.json(trainHttp);
104
+
109
105
  } else {
110
106
  return res.status(400).send({ success: false, msg: 'no faq to train on external bot.' });
111
107
  }
@@ -116,34 +112,9 @@ router.post('/train', function (req, res) {
116
112
  }
117
113
 
118
114
  });
119
-
120
- /*
121
- {
122
- "language":"it",
123
- "nlu":[
124
- {
125
- "intent":"eta",
126
- "examples":[
127
- "quanti anni hai",
128
- "dimmi la tua età",
129
- "quanto sei grande",
130
- "parlami della tua età"
131
- ]
132
- },
133
- {
134
- "intent":"brutteparole",
135
- "examples":[
136
- "non dire parolacce",
137
- "le brutte parole non dovrebbero dirsi"
138
- ]
139
- }
140
- ]
141
- }
142
- */
143
-
144
115
  });
145
116
 
146
- router.post('/aitrain/', async (req, res) => {
117
+ router.post('/aitrain/', roleChecker.hasRole('admin'), async (req, res) => {
147
118
 
148
119
  let id_faq_kb = req.body.id_faq_kb;
149
120
  let webhook_enabled = req.body.webhook_enabled;
@@ -177,18 +148,13 @@ router.post('/aitrain/', async (req, res) => {
177
148
  return res.status(200).send({ success: false, message: "Trained not started", error: err });
178
149
  })
179
150
 
180
-
181
-
182
151
  } else {
183
152
  return res.status(200).send({ success: true, message: "Trained not started", reason: "Training available for intentsEngine equal to tiledesk-ai only" })
184
153
  }
185
154
  })
186
-
187
-
188
155
  })
189
156
 
190
-
191
- router.post('/askbot', function (req, res) {
157
+ router.post('/askbot', roleChecker.hasRole('admin'), function (req, res) {
192
158
 
193
159
  winston.debug('ASK BOT ', req.body);
194
160
 
@@ -203,10 +169,6 @@ router.post('/askbot', function (req, res) {
203
169
  winston.debug('faq_kb.type :' + faq_kb.type);
204
170
  if (faq_kb.type == "internal" || faq_kb.type == "tilebot") {
205
171
 
206
-
207
-
208
-
209
-
210
172
  var query = { "id_project": req.projectid, "id_faq_kb": req.body.id_faq_kb, "question": req.body.question };
211
173
 
212
174
  Faq.find(query)
@@ -223,7 +185,6 @@ router.post('/askbot', function (req, res) {
223
185
  });
224
186
  var result = { hits: faqs };
225
187
 
226
-
227
188
  res.json(result);
228
189
  } else {
229
190
  query = { "id_project": req.projectid, "id_faq_kb": req.body.id_faq_kb };
@@ -235,8 +196,6 @@ router.post('/askbot', function (req, res) {
235
196
  }
236
197
  query.$text = search_obj;
237
198
  winston.debug("fulltext search query", query);
238
-
239
-
240
199
  winston.debug('internal ft query: ' + query);
241
200
 
242
201
  Faq.find(query, { score: { $meta: "textScore" } })
@@ -253,27 +212,16 @@ router.post('/askbot', function (req, res) {
253
212
  var result = { hits: faqs };
254
213
  res.json(result);
255
214
  });
256
-
257
-
258
215
  }
259
-
260
-
261
216
  });
262
-
263
-
264
217
  } else {
265
218
  winston.debug('external query: ');
266
219
  return res.status(400).send({ success: false, msg: 'askbot on external bot.' });
267
220
  }
268
-
269
-
270
221
  });
271
-
272
222
  });
273
223
 
274
-
275
-
276
- router.put('/:faq_kbid/publish', async (req, res) => {
224
+ router.put('/:faq_kbid/publish', roleChecker.hasRole('admin'), async (req, res) => {
277
225
 
278
226
  let id_faq_kb = req.params.faq_kbid;
279
227
  winston.debug('id_faq_kb: ' + id_faq_kb);
@@ -289,19 +237,17 @@ router.put('/:faq_kbid/publish', async (req, res) => {
289
237
  let cs = req.app.get('chatbot_service')
290
238
 
291
239
  try {
292
- // fork(id_faq_kb, api_url, token, project_id)
240
+ // fork(id_faq_kb, api_url, token, project_id)
293
241
  let forked = await cs.fork(id_faq_kb, api_url, token, current_project_id);
294
- // winston.debug("forked: ", forked)
242
+ // winston.debug("forked: ", forked)
295
243
 
296
244
  let forkedChatBotId = forked.bot_id;
297
245
  winston.debug("forkedChatBotId: "+forkedChatBotId);
298
246
 
299
-
300
247
  let updatedForkedChabot = await Faq_kb.findByIdAndUpdate(forkedChatBotId, {trashed: true, publishedBy: req.user.id, publishedAt: new Date().getTime()}, { new: true, upsert: true }).exec();
301
248
  winston.debug("updatedForkedChabot: ",updatedForkedChabot);
302
249
  botEvent.emit('faqbot.update', updatedForkedChabot);
303
250
 
304
-
305
251
  const port = process.env.PORT || '3000';
306
252
  const TILEBOT_ENDPOINT = process.env.TILEBOT_ENDPOINT || "http://localhost:" + port+ "/modules/tilebot/ext/";
307
253
  winston.debug("TILEBOT_ENDPOINT: " + TILEBOT_ENDPOINT);
@@ -311,23 +257,15 @@ router.put('/:faq_kbid/publish', async (req, res) => {
311
257
 
312
258
  botEvent.emit('faqbot.update', updatedOriginalChabot);
313
259
 
314
-
315
260
  return res.status(200).send({ message: "Chatbot published successfully", bot_id: forkedChatBotId });
316
261
 
317
262
  } catch(e) {
318
263
  winston.error("Error Unable publish chatbot: ", e);
319
264
  return res.status(500).send({ success: false, message: "Unable publish chatbot" });
320
265
  }
321
-
322
-
323
-
324
-
325
266
  });
326
267
 
327
-
328
-
329
-
330
- router.put('/:faq_kbid', function (req, res) {
268
+ router.put('/:faq_kbid', roleChecker.hasRoleOrTypes('admin', ['bot','subscription']), function (req, res) {
331
269
 
332
270
  winston.debug(req.body);
333
271
 
@@ -385,10 +323,12 @@ router.put('/:faq_kbid', function (req, res) {
385
323
  if (req.body.certifiedTags != undefined) {
386
324
  update.certifiedTags = req.body.certifiedTags
387
325
  }
388
- // update._id = req.params.faq_kbid;
326
+
327
+ if (req.body.agents_visible != undefined) {
328
+ update.agents_visible = req.body.agents_visible
329
+ }
389
330
 
390
331
  winston.debug("update", update);
391
- // "$set": req.params.faq_kbid
392
332
 
393
333
  Faq_kb.findByIdAndUpdate(req.params.faq_kbid, update, { new: true, upsert: true }, function (err, updatedFaq_kb) { //TODO add cache_bot_here
394
334
  if (err) {
@@ -400,7 +340,7 @@ router.put('/:faq_kbid', function (req, res) {
400
340
  });
401
341
  });
402
342
 
403
- router.put('/:faq_kbid/language/:language', (req, res) => {
343
+ router.put('/:faq_kbid/language/:language', roleChecker.hasRoleOrTypes('admin', ['bot','subscription']), (req, res) => {
404
344
 
405
345
  winston.debug("update language: ", req.params);
406
346
 
@@ -427,18 +367,10 @@ router.put('/:faq_kbid/language/:language', (req, res) => {
427
367
 
428
368
  })
429
369
 
430
-
431
-
432
-
433
-
434
-
435
-
436
-
437
- router.patch('/:faq_kbid/attributes', function (req, res) { //TODO add cache_bot_here
370
+ router.patch('/:faq_kbid/attributes', roleChecker.hasRoleOrTypes('admin', ['bot','subscription']), function (req, res) { //TODO add cache_bot_here
438
371
  var data = req.body;
439
372
 
440
373
  // TODO use service method
441
-
442
374
  Faq_kb.findById(req.params.faq_kbid, function (err, updatedBot) {
443
375
  if (err) {
444
376
  winston.error('--- > ERROR ', err);
@@ -481,22 +413,23 @@ router.patch('/:faq_kbid/attributes', function (req, res) { //TODO add cache_b
481
413
 
482
414
  });
483
415
 
484
- router.delete('/:faq_kbid', function (req, res) {
416
+ router.delete('/:faq_kbid', roleChecker.hasRole('admin'), function (req, res) {
485
417
 
486
418
  winston.debug(req.body);
487
419
 
488
-
489
420
  Faq_kb.remove({ _id: req.params.faq_kbid }, function (err, faq_kb) {
490
421
  if (err) {
491
422
  return res.status(500).send({ success: false, msg: 'Error deleting object.' });
492
423
  }
493
- botEvent.emit('faqbot.delete', faq_kb);
494
- res.json(faq_kb);
424
+ /**
425
+ * WARNING: faq_kb is the operation result, not the faq_kb object. The event subscriber will not receive the object as expected.
426
+ */
427
+ botEvent.emit('faqbot.delete', faq_kb);
428
+ res.status(200).send({ success: true, message: "Chatbot with id " + req.params.faq_kbid + " deleted successfully"})
495
429
  });
496
430
  });
497
431
 
498
-
499
- router.get('/:faq_kbid', function (req, res) {
432
+ router.get('/:faq_kbid', roleChecker.hasRoleOrTypes('admin', ['bot','subscription']), function (req, res) {
500
433
 
501
434
  winston.debug(req.query);
502
435
 
@@ -541,9 +474,7 @@ router.get('/:faq_kbid', function (req, res) {
541
474
  });
542
475
  });
543
476
 
544
-
545
-
546
- router.get('/:faq_kbid/jwt', function (req, res) {
477
+ router.get('/:faq_kbid/jwt', roleChecker.hasRoleOrTypes('admin', ['bot','subscription']), function (req, res) {
547
478
 
548
479
  winston.debug(req.query);
549
480
 
@@ -555,7 +486,6 @@ router.get('/:faq_kbid/jwt', function (req, res) {
555
486
  return res.status(404).send({ success: false, msg: 'Object not found.' });
556
487
  }
557
488
 
558
-
559
489
  var signOptions = {
560
490
  issuer: 'https://tiledesk.com',
561
491
  subject: 'bot',
@@ -577,20 +507,28 @@ router.get('/:faq_kbid/jwt', function (req, res) {
577
507
 
578
508
  var token = jwt.sign(botPayload, botSecret, signOptions);
579
509
 
580
-
581
510
  res.json({"jwt":token});
582
-
583
511
  });
584
512
  });
585
513
 
514
+ /**
515
+ * This endpoint should be the only one reachble with role agent.
516
+ * If the role is agent the response must contain only _id, name, or other non relevant info.
517
+ */
518
+ router.get('/', roleChecker.hasRoleOrTypes('agent', ['bot','subscription']), function (req, res) {
586
519
 
587
- // NEW - GET ALL FAQKB WITH THE PASSED PROJECT ID
588
- router.get('/', function (req, res) {
589
520
 
590
521
  winston.debug("req.query", req.query);
522
+ winston.debug("GET FAQ-KB req projectid", req.projectid);
523
+
524
+ let restricted_mode = false;
525
+
526
+ let project_user = req.projectuser;
527
+ if (project_user && project_user.role === roleConstants.AGENT) {
528
+ restricted_mode = true;
529
+ }
591
530
 
592
531
 
593
- winston.debug("GET FAQ-KB req projectid", req.projectid);
594
532
  /**
595
533
  * if filter only for 'trashed = false',
596
534
  * the bots created before the implementation of the 'trashed' property are not returned
@@ -616,24 +554,27 @@ router.get('/', function (req, res) {
616
554
 
617
555
  if (req.query.certified) {
618
556
  query.certified = req.query.certified;
619
- }
620
-
557
+ }
621
558
 
622
559
  winston.debug("query", query);
623
560
 
624
- Faq_kb.find(query, function (err, faq_kb) { //TODO add cache_bot_here
561
+ Faq_kb.find(query).lean().exec((err, faq_kbs) => {
625
562
  if (err) {
626
563
  winston.error('GET FAQ-KB ERROR ', err)
627
564
  return res.status(500).send({ success: false, message: "Unable to get chatbots" });
628
565
  }
566
+
567
+ if (restricted_mode === true) {
568
+ // Returns only: _id, name, id_project, language
569
+ faq_kbs = faq_kbs.map(({ webhook_enabled, type, public, certified, intentsEngine, tags, score, url, attributes, trained, certifiedTags, trashed, createdBy, createdAt, updatedAt, __v, ...keepAttrs }) => keepAttrs)
570
+ }
571
+
572
+ res.json(faq_kbs)
629
573
 
630
- res.json(faq_kb);
631
-
632
- });
633
-
574
+ })
634
575
  });
635
576
 
636
- router.post('/fork/:id_faq_kb', async (req, res) => {
577
+ router.post('/fork/:id_faq_kb', roleChecker.hasRole('admin'), async (req, res) => {
637
578
 
638
579
  let id_faq_kb = req.params.id_faq_kb;
639
580
  winston.debug('id_faq_kb: ' + id_faq_kb);
@@ -653,7 +594,6 @@ router.post('/fork/:id_faq_kb', async (req, res) => {
653
594
  let globals = req.query.globals;
654
595
  winston.debug("export globals " + globals);
655
596
 
656
-
657
597
  let token = req.headers.authorization;
658
598
 
659
599
  let cs = req.app.get('chatbot_service')
@@ -689,7 +629,7 @@ router.post('/fork/:id_faq_kb', async (req, res) => {
689
629
 
690
630
  })
691
631
 
692
- router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), async (req, res) => {
632
+ router.post('/importjson/:id_faq_kb', roleChecker.hasRole('admin'), upload.single('uploadFile'), async (req, res) => {
693
633
 
694
634
  let id_faq_kb = req.params.id_faq_kb;
695
635
  winston.debug('import on id_faq_kb: ' + id_faq_kb);
@@ -921,9 +861,7 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), async (req, r
921
861
  faqBotEvent.emit('faq.create', faq);
922
862
  }
923
863
  }
924
-
925
864
  })
926
-
927
865
  }
928
866
 
929
867
  if (updatedChatbot) {
@@ -931,326 +869,10 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), async (req, r
931
869
  } else {
932
870
  return res.send(chatbot);
933
871
  }
934
-
935
872
  }
936
-
937
873
  })
938
874
 
939
- // OLD
940
- // router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), async (req, res) => {
941
-
942
- // let id_faq_kb = req.params.id_faq_kb;
943
- // winston.debug('import on id_faq_kb: ' + id_faq_kb);
944
-
945
- // let json_string;
946
- // let json;
947
- // if (req.file) {
948
- // json_string = req.file.buffer.toString('utf-8');
949
- // json = JSON.parse(json_string);
950
- // } else {
951
- // json = req.body;
952
- // }
953
-
954
- // winston.debug("json source " + json_string)
955
-
956
- // // intentOnly still existing?
957
- // if (req.query.intentsOnly && req.query.intentsOnly == "true") {
958
-
959
- // winston.debug("intents only")
960
-
961
- // await json.intents.forEach((intent) => {
962
-
963
- // let new_faq = {
964
- // id_faq_kb: id_faq_kb,
965
- // id_project: req.projectid,
966
- // createdBy: req.user.id,
967
- // intent_display_name: intent.intent_display_name,
968
- // intent_id: intent.intent_id,
969
- // question: intent.question,
970
- // answer: intent.answer,
971
- // reply: intent.reply,
972
- // form: intent.form,
973
- // enabled: intent.enabled,
974
- // webhook_enabled: intent.webhook_enabled,
975
- // language: intent.language,
976
- // actions: intent.actions,
977
- // attributes: intent.attributes
978
- // }
979
-
980
- // // overwrite duplicated intents
981
- // if (req.query.overwrite == "true") {
982
- // Faq.findOneAndUpdate({ id_faq_kb: id_faq_kb, intent_display_name: intent.intent_display_name }, new_faq, { new: true, upsert: true, rawResult: true }, (err, savingResult) => {
983
- // if (err) {
984
- // winston.error("findOneAndUpdate (upsert) FAQ ERROR ", err);
985
- // } else {
986
- // if (savingResult.lastErrorObject.updatedExisting == true) {
987
- // winston.debug("updated existing intent")
988
- // faqBotEvent.emit('faq.update', savingResult.value);
989
- // } else {
990
- // winston.debug("new intent created")
991
- // faqBotEvent.emit('faq.create', savingResult.value);
992
- // }
993
- // }
994
- // })
995
-
996
- // // don't overwrite duplicated intents
997
- // } else {
998
- // Faq.create(new_faq, (err, savedFaq) => {
999
- // if (err) {
1000
- // winston.debug("create new FAQ ERROR ", err);
1001
- // if (err.code == 11000) {
1002
- // winston.error("Duplicate intent_display_name.");
1003
- // winston.debug("Skip duplicated intent_display_name");
1004
- // } else {
1005
- // winston.debug("new intent created")
1006
- // faqBotEvent.emit('faq.create', savedFaq);
1007
- // }
1008
- // }
1009
- // })
1010
- // }
1011
-
1012
- // })
1013
- // //faqBotEvent.emit('faq_train.importedall', id_faq_kb);
1014
- // return res.status(200).send({ success: true, msg: "Intents imported successfully" })
1015
-
1016
- // } else {
1017
-
1018
- // // ****************************
1019
- // // **** CREATE TRUE option ****
1020
- // // ****************************
1021
- // if (req.query.create && req.query.create == 'true') {
1022
-
1023
-
1024
- // faqService.create(json.name, undefined, req.projectid, req.user.id, "tilebot", json.description, json.webhook_url, json.webhook_enabled, json.language, undefined, undefined, undefined, json.attributes).then( async (savedFaq_kb) => {
1025
- // winston.debug("saved (and imported) faq kb: ", savedFaq_kb);
1026
-
1027
- // // edit attributes.rules
1028
- // let attributes = json.attributes;
1029
- // if (attributes &&
1030
- // attributes.rules &&
1031
- // attributes.rules.length > 0) {
1032
-
1033
- // await attributes.rules.forEach((rule) => {
1034
- // if (rule.do &&
1035
- // rule.do[0] &&
1036
- // rule.do[0].message &&
1037
- // rule.do[0].message.participants &&
1038
- // rule.do[0].message.participants[0]) {
1039
- // rule.do[0].message.participants[0] = "bot_" + savedFaq_kb._id
1040
- // winston.debug("attributes rule new participant: ", rule.do[0].message.participants[0])
1041
- // }
1042
- // })
1043
- // }
1044
- // let chatbot_edited = { attributes: attributes };
1045
- // Faq_kb.findByIdAndUpdate(savedFaq_kb._id, chatbot_edited, { new: true }, (err, savedEditedFaq_kb) => {
1046
- // if (err) {
1047
- // winston.error("error during saving edited faq_kb: ", err)
1048
- // }
1049
- // botEvent.emit('faqbot.create', savedFaq_kb);
1050
-
1051
- // if (json.intents) {
1052
-
1053
- // json.intents.forEach((intent) => {
1054
-
1055
- // let new_faq = {
1056
- // id_faq_kb: savedFaq_kb._id,
1057
- // id_project: req.projectid,
1058
- // createdBy: req.user.id,
1059
- // intent_display_name: intent.intent_display_name,
1060
- // intent_id: intent.intent_id,
1061
- // question: intent.question,
1062
- // answer: intent.answer,
1063
- // reply: intent.reply,
1064
- // form: intent.form,
1065
- // enabled: intent.enabled,
1066
- // webhook_enabled: intent.webhook_enabled,
1067
- // language: intent.language,
1068
- // actions: intent.actions,
1069
- // attributes: intent.attributes
1070
- // }
1071
-
1072
- // // TO DELETE: no used when req.query.create = 'true'
1073
- // if (req.query.overwrite == "true") {
1074
- // Faq.findOneAndUpdate({ id_faq_kb: id_faq_kb, intent_display_name: intent.intent_display_name }, new_faq, { new: true, upsert: true, rawResult: true }, (err, savingResult) => {
1075
- // if (err) {
1076
- // winston.error("findOneAndUpdate (upsert) FAQ ERROR ", err);
1077
- // } else {
1078
-
1079
- // if (savingResult.lastErrorObject.updatedExisting == true) {
1080
- // winston.debug("updated existing intent")
1081
- // faqBotEvent.emit('faq.update', savingResult.value);
1082
- // } else {
1083
- // winston.debug("new intent created")
1084
- // faqBotEvent.emit('faq.create', savingResult.value);
1085
- // }
1086
-
1087
- // }
1088
-
1089
- // })
1090
- // // don't overwrite duplicated intents
1091
- // } else {
1092
- // Faq.create(new_faq, (err, savedFaq) => {
1093
- // if (err) {
1094
- // winston.debug("create new FAQ ERROR ", err);
1095
- // if (err.code == 11000) {
1096
- // winston.error("Duplicate intent_display_name.");
1097
- // winston.debug("Skip duplicated intent_display_name");
1098
- // } else {
1099
- // winston.debug("new intent created")
1100
- // faqBotEvent.emit('faq.create', savedFaq);
1101
- // }
1102
- // }
1103
- // })
1104
- // }
1105
-
1106
- // })
1107
- // }
1108
- // //faqBotEvent.emit('faq_train.importedall', savedEditedFaq_kb._id);
1109
- // return res.status(200).send(savedEditedFaq_kb);
1110
- // })
1111
-
1112
- // }).catch((err) => {
1113
- // winston.error("error saving faq_kb: ", err);
1114
- // return res.status(500).send(err);
1115
- // })
1116
-
1117
- // }
1118
- // // ****************************
1119
- // // **** CREATE FALSE option ****
1120
- // // ****************************
1121
- // else {
1122
-
1123
- // Faq_kb.findById(id_faq_kb, async (err, faq_kb) => {
1124
- // if (err) {
1125
- // winston.error("GET FAQ-KB ERROR", err);
1126
- // return res.status(500).send({ success: false, msg: "Error getting bot." });
1127
- // }
1128
- // if (!faq_kb) {
1129
- // return res.status(404).send({ success: false, msg: 'Bot not found.' });
1130
- // }
1131
-
1132
- // // should be wrong
1133
- // //const json = JSON.parse(json_string);
1134
-
1135
- // if (json.webhook_enabled) {
1136
- // faq_kb.webhook_enabled = json.webhook_enabled;
1137
- // }
1138
- // if (json.webhook_url) {
1139
- // faq_kb.webhook_url = json.webhook_url;
1140
- // }
1141
- // if (json.language) {
1142
- // faq_kb.language = json.language;
1143
- // }
1144
- // if (json.name) {
1145
- // faq_kb.name = json.name;
1146
- // }
1147
- // if (json.description) {
1148
- // faq_kb.description = json.description;
1149
- // }
1150
-
1151
- // if (json.attributes) {
1152
- // let attributes = json.attributes;
1153
- // if (attributes.rules &&
1154
- // attributes.rules.length > 0) {
1155
- // await attributes.rules.forEach((rule) => {
1156
- // if (rule.do &&
1157
- // rule.do[0] &&
1158
- // rule.do[0].message &&
1159
- // rule.do[0].message.participants &&
1160
- // rule.do[0].message.participants[0]) {
1161
- // rule.do[0].message.participants[0] = "bot_" + faq_kb._id
1162
- // winston.debug("attributes rule new participant: " + rule.do[0].message.participants[0])
1163
- // }
1164
- // })
1165
- // faq_kb.attributes = json.attributes;
1166
- // }
1167
- // }
1168
- // // if (json.intentsEngine) {
1169
- // // faq_kb.intentsEngine = json.intentsEngine;
1170
- // // }
1171
-
1172
-
1173
- // Faq_kb.findByIdAndUpdate(id_faq_kb, faq_kb, { new: true }, async (err, updatedFaq_kb) => { //TODO add cache_bot_here
1174
- // if (err) {
1175
- // return res.status(500).send({ success: false, msg: "Error updating bot." });
1176
- // }
1177
-
1178
- // botEvent.emit('faqbot.update', updatedFaq_kb);
1179
-
1180
- // if (json.intents) {
1181
- // await json.intents.forEach((intent) => {
1182
-
1183
- // let new_faq = {
1184
- // id_faq_kb: updatedFaq_kb._id,
1185
- // id_project: req.projectid,
1186
- // createdBy: req.user.id,
1187
- // intent_display_name: intent.intent_display_name,
1188
- // intent_id: intent.intent_id,
1189
- // question: intent.question,
1190
- // answer: intent.answer,
1191
- // reply: intent.reply,
1192
- // form: intent.form,
1193
- // enabled: intent.enabled,
1194
- // webhook_enabled: intent.webhook_enabled,
1195
- // language: intent.language,
1196
- // actions: intent.actions,
1197
- // attributes: intent.attributes
1198
- // }
1199
-
1200
- // // *******************************
1201
- // // **** OVERWRITE TRUE option ****
1202
- // // *******************************
1203
- // if (req.query.overwrite == "true") {
1204
- // Faq.findOneAndUpdate({ id_faq_kb: id_faq_kb, intent_display_name: intent.intent_display_name }, new_faq, { new: true, upsert: true, rawResult: true }, (err, savingResult) => {
1205
- // if (err) {
1206
- // winston.error("findOneAndUpdate (upsert) FAQ ERROR ", err);
1207
- // } else {
1208
-
1209
- // if (savingResult.lastErrorObject.updatedExisting == true) {
1210
- // winston.info("updated existing intent")
1211
- // faqBotEvent.emit('faq.update', savingResult.value);
1212
- // } else {
1213
- // winston.info("new intent created")
1214
- // faqBotEvent.emit('faq.create', savingResult.value);
1215
- // }
1216
-
1217
- // }
1218
-
1219
- // })
1220
-
1221
- // // ********************************
1222
- // // **** OVERWRITE FALSE option ****
1223
- // // ********************************
1224
- // } else {
1225
- // Faq.create(new_faq, (err, savedFaq) => {
1226
- // if (err) {
1227
- // winston.debug("create new FAQ ERROR ", err);
1228
- // if (err.code == 11000) {
1229
- // winston.error("Duplicate intent_display_name.");
1230
- // winston.info("Skip duplicated intent_display_name");
1231
- // } else {
1232
- // winston.info("new intent created")
1233
- // faqBotEvent.emit('faq.create', savedFaq);
1234
- // }
1235
- // }
1236
- // })
1237
- // }
1238
-
1239
- // })
1240
-
1241
- // }
1242
- // //faqBotEvent.emit('faq_train.importedall', id_faq_kb);
1243
- // return res.send(updatedFaq_kb);
1244
-
1245
- // })
1246
-
1247
- // })
1248
- // }
1249
-
1250
- // }
1251
- // })
1252
-
1253
- router.get('/exportjson/:id_faq_kb', (req, res) => {
875
+ router.get('/exportjson/:id_faq_kb', roleChecker.hasRole('admin'), (req, res) => {
1254
876
 
1255
877
  winston.debug("exporting bot...")
1256
878
 
@@ -1304,7 +926,6 @@ router.get('/exportjson/:id_faq_kb', (req, res) => {
1304
926
  return res.send(json_string);
1305
927
  }
1306
928
 
1307
-
1308
929
  }).catch((err) => {
1309
930
  winston.error('GET FAQ ERROR: ', err)
1310
931
  return res.status(500).send({ success: false, msg: 'Error getting faqs.' });
@@ -1314,10 +935,7 @@ router.get('/exportjson/:id_faq_kb', (req, res) => {
1314
935
 
1315
936
  })
1316
937
 
1317
-
1318
-
1319
-
1320
- router.post('/:faq_kbid/training', function (req, res) {
938
+ router.post('/:faq_kbid/training', roleChecker.hasRole('admin'), function (req, res) {
1321
939
 
1322
940
  winston.debug(req.body);
1323
941
  winston.info(req.params.faq_kbid + "/training called" );
@@ -1339,7 +957,4 @@ router.post('/:faq_kbid/training', function (req, res) {
1339
957
  });
1340
958
  });
1341
959
 
1342
-
1343
-
1344
-
1345
960
  module.exports = router;