@tiledesk/tiledesk-server 2.9.13 → 2.9.14

Sign up to get free protection for your applications and to get access to all the features.
package/routes/faq_kb.js CHANGED
@@ -689,12 +689,15 @@ router.post('/fork/:id_faq_kb', async (req, res) => {
689
689
 
690
690
  })
691
691
 
692
-
693
692
  router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), async (req, res) => {
694
693
 
695
694
  let id_faq_kb = req.params.id_faq_kb;
696
695
  winston.debug('import on id_faq_kb: ' + id_faq_kb);
697
-
696
+
697
+ winston.debug('import with option create: ' + req.query.create);
698
+ winston.debug('import with option replace: ' + req.query.replace);
699
+ winston.debug('import with option overwrite: ' + req.query.overwrite);
700
+
698
701
  let json_string;
699
702
  let json;
700
703
  if (req.file) {
@@ -706,291 +709,546 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), async (req, r
706
709
 
707
710
  winston.debug("json source " + json_string)
708
711
 
709
- // intentOnly still existing?
710
- if (req.query.intentsOnly && req.query.intentsOnly == "true") {
711
-
712
- winston.debug("intents only")
713
-
714
- await json.intents.forEach((intent) => {
715
-
716
- let new_faq = {
717
- id_faq_kb: id_faq_kb,
718
- id_project: req.projectid,
719
- createdBy: req.user.id,
720
- intent_display_name: intent.intent_display_name,
721
- intent_id: intent.intent_id,
722
- question: intent.question,
723
- answer: intent.answer,
724
- reply: intent.reply,
725
- form: intent.form,
726
- enabled: intent.enabled,
727
- webhook_enabled: intent.webhook_enabled,
728
- language: intent.language,
729
- actions: intent.actions,
730
- attributes: intent.attributes
731
- }
712
+ // ****************************
713
+ // **** CREATE TRUE option ****
714
+ // ****************************
715
+ if (req.query.create === 'true') {
716
+ let savedChatbot = await 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)
717
+ .catch((err) => {
718
+ winston.error("Error creating new chatbot")
719
+ return res.status(400).send({ succes: false, message: "Error creatings new chatbot", error: err })
720
+ })
721
+
722
+ // Edit attributes.rules
723
+ let attributes = json.attributes;
724
+ if (attributes &&
725
+ attributes.rules &&
726
+ attributes.rules.length > 0) {
727
+
728
+ await attributes.rules.forEach((rule) => {
729
+ if (rule.do &&
730
+ rule.do[0] &&
731
+ rule.do[0].message &&
732
+ rule.do[0].message.participants &&
733
+ rule.do[0].message.participants[0]) {
734
+ rule.do[0].message.participants[0] = "bot_" + savedChatbot._id
735
+ winston.debug("attributes rule new participant: ", rule.do[0].message.participants[0])
736
+ }
737
+ })
738
+ }
739
+
740
+ let chatbot_edited = { attributes: attributes };
732
741
 
733
- // overwrite duplicated intents
734
- if (req.query.overwrite == "true") {
735
- 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) => {
736
- if (err) {
737
- winston.error("findOneAndUpdate (upsert) FAQ ERROR ", err);
742
+ let updatedChatbot = await Faq_kb.findByIdAndUpdate(savedChatbot._id, chatbot_edited, { new: true }).catch((err) => {
743
+ winston.error("Error updating chatbot attributes: ", err);
744
+ winston.debug("Skip Error updating chatbot attributes: ", err);
745
+ // return res.status(400).send({ success: false, message: "Error updating chatbot attributes", error: err })
746
+ })
747
+
748
+ botEvent.emit('faqbot.create', savedChatbot);
749
+
750
+ if (json.intents) {
751
+
752
+ json.intents.forEach( async (intent) => {
753
+
754
+ let new_faq = {
755
+ id_faq_kb: savedChatbot._id,
756
+ id_project: req.projectid,
757
+ createdBy: req.user.id,
758
+ intent_display_name: intent.intent_display_name,
759
+ intent_id: intent.intent_id,
760
+ question: intent.question,
761
+ answer: intent.answer,
762
+ reply: intent.reply,
763
+ form: intent.form,
764
+ enabled: intent.enabled,
765
+ webhook_enabled: intent.webhook_enabled,
766
+ language: intent.language,
767
+ actions: intent.actions,
768
+ attributes: intent.attributes
769
+ }
770
+
771
+ let faq = await Faq.create(new_faq).catch((err) => {
772
+ if (err.code == 11000) { // should never happen
773
+ winston.error("Duplicate intent_display_name.");
774
+ winston.debug("Skip duplicated intent_display_name"); // Don't stop the flow
738
775
  } else {
739
- if (savingResult.lastErrorObject.updatedExisting == true) {
740
- winston.debug("updated existing intent")
776
+ winston.error("Error saving new faq: ", err)
777
+ }
778
+ })
779
+
780
+ if (faq) {
781
+ winston.debug("new intent created")
782
+ faqBotEvent.emit('faq.create', savedFaq);
783
+ }
784
+ })
785
+ }
786
+
787
+ if (updatedChatbot) {
788
+ return res.status(200).send(updatedChatbot)
789
+ } else {
790
+ return res.status(200).send(savedChatbot)
791
+ }
792
+ }
793
+ // *****************************
794
+ // **** CREATE FALSE option ****
795
+ // *****************************
796
+ else {
797
+
798
+ if (!id_faq_kb) {
799
+ return res.status(400).send({ success: false, message: "With replace or overwrite option a id_faq_kb must be provided" })
800
+ }
801
+
802
+ let chatbot = Faq_kb.findById(id_faq_kb).catch((err) => {
803
+ winston.error("Error finding chatbot with id " + id_faq_kb);
804
+ return res.status(404).send({ success: false, message: "Error finding chatbot with id " + id_faq_kb, error: err });
805
+ })
806
+
807
+ if (!chatbot) {
808
+ winston.error("Chatbot not found with id " + id_faq_kb);
809
+ return res.status(404).send({ success: false, message: "Chatbot not found with id " + id_faq_kb });
810
+ }
811
+
812
+ if (json.webhook_enabled) {
813
+ chatbot.webhook_enabled = json.webhook_enabled;
814
+ }
815
+ if (json.webhook_url) {
816
+ chatbot.webhook_url = json.webhook_url;
817
+ }
818
+ if (json.language) {
819
+ chatbot.language = json.language;
820
+ }
821
+ if (json.name) {
822
+ chatbot.name = json.name;
823
+ }
824
+ if (json.description) {
825
+ chatbot.description = json.description;
826
+ }
827
+
828
+ if (json.attributes) {
829
+ let attributes = json.attributes;
830
+ if (attributes.rules &&
831
+ attributes.rules.length > 0) {
832
+ await attributes.rules.forEach((rule) => {
833
+ if (rule.do &&
834
+ rule.do[0] &&
835
+ rule.do[0].message &&
836
+ rule.do[0].message.participants &&
837
+ rule.do[0].message.participants[0]) {
838
+ rule.do[0].message.participants[0] = "bot_" + chatbot._id
839
+ winston.debug("attributes rule new participant: " + rule.do[0].message.participants[0])
840
+ }
841
+ })
842
+ chatbot.attributes = json.attributes;
843
+ }
844
+ }
845
+
846
+ let updatedChatbot = await Faq_kb.findByIdAndUpdate(id_faq_kb, chatbot, { new: true }).catch((err) => {
847
+ winston.error("Error updating chatbot");
848
+ return res.status(400).send({ success: false, message: "Error updating chatbot", error: err })
849
+ })
850
+
851
+ botEvent.emit('faqbot.update', updatedChatbot);
852
+
853
+ // *****************************
854
+ // **** REPLACE TRUE option ****
855
+ // *****************************
856
+ if (req.query.replace === 'true') {
857
+ let result = await Faq.deleteMany({ id_faq_kb: id_faq_kb }).catch((err) => {
858
+ winston.error("Unable to delete all existing faqs with id_faq_kb " + id_faq_kb);
859
+ })
860
+
861
+ winston.verbose("All faq for chatbot " + id_faq_kb + " deleted successfully")
862
+ winston.debug("DeleteMany faqs result ", result);
863
+ }
864
+
865
+ if (json.intents) {
866
+ await json.intents.forEach( async (intent) => {
867
+
868
+ let new_faq = {
869
+ id_faq_kb: updatedChatbot._id,
870
+ id_project: req.projectid,
871
+ createdBy: req.user.id,
872
+ intent_display_name: intent.intent_display_name,
873
+ intent_id: intent.intent_id,
874
+ question: intent.question,
875
+ answer: intent.answer,
876
+ reply: intent.reply,
877
+ form: intent.form,
878
+ enabled: intent.enabled,
879
+ webhook_enabled: intent.webhook_enabled,
880
+ language: intent.language,
881
+ actions: intent.actions,
882
+ attributes: intent.attributes
883
+ }
884
+
885
+ // *******************************
886
+ // **** OVERWRITE TRUE option ****
887
+ // *******************************
888
+ if (req.query.overwrite == "true") {
889
+
890
+ 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) => {
891
+ winston.error("Unable to create faq: ", err);
892
+ })
893
+
894
+ if (savingResult) {
895
+ if (savingResult.lastErrorObject.updatedExisting === true) {
896
+ winston.info("updated existing intent")
741
897
  faqBotEvent.emit('faq.update', savingResult.value);
742
898
  } else {
743
- winston.debug("new intent created")
899
+ winston.info("new intent created")
744
900
  faqBotEvent.emit('faq.create', savingResult.value);
745
901
  }
746
902
  }
747
- })
748
903
 
749
- // don't overwrite duplicated intents
750
- } else {
751
- Faq.create(new_faq, (err, savedFaq) => {
752
- if (err) {
753
- winston.debug("create new FAQ ERROR ", err);
904
+ // ********************************
905
+ // **** OVERWRITE FALSE option ****
906
+ // ********************************
907
+ } else {
908
+
909
+ let faq = await Faq.create(new_faq).catch((err) => {
754
910
  if (err.code == 11000) {
755
911
  winston.error("Duplicate intent_display_name.");
756
- winston.debug("Skip duplicated intent_display_name");
912
+ winston.info("Skip duplicated intent_display_name");
757
913
  } else {
758
- winston.debug("new intent created")
759
- faqBotEvent.emit('faq.create', savedFaq);
914
+ winston.error("Error creating new intent: ", err);
760
915
  }
916
+ winston.error("Error creating new intent: ", err);
917
+ })
918
+
919
+ if (faq) {
920
+ winston.info("new intent created")
921
+ faqBotEvent.emit('faq.create', savedFaq);
761
922
  }
762
- })
763
- }
923
+ }
764
924
 
765
- })
766
- //faqBotEvent.emit('faq_train.importedall', id_faq_kb);
767
- return res.status(200).send({ success: true, msg: "Intents imported successfully" })
925
+ })
768
926
 
769
- } else {
927
+ }
770
928
 
771
- if (req.query.create && req.query.create == 'true') {
929
+ if (updatedChatbot) {
930
+ return res.send(updatedChatbot);
931
+ } else {
932
+ return res.send(chatbot);
933
+ }
934
+
935
+ }
936
+
937
+ })
938
+
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') {
772
1022
 
