@@ -402,7 +402,7 @@ router.post('/fork/:id_faq_kb', async (req, res) => {
402402router . post ( '/importjson/:id_faq_kb' , upload . single ( 'uploadFile' ) , ( req , res ) => {
403403
404404 let id_faq_kb = req . params . id_faq_kb ;
405- winston . debug ( ' id_faq_kb: ', id_faq_kb ) ;
405+ winston . info ( 'import on id_faq_kb: ', id_faq_kb ) ;
406406
407407 let json_string ;
408408 let json ;
@@ -413,8 +413,6 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), (req, res) =>
413413 json = req . body ;
414414 }
415415
416- winston . info ( "json_string: " + json_string ) ;
417-
418416 if ( req . query . intentsOnly == "true" ) {
419417
420418 winston . info ( "intents only" )
@@ -473,44 +471,15 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), (req, res) =>
473471
474472 } else {
475473
476- Faq_kb . findById ( id_faq_kb , ( err , faq_kb ) => {
477- if ( err ) {
478- winston . error ( "GET FAQ-KB ERROR" , err ) ;
479- return res . status ( 500 ) . send ( { success : false , msg : "Error getting bot." } ) ;
480- }
481- if ( ! faq_kb ) {
482- return res . status ( 404 ) . send ( { success : false , msg : 'Bot not found.' } ) ;
483- }
484-
485- const json = JSON . parse ( json_string ) ;
486-
487- if ( json . webhook_enabled ) {
488- faq_kb . webhook_enabled = json . webhook_enabled ;
489- }
490- if ( json . webhook_url ) {
491- faq_kb . webhook_url = json . webhook_url ;
492- }
493- if ( json . language ) {
494- faq_kb . language = json . language ;
495- }
496- if ( json . name ) {
497- faq_kb . name = json . name ;
498- }
499- if ( json . description ) {
500- faq_kb . description = json . description ;
501- }
502-
503- Faq_kb . findByIdAndUpdate ( id_faq_kb , faq_kb , { new : true } , ( err , updatedFaq_kb ) => {
504- if ( err ) {
505- return res . status ( 500 ) . send ( { success : false , msg : "Error updating bot." } ) ;
506- }
507-
508- botEvent . emit ( 'faqbot.update' , updatedFaq_kb ) ;
474+ if ( req . query . create == 'true' ) {
475+ faqService . create ( json . name , undefined , req . projectid , req . user . id , "tilebot" , json . description , json . webhook_url , json . webhook_enabled , json . language , undefined , undefined , undefined ) . then ( ( savedFaq_kb ) => {
476+ winston . debug ( "saved (and imported) faq kb: " , savedFaq_kb ) ;
477+ botEvent . emit ( 'faqbot.create' , savedFaq_kb ) ;
509478
510479 json . intents . forEach ( ( intent ) => {
511-
480+
512481 let new_faq = {
513- id_faq_kb : updatedFaq_kb . _id ,
482+ id_faq_kb : savedFaq_kb . _id ,
514483 id_project : req . projectid ,
515484 createdBy : req . user . id ,
516485 intent_display_name : intent . intent_display_name ,
@@ -523,7 +492,7 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), (req, res) =>
523492 language : intent . language
524493 }
525494
526- // overwrite duplicated intents
495+ // TO DELETE: no used when req.query.create = 'true'
527496 if ( req . query . overwrite == "true" ) {
528497 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 ) => {
529498 if ( err ) {
@@ -559,12 +528,110 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), (req, res) =>
559528 }
560529
561530 } )
531+ return res . status ( 200 ) . send ( savedFaq_kb ) ;
562532
563- return res . send ( updatedFaq_kb ) ;
533+ } ) . catch ( ( err ) => {
534+ console . error ( "error saving faq_kb: " , err ) ;
535+ return res . status ( 500 ) . send ( err ) ;
536+ } )
564537
538+ } else {
539+
540+ Faq_kb . findById ( id_faq_kb , ( err , faq_kb ) => {
541+ if ( err ) {
542+ winston . error ( "GET FAQ-KB ERROR" , err ) ;
543+ return res . status ( 500 ) . send ( { success : false , msg : "Error getting bot." } ) ;
544+ }
545+ if ( ! faq_kb ) {
546+ return res . status ( 404 ) . send ( { success : false , msg : 'Bot not found.' } ) ;
547+ }
548+
549+ // should be wrong
550+ //const json = JSON.parse(json_string);
551+
552+ if ( json . webhook_enabled ) {
553+ faq_kb . webhook_enabled = json . webhook_enabled ;
554+ }
555+ if ( json . webhook_url ) {
556+ faq_kb . webhook_url = json . webhook_url ;
557+ }
558+ if ( json . language ) {
559+ faq_kb . language = json . language ;
560+ }
561+ if ( json . name ) {
562+ faq_kb . name = json . name ;
563+ }
564+ if ( json . description ) {
565+ faq_kb . description = json . description ;
566+ }
567+
568+ Faq_kb . findByIdAndUpdate ( id_faq_kb , faq_kb , { new : true } , ( err , updatedFaq_kb ) => {
569+ if ( err ) {
570+ return res . status ( 500 ) . send ( { success : false , msg : "Error updating bot." } ) ;
571+ }
572+
573+ botEvent . emit ( 'faqbot.update' , updatedFaq_kb ) ;
574+
575+ json . intents . forEach ( ( intent ) => {
576+
577+ let new_faq = {
578+ id_faq_kb : updatedFaq_kb . _id ,
579+ id_project : req . projectid ,
580+ createdBy : req . user . id ,
581+ intent_display_name : intent . intent_display_name ,
582+ question : intent . question ,
583+ answer : intent . answer ,
584+ reply : intent . reply ,
585+ form : intent . form ,
586+ enabled : intent . enabled ,
587+ webhook_enabled : intent . webhook_enabled ,
588+ language : intent . language
589+ }
590+
591+ // overwrite duplicated intents
592+ if ( req . query . overwrite == "true" ) {
593+ 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 ) => {
594+ if ( err ) {
595+ winston . error ( "findOneAndUpdate (upsert) FAQ ERROR " , err ) ;
596+ } else {
597+
598+ if ( savingResult . lastErrorObject . updatedExisting == true ) {
599+ winston . info ( "updated existing intent" )
600+ faqBotEvent . emit ( 'faq.update' , savingResult . value ) ;
601+ } else {
602+ winston . info ( "new intent crated" )
603+ faqBotEvent . emit ( 'faq.create' , savingResult . value ) ;
604+ }
605+
606+ }
607+
608+ } )
609+
610+ // don't overwrite duplicated intents
611+ } else {
612+ Faq . create ( new_faq , ( err , savedFaq ) => {
613+ if ( err ) {
614+ winston . debug ( "create new FAQ ERROR " , err ) ;
615+ if ( err . code == 11000 ) {
616+ winston . error ( "Duplicate intent_display_name." ) ;
617+ winston . info ( "Skip duplicated intent_display_name" ) ;
618+ } else {
619+ winston . info ( "new intent crated" )
620+ faqBotEvent . emit ( 'faq.create' , savedFaq ) ;
621+ }
622+ }
623+ } )
624+ }
625+
626+ } )
627+
628+ return res . send ( updatedFaq_kb ) ;
629+
630+ } )
631+
565632 } )
633+ }
566634
567- } )
568635 }
569636
570637} )
0 commit comments