@tiledesk/tiledesk-server 2.4.100 → 2.4.101
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 +4 -0
- package/app.js +45 -9
- package/event/emailEvent.js +13 -0
- package/event/integrationEvent.js +13 -0
- package/models/integrations.js +23 -0
- package/package.json +1 -1
- package/pubmodules/cache/mongoose-cachegoose-fn.js +12 -0
- package/pubmodules/emailNotification/requestNotification.js +1 -0
- package/routes/auth.js +14 -5
- package/routes/email.js +4 -2
- package/routes/faq.js +1 -0
- package/routes/integration.js +199 -0
- package/routes/kbsettings.js +8 -76
- package/routes/openai.js +52 -14
- package/routes/project.js +3 -4
- package/routes/quotes.js +52 -0
- package/routes/request.js +515 -499
- package/routes/users.js +5 -1
- package/services/QuoteManager.js +317 -0
- package/services/cacheEnabler.js +5 -0
- package/services/emailService.js +610 -586
- package/services/messageService.js +283 -202
- package/services/openaiService.js +12 -12
- package/services/requestService.js +1764 -1421
- package/services/trainingService.js +6 -2
- package/test/messageService.js +154 -92
- package/test/mock/MockTdCache.js +46 -0
- package/test/mock/emailMock.js +9 -0
- package/test/mock/messageMock.js +46 -0
- package/test/mock/projectMock.js +171 -0
- package/test/mock/requestMock.js +127 -0
- package/test/quoteManager.js +282 -0
- package/test/requestRoute.js +1 -1
- package/test/requestService.js +1196 -1079
- package/utils/TdCache.js +253 -0
    
        package/CHANGELOG.md
    CHANGED
    
    | @@ -5,6 +5,10 @@ | |
| 5 5 | 
             
            🚀        IN PRODUCTION                        🚀
         | 
