@tiledesk/tiledesk-server 2.4.100 → 2.4.102
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +9 -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 +3 -3
- package/pubmodules/cache/mongoose-cachegoose-fn.js +12 -0
- package/pubmodules/emailNotification/requestNotification.js +1 -0
- package/routes/auth.js +29 -8
- package/routes/email.js +4 -2
- package/routes/faq_kb.js +3 -8
- package/routes/integration.js +199 -0
- package/routes/kb.js +0 -2
- 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 +304 -0
- package/services/cacheEnabler.js +5 -0
- package/services/chatbotService.js +0 -1
- package/services/emailService.js +610 -586
- package/services/messageService.js +236 -202
- package/services/openaiService.js +15 -17
- package/services/requestService.js +1739 -1420
- 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,15 @@
|
|
5
5
|
🚀 IN PRODUCTION 🚀
|
6
6
|
(https://www.npmjs.com/package/@tiledesk/tiledesk-server/v/2.3.77)
|
7
7
|
|
8
|
+
# 2.4.102
|
9
|
+
- Updated whatsapp-connector to 0.1.63
|
10
|
+
- Updated messenger-connector to 0.1.17
|
11
|
+
- Added quote management
|
12
|
+
|
13
|
+
# 2.4.101
|
14
|
+
- Added new route for knowledge base
|
15
|
+
- Bug fix: conflicts with old knowledge base
|
16
|
+
|
8
17
|
# 2.4.100
|
9
18
|
- Updated tybot-connector to 0.2.50
|
10
19
|
- 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
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@tiledesk/tiledesk-server",
|
3
3
|
"description": "The Tiledesk server module",
|
4
|
-
"version": "2.4.
|
4
|
+
"version": "2.4.102",
|
5
5
|
"scripts": {
|
6
6
|
"start": "node ./bin/www",
|
7
7
|
"pretest": "mongodb-runner start",
|
@@ -42,11 +42,11 @@
|
|
42
42
|
"@tiledesk/tiledesk-dialogflow-connector": "^1.8.4",
|
43
43
|
"@tiledesk/tiledesk-json-rules-engine": "^4.0.3",
|
44
44
|
"@tiledesk/tiledesk-kaleyra-proxy": "^0.1.7",
|
45
|
-
"@tiledesk/tiledesk-messenger-connector": "^0.1.
|
45
|
+
"@tiledesk/tiledesk-messenger-connector": "^0.1.17",
|
46
46
|
"@tiledesk/tiledesk-rasa-connector": "^1.0.10",
|
47
47
|
"@tiledesk/tiledesk-telegram-connector": "^0.1.10",
|
48
48
|
"@tiledesk/tiledesk-tybot-connector": "^0.2.50",
|
49
|
-
"@tiledesk/tiledesk-whatsapp-connector": "^0.1.
|
49
|
+
"@tiledesk/tiledesk-whatsapp-connector": "^0.1.63",
|
50
50
|
"@tiledesk/tiledesk-whatsapp-jobworker": "^0.0.7",
|
51
51
|
"@tiledesk/tiledesk-chatbot-templates": "^0.1.2",
|
52
52
|
"amqplib": "^0.5.5",
|
@@ -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
@@ -276,7 +276,8 @@ router.post('/signinWithCustomToken', [
|
|
276
276
|
if (req.user.role) {
|
277
277
|
role = req.user.role;
|
278
278
|
}
|
279
|
-
winston.debug("
|
279
|
+
winston.debug("role1: " + role );
|
280
|
+
winston.debug("id_project: " + id_project + " uuid_user " + req.user._id + " role " + role);
|
280
281
|
|
281
282
|
|
282
283
|
Project_user.findOne({ id_project: id_project, uuid_user: req.user._id, role: role}).
|
@@ -285,23 +286,28 @@ router.post('/signinWithCustomToken', [
|
|
285
286
|
winston.error(err);
|
286
287
|
return res.json({ success: true, token: req.headers["authorization"], user: req.user });
|
287
288
|
}
|
289
|
+
winston.debug("project_user: ", project_user );
|
290
|
+
|
291
|
+
|
288
292
|
if (!project_user) {
|
289
293
|
|
290
294
|
let createNewUser = false;
|
291
|
-
winston.debug('
|
295
|
+
winston.debug('role2: '+ role)
|
292
296
|
|
293
297
|
|
294
298
|
if (role === RoleConstants.OWNER || role === RoleConstants.ADMIN || role === RoleConstants.AGENT) {
|
295
299
|
createNewUser = true;
|
296
|
-
|
300
|
+
winston.debug('role owner admin agent');
|
297
301
|
var newUser;
|
298
302
|
try {
|
299
|
-
|
303
|
+
|
304
|
+
// Bug with email in camelcase
|
305
|
+
newUser = await userService.signup(req.user.email.toLowerCase(), uuidv4(), req.user.firstname, req.user.lastname, false);
|
300
306
|
} catch(e) {
|
301
307
|
winston.debug('error signup already exists??: ')
|
302
308
|
|
303
309
|
if (e.code = "E11000") {
|
304
|
-
newUser = await User.findOne({email: req.user.email
|
310
|
+
newUser = await User.findOne({email: req.user.email.toLowerCase(), status: 100}).exec();
|
305
311
|
winston.debug('signup found')
|
306
312
|
|
307
313
|
}
|
@@ -326,6 +332,8 @@ router.post('/signinWithCustomToken', [
|
|
326
332
|
updatedBy: req.user._id
|
327
333
|
});
|
328
334
|
|
335
|
+
winston.debug('newProject_user', newProject_user);
|
336
|
+
|
329
337
|
// testtare qiestp cpm dpcker dev partemdp da ui
|
330
338
|
if (createNewUser===true) {
|
331
339
|
newProject_user.id_user = newUser._id;
|
@@ -373,7 +381,15 @@ router.post('/signinWithCustomToken', [
|
|
373
381
|
|
374
382
|
}
|
375
383
|
|
376
|
-
|
384
|
+
winston.debug('returnToken '+returnToken);
|
385
|
+
|
386
|
+
winston.debug('returnToken.indexOf("JWT") '+returnToken.indexOf("JWT"));
|
387
|
+
|
388
|
+
if (returnToken.indexOf("JWT")<0) {
|
389
|
+
returnToken = "JWT " + returnToken;
|
390
|
+
}
|
391
|
+
|
392
|
+
return res.json({ success: true, token: returnToken, user: userToReturn });
|
377
393
|
});
|
378
394
|
} else {
|
379
395
|
winston.debug('project user already exists ');
|
@@ -381,7 +397,7 @@ router.post('/signinWithCustomToken', [
|
|
381
397
|
if (project_user.status==="active") {
|
382
398
|
|
383
399
|
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
|
400
|
+
let userFromDB = await User.findOne({email: req.user.email.toLowerCase(), status: 100}).exec();
|
385
401
|
|
386
402
|
var signOptions = {
|
387
403
|
issuer: 'https://tiledesk.com',
|
@@ -402,11 +418,16 @@ router.post('/signinWithCustomToken', [
|
|
402
418
|
let returnToken = jwt.sign(userJson, configSecret, signOptions); //priv_jwt pp_jwt
|
403
419
|
|
404
420
|
|
405
|
-
|
421
|
+
if (returnToken.indexOf("JWT")<0) {
|
422
|
+
returnToken = "JWT " + returnToken;
|
423
|
+
}
|
424
|
+
return res.json({ success: true, token: returnToken, user: userFromDB });
|
406
425
|
// return res.json({ success: true, token: req.headers["authorization"], user: userFromDB });
|
407
426
|
|
408
427
|
|
409
428
|
} else {
|
429
|
+
winston.debug('req.headers["authorization"]: '+req.headers["authorization"]);
|
430
|
+
|
410
431
|
return res.json({ success: true, token: req.headers["authorization"], user: userToReturn });
|
411
432
|
}
|
412
433
|
|
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_kb.js
CHANGED
@@ -639,7 +639,7 @@ router.post('/fork/:id_faq_kb', async (req, res) => {
|
|
639
639
|
|
640
640
|
let globals = req.query.globals;
|
641
641
|
winston.debug("export globals " + globals);
|
642
|
-
|
642
|
+
|
643
643
|
|
644
644
|
let token = req.headers.authorization;
|
645
645
|
|
@@ -647,7 +647,7 @@ router.post('/fork/:id_faq_kb', async (req, res) => {
|
|
647
647
|
|
648
648
|
let chatbot = await cs.getBotById(id_faq_kb, public, api_url, chatbot_templates_api_url, token, current_project_id, globals);
|
649
649
|
winston.debug("chatbot: ", chatbot)
|
650
|
-
|
650
|
+
|
651
651
|
if (!chatbot) {
|
652
652
|
return res.status(500).send({ success: false, message: "Unable to get chatbot" });
|
653
653
|
}
|
@@ -982,8 +982,6 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), async (req, r
|
|
982
982
|
router.get('/exportjson/:id_faq_kb', (req, res) => {
|
983
983
|
|
984
984
|
winston.debug("exporting bot...")
|
985
|
-
console.log("exportjson req.query.globals: ", req.query.globals);
|
986
|
-
|
987
985
|
|
988
986
|
let id_faq_kb = req.params.id_faq_kb;
|
989
987
|
|
@@ -1000,12 +998,10 @@ router.get('/exportjson/:id_faq_kb', (req, res) => {
|
|
1000
998
|
const intents = faqs.map(({ _id, id_project, topic, status, id_faq_kb, createdBy, createdAt, updatedAt, __v, ...keepAttrs }) => keepAttrs)
|
1001
999
|
|
1002
1000
|
if (!req.query.globals) {
|
1003
|
-
|
1001
|
+
winston.verbose("Delete globals from attributes!")
|
1004
1002
|
if (faq_kb.attributes) {
|
1005
1003
|
delete faq_kb.attributes.globals;
|
1006
1004
|
}
|
1007
|
-
} else {
|
1008
|
-
console.log("Keep globals")
|
1009
1005
|
}
|
1010
1006
|
|
1011
1007
|
let json = {
|
@@ -1033,7 +1029,6 @@ router.get('/exportjson/:id_faq_kb', (req, res) => {
|
|
1033
1029
|
// return res.status(200).send(json);
|
1034
1030
|
// }
|
1035
1031
|
let json_string = JSON.stringify(json);
|
1036
|
-
console.log("json_string: ", json_string)
|
1037
1032
|
res.set({ "Content-Disposition": "attachment; filename=\"bot.json\"" });
|
1038
1033
|
return res.send(json_string);
|
1039
1034
|
}
|
@@ -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/kb.js
CHANGED