773
1023
 
774
- 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) => {
775
- winston.debug("saved (and imported) faq kb: ", savedFaq_kb);
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);
776
1026
 
777
- // edit attributes.rules
778
- let attributes = json.attributes;
779
- if (attributes &&
780
- attributes.rules &&
781
- attributes.rules.length > 0) {
1027
+ // // edit attributes.rules
1028
+ // let attributes = json.attributes;
1029
+ // if (attributes &&
1030
+ // attributes.rules &&
1031
+ // attributes.rules.length > 0) {
782
1032
 
783
- await attributes.rules.forEach((rule) => {
784
- if (rule.do &&
785
- rule.do[0] &&
786
- rule.do[0].message &&
787
- rule.do[0].message.participants &&
788
- rule.do[0].message.participants[0]) {
789
- rule.do[0].message.participants[0] = "bot_" + savedFaq_kb._id
790
- winston.debug("attributes rule new participant: ", rule.do[0].message.participants[0])
791
- }
792
- })
793
- }
794
- let chatbot_edited = { attributes: attributes };
795
- Faq_kb.findByIdAndUpdate(savedFaq_kb._id, chatbot_edited, { new: true }, (err, savedEditedFaq_kb) => {
796
- if (err) {
797
- winston.error("error during saving edited faq_kb: ", err)
798
- }
799
- botEvent.emit('faqbot.create', savedFaq_kb);
800
-
801
- if (json.intents) {
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) {
802
1052
 
803
- json.intents.forEach((intent) => {
1053
+ // json.intents.forEach((intent) => {
804
1054
 
805
- let new_faq = {
806
- id_faq_kb: savedFaq_kb._id,
807
- id_project: req.projectid,
808
- createdBy: req.user.id,
809
- intent_display_name: intent.intent_display_name,
810
- intent_id: intent.intent_id,
811
- question: intent.question,
812
- answer: intent.answer,
813
- reply: intent.reply,
814
- form: intent.form,
815
- enabled: intent.enabled,
816
- webhook_enabled: intent.webhook_enabled,
817
- language: intent.language,
818
- actions: intent.actions,
819
- attributes: intent.attributes
820
- }
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
+ // }
821
1071
 
822
- // TO DELETE: no used when req.query.create = 'true'
823
- if (req.query.overwrite == "true") {
824
- 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) => {
825
- if (err) {
826
- winston.error("findOneAndUpdate (upsert) FAQ ERROR ", err);
827
- } else {
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 {
828
1078
 
829
- if (savingResult.lastErrorObject.updatedExisting == true) {
830
- winston.debug("updated existing intent")
831
- faqBotEvent.emit('faq.update', savingResult.value);
832
- } else {
833
- winston.debug("new intent created")
834
- faqBotEvent.emit('faq.create', savingResult.value);
835
- }
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
+ // }
836
1086
 
837
- }
1087
+ // }
838
1088
 
839
- })
840
- // don't overwrite duplicated intents
841
- } else {
842
- Faq.create(new_faq, (err, savedFaq) => {
843
- if (err) {
844
- winston.debug("create new FAQ ERROR ", err);
845
- if (err.code == 11000) {
846
- winston.error("Duplicate intent_display_name.");
847
- winston.debug("Skip duplicated intent_display_name");
848
- } else {
849
- winston.debug("new intent created")
850
- faqBotEvent.emit('faq.create', savedFaq);
851
- }
852
- }
853
- })
854
- }
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
+ // }
855
1105
 
856
- })
857
- }
858
- //faqBotEvent.emit('faq_train.importedall', savedEditedFaq_kb._id);
859
- return res.status(200).send(savedEditedFaq_kb);
860
- })
861
-
862
- }).catch((err) => {
863
- winston.error("error saving faq_kb: ", err);
864
- return res.status(500).send(err);
865
- })
866
-
867
- } else {
868
-
869
- Faq_kb.findById(id_faq_kb, async (err, faq_kb) => {
870
- if (err) {
871
- winston.error("GET FAQ-KB ERROR", err);
872
- return res.status(500).send({ success: false, msg: "Error getting bot." });
873
- }
874
- if (!faq_kb) {
875
- return res.status(404).send({ success: false, msg: 'Bot not found.' });
876
- }
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
+ // }
877
1131
 
878
- // should be wrong
879
- //const json = JSON.parse(json_string);
1132
+ // // should be wrong
1133
+ // //const json = JSON.parse(json_string);
880
1134
 
881
- if (json.webhook_enabled) {
882
- faq_kb.webhook_enabled = json.webhook_enabled;
883
- }
884
- if (json.webhook_url) {
885
- faq_kb.webhook_url = json.webhook_url;
886
- }
887
- if (json.language) {
888
- faq_kb.language = json.language;
889
- }
890
- if (json.name) {
891
- faq_kb.name = json.name;
892
- }
893
- if (json.description) {
894
- faq_kb.description = json.description;
895
- }
896
-
897
- if (json.attributes) {
898
- let attributes = json.attributes;
899
- if (attributes.rules &&
900
- attributes.rules.length > 0) {
901
- await attributes.rules.forEach((rule) => {
902
- if (rule.do &&
903
- rule.do[0] &&
904
- rule.do[0].message &&
905
- rule.do[0].message.participants &&
906
- rule.do[0].message.participants[0]) {
907
- rule.do[0].message.participants[0] = "bot_" + faq_kb._id
908
- winston.debug("attributes rule new participant: " + rule.do[0].message.participants[0])
909
- }
910
- })
911
- faq_kb.attributes = json.attributes;
912
- }
913
- }
914
- // if (json.intentsEngine) {
915
- // faq_kb.intentsEngine = json.intentsEngine;
916
- // }
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
+ // // }
917
1171
 
918
1172
 
919
- Faq_kb.findByIdAndUpdate(id_faq_kb, faq_kb, { new: true }, async (err, updatedFaq_kb) => { //TODO add cache_bot_here
920
- if (err) {
921
- return res.status(500).send({ success: false, msg: "Error updating bot." });
922
- }
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
+ // }
923
1177
 
924
- botEvent.emit('faqbot.update', updatedFaq_kb);
1178
+ // botEvent.emit('faqbot.update', updatedFaq_kb);
925
1179
 
926
- if (json.intents) {
927
- await json.intents.forEach((intent) => {
1180
+ // if (json.intents) {
1181
+ // await json.intents.forEach((intent) => {
928
1182
 
929
- let new_faq = {
930
- id_faq_kb: updatedFaq_kb._id,
931
- id_project: req.projectid,
932
- createdBy: req.user.id,
933
- intent_display_name: intent.intent_display_name,
934
- intent_id: intent.intent_id,
935
- question: intent.question,
936
- answer: intent.answer,
937
- reply: intent.reply,
938
- form: intent.form,
939
- enabled: intent.enabled,
940
- webhook_enabled: intent.webhook_enabled,
941
- language: intent.language,
942
- actions: intent.actions,
943
- attributes: intent.attributes
944
- }
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
+ // }
945
1199
 
946
- // overwrite duplicated intents
947
- if (req.query.overwrite == "true") {
948
- 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) => {
949
- if (err) {
950
- winston.error("findOneAndUpdate (upsert) FAQ ERROR ", err);
951
- } else {
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 {
952
1208
 
953
- if (savingResult.lastErrorObject.updatedExisting == true) {
954
- winston.info("updated existing intent")
955
- faqBotEvent.emit('faq.update', savingResult.value);
956
- } else {
957
- winston.info("new intent created")
958
- faqBotEvent.emit('faq.create', savingResult.value);
959
- }
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
+ // }
960
1216
 
961
- }
1217
+ // }
962
1218
 
963
- })
1219
+ // })
964
1220
 
965
- // don't overwrite duplicated intents
966
- } else {
967
- Faq.create(new_faq, (err, savedFaq) => {
968
- if (err) {
969
- winston.debug("create new FAQ ERROR ", err);
970
- if (err.code == 11000) {
971
- winston.error("Duplicate intent_display_name.");
972
- winston.info("Skip duplicated intent_display_name");
973
- } else {
974
- winston.info("new intent created")
975
- faqBotEvent.emit('faq.create', savedFaq);
976
- }
977
- }
978
- })
979
- }
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
+ // }
980
1238
 
981
- })
1239
+ // })
982
1240
 
983
- }
984
- //faqBotEvent.emit('faq_train.importedall', id_faq_kb);
985
- return res.send(updatedFaq_kb);
1241
+ // }
1242
+ // //faqBotEvent.emit('faq_train.importedall', id_faq_kb);
1243
+ // return res.send(updatedFaq_kb);
986
1244
 
987
- })
1245
+ // })
988
1246
 
989
- })
990
- }
1247
+ // })
1248
+ // }
991
1249
 
992
- }
993
- })
1250
+ // }
1251
+ // })
994
1252
 
995
1253
  router.get('/exportjson/:id_faq_kb', (req, res) => {
996
1254