@tiledesk/tiledesk-server 2.4.81 → 2.4.83

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,14 @@
5
5
  🚀 IN PRODUCTION 🚀
6
6
  (https://www.npmjs.com/package/@tiledesk/tiledesk-server/v/2.3.77)
7
7
 
8
+ # 2.4.83
9
+ - Improved whatsapp log services
10
+ - Updated whatsapp-connector to 0.1.57
11
+ - Updated whatsapp-jobworker to 0.0.7
12
+
13
+ # 2.4.82
14
+ - Added whatsapp log services
15
+
8
16
  # 2.4.81
9
17
  - update whatsapp-connector to 0.1.56
10
18
 
package/app.js CHANGED
@@ -462,7 +462,7 @@ app.use('/files', files);
462
462
  app.use('/urls', urls);
463
463
  app.use('/users', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken], users);
464
464
  app.use('/users_util', usersUtil);
465
- app.use('/logs', logs);
465
+ // app.use('/logs', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken], logs);
466
466
  app.use('/requests_util', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken], requestUtilRoot);
467
467
 
468
468
  // TODO security issues
@@ -562,6 +562,8 @@ app.use('/:projectid/segments',[passport.authenticate(['basic', 'jwt'], { sessio
562
562
  app.use('/:projectid/openai', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('agent')], openai);
563
563
  app.use('/:projectid/kbsettings', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('agent', ['bot','subscription'])], kbsettings);
564
564
 
565
+ app.use('/:projectid/logs', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRole('admin')], logs);
566
+
565
567
 
566
568
 
567
569
 
@@ -0,0 +1,37 @@
1
+ const mongoose = require('mongoose');
2
+
3
+ const TransactionSchema = mongoose.Schema({
4
+ transaction_id: {
5
+ type: String,
6
+ required: true
7
+ },
8
+ id_project: {
9
+ type: String,
10
+ required: true
11
+ },
12
+ template_name: {
13
+ type: String,
14
+ required: true
15
+ },
16
+ status: {
17
+ type: String,
18
+ required: false
19
+ },
20
+ channel: {
21
+ type: String,
22
+ required: false
23
+ },
24
+ createdAt: {
25
+ type: Date,
26
+ default: Date.now
27
+ },
28
+ updatedAt: {
29
+ type: Date,
30
+ default: Date.now,
31
+ }
32
+ })
33
+
34
+
35
+ const Transaction = mongoose.model("Transactions", TransactionSchema);
36
+
37
+ module.exports = { Transaction };
@@ -0,0 +1,40 @@
1
+ const mongoose = require('mongoose');
2
+
3
+ const MessageLogSchema = mongoose.Schema({
4
+ id_project: {
5
+ type: String,
6
+ required: true
7
+ },
8
+ json_message: {
9
+ type: Object,
10
+ required: true
11
+ },
12
+ transaction_id: {
13
+ type: String,
14
+ required: true
15
+ },
16
+ message_id: {
17
+ type: String,
18
+ required: false
19
+ },
20
+ status: {
21
+ type: String,
22
+ required: true
23
+ },
24
+ status_code: {
25
+ type: Number,
26
+ required: true
27
+ },
28
+ error: {
29
+ type: String
30
+ },
31
+ timestamp: {
32
+ type: Date,
33
+ default: Date.now
34
+ }
35
+ })
36
+
37
+
38
+ const MessageLog = mongoose.model("MessageLog", MessageLogSchema);
39
+
40
+ module.exports = { MessageLog };
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.81",
4
+ "version": "2.4.83",
5
5
  "scripts": {
6
6
  "start": "node ./bin/www",
7
7
  "pretest": "mongodb-runner start",
@@ -46,8 +46,8 @@
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.30",
49
- "@tiledesk/tiledesk-whatsapp-connector": "^0.1.56",
50
- "@tiledesk/tiledesk-whatsapp-jobworker": "^0.0.4",
49
+ "@tiledesk/tiledesk-whatsapp-connector": "^0.1.57",
50
+ "@tiledesk/tiledesk-whatsapp-jobworker": "^0.0.7",
51
51
  "amqplib": "^0.5.5",
52
52
  "app-root-path": "^3.0.0",
53
53
  "bcrypt-nodejs": "0.0.3",
package/routes/logs.js CHANGED
@@ -1,26 +1,94 @@
1
1
  var express = require('express');
2
2
  var router = express.Router();
3
3
  var winston = require('../config/winston');
4
+ const { MessageLog } = require('../models/whatsappLog');
5
+ const { Transaction } = require('../models/transaction');
4
6
 
5
7
 
6
8
 
7
- router.get('/', function(req, res, next) {
9
+ router.get('/', function (req, res, next) {
8
10
  winston.info("logs", req.body);
9
- return res.status(200).send({ success: true});
11
+ return res.status(200).send({ success: true });
10
12
  });
11
13
 
12
14
 
13
- router.post('/', function(req, res, next) {
15
+ router.post('/', function (req, res, next) {
14
16
  winston.info("logs", req.body);
15
- return res.status(200).send({ success: true});
17
+ return res.status(200).send({ success: true });
16
18
  });
17
19
 
18
-
20
+ router.get('/whatsapp', async (req, res) => {
21
+
22
+ let project_id = req.projectid;
23
+
24
+ Transaction.find({ id_project: project_id }, (err, transactions) => {
25
+ if (err) {
26
+ winston.error("Error find transactions for project_id: " + project_id);
27
+ return res.status(400).send({ success: false, message: "Unable to find transaction for project_id " + project_id });
28
+ }
29
+
30
+ winston.verbose("Transactions: ", transactions);
31
+
32
+ res.status(200).send(transactions);
33
+ })
34
+
35
+ // res.stats(200).send({ success: true });
36
+ })
37
+
38
+
39
+ router.get('/whatsapp/:transaction_id', async (req, res) => {
40
+
41
+ let project_id = req.projectid;
42
+
43
+ let transaction_id = req.params.transaction_id;
44
+ winston.info("Get logs for whatsapp transaction_id " + transaction_id);;
45
+
46
+ MessageLog.find({ id_project: project_id, transaction_id: transaction_id }).lean().exec((err, logs) => {
47
+ if (err) {
48
+ winston.error("Error find logs for transaction_id " + transaction_id);
49
+ return res.status(400).send({ success: false, message: "Unable to find logs for transaction_id " + transaction_id })
50
+ }
51
+
52
+ winston.verbose("Logs found: ", logs);
53
+
54
+ let clearLogs = logs.map(({_id, __v, ...keepAttrs}) => keepAttrs)
55
+ winston.verbose("clearLogs: ", clearLogs)
56
+
57
+ res.status(200).send(clearLogs);
58
+ })
59
+
60
+ })
61
+
62
+ router.post('/whatsapp', async (req, res) => {
63
+
64
+ winston.info("save following log: ", req.body);
65
+
66
+ let log = new MessageLog({
67
+ id_project: req.body.id_project,
68
+ json_message: req.body.json_message,
69
+ transaction_id: req.body.transaction_id,
70
+ message_id: req.body.message_id,
71
+ status: req.body.status,
72
+ status_code: req.body.status_code,
73
+ error: req.body.error
74
+ });
75
+
76
+ log.save((err, savedLog) => {
77
+ if (err) {
78
+ winston.error("Unable to save log: ", err);
79
+ return res.status(400).send(err);
80
+ }
81
+
82
+ winston.info("savedLog: ", savedLog);
83
+ res.status(200).send(savedLog);
84
+ })
85
+ })
86
+
87
+
88
+
19
89
 
20
90
 
21
-
22
91
 
23
-
24
92
 
25
93
 
26
94
  module.exports = router;
@@ -0,0 +1,100 @@
1
+ //During the test the env variable is set to test
2
+ process.env.NODE_ENV = 'test';
3
+
4
+ var User = require('../models/user');
5
+ var projectService = require('../services/projectService');
6
+ var userService = require('../services/userService');
7
+
8
+ //Require the dev-dependencies
9
+ let chai = require('chai');
10
+ let chaiHttp = require('chai-http');
11
+ let server = require('../app');
12
+ let should = chai.should();
13
+
14
+ // chai.config.includeStack = true;
15
+
16
+ var expect = chai.expect;
17
+ var assert = chai.assert;
18
+
19
+ let mock_log = {
20
+ json_message: {
21
+ messaging_product: "whatsapp",
22
+ to: "+393484511111",
23
+ type: "template",
24
+ template: {
25
+ name: "codice_sconto",
26
+ language: {
27
+ code: "it"
28
+ },
29
+ components: [
30
+ {
31
+ type: "body",
32
+ parameters: [
33
+ {
34
+ "type": "text",
35
+ "text": "Giovanni"
36
+ }
37
+ ]
38
+ }
39
+ ]
40
+ }
41
+ },
42
+ id_project: null,
43
+ transaction_id: null,
44
+ message_id: "wamid.HBgMMzkzNDg0NTA2NjI3FQIAERgSQTRDNzRDOTM3NzA5Mjk3NzJFAA==",
45
+ status: "read",
46
+ status_code: 3,
47
+ error: null,
48
+ }
49
+ chai.use(chaiHttp);
50
+
51
+ describe('LogsRoute', () => {
52
+
53
+ describe('/getlogs', () => {
54
+
55
+ it('whatsapp', (done) => {
56
+
57
+ var email = "test-signup-" + Date.now() + "@email.com";
58
+ var pwd = "pwd";
59
+
60
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
61
+ projectService.create("test1", savedUser._id).then(function (savedProject) {
62
+
63
+ mock_log.id_project = savedProject._id;
64
+ mock_log.transaction_id = "automation-request-" + savedProject._id;
65
+ console.log("mock_log.transaction_id: ", mock_log.transaction_id);
66
+
67
+ chai.request(server)
68
+ .post('/' + savedProject._id + '/logs/whatsapp')
69
+ .auth(email, pwd)
70
+ .send(mock_log)
71
+ .end((err, res) => {
72
+ console.log("err: ", err);
73
+ // console.log("res.body: ", res.body);
74
+ console.log("Added example log")
75
+ res.should.have.status(200);
76
+ res.body.should.be.a('object');
77
+
78
+ chai.request(server)
79
+ .get('/' + savedProject._id + '/logs/whatsapp/' + mock_log.transaction_id)
80
+ .auth(email, pwd)
81
+ .end((err, res) => {
82
+ console.log("err: ", err);
83
+ console.log("res.body: ", res.body);
84
+ res.should.have.status(200);
85
+
86
+ done();
87
+
88
+ })
89
+
90
+ })
91
+
92
+ });
93
+ });
94
+
95
+ });
96
+
97
+
98
+ });
99
+ });
100
+