| 6 6 | 
             
            (https://www.npmjs.com/package/@tiledesk/tiledesk-server/v/2.3.77) 
         | 
| 7 7 |  | 
| 8 | 
            +
            # 2.4.101
         | 
| 9 | 
            +
            - Added new route for knowledge base
         | 
| 10 | 
            +
            - Bug fix: conflicts with old knowledge base
         | 
| 11 | 
            +
             | 
| 8 12 | 
             
            # 2.4.100
         | 
| 9 13 | 
             
            - Updated tybot-connector to 0.2.50
         | 
| 10 14 | 
             
            - Added new route for knowledge base
         | 
    
        package/app.js
    CHANGED
    
    | @@ -81,6 +81,15 @@ mongoose.set('useFindAndModify', false); // https://mongoosejs.com/docs/deprecat | |
| 81 81 | 
             
            mongoose.set('useCreateIndex', true);
         | 
| 82 82 | 
             
            mongoose.set('useUnifiedTopology', false); 
         | 
| 83 83 |  | 
| 84 | 
            +
            // CONNECT REDIS - CHECK IT
         | 
| 85 | 
            +
            const { TdCache } = require('./utils/TdCache');
         | 
| 86 | 
            +
            let tdCache = new TdCache({
         | 
| 87 | 
            +
                host: process.env.CACHE_REDIS_HOST,
         | 
| 88 | 
            +
                port: process.env.CACHE_REDIS_PORT,
         | 
| 89 | 
            +
                password: process.env.CACHE_REDIS_PASSWORD
         | 
| 90 | 
            +
            });
         | 
| 91 | 
            +
             | 
| 92 | 
            +
            tdCache.connect();
         | 
| 84 93 |  | 
| 85 94 | 
             
            // ROUTES DECLARATION
         | 
| 86 95 | 
             
            var troubleshooting = require('./routes/troubleshooting');
         | 
| @@ -113,6 +122,8 @@ var key = require('./routes/key'); | |
| 113 122 | 
             
            var widgets = require('./routes/widget');
         | 
| 114 123 | 
             
            var widgetsLoader = require('./routes/widgetLoader');
         | 
| 115 124 | 
             
            var openai = require('./routes/openai');
         | 
| 125 | 
            +
            var quotes = require('./routes/quotes');
         | 
| 126 | 
            +
            var integration = require('./routes/integration')
         | 
| 116 127 | 
             
            var kbsettings = require('./routes/kbsettings');
         | 
| 117 128 | 
             
            var kb = require('./routes/kb');
         | 
| 118 129 |  | 
| @@ -158,7 +169,7 @@ botEvent.listen(); //queued but disabled | |
| 158 169 |  | 
| 159 170 | 
             
            var trainingService = require('./services/trainingService');
         | 
| 160 171 | 
             
            trainingService.start();
         | 
| 161 | 
            -
             | 
| 172 | 
            +
             | 
| 162 173 | 
             
            // job_here
         | 
| 163 174 |  | 
| 164 175 | 
             
            var geoService = require('./services/geoService');
         | 
| @@ -196,6 +207,10 @@ var IPFilter = require('./middleware/ipFilter'); | |
| 196 207 | 
             
            var BanUserNotifier = require('./services/banUserNotifier');
         | 
| 197 208 | 
             
            BanUserNotifier.listen();
         | 
| 198 209 | 
             
            const { ChatbotService } = require('./services/chatbotService');
         | 
| 210 | 
            +
            const { QuoteManager } = require('./services/QuoteManager');
         | 
| 211 | 
            +
             | 
| 212 | 
            +
            let qm = new QuoteManager({ tdCache: tdCache });
         | 
| 213 | 
            +
            qm.start();
         | 
| 199 214 |  | 
| 200 215 | 
             
            var modulesManager = undefined;
         | 
| 201 216 | 
             
            try {
         | 
| @@ -224,13 +239,13 @@ if (process.env.CREATE_INITIAL_DATA !== "false") { | |
| 224 239 |  | 
| 225 240 | 
             
            var app = express();
         | 
| 226 241 |  | 
| 227 | 
            -
             | 
| 228 | 
            -
             | 
| 229 242 | 
             
            // view engine setup
         | 
| 230 243 | 
             
            app.set('views', path.join(__dirname, 'views'));
         | 
| 231 244 | 
             
            app.set('view engine', 'jade');
         | 
| 232 245 |  | 
| 233 246 | 
             
            app.set('chatbot_service', new ChatbotService())
         | 
| 247 | 
            +
            app.set('redis_client', tdCache);
         | 
| 248 | 
            +
            app.set('quote_manager', qm);
         | 
| 234 249 |  | 
| 235 250 |  | 
| 236 251 | 
             
            // TODO DELETE IT IN THE NEXT RELEASE
         | 
| @@ -255,7 +270,6 @@ if (process.env.ENABLE_ALTERNATIVE_CORS_MIDDLEWARE === "true") { | |
| 255 270 | 
             
            // app.use(morgan('combined'));
         | 
| 256 271 |  | 
| 257 272 |  | 
| 258 | 
            -
             | 
| 259 273 | 
             
            // app.use(bodyParser.json());
         | 
| 260 274 |  | 
| 261 275 | 
             
            // https://stackoverflow.com/questions/18710225/node-js-get-raw-request-body-using-express
         | 
| @@ -297,6 +311,7 @@ if (process.env.DISABLE_SESSION_STRATEGY==true ||  process.env.DISABLE_SESSION_S | |
| 297 311 |  | 
| 298 312 | 
             
              if (process.env.ENABLE_REDIS_SESSION==true ||  process.env.ENABLE_REDIS_SESSION=="true" ) {
         | 
| 299 313 |  | 
| 314 | 
            +
                  console.log("Starting redis...") // errors occurs
         | 
| 300 315 | 
             
                  // Initialize client.
         | 
| 301 316 | 
             
                  // let redisClient = createClient()
         | 
| 302 317 | 
             
                  // redisClient.connect().catch(console.error)
         | 
| @@ -343,6 +358,24 @@ app.options('*', cors()); | |
| 343 358 | 
             
            // const customRedisRateLimiter = require("./rateLimiter").customRedisRateLimiter;
         | 
| 344 359 | 
             
            // app.use(customRedisRateLimiter);
         | 
| 345 360 |  | 
| 361 | 
            +
            // MIDDLEWARE FOR REQUESTS QUOTE
         | 
| 362 | 
            +
            // app.use('/:projectid/requests', function (req, res, next) {
         | 
| 363 | 
            +
              
         | 
| 364 | 
            +
            //   console.log("MIDDLEWARE FIRED ---> REQUESTS");
         | 
| 365 | 
            +
            //   console.log("(Requests Middleware) method: ", req.method);
         | 
| 366 | 
            +
            //   if (req.method === 'POST') {
         | 
| 367 | 
            +
             | 
| 368 | 
            +
            //   let quoteManager = new QuoteManager({ project: mockProject, tdCache: mockTdCache } )
         | 
| 369 | 
            +
                
         | 
| 370 | 
            +
            //   } else {
         | 
| 371 | 
            +
            //     next();
         | 
| 372 | 
            +
            //   }
         | 
| 373 | 
            +
             | 
| 374 | 
            +
             | 
| 375 | 
            +
            // });
         | 
| 376 | 
            +
             | 
| 377 | 
            +
             | 
| 378 | 
            +
             | 
| 346 379 | 
             
            if (process.env.ROUTELOGGER_ENABLED==="true") {
         | 
| 347 380 | 
             
              winston.info("RouterLogger enabled ");
         | 
| 348 381 | 
             
              app.use(function (req, res, next) {
         | 
| @@ -386,10 +419,10 @@ if (process.env.ROUTELOGGER_ENABLED==="true") { | |
| 386 419 | 
             
            app.get('/', function (req, res) {  
         | 
| 387 420 | 
             
              res.send('Hello from Tiledesk server. It\'s UP. See the documentation here http://developer.tiledesk.com');
         | 
| 388 421 | 
             
            });
         | 
| 389 | 
            -
             | 
| 390 422 |  | 
| 391 423 |  | 
| 392 424 |  | 
| 425 | 
            +
             | 
| 393 426 | 
             
            var projectIdSetter = function (req, res, next) {
         | 
| 394 427 | 
             
              var projectid = req.params.projectid;
         | 
| 395 428 | 
             
              winston.debug("projectIdSetter projectid: "+ projectid);
         | 
| @@ -439,8 +472,6 @@ var projectSetter = function (req, res, next) { | |
| 439 472 | 
             
            }
         | 
| 440 473 |  | 
| 441 474 |  | 
| 442 | 
            -
             | 
| 443 | 
            -
             | 
| 444 475 | 
             
            // app.use('/admin', admin);
         | 
| 445 476 |  | 
| 446 477 | 
             
            //oauth2
         | 
| @@ -560,7 +591,12 @@ app.use('/:projectid/emails',[passport.authenticate(['basic', 'jwt'], { session: | |
| 560 591 | 
             
            app.use('/:projectid/properties',[passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('agent', ['bot','subscription'])], property);
         | 
| 561 592 | 
             
            app.use('/:projectid/segments',[passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('agent', ['bot','subscription'])], segment);
         | 
| 562 593 |  | 
| 563 | 
            -
            app.use('/:projectid/openai', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('agent')], openai);
         | 
| 594 | 
            +
            // app.use('/:projectid/openai', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('agent')], openai);
         | 
| 595 | 
            +
            app.use('/:projectid/openai', openai);
         | 
| 596 | 
            +
            app.use('/:projectid/quotes', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('agent', ['bot','subscription'])], quotes)
         | 
| 597 | 
            +
             | 
| 598 | 
            +
            app.use('/:projectid/integration', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('agent', ['bot','subscription'])], integration )
         | 
| 599 | 
            +
             | 
| 564 600 | 
             
            app.use('/:projectid/kbsettings', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('agent', ['bot','subscription'])], kbsettings);
         | 
| 565 601 | 
             
            app.use('/:projectid/kb', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('admin', ['bot','subscription'])], kb);
         | 
| 566 602 |  | 
| @@ -601,7 +637,7 @@ app.use(function (err, req, res, next) { | |
| 601 637 |  | 
| 602 638 |  | 
| 603 639 |  | 
| 604 | 
            -
             | 
| 640 | 
            +
            // mettere middleware qui per le quote
         | 
| 605 641 |  | 
| 606 642 |  | 
| 607 643 |  | 
| @@ -0,0 +1,13 @@ | |
| 1 | 
            +
            const EventEmitter = require('events');
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            let winston = require('../config/winston');
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            class IntegrationEvent extends EventEmitter {
         | 
| 6 | 
            +
                constructor() {
         | 
| 7 | 
            +
                    super();
         | 
| 8 | 
            +
                }
         | 
| 9 | 
            +
            }
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            const integrationEvent = new IntegrationEvent();
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            module.exports = integrationEvent;
         | 
| @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            var mongoose = require('mongoose');
         | 
| 2 | 
            +
            var Schema = mongoose.Schema;
         | 
| 3 | 
            +
            var winston = require('../config/winston');
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            var IntegrationsSchema = new Schema({
         | 
| 6 | 
            +
                id_project: {
         | 
| 7 | 
            +
                    type: String,
         | 
| 8 | 
            +
                    required: true,
         | 
| 9 | 
            +
                    index: true
         | 
| 10 | 
            +
                },
         | 
| 11 | 
            +
                name: {
         | 
| 12 | 
            +
                    type: String,
         | 
| 13 | 
            +
                    required: true
         | 
| 14 | 
            +
                },
         | 
| 15 | 
            +
                value: {
         | 
| 16 | 
            +
                    type: Object,
         | 
| 17 | 
            +
                    required: true,
         | 
| 18 | 
            +
                    default: {}
         | 
| 19 | 
            +
                }
         | 
| 20 | 
            +
            })
         | 
| 21 | 
            +
             | 
| 22 | 
            +
             | 
| 23 | 
            +
            module.exports = mongoose.model('integration', IntegrationsSchema);
         | 
    
        package/package.json
    CHANGED
    
    
| @@ -6,6 +6,7 @@ | |
| 6 6 | 
             
             var departmentEvent = require("../../event/departmentEvent");   
         | 
| 7 7 | 
             
             var authEvent = require("../../event/authEvent");   
         | 
| 8 8 | 
             
             var labelEvent = require("../../event/labelEvent");
         | 
| 9 | 
            +
             var integrationEvent = require("../../event/integrationEvent");
         | 
| 9 10 |  | 
| 10 11 | 
             
             var triggerEventEmitter = require("../trigger/event/triggerEventEmitter");
         | 
| 11 12 | 
             
             var subscriptionEvent = require("../../event/subscriptionEvent");   
         | 
| @@ -725,6 +726,17 @@ | |
| 725 726 | 
             
                    });
         | 
| 726 727 | 
             
                });
         | 
| 727 728 |  | 
| 729 | 
            +
                // l'evento è relativo a tutte le integrazioni, è sufficiente solo un evento
         | 
| 730 | 
            +
                // per create, update, delete di una singola creation
         | 
| 731 | 
            +
                integrationEvent.on("integration.update", (integrations, id_project) => {
         | 
| 732 | 
            +
                    let key = "project:" + id_project + ":integrations";
         | 
| 733 | 
            +
                    winston.verbose("Creating cache for integration.create with key: " + key);
         | 
| 734 | 
            +
                    client.set(key, integrations, cacheUtil.longTTL, (err, reply) => {
         | 
| 735 | 
            +
                        winston.verbose("Created cache for integration.create", {err: err} );
         | 
| 736 | 
            +
                        winston.debug("Created cache for integration.create reply", reply);
         | 
| 737 | 
            +
                    })
         | 
| 738 | 
            +
                })
         | 
| 739 | 
            +
             | 
| 728 740 | 
             
                // fai cache per subscription.create, .update .delete
         | 
| 729 741 |  | 
| 730 742 |  | 
| @@ -118,6 +118,7 @@ listen() { | |
| 118 118 | 
             
                        // send an email only if offline and has an email (send also to followers)
         | 
| 119 119 | 
             
                        return that.sendUserEmail(message.id_project, message);
         | 
| 120 120 | 
             
                      } else { //send email  to followers
         | 
| 121 | 
            +
                        winston.debug("send direct email****");
         | 
| 121 122 |  | 
| 122 123 | 
             
                        that.sendToFollower(message.id_project, message);
         | 
| 123 124 |  | 
    
        package/routes/auth.js
    CHANGED
    
    | @@ -296,12 +296,14 @@ router.post('/signinWithCustomToken', [ | |
| 296 296 |  | 
| 297 297 | 
             
                       var newUser;
         | 
| 298 298 | 
             
                       try {
         | 
| 299 | 
            -
             | 
| 299 | 
            +
             | 
| 300 | 
            +
                        // Bug with email in camelcase
         | 
| 301 | 
            +
                        newUser = await userService.signup(req.user.email.toLowerCase(), uuidv4(), req.user.firstname, req.user.lastname, false);
         | 
| 300 302 | 
             
                       } catch(e) {
         | 
| 301 303 | 
             
                        winston.debug('error signup already exists??: ')
         | 
| 302 304 |  | 
| 303 305 | 
             
                        if (e.code = "E11000") {
         | 
| 304 | 
            -
                          newUser = await User.findOne({email: req.user.email | 
| 306 | 
            +
                          newUser = await User.findOne({email: req.user.email.toLowerCase(), status: 100}).exec();
         | 
| 305 307 | 
             
                          winston.debug('signup found')
         | 
| 306 308 |  | 
| 307 309 | 
             
                        } 
         | 
| @@ -373,7 +375,11 @@ router.post('/signinWithCustomToken', [ | |
| 373 375 |  | 
| 374 376 | 
             
                          }
         | 
| 375 377 |  | 
| 376 | 
            -
                           | 
| 378 | 
            +
                          if (returnToken.indexOf("JWT")==0) {
         | 
| 379 | 
            +
                            returnToken = "JWT " + returnToken;
         | 
| 380 | 
            +
                          }
         | 
| 381 | 
            +
             | 
| 382 | 
            +
                          return res.json({ success: true, token: returnToken, user: userToReturn });
         | 
| 377 383 | 
             
                      });
         | 
| 378 384 | 
             
                    } else {
         | 
| 379 385 | 
             
                      winston.debug('project user already exists ');
         | 
| @@ -381,7 +387,7 @@ router.post('/signinWithCustomToken', [ | |
| 381 387 | 
             
                      if (project_user.status==="active") {
         | 
| 382 388 |  | 
| 383 389 | 
             
                        if (req.user.role && (req.user.role === RoleConstants.OWNER || req.user.role === RoleConstants.ADMIN || req.user.role === RoleConstants.AGENT)) {
         | 
| 384 | 
            -
                          let userFromDB = await User.findOne({email: req.user.email | 
| 390 | 
            +
                          let userFromDB = await User.findOne({email: req.user.email.toLowerCase(), status: 100}).exec();
         | 
| 385 391 |  | 
| 386 392 | 
             
                          var signOptions = {         
         | 
| 387 393 | 
             
                            issuer:  'https://tiledesk.com',   
         | 
| @@ -402,7 +408,10 @@ router.post('/signinWithCustomToken', [ | |
| 402 408 | 
             
                          let returnToken = jwt.sign(userJson, configSecret, signOptions); //priv_jwt pp_jwt
         | 
| 403 409 |  | 
| 404 410 |  | 
| 405 | 
            -
                           | 
| 411 | 
            +
                          if (returnToken.indexOf("JWT")==0) {
         | 
| 412 | 
            +
                            returnToken = "JWT " + returnToken;
         | 
| 413 | 
            +
                          }
         | 
| 414 | 
            +
                          return res.json({ success: true, token: returnToken, user: userFromDB });
         | 
| 406 415 | 
             
                          // return res.json({ success: true, token: req.headers["authorization"], user: userFromDB });
         | 
| 407 416 |  | 
| 408 417 |  | 
    
        package/routes/email.js
    CHANGED
    
    | @@ -327,8 +327,10 @@ router.post('/internal/send', | |
| 327 327 |  | 
| 328 328 | 
             
              winston.info("Sending an email with text : " + text + " to " + to);
         | 
| 329 329 |  | 
| 330 | 
            -
             | 
| 331 | 
            -
             | 
| 330 | 
            +
              let quoteManager = req.app.get('quote_manager');
         | 
| 331 | 
            +
             | 
| 332 | 
            +
              //sendEmailDirect(to, text, project, request_id, subject, tokenQueryString, sourcePage, payload)
         | 
| 333 | 
            +
              emailService.sendEmailDirect(newto, text, req.project, request_id, subject, undefined, undefined, undefined, replyto, quoteManager);
         | 
| 332 334 |  | 
| 333 335 | 
             
              res.json({"queued": true});
         | 
| 334 336 |  | 
    
        package/routes/faq.js
    CHANGED
    
    | @@ -467,6 +467,7 @@ router.put('/:faqid', function (req, res) { | |
| 467 467 | 
             
            // DELETE REMOTE AND LOCAL FAQ
         | 
| 468 468 | 
             
            router.delete('/:faqid', function (req, res) {
         | 
| 469 469 |  | 
| 470 | 
            +
              console.log("delete called")
         | 
| 470 471 | 
             
              winston.debug('DELETE FAQ - FAQ ID ', req.params.faqid);
         | 
| 471 472 |  | 
| 472 473 | 
             
              let faqid = req.params.faqid;
         | 
| @@ -0,0 +1,199 @@ | |
| 1 | 
            +
            let express = require('express');
         | 
| 2 | 
            +
            let router = express.Router();
         | 
| 3 | 
            +
            var winston = require('../config/winston');
         | 
| 4 | 
            +
            let Integration = require('../models/integrations');
         | 
| 5 | 
            +
            const cacheEnabler = require('../services/cacheEnabler');
         | 
| 6 | 
            +
            var cacheUtil = require('../utils/cacheUtil');
         | 
| 7 | 
            +
            const integrationEvent = require('../event/integrationEvent');
         | 
| 8 | 
            +
             | 
| 9 | 
            +
             | 
| 10 | 
            +
            // Get all integration for a project id 
         | 
| 11 | 
            +
            router.get('/', async (req, res) => {
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                let id_project = req.projectid;
         | 
| 14 | 
            +
                winston.debug("Get all integration for the project " + id_project);
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                let i = Integration.find({ id_project: id_project });
         | 
| 17 | 
            +
                if (cacheEnabler.integrations) {
         | 
| 18 | 
            +
                    // cacheUtil.longTTL is 1 hour (default), evaluate 1 month (2592000 s)
         | 
| 19 | 
            +
                    i.cache(cacheUtil.longTTL, "project:" + id_project + ":integrations");
         | 
| 20 | 
            +
                    winston.debug('integration cache enabled for get all integrations');
         | 
| 21 | 
            +
                }
         | 
| 22 | 
            +
                i.exec((err, integrations) => {
         | 
| 23 | 
            +
                    if (err) {
         | 
| 24 | 
            +
                        winston.error("Error getting integrations: ", err);
         | 
| 25 | 
            +
                        return res.status(500).send({ success: false, message: "Error getting integrations "});
         | 
| 26 | 
            +
                    }
         | 
| 27 | 
            +
                    res.status(200).send(integrations);
         | 
| 28 | 
            +
                })
         | 
| 29 | 
            +
                // without cache
         | 
| 30 | 
            +
                // Integration.find({ id_project: id_project }, (err, integrations) => {
         | 
| 31 | 
            +
                //     if (err) {
         | 
| 32 | 
            +
                //         console.error("Error finding all integrations for the project " + id_project + " - err: " + err);
         | 
| 33 | 
            +
                //         return res.status(404).send({ success: false, err: err })
         | 
| 34 | 
            +
                //     }
         | 
| 35 | 
            +
                //     console.log("Integrations found: ", integrations);
         | 
| 36 | 
            +
                //     res.status(200).send(integrations);
         | 
| 37 | 
            +
                // })
         | 
| 38 | 
            +
            })
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            // Get one integration
         | 
| 41 | 
            +
            router.get('/:integration_id', async (req, res) => {
         | 
| 42 | 
            +
                
         | 
| 43 | 
            +
                let integration_id = req.params.integration_id;
         | 
| 44 | 
            +
                winston.debug("Get integration with id " + integration_id);
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                Integration.findById(integration_id, (err, integration) => {
         | 
| 47 | 
            +
                    if (err) {
         | 
| 48 | 
            +
                        winston.error("Error find integration by id: ", err);
         | 
| 49 | 
            +
                        return res.status(404).send({ success: false, err: err });
         | 
| 50 | 
            +
                    }
         | 
| 51 | 
            +
                    res.status(200).send(integration);
         | 
| 52 | 
            +
                })
         | 
| 53 | 
            +
             | 
| 54 | 
            +
            })
         | 
| 55 | 
            +
             | 
| 56 | 
            +
            router.get('/name/:integration_name', async (req, res) => {
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                let id_project = req.projectid;
         | 
| 59 | 
            +
                winston.debug("Get all integration for the project " + id_project);
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                let integration_name = req.params.integration_name;
         | 
| 62 | 
            +
                winston.debug("Get integration with id " + integration_name);
         | 
| 63 | 
            +
             | 
| 64 | 
            +
                Integration.findOne({ id_project: id_project, name: integration_name }, (err, integration) => {
         | 
| 65 | 
            +
                    if (err) {
         | 
| 66 | 
            +
                        winston.error("Error find integration by name: ", err);
         | 
| 67 | 
            +
                        return res.status(404).send({ success: false, err: err });
         | 
| 68 | 
            +
                    }
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                    if (!integration) {
         | 
| 71 | 
            +
                        winston.debug("Integration not found");
         | 
| 72 | 
            +
                        return res.status(200).send("Integration not found");
         | 
| 73 | 
            +
                    }
         | 
| 74 | 
            +
                    
         | 
| 75 | 
            +
                    res.status(200).send(integration);
         | 
| 76 | 
            +
                })
         | 
| 77 | 
            +
            })
         | 
| 78 | 
            +
             | 
| 79 | 
            +
            // Add new integration
         | 
| 80 | 
            +
            router.post('/', async (req, res) => {
         | 
| 81 | 
            +
             | 
| 82 | 
            +
                let id_project = req.projectid;
         | 
| 83 | 
            +
                winston.debug("Add new integration ", req.body);
         | 
| 84 | 
            +
             | 
| 85 | 
            +
             | 
| 86 | 
            +
                let newIntegration = {
         | 
| 87 | 
            +
                    id_project: id_project,
         | 
| 88 | 
            +
                    name: req.body.name
         | 
| 89 | 
            +
                }
         | 
| 90 | 
            +
                if (req.body.value) {
         | 
| 91 | 
            +
                    newIntegration.value = req.body.value;
         | 
| 92 | 
            +
                }
         | 
| 93 | 
            +
             | 
| 94 | 
            +
                Integration.findOneAndUpdate({ id_project: id_project, name: req.body.name },  newIntegration, { new: true, upsert: true, setDefaultsOnInsert: false}, (err, savedIntegration) => {
         | 
| 95 | 
            +
                    if (err) {
         | 
| 96 | 
            +
                        winston.error("Error creating new integration ", err);
         | 
| 97 | 
            +
                        return res.status(404).send({ success: false, err: err })
         | 
| 98 | 
            +
                    }
         | 
| 99 | 
            +
             | 
| 100 | 
            +
                    winston.debug("New integration created: ", savedIntegration);
         | 
| 101 | 
            +
                    
         | 
| 102 | 
            +
                    Integration.find({ id_project: id_project }, (err, integrations) => {
         | 
| 103 | 
            +
                        if (err) {
         | 
| 104 | 
            +
                            winston.error("Error getting all integrations");
         | 
| 105 | 
            +
                        } else {
         | 
| 106 | 
            +
                            integrationEvent.emit('integration.update', integrations, id_project);
         | 
| 107 | 
            +
                        }
         | 
| 108 | 
            +
                    })
         | 
| 109 | 
            +
                    
         | 
| 110 | 
            +
                    res.status(200).send(savedIntegration);
         | 
| 111 | 
            +
                })
         | 
| 112 | 
            +
             | 
| 113 | 
            +
                // let newIntegration = new Integration({
         | 
| 114 | 
            +
                //     id_project: id_project,
         | 
| 115 | 
            +
                //     name: req.body.name,
         | 
| 116 | 
            +
                //     value: req.body.value
         | 
| 117 | 
            +
                // })
         | 
| 118 | 
            +
             | 
| 119 | 
            +
                // newIntegration.save((err, savedIntegration) => {
         | 
| 120 | 
            +
                //     if (err) {
         | 
| 121 | 
            +
                //         console.error("Error creating new integration ", err);
         | 
| 122 | 
            +
                //         return res.status(404).send({ success: false, err: err })
         | 
| 123 | 
            +
                //     }
         | 
| 124 | 
            +
             | 
| 125 | 
            +
                //     console.log("New integration created: ", savedIntegration);
         | 
| 126 | 
            +
             | 
| 127 | 
            +
                //     Integration.find({ id_project: id_project }, (err, integrations) => {
         | 
| 128 | 
            +
                //         if (err) {
         | 
| 129 | 
            +
                //             console.error("Error getting all integrations");
         | 
| 130 | 
            +
                //         } else {
         | 
| 131 | 
            +
                //             console.log("emit integration.create event")
         | 
| 132 | 
            +
                //             integrationEvent.emit('integration.create', integrations, id_project);
         | 
| 133 | 
            +
                //         }
         | 
| 134 | 
            +
             | 
| 135 | 
            +
                //     })
         | 
| 136 | 
            +
                    
         | 
| 137 | 
            +
                //     res.status(200).send(savedIntegration);
         | 
| 138 | 
            +
             | 
| 139 | 
            +
                // })
         | 
| 140 | 
            +
                
         | 
| 141 | 
            +
            })
         | 
| 142 | 
            +
             | 
| 143 | 
            +
            router.put('/:integration_id', async (req, res) => {
         | 
| 144 | 
            +
             | 
| 145 | 
            +
                let id_project = req.projectid;
         | 
| 146 | 
            +
                let integration_id = req.params.integration_id;
         | 
| 147 | 
            +
                
         | 
| 148 | 
            +
                let update = {};
         | 
| 149 | 
            +
                if (req.body.name != undefined) {
         | 
| 150 | 
            +
                    update.name = req.body.name;
         | 
| 151 | 
            +
                }
         | 
| 152 | 
            +
                if (req.body.value != undefined) {
         | 
| 153 | 
            +
                    update.value = req.body.value
         | 
| 154 | 
            +
                }
         | 
| 155 | 
            +
             | 
| 156 | 
            +
                Integration.findByIdAndUpdate(integration_id, update, { new: true, upsert: true }, (err, savedIntegration) => {
         | 
| 157 | 
            +
                    if (err) {
         | 
| 158 | 
            +
                        winston.error("Error find by id and update integration: ", err);
         | 
| 159 | 
            +
                        return res.status({ success: false, error: err })
         | 
| 160 | 
            +
                    }
         | 
| 161 | 
            +
             | 
| 162 | 
            +
                    Integration.find({ id_project: id_project }, (err, integrations) => {
         | 
| 163 | 
            +
                        if (err) {
         | 
| 164 | 
            +
                            winston.error("Error getting all integrations");
         | 
| 165 | 
            +
                        } else {
         | 
| 166 | 
            +
                            integrationEvent.emit('integration.update', integrations, id_project);
         | 
| 167 | 
            +
                        }
         | 
| 168 | 
            +
                    })
         | 
| 169 | 
            +
             | 
| 170 | 
            +
                    res.status(200).send(savedIntegration);
         | 
| 171 | 
            +
                })
         | 
| 172 | 
            +
            })
         | 
| 173 | 
            +
             | 
| 174 | 
            +
            router.delete('/:integration_id', async (req, res) => {
         | 
| 175 | 
            +
             | 
| 176 | 
            +
                let id_project = req.projectid;
         | 
| 177 | 
            +
                let integration_id = req.params.integration_id;
         | 
| 178 | 
            +
             | 
| 179 | 
            +
                Integration.findByIdAndDelete(integration_id, (err, result) => {
         | 
| 180 | 
            +
                    if (err) {
         | 
| 181 | 
            +
                        winston.error("Error find by id and delete integration: ", err);
         | 
| 182 | 
            +
                        return res.status({ success: false, error: err })
         | 
| 183 | 
            +
                    }
         | 
| 184 | 
            +
             | 
| 185 | 
            +
                    Integration.find({ id_project: id_project }, (err, integrations) => {
         | 
| 186 | 
            +
                        if (err) {
         | 
| 187 | 
            +
                            winston.error("Error getting all integrations");
         | 
| 188 | 
            +
                        } else {
         | 
| 189 | 
            +
                            integrationEvent.emit('integration.update', integrations, id_project);
         | 
| 190 | 
            +
                        }
         | 
| 191 | 
            +
                    })
         | 
| 192 | 
            +
             | 
| 193 | 
            +
                    res.status(200).send({ success: true, messages: "Integration deleted successfully"});
         | 
| 194 | 
            +
             | 
| 195 | 
            +
                })
         | 
| 196 | 
            +
            })
         | 
| 197 | 
            +
             | 
| 198 | 
            +
             | 
| 199 | 
            +
            module.exports = router;
         | 
    
        package/routes/kbsettings.js
    CHANGED
    
    | @@ -1,6 +1,6 @@ | |
| 1 1 | 
             
            var express = require('express');
         | 
| 2 2 | 
             
            var { KBSettings } = require('../models/kb_setting');
         | 
| 3 | 
            -
            var { KB } = require('../models/kb_setting');
         | 
| 3 | 
            +
            // var { KB } = require('../models/kb_setting');
         | 
| 4 4 | 
             
            // var KB = require('../models/kb_setting')
         | 
| 5 5 | 
             
            var router = express.Router();
         | 
| 6 6 | 
             
            var winston = require('../config/winston');
         | 
| @@ -99,75 +99,11 @@ router.delete('/:settings_id/:kb_id', async (req, res) => { | |
| 99 99 |  | 
| 100 100 | 
             
            })
         | 
| 101 101 |  | 
| 102 | 
            -
            // PROXY PUGLIA AI V2 - START
         | 
| 103 | 
            -
            router.post('/scrape/single', async (req, res) => {
         | 
| 104 | 
            -
             | 
| 105 | 
            -
                let data = req.body;
         | 
| 106 | 
            -
                winston.debug("/scrape/single data: ", data);
         | 
| 107 | 
            -
             | 
| 108 | 
            -
                let gptkey = process.env.GPTKEY;
         | 
| 109 | 
            -
                if (!gptkey) {
         | 
| 110 | 
            -
                    return res.status(403).send({ success: false, error: "GPT apikey undefined"})
         | 
| 111 | 
            -
                }
         | 
| 112 | 
            -
             | 
| 113 | 
            -
                data.gptkey = gptkey;
         | 
| 114 | 
            -
                
         | 
| 115 | 
            -
                openaiService.singleScrape(data).then((resp) => {
         | 
| 116 | 
            -
                    winston.debug("singleScrape resp: ", resp.data);
         | 
| 117 | 
            -
                    return res.status(200).send(resp.data);
         | 
| 118 | 
            -
                }).catch((err) => {
         | 
| 119 | 
            -
                    winston.error("singleScrape err: ", err);
         | 
| 120 | 
            -
                    let status = err.response.status;
         | 
| 121 | 
            -
                    return res.status(status).send({ statusText: err.response.statusText, detail: err.response.data.detail });
         | 
| 122 | 
            -
                })
         | 
| 123 | 
            -
            })
         | 
| 124 | 
            -
             | 
| 125 | 
            -
            router.post('/scrape/status', async (req, res) => {
         | 
| 126 | 
            -
             | 
| 127 | 
            -
                let data = req.body;
         | 
| 128 | 
            -
                winston.debug("/scrapeStatus req.body: ", req.body);
         | 
| 129 | 
            -
             | 
| 130 | 
            -
                openaiService.scrapeStatus(data).then((response) => {
         | 
| 131 | 
            -
             | 
| 132 | 
            -
                    winston.debug("scrapeStatus response.data: ", response.data);
         | 
| 133 | 
            -
                    res.status(200).send(response.data);
         | 
| 134 | 
            -
                }).catch((err) => {
         | 
| 135 | 
            -
                    winston.error("scrapeStatus err: ", err);
         | 
| 136 | 
            -
                    let status = err.response.status;
         | 
| 137 | 
            -
                    res.status(status).send({ statusText: err.response.statusText, detail: err.response.data.detail });
         | 
| 138 | 
            -
                })
         | 
| 139 | 
            -
            })
         | 
| 140 | 
            -
             | 
| 141 | 
            -
            router.post('/ask', async (req, res) => {
         | 
| 142 | 
            -
                let data = req.body;
         | 
| 143 | 
            -
                winston.debug("/qa data: ", data);
         | 
| 144 | 
            -
             | 
| 145 | 
            -
                if (!data.gptkey) {
         | 
| 146 | 
            -
                    let gptkey = process.env.GPTKEY;
         | 
| 147 | 
            -
                    if (!gptkey) {
         | 
| 148 | 
            -
                        return res.status(403).send({ success: false, error: "GPT apikey undefined"})
         | 
| 149 | 
            -
                    }
         | 
| 150 | 
            -
                    data.gptkey = gptkey;
         | 
| 151 | 
            -
                }
         | 
| 152 | 
            -
             | 
| 153 | 
            -
                openaiService.askNamespace(data).then((resp) => {
         | 
| 154 | 
            -
                    winston.debug("qa resp: ", resp.data);
         | 
| 155 | 
            -
                    res.status(200).send(resp.data);
         | 
| 156 | 
            -
                }).catch((err) => {
         | 
| 157 | 
            -
                    winston.error("qa err: ", err);
         | 
| 158 | 
            -
                    let status = err.response.status;
         | 
| 159 | 
            -
                    res.status(status).send({ statusText: err.response.statusText, detail: err.response.data.detail });
         | 
| 160 | 
            -
                })
         | 
| 161 | 
            -
            })
         | 
| 162 | 
            -
             | 
| 163 | 
            -
            // PROXY PUGLIA AI V2 - END
         | 
| 164 | 
            -
             | 
| 165 102 |  | 
| 166 103 | 
             
            // PROXY PUGLIA AI - START
         | 
| 167 104 | 
             
            router.post('/qa', async (req, res) => {
         | 
| 168 105 | 
             
                let data = req.body;
         | 
| 169 106 | 
             
                winston.debug("/qa data: ", data);
         | 
| 170 | 
            -
                winston.info("/qa data: ", data);
         | 
| 171 107 |  | 
| 172 108 | 
             
                openaiService.ask(data).then((resp) => {
         | 
| 173 109 | 
             
                    winston.debug("qa resp: ", resp.data);
         | 
| @@ -175,7 +111,6 @@ router.post('/qa', async (req, res) => { | |
| 175 111 | 
             
                }).catch((err) => {
         | 
| 176 112 | 
             
                    winston.error("qa err: ", err);
         | 
| 177 113 | 
             
                    let status = err.response.status;
         | 
| 178 | 
            -
                    winston.info("status on error: ", status)
         | 
| 179 114 | 
             
                    res.status(status).send({ statusText: err.response.statusText, detail: err.response.data.detail });
         | 
| 180 115 | 
             
                })
         | 
| 181 116 | 
             
            })
         | 
| @@ -234,9 +169,9 @@ router.post('/checkstatus', async (req, res) => { | |
| 234 169 | 
             
                    }
         | 
| 235 170 |  | 
| 236 171 |  | 
| 237 | 
            -
                    res.status(200).send( | 
| 172 | 
            +
                    res.status(200).send(return_data);
         | 
| 238 173 | 
             
                }).catch((err) => {
         | 
| 239 | 
            -
                    winston.error("checkstatus err: ", err);
         | 
| 174 | 
            +
                    //winston.error("checkstatus err: ", err);
         | 
| 240 175 | 
             
                    let status = err.response.status;
         | 
| 241 176 | 
             
                    res.status(status).send({ statusText: err.response.statusText, detail: err.response.data.detail });
         | 
| 242 177 | 
             
                })
         | 
| @@ -254,14 +189,10 @@ router.post('/:settings_id', async (req, res) => { | |
| 254 189 | 
             
                        return res.status(500).send({ success: false, error: err});
         | 
| 255 190 | 
             
                    } else {
         | 
| 256 191 |  | 
| 257 | 
            -
                        let new_kb =  | 
| 192 | 
            +
                        let new_kb = {
         | 
| 258 193 | 
             
                            name: body.name,
         | 
| 259 | 
            -
                            url: body.url | 
| 260 | 
            -
             | 
| 261 | 
            -
                            type: body.type,
         | 
| 262 | 
            -
                            content: body.content,
         | 
| 263 | 
            -
                            namespace: body.namespace
         | 
| 264 | 
            -
                        })
         | 
| 194 | 
            +
                            url: body.url
         | 
| 195 | 
            +
                        }
         | 
| 265 196 | 
             
                        settings.kbs.push(new_kb);
         | 
| 266 197 |  | 
| 267 198 | 
             
                        KBSettings.findByIdAndUpdate( settings_id, settings, { new: true }, (err, savedSettings) => {
         | 
| @@ -269,11 +200,12 @@ router.post('/:settings_id', async (req, res) => { | |
| 269 200 | 
             
                                winston.err("findByIdAndUpdate error: ", err);
         | 
| 270 201 | 
             
                                res.status(500).send({ success: false, error: err });
         | 
| 271 202 | 
             
                            } else {
         | 
| 272 | 
            -
                                res.status(200).send( | 
| 203 | 
            +
                                res.status(200).send(savedSettings);
         | 
| 273 204 | 
             
                            }
         | 
| 274 205 | 
             
                        })
         | 
| 275 206 | 
             
                    }
         | 
| 276 207 | 
             
                })
         | 
| 277 208 | 
             
            })
         | 
| 278 209 |  | 
| 210 | 
            +
             | 
| 279 211 | 
             
            module.exports = router;
         |