@tiledesk/tiledesk-server 2.4.80 → 2.4.82

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -5,7 +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.80
8
+ # 2.4.82
9
+ - Added whatsapp log services
10
+
11
+ # 2.4.81
9
12
  - update whatsapp-connector to 0.1.56
10
13
 
11
14
  # 2.4.79
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
@@ -0,0 +1,36 @@
1
+ const mongoose = require('mongoose');
2
+
3
+ const MessageLogSchema = mongoose.Schema({
4
+ json_message: {
5
+ type: Object,
6
+ required: true
7
+ },
8
+ transaction_id: {
9
+ type: String,
10
+ required: true
11
+ },
12
+ message_id: {
13
+ type: String,
14
+ required: false
15
+ },
16
+ status: {
17
+ type: String,
18
+ required: true
19
+ },
20
+ status_code: {
21
+ type: Number,
22
+ required: true
23
+ },
24
+ error: {
25
+ type: String
26
+ },
27
+ timestamp: {
28
+ type: Date,
29
+ default: Date.now
30
+ }
31
+ })
32
+
33
+
34
+ const MessageLog = mongoose.model("MessageLog", MessageLogSchema);
35
+
36
+ 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.80",
4
+ "version": "2.4.82",
5
5
  "scripts": {
6
6
  "start": "node ./bin/www",
7
7
  "pretest": "mongodb-runner start",
@@ -46,7 +46,7 @@
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.55",
49
+ "@tiledesk/tiledesk-whatsapp-connector": "^0.1.56",
50
50
  "@tiledesk/tiledesk-whatsapp-jobworker": "^0.0.4",
51
51
  "amqplib": "^0.5.5",
52
52
  "app-root-path": "^3.0.0",
package/routes/logs.js CHANGED
@@ -1,26 +1,76 @@
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');
4
5
 
5
6
 
6
7
 
7
- router.get('/', function(req, res, next) {
8
+ router.get('/', function (req, res, next) {
8
9
  winston.info("logs", req.body);
9
- return res.status(200).send({ success: true});
10
+ return res.status(200).send({ success: true });
10
11
  });
11
12
 
12
13
 
13
- router.post('/', function(req, res, next) {
14
+ router.post('/', function (req, res, next) {
14
15
  winston.info("logs", req.body);
15
- return res.status(200).send({ success: true});
16
+ return res.status(200).send({ success: true });
16
17
  });
17
18
 
18
-
19
+ router.get('/whatsapp', async (req, res) => {
20
+ res.stats(200).send({ success: true });
21
+ })
22
+
23
+
24
+ router.get('/whatsapp/:transaction_id', async (req, res) => {
25
+
26
+ let transaction_id = req.params.transaction_id;
27
+ winston.info("Get logs for whatsapp transaction_id " + transaction_id);;
28
+
29
+ MessageLog.find({ transaction_id: transaction_id }).lean().exec((err, logs) => {
30
+ if (err) {
31
+ winston.error("Error find logs for transaction_id " + transaction_id);
32
+ return res.status(400).send({ success: false, message: "Unable to find logs for transaction_id " + transaction_id })
33
+ }
34
+
35
+ winston.verbose("Logs found: ", logs);
36
+
37
+ let clearLogs = logs.map(({_id, __v, ...keepAttrs}) => keepAttrs)
38
+ winston.verbose("clearLogs: ", clearLogs)
39
+
40
+ res.status(200).send(logs);
41
+ })
42
+
43
+ })
44
+
45
+ router.post('/whatsapp', async (req, res) => {
46
+
47
+ winston.info("save following log: ", req.body);
48
+
49
+ let log = new MessageLog({
50
+ json_message: req.body.json_message,
51
+ transaction_id: req.body.transaction_id,
52
+ message_id: req.body.message_id,
53
+ status: req.body.status,
54
+ status_code: req.body.status_code,
55
+ error: req.body.error
56
+ });
57
+
58
+ log.save((err, savedLog) => {
59
+ if (err) {
60
+ winston.error("Unable to save log: ", err);
61
+ return res.status(400).send(err);
62
+ }
63
+
64
+ winston.info("savedLog: ", savedLog);
65
+ res.status(200).send(savedLog);
66
+ })
67
+ })
68
+
69
+
70
+
19
71
 
20
72
 
21
-
22
73
 
23
-
24
74
 
25
75
 
26
76
  module.exports = router;
@@ -0,0 +1,97 @@
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 example_log = {
20
+ json_message: {
21
+ messaging_product: "whatsapp",
22
+ to: "+393484506627",
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
+ transaction_id: null,
43
+ message_id: "wamid.HBgMMzkzNDg0NTA2NjI3FQIAERgSQTRDNzRDOTM3NzA5Mjk3NzJFAA==",
44
+ status: "read",
45
+ status_code: 3,
46
+ error: null,
47
+ }
48
+ chai.use(chaiHttp);
49
+
50
+ describe('LogsRoute', () => {
51
+
52
+ describe('/getlogs', () => {
53
+
54
+ it('whatsapp', (done) => {
55
+
56
+ var email = "test-signup-" + Date.now() + "@email.com";
57
+ var pwd = "pwd";
58
+
59
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
60
+ projectService.create("test1", savedUser._id).then(function (savedProject) {
61
+
62
+ example_log.transaction_id = "automation-request-" + savedProject._id;
63
+ console.log("example_log.transaction_id: ", example_log.transaction_id);
64
+
65
+ chai.request(server)
66
+ .post('/logs/whatsapp')
67
+ .auth(email, pwd)
68
+ .send(example_log)
69
+ .end((err, res) => {
70
+ console.log("err: ", err);
71
+ console.log("res.body: ", res.body);
72
+ res.should.have.status(200);
73
+ res.body.should.be.a('object');
74
+
75
+ chai.request(server)
76
+ .get('/logs/whatsapp/' + example_log.transaction_id)
77
+ .auth(email, pwd)
78
+ .end((err, res) => {
79
+ console.log("err: ", err);
80
+ console.log("res.body: ", res.body);
81
+ res.should.have.status(200);
82
+
83
+ done();
84
+
85
+ })
86
+
87
+ })
88
+
89
+ });
90
+ });
91
+
92
+ });
93
+
94
+
95
+ });
96
+ });
97
+