@tiledesk/tiledesk-server 2.4.50 → 2.4.52

Sign up to get free protection for your applications and to get access to all the features.
package/app.js CHANGED
@@ -113,6 +113,7 @@ var key = require('./routes/key');
113
113
  var widgets = require('./routes/widget');
114
114
  var widgetsLoader = require('./routes/widgetLoader');
115
115
  var openai_kbs = require('./routes/openai_kbs');
116
+ var kbsettings = require('./routes/kbsettings');
116
117
 
117
118
  // var admin = require('./routes/admin');
118
119
  var faqpub = require('./routes/faqpub');
@@ -551,6 +552,8 @@ app.use('/:projectid/emails',[passport.authenticate(['basic', 'jwt'], { session:
551
552
  app.use('/:projectid/properties',[passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('agent', ['bot','subscription'])], property);
552
553
 
553
554
  app.use('/:projectid/openai_kbs', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('agent')], openai_kbs);
555
+ app.use('/:projectid/kbsettings', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRoleOrTypes('agent', ['bot','subscription'])], kbsettings);
556
+
554
557
 
555
558
 
556
559
 
@@ -0,0 +1,48 @@
1
+ var mongoose = require('mongoose');
2
+ var Schema = mongoose.Schema;
3
+ var winston = require('../config/winston');
4
+
5
+ var KBSchema = new Schema({
6
+ name: {
7
+ type: String,
8
+ required: true
9
+ },
10
+ url: {
11
+ type: String,
12
+ required: true
13
+ },
14
+ createdAt: {
15
+ type: Date,
16
+ default: Date.now,
17
+ },
18
+ status: {
19
+ type: Number,
20
+ required: false,
21
+ default: -1
22
+ }
23
+ })
24
+
25
+ var KBSettingSchema = new Schema({
26
+ id_project: {
27
+ type: String,
28
+ required: true,
29
+ index: true
30
+ },
31
+ gptkey: {
32
+ type: String,
33
+ //required: true
34
+ },
35
+ maxKbsNumber: {
36
+ type: Number,
37
+ default: 3
38
+ },
39
+ maxPagesNumber: {
40
+ type: Number,
41
+ default: 1000
42
+ },
43
+ kbs: [ KBSchema ]
44
+ });
45
+
46
+
47
+ module.exports = mongoose.model('KBSettings', KBSettingSchema);
48
+ // module.exports = mongoose.model('KB', KBSchema)
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.50",
4
+ "version": "2.4.52",
5
5
  "scripts": {
6
6
  "start": "node ./bin/www",
7
7
  "pretest": "mongodb-runner start",
@@ -44,7 +44,7 @@
44
44
  "@tiledesk/tiledesk-kaleyra-proxy": "^0.1.7",
45
45
  "@tiledesk/tiledesk-messenger-connector": "0.1.9",
46
46
  "@tiledesk/tiledesk-rasa-connector": "^1.0.10",
47
- "@tiledesk/tiledesk-tybot-connector": "^0.1.91",
47
+ "@tiledesk/tiledesk-tybot-connector": "^0.1.93",
48
48
  "@tiledesk/tiledesk-whatsapp-connector": "^0.1.51",
49
49
  "amqplib": "^0.5.5",
50
50
  "app-root-path": "^3.0.0",
@@ -0,0 +1,128 @@
1
+ var express = require('express');
2
+ var KBSettings = require('../models/kb_setting');
3
+ // var KB = require('../models/kb_setting')
4
+ var router = express.Router();
5
+ var winston = require('../config/winston');
6
+
7
+ router.get('/', async (req, res) => {
8
+ let project_id = req.projectid;
9
+
10
+ KBSettings.findOne({ id_project: project_id }, (err, kb_setting) => {
11
+ if (err) {
12
+ winston.error("find knoledge base settings error: ", err);
13
+ return res.status(500).send({ success: false, error: err });
14
+ }
15
+ else if (!kb_setting) {
16
+ // Automatically creates the kb settings object if it does not exist
17
+ let new_settings = new KBSettings({
18
+ id_project: req.projectid
19
+ })
20
+ new_settings.save(function (err, savedKbSettings) {
21
+ if (err) {
22
+ winston.error("save new kbs error: ", err);
23
+ return res.status(500).send({ success: false, error: err});
24
+ } else {
25
+ return res.status(200).send(savedKbSettings);
26
+ }
27
+ })
28
+ }
29
+ else {
30
+ return res.status(200).send(kb_setting)
31
+ }
32
+ })
33
+ })
34
+
35
+
36
+ // Never used?
37
+ router.post('/', async (req, res) => {
38
+
39
+ let body = req.body;
40
+
41
+ let new_settings = new KBSettings({
42
+ id_project: req.projectid,
43
+ // gptkey: body.gptkey
44
+ // others params are set with default values
45
+ })
46
+
47
+ new_settings.save(function (err, savedKbSettings) {
48
+ if (err) {
49
+ winston.error("save new kbs error: ", err);
50
+ return res.status(500).send({ success: false, error: err});
51
+ } else {
52
+ return res.status(200).send(savedKbSettings);
53
+ }
54
+ })
55
+ })
56
+
57
+ router.post('/:settings_id', async (req, res) => {
58
+
59
+ let settings_id = req.params.settings_id;
60
+ let body = req.body;
61
+
62
+ KBSettings.findById(settings_id, (err, settings) => {
63
+ if (err) {
64
+ winston.error("find knoledge base error: ", err);
65
+ return res.status(500).send({ success: false, error: err});
66
+ } else {
67
+
68
+ let new_kb = {
69
+ name: body.name,
70
+ url: body.url
71
+ }
72
+ settings.kbs.push(new_kb);
73
+
74
+ KBSettings.findByIdAndUpdate( settings_id, settings, { new: true }, (err, savedSettings) => {
75
+ if (err) {
76
+ winston.err("findByIdAndUpdate error: ", err);
77
+ res.status(500).send({ success: false, error: err });
78
+ } else {
79
+ res.status(200).send(savedSettings);
80
+ }
81
+ })
82
+ }
83
+ })
84
+ })
85
+
86
+ router.put('/:settings_id', async (req, res) => {
87
+
88
+ let settings_id = req.params.settings_id;
89
+ let update = {};
90
+
91
+ if (req.body.gptkey != undefined) {
92
+ update.gptkey = req.body.gptkey
93
+ }
94
+ // check if it is eligible
95
+ // if (req.body.maxKbsNumber != undefined) {
96
+ // update.maxKbsNumber = req.body.maxKbsNumber
97
+ // }
98
+ // if (req.body.maxPagesNumber != undefined) {
99
+ // update.maxPagesNumber = req.body.maxPagesNumber
100
+ // }
101
+
102
+ winston.debug("update: ", update);
103
+ KBSettings.findByIdAndUpdate(settings_id, update, { new: true }, (err, settings) => {
104
+ if (err) {
105
+ winston.error("findByIdAndUpdate error: ", err);
106
+ res.status(500).send({ success: false, error: err });
107
+ } else {
108
+ res.status(200).send(settings);
109
+ }
110
+ })
111
+ })
112
+
113
+ router.delete('/:settings_id/:kb_id', async (req, res) => {
114
+
115
+ let settings_id = req.params.settings_id;
116
+ let kb_id = req.params.kb_id;
117
+
118
+ KBSettings.findByIdAndUpdate(settings_id, { $pull: { kbs: { _id: kb_id }}}, { new: true}, (err, settings) => {
119
+ if (err) {
120
+ winston.error("delete kb from list error: ", err);
121
+ res.status(500).send({ success: false, error: err });
122
+ } else {
123
+ res.status(200).send(settings);
124
+ }
125
+ })
126
+ })
127
+
128
+ module.exports = router;
@@ -0,0 +1,253 @@
1
+ //During the test the env variable is set to test
2
+ process.env.NODE_ENV = 'test';
3
+
4
+ var userService = require('../services/userService');
5
+ var projectService = require('../services/projectService');
6
+
7
+ let log = false;
8
+
9
+ //Require the dev-dependencies
10
+ let chai = require('chai');
11
+ let chaiHttp = require('chai-http');
12
+ let server = require('../app');
13
+ let should = chai.should();
14
+ var fs = require('fs');
15
+ const path = require('path');
16
+
17
+ // chai.config.includeStack = true;
18
+
19
+ var expect = chai.expect;
20
+ var assert = chai.assert;
21
+
22
+ chai.use(chaiHttp);
23
+
24
+ describe('KbSettingsRoute', () => {
25
+
26
+ describe('/create', () => {
27
+
28
+ it('create kb settings', (done) => {
29
+
30
+ var email = "test-signup-" + Date.now() + "@email.com";
31
+ var pwd = "pwd";
32
+
33
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
34
+ projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
35
+
36
+ chai.request(server)
37
+ .post('/' + savedProject._id + '/kbsettings')
38
+ .auth(email, pwd)
39
+ .send({}) // can be empty
40
+ .end((err, res) => {
41
+ if (log) { console.log("create kbsettings res.body: ", res.body); }
42
+ res.should.have.status(200);
43
+ res.body.should.be.a('object');
44
+ expect(res.body.id_project).to.equal(savedProject._id.toString());
45
+
46
+ chai.request(server)
47
+ .get('/' + savedProject._id + "/kbsettings")
48
+ .auth(email, pwd)
49
+ .end((err, res) => {
50
+ if (log) { console.log("get kbsettings res.body: ", res.body); }
51
+ res.should.have.status(200);
52
+ res.body.should.be.a('object');
53
+ expect(res.body.id_project).to.equal(savedProject._id.toString())
54
+ expect(res.body.maxKbsNumber).to.equal(3);
55
+ expect(res.body.maxPagesNumber).to.equal(1000);
56
+ expect(res.body.kbs).is.an('array').that.is.empty;
57
+
58
+ done();
59
+ })
60
+
61
+ })
62
+
63
+ });
64
+ });
65
+
66
+ });
67
+
68
+ it('create kb settings if not exists', (done) => {
69
+
70
+ var email = "test-signup-" + Date.now() + "@email.com";
71
+ var pwd = "pwd";
72
+
73
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
74
+ projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
75
+
76
+ chai.request(server)
77
+ .get('/' + savedProject._id + "/kbsettings")
78
+ .auth(email, pwd)
79
+ .end((err, res) => {
80
+ if (log) { console.log("get kbsettings res.body: ", res.body); }
81
+ res.should.have.status(200);
82
+ res.body.should.be.a('object');
83
+ expect(res.body.id_project).to.equal(savedProject._id.toString())
84
+ expect(res.body.maxKbsNumber).to.equal(3);
85
+ expect(res.body.maxPagesNumber).to.equal(1000);
86
+ expect(res.body.kbs).is.an('array').that.is.empty;
87
+
88
+ done();
89
+ })
90
+ });
91
+ });
92
+
93
+ });
94
+
95
+
96
+ it('add kb to kb settings', (done) => {
97
+
98
+ var email = "test-signup-" + Date.now() + "@email.com";
99
+ var pwd = "pwd";
100
+
101
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
102
+ projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
103
+
104
+ chai.request(server)
105
+ .post('/' + savedProject._id + '/kbsettings')
106
+ .auth(email, pwd)
107
+ .send({}) // can be empty
108
+ .end((err, res) => {
109
+ if (log) { console.log("create kbsettings res.body: ", res.body); }
110
+ res.should.have.status(200);
111
+ res.body.should.be.a('object');
112
+ expect(res.body.id_project).to.equal(savedProject._id.toString());
113
+
114
+ chai.request(server)
115
+ .post('/' + savedProject._id + "/kbsettings/" + res.body._id)
116
+ .auth(email, pwd)
117
+ .send({ name: "exampleurl.com/kb/", url: "https://exampleurl.com/kb/" })
118
+ .end((err, res) => {
119
+ if (log) { console.log("add kb to kb settings res.body: ", res.body); }
120
+ res.should.have.status(200);
121
+ res.body.should.be.a('object');
122
+ expect(res.body.kbs).to.have.length(1)
123
+
124
+ chai.request(server)
125
+ .post('/' + savedProject._id + "/kbsettings/" + res.body._id)
126
+ .auth(email, pwd)
127
+ .send({ name: "secondurl.com/support/", url: "https://secondurl.com/support/" })
128
+ .end((err, res) => {
129
+ if (log) { console.log("add kb to kb settings res.body: ", res.body); }
130
+ res.should.have.status(200);
131
+ res.body.should.be.a('object');
132
+ expect(res.body.kbs).to.have.length(2)
133
+
134
+ done();
135
+ })
136
+
137
+ })
138
+ })
139
+
140
+ });
141
+ });
142
+
143
+ });
144
+
145
+ it('update kb settings', (done) => {
146
+
147
+ var email = "test-signup-" + Date.now() + "@email.com";
148
+ var pwd = "pwd";
149
+
150
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
151
+ projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
152
+
153
+ chai.request(server)
154
+ .post('/' + savedProject._id + '/kbsettings')
155
+ .auth(email, pwd)
156
+ .send({}) // can be empty
157
+ .end((err, res) => {
158
+ if (log) { console.log("create kbsettings res.body: ", res.body); }
159
+ res.should.have.status(200);
160
+ res.body.should.be.a('object');
161
+
162
+ chai.request(server)
163
+ .put('/' + savedProject._id + "/kbsettings/" + res.body._id)
164
+ .auth(email, pwd)
165
+ .send({ gptkey: "sk-12345678" })
166
+ .end((err, res) => {
167
+ if (log) { console.log("add kb to kb settings res.body: ", res.body); }
168
+ res.should.have.status(200);
169
+ res.body.should.be.a('object');
170
+
171
+ done();
172
+
173
+ })
174
+ })
175
+
176
+ });
177
+ });
178
+
179
+ });
180
+
181
+ it('delete kb from list', (done) => {
182
+
183
+ var email = "test-signup-" + Date.now() + "@email.com";
184
+ var pwd = "pwd";
185
+
186
+ userService.signup(email, pwd, "Test Firstname", "Test lastname").then(function (savedUser) {
187
+ projectService.create("test-faqkb-create", savedUser._id).then(function (savedProject) {
188
+
189
+ chai.request(server)
190
+ .post('/' + savedProject._id + '/kbsettings')
191
+ .auth(email, pwd)
192
+ .send({}) // can be empty
193
+ .end((err, res) => {
194
+ if (log) { console.log("create kbsettings res.body: ", res.body); }
195
+ res.should.have.status(200);
196
+ res.body.should.be.a('object');
197
+ expect(res.body.id_project).to.equal(savedProject._id.toString());
198
+
199
+ let settings_id = res.body._id;
200
+
201
+ chai.request(server)
202
+ .post('/' + savedProject._id + "/kbsettings/" + settings_id)
203
+ .auth(email, pwd)
204
+ .send({ name: "exampleurl.com/kb/", url: "https://exampleurl.com/kb/" })
205
+ .end((err, res) => {
206
+ if (log) { console.log("add kb to kb settings res.body: ", res.body); }
207
+ res.should.have.status(200);
208
+ res.body.should.be.a('object');
209
+ expect(res.body.kbs).to.have.length(1)
210
+
211
+ let kb_to_delete_id = res.body.kbs[0]._id;
212
+
213
+ chai.request(server)
214
+ .post('/' + savedProject._id + "/kbsettings/" + settings_id)
215
+ .auth(email, pwd)
216
+ .send({ name: "secondurl.com/support/", url: "https://secondurl.com/support/" })
217
+ .end((err, res) => {
218
+ if (log) { console.log("add kb to kb settings res.body: ", res.body); }
219
+ res.should.have.status(200);
220
+ res.body.should.be.a('object');
221
+ expect(res.body.kbs).to.have.length(2)
222
+
223
+ chai.request(server)
224
+ .delete('/' + savedProject._id + "/kbsettings/" + settings_id + "/" + kb_to_delete_id)
225
+ .auth(email, pwd)
226
+ .end((err, res) => {
227
+ if (log) { console.log("delete kb res.body: ", res.body); };
228
+ res.should.have.status(200);
229
+ res.body.should.be.a('object');
230
+ expect(res.body.kbs).to.have.length(1)
231
+ expect(res.body.kbs[0].name).to.equal("secondurl.com/support/");
232
+ expect(res.body.kbs[0].url).to.equal("https://secondurl.com/support/");
233
+
234
+ done();
235
+ })
236
+ })
237
+
238
+ })
239
+ })
240
+ });
241
+ });
242
+
243
+ });
244
+
245
+
246
+
247
+
248
+
249
+ });
250
+
251
+ });
252
+
253
+