@tiledesk/tiledesk-voice-twilio-connector 0.1.28 → 0.2.0-rc6
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/LICENSE +179 -0
- package/README.md +515 -0
- package/index.js +7 -1562
- package/package.json +23 -21
- package/src/app.js +154 -0
- package/src/config/index.js +32 -0
- package/src/controllers/VoiceController.js +493 -0
- package/src/middlewares/httpLogger.js +43 -0
- package/src/models/KeyValueStore.js +78 -0
- package/src/routes/manageApp.js +298 -0
- package/src/routes/voice.js +22 -0
- package/src/services/AiService.js +219 -0
- package/src/services/AiService.sdk.js +367 -0
- package/src/services/IntegrationService.js +74 -0
- package/src/services/MessageService.js +139 -0
- package/src/services/README_SDK.md +107 -0
- package/src/services/SessionService.js +143 -0
- package/src/services/SpeechService.js +134 -0
- package/src/services/TiledeskMessageBuilder.js +135 -0
- package/src/services/TwilioService.js +129 -0
- package/src/services/UploadService.js +78 -0
- package/src/services/channels/TiledeskChannel.js +268 -0
- package/{tiledesk → src/services/channels}/VoiceChannel.js +20 -59
- package/src/services/clients/TiledeskSubscriptionClient.js +78 -0
- package/src/services/index.js +45 -0
- package/src/services/translators/TiledeskTwilioTranslator.js +514 -0
- package/src/utils/fileUtils.js +24 -0
- package/src/utils/logger.js +32 -0
- package/{tiledesk → src/utils}/utils-message.js +6 -21
- package/logs/app.log +0 -3082
- package/routes/manageApp.js +0 -419
- package/tiledesk/KVBaseMongo.js +0 -101
- package/tiledesk/TiledeskChannel.js +0 -363
- package/tiledesk/TiledeskSubscriptionClient.js +0 -135
- package/tiledesk/TiledeskTwilioTranslator.js +0 -707
- package/tiledesk/fileUtils.js +0 -55
- package/tiledesk/services/AiService.js +0 -230
- package/tiledesk/services/IntegrationService.js +0 -81
- package/tiledesk/services/UploadService.js +0 -88
- /package/{winston.js → src/config/logger.js} +0 -0
- /package/{tiledesk → src}/services/voiceEventEmitter.js +0 -0
- /package/{template → src/template}/configure.html +0 -0
- /package/{template → src/template}/css/configure.css +0 -0
- /package/{template → src/template}/css/error.css +0 -0
- /package/{template → src/template}/css/style.css +0 -0
- /package/{template → src/template}/error.html +0 -0
- /package/{tiledesk → src/utils}/constants.js +0 -0
- /package/{tiledesk → src/utils}/errors.js +0 -0
- /package/{tiledesk → src/utils}/utils.js +0 -0
package/routes/manageApp.js
DELETED
|
@@ -1,419 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const express = require("express");
|
|
3
|
-
const bodyParser = require("body-parser")
|
|
4
|
-
const router = express.Router();
|
|
5
|
-
const winston = require("../winston");
|
|
6
|
-
const fs = require("fs");
|
|
7
|
-
const path = require('path');
|
|
8
|
-
const pjson = require('../package.json');
|
|
9
|
-
const handlebars = require('handlebars');
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
// tiledesk clients
|
|
13
|
-
const { TiledeskChannel } = require("../tiledesk/TiledeskChannel")
|
|
14
|
-
const { TiledeskSubscriptionClient } = require('../tiledesk/TiledeskSubscriptionClient');
|
|
15
|
-
|
|
16
|
-
//services
|
|
17
|
-
const { IntegrationService } = require('../tiledesk/services/IntegrationService');
|
|
18
|
-
const { AiService } = require('../tiledesk/services/AiService')
|
|
19
|
-
let integrationService = null;
|
|
20
|
-
let aiService = null;
|
|
21
|
-
|
|
22
|
-
//constant
|
|
23
|
-
const CHANNEL_NAME = require('../tiledesk/constants').CHANNEL_NAME;
|
|
24
|
-
|
|
25
|
-
router.use(bodyParser.json()); // support json encoded bodies
|
|
26
|
-
router.use(express.urlencoded({ extended: true })); // support encoded bodies
|
|
27
|
-
router.use(express.static(path.join(__dirname, '..', '/template')));
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
let API_URL = null;
|
|
31
|
-
let BASE_URL = null;
|
|
32
|
-
let OPENAI_ENDPOINT = null;
|
|
33
|
-
let GPT_KEY = null;
|
|
34
|
-
let BRAND_NAME = null;
|
|
35
|
-
let redis_client = null;
|
|
36
|
-
let db = null;
|
|
37
|
-
|
|
38
|
-
// Handlebars register helpers
|
|
39
|
-
handlebars.registerHelper('isEqual', (a, b) => {
|
|
40
|
-
if (a == b) {
|
|
41
|
-
return true
|
|
42
|
-
} else {
|
|
43
|
-
return false
|
|
44
|
-
}
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
handlebars.registerHelper('json', (a) => {
|
|
48
|
-
return JSON.stringify(a);
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
router.get("/", async (req, res) => {
|
|
52
|
-
res.send("Welcome to Tiledesk-VOICE connector (manage ROUTE)");
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
router.get('/configure', async (req, res) => {
|
|
57
|
-
|
|
58
|
-
winston.debug("(voice) /configure :params", req.query);
|
|
59
|
-
|
|
60
|
-
let project_id = req.query.project_id;
|
|
61
|
-
let token = req.query.token;
|
|
62
|
-
let popup_view = false;
|
|
63
|
-
|
|
64
|
-
if (req.query.view && req.query.view === 'popup') {
|
|
65
|
-
popup_view = true
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
//project_id="65d77f9e514a7c0013f430db"
|
|
69
|
-
//token = "JWT eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2Mzg3OGI5OTZiMjZiYzAwMTMzMDVhZDQiLCJlbWFpbCI6ImdhYnJpZWxlQHRpbGVkZXNrLmNvbSIsImZpcnN0bmFtZSI6IkdhYnJpZWxlIiwibGFzdG5hbWUiOiJQYW5pY28iLCJlbWFpbHZlcmlmaWVkIjp0cnVlLCJpYXQiOjE3MDEwOTUyMTcsImF1ZCI6Imh0dHBzOi8vdGlsZWRlc2suY29tIiwiaXNzIjoiaHR0cHM6Ly90aWxlZGVzay5jb20iLCJzdWIiOiJ1c2VyIiwianRpIjoiMzk2ZWJlNjUtNmYxMS00YzM3LTk2MjItNzEzNjVhMjEzYTlmIn0.AGSCCvVfcbfRslfSfdvjLqc14HUwcojpIRYrtXfk1QbnAD4D6UnorfwWfmEFEYxmMEiRrXGqvuLv0EPKY3PJEZ14Bo9Hy2GAjSnTnawhB_evjP7qXp0Hk3Ny8Zw906zRhcrpXHUYRV821IwAoD8QpjG3fy8wohk6Nmw51eS4H6yRdKmMX3GoARd1ueQPd1G2UVKXOkLJ4G6-zqpBaEQLFPoWaQaFB2-YNDtd_XhFbTUWRbX32NyDsg3OwayCNvbVzgoX3NoLboniidrkysDrPulAffc7pZSE9sGnIN7oCXY4dF2PJBc39WFB8Lv0azUzAL69ChN07ApeeUaI3Eymkg"
|
|
70
|
-
|
|
71
|
-
if (!project_id || !token) {
|
|
72
|
-
let error_message = "Query params project_id and token are required."
|
|
73
|
-
readHTMLFile('/error.html', (err, html) => {
|
|
74
|
-
var template = handlebars.compile(html);
|
|
75
|
-
|
|
76
|
-
var replacements = {
|
|
77
|
-
app_version: pjson.version,
|
|
78
|
-
error_message: error_message
|
|
79
|
-
}
|
|
80
|
-
var html = template(replacements);
|
|
81
|
-
return res.send(html);
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
} else {
|
|
85
|
-
|
|
86
|
-
let proxy_url = BASE_URL + "/webhook/" + project_id
|
|
87
|
-
let status_url = BASE_URL + "/twilio/status"
|
|
88
|
-
|
|
89
|
-
let CONTENT_KEY = CHANNEL_NAME + "-" + project_id;
|
|
90
|
-
|
|
91
|
-
let settings = await db.get(CONTENT_KEY);
|
|
92
|
-
winston.debug("(voice) settings: ", settings);
|
|
93
|
-
|
|
94
|
-
// get departments
|
|
95
|
-
const tdChannel = new TiledeskChannel({
|
|
96
|
-
API_URL: API_URL,
|
|
97
|
-
redis_client: redis_client
|
|
98
|
-
})
|
|
99
|
-
tdChannel.setProjectId(project_id)
|
|
100
|
-
|
|
101
|
-
let departments = await tdChannel.getDepartments(token).catch((err) => {
|
|
102
|
-
winston.error("Error getting departments", err.response);
|
|
103
|
-
return [];
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
//winston.verbose("(sms) found departments", departments)
|
|
107
|
-
if (settings) {
|
|
108
|
-
readHTMLFile('/configure.html', (err, html) => {
|
|
109
|
-
var template = handlebars.compile(html);
|
|
110
|
-
var replacements = {
|
|
111
|
-
app_version: pjson.version,
|
|
112
|
-
project_id: project_id,
|
|
113
|
-
token: token,
|
|
114
|
-
proxy_url: proxy_url,
|
|
115
|
-
status_url: status_url,
|
|
116
|
-
subscription_id: settings.subscriptionId,
|
|
117
|
-
department_id: settings.department_id,
|
|
118
|
-
account_sid: settings.account_sid,
|
|
119
|
-
auth_token: settings.auth_token,
|
|
120
|
-
departments: departments,
|
|
121
|
-
brand_name: BRAND_NAME
|
|
122
|
-
}
|
|
123
|
-
var html = template(replacements);
|
|
124
|
-
res.send(html);
|
|
125
|
-
|
|
126
|
-
})
|
|
127
|
-
} else {
|
|
128
|
-
|
|
129
|
-
subscribe(token, project_id)
|
|
130
|
-
|
|
131
|
-
readHTMLFile('/configure.html', (err, html) => {
|
|
132
|
-
|
|
133
|
-
if (err) {
|
|
134
|
-
winston.error("(voice) error read html file: " + err);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
var template = handlebars.compile(html);
|
|
138
|
-
var replacements = {
|
|
139
|
-
app_version: pjson.version,
|
|
140
|
-
project_id: project_id,
|
|
141
|
-
token: token,
|
|
142
|
-
proxy_url: proxy_url,
|
|
143
|
-
status_url: status_url,
|
|
144
|
-
departments: departments,
|
|
145
|
-
popup_view: popup_view,
|
|
146
|
-
brand_name: BRAND_NAME
|
|
147
|
-
}
|
|
148
|
-
var html = template(replacements);
|
|
149
|
-
res.send(html);
|
|
150
|
-
|
|
151
|
-
})
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
//res.status(200).send("CONNECTED")
|
|
157
|
-
})
|
|
158
|
-
|
|
159
|
-
router.post('/update', async (req, res) => {
|
|
160
|
-
winston.debug("(voice) /update", req.body);
|
|
161
|
-
|
|
162
|
-
let project_id = req.body.project_id;
|
|
163
|
-
let token = req.body.token;
|
|
164
|
-
let department_id = req.body.department;
|
|
165
|
-
let account_sid = req.body.account_sid;
|
|
166
|
-
let auth_token = req.body.auth_token;
|
|
167
|
-
|
|
168
|
-
let CONTENT_KEY = CHANNEL_NAME + "-" + project_id;
|
|
169
|
-
let settings = await db.get(CONTENT_KEY);
|
|
170
|
-
|
|
171
|
-
winston.debug("(voice) /update settings", settings);
|
|
172
|
-
|
|
173
|
-
let proxy_url = BASE_URL + "/webhook/" + project_id;
|
|
174
|
-
let status_url = BASE_URL + "/twilio/status";
|
|
175
|
-
|
|
176
|
-
// get departments
|
|
177
|
-
const tdChannel = new TiledeskChannel({
|
|
178
|
-
API_URL: API_URL,
|
|
179
|
-
redis_client: redis_client
|
|
180
|
-
})
|
|
181
|
-
tdChannel.setProjectId(project_id);
|
|
182
|
-
|
|
183
|
-
let departments = await tdChannel.getDepartments(token).catch((err) => {
|
|
184
|
-
winston.error("Error getting departments", err.response);
|
|
185
|
-
return [];
|
|
186
|
-
});
|
|
187
|
-
winston.debug("(voice) found departments", departments)
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
if (settings) {
|
|
192
|
-
|
|
193
|
-
settings.department_id = department_id;
|
|
194
|
-
settings.account_sid = account_sid;
|
|
195
|
-
settings.auth_token = auth_token;
|
|
196
|
-
|
|
197
|
-
await db.set(CONTENT_KEY, settings);
|
|
198
|
-
|
|
199
|
-
readHTMLFile('/configure.html', (err, html) => {
|
|
200
|
-
var template = handlebars.compile(html);
|
|
201
|
-
var replacements = {
|
|
202
|
-
app_version: pjson.version,
|
|
203
|
-
project_id: project_id,
|
|
204
|
-
token: token,
|
|
205
|
-
proxy_url: proxy_url,
|
|
206
|
-
status_url: status_url,
|
|
207
|
-
show_success_modal: true,
|
|
208
|
-
subscription_id: settings.subscriptionId,
|
|
209
|
-
department_id: settings.department_id,
|
|
210
|
-
account_sid: settings.account_sid,
|
|
211
|
-
auth_token: settings.auth_token,
|
|
212
|
-
departments: departments,
|
|
213
|
-
brand_name: BRAND_NAME
|
|
214
|
-
}
|
|
215
|
-
var html = template(replacements);
|
|
216
|
-
res.send(html);
|
|
217
|
-
})
|
|
218
|
-
|
|
219
|
-
} else {
|
|
220
|
-
|
|
221
|
-
subscribe(token, project_id).then((subscription)=> {
|
|
222
|
-
winston.debug("\n(voice) Subscription: ", subscription)
|
|
223
|
-
|
|
224
|
-
readHTMLFile('/configure.html', (err, html) => {
|
|
225
|
-
var template = handlebars.compile(html);
|
|
226
|
-
var replacements = {
|
|
227
|
-
app_version: pjson.version,
|
|
228
|
-
project_id: project_id,
|
|
229
|
-
token: token,
|
|
230
|
-
proxy_url: proxy_url,
|
|
231
|
-
status_url: status_url,
|
|
232
|
-
show_success_modal: true,
|
|
233
|
-
subscription_id: subscription.subscriptionId,
|
|
234
|
-
account_sid: account_sid,
|
|
235
|
-
auth_token: auth_token,
|
|
236
|
-
department_id: department_id,
|
|
237
|
-
departments: departments,
|
|
238
|
-
brand_name: BRAND_NAME
|
|
239
|
-
}
|
|
240
|
-
var html = template(replacements);
|
|
241
|
-
res.send(html);
|
|
242
|
-
})
|
|
243
|
-
|
|
244
|
-
}).catch((error)=> {
|
|
245
|
-
readHTMLFile('/configure.html', (err, html) => {
|
|
246
|
-
var template = handlebars.compile(html);
|
|
247
|
-
var replacements = {
|
|
248
|
-
app_version: pjson.version,
|
|
249
|
-
project_id: project_id,
|
|
250
|
-
token: token,
|
|
251
|
-
proxy_url: proxy_url,
|
|
252
|
-
status_url: status_url,
|
|
253
|
-
departments: departments,
|
|
254
|
-
show_error_modal: true,
|
|
255
|
-
brand_name: BRAND_NAME
|
|
256
|
-
}
|
|
257
|
-
var html = template(replacements);
|
|
258
|
-
res.send(html);
|
|
259
|
-
})
|
|
260
|
-
})
|
|
261
|
-
|
|
262
|
-
}
|
|
263
|
-
})
|
|
264
|
-
|
|
265
|
-
router.post('/disconnect', async (req, res) => {
|
|
266
|
-
|
|
267
|
-
winston.debug("(voice) /disconnect")
|
|
268
|
-
|
|
269
|
-
let project_id = req.body.project_id;
|
|
270
|
-
let token = req.body.token;
|
|
271
|
-
let subscriptionId = req.body.subscription_id;
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
let CONTENT_KEY = CHANNEL_NAME + "-" + project_id;
|
|
275
|
-
await db.remove(CONTENT_KEY);
|
|
276
|
-
winston.debug("(voice) Content deleted.");
|
|
277
|
-
|
|
278
|
-
let proxy_url = BASE_URL + "/webhook/" + project_id;
|
|
279
|
-
let status_url = BASE_URL + "/twilio/status";
|
|
280
|
-
|
|
281
|
-
// get departments
|
|
282
|
-
const tdChannel = new TiledeskChannel({
|
|
283
|
-
API_URL: API_URL,
|
|
284
|
-
redis_client: redis_client
|
|
285
|
-
})
|
|
286
|
-
tdChannel.setProjectId(project_id)
|
|
287
|
-
|
|
288
|
-
let departments = await tdChannel.getDepartments(token).catch((err) => {
|
|
289
|
-
winston.error("Error getting departments", err.response);
|
|
290
|
-
return [];
|
|
291
|
-
});
|
|
292
|
-
winston.debug("(sms) found departments", departments)
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
unsubscribe(token, project_id, subscriptionId).then((data) => {
|
|
296
|
-
|
|
297
|
-
readHTMLFile('/configure.html', (err, html) => {
|
|
298
|
-
var template = handlebars.compile(html);
|
|
299
|
-
var replacements = {
|
|
300
|
-
app_version: pjson.version,
|
|
301
|
-
project_id: project_id,
|
|
302
|
-
token: token,
|
|
303
|
-
proxy_url: proxy_url,
|
|
304
|
-
status_url: status_url,
|
|
305
|
-
departments: departments,
|
|
306
|
-
brand_name: BRAND_NAME
|
|
307
|
-
}
|
|
308
|
-
var html = template(replacements);
|
|
309
|
-
res.send(html);
|
|
310
|
-
})
|
|
311
|
-
|
|
312
|
-
}).catch((err) => {
|
|
313
|
-
winston.error("(voice) unsubscribe error: " + err);
|
|
314
|
-
})
|
|
315
|
-
|
|
316
|
-
})
|
|
317
|
-
|
|
318
|
-
async function subscribe(token, project_id){
|
|
319
|
-
const tdClient = new TiledeskSubscriptionClient({ API_URL: API_URL, project_id: project_id, token: token })
|
|
320
|
-
|
|
321
|
-
const CONTENT_KEY = CHANNEL_NAME + "-" + project_id;
|
|
322
|
-
const subscription_info = {
|
|
323
|
-
target: BASE_URL + "/tiledesk",
|
|
324
|
-
event: 'message.create.request.channel.'+ CHANNEL_NAME
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
return new Promise((resolve, reject)=> {
|
|
328
|
-
tdClient.subscribe(subscription_info).then(async (data)=> {
|
|
329
|
-
let subscription = data;
|
|
330
|
-
winston.debug("(voice) Subscription: ", subscription)
|
|
331
|
-
|
|
332
|
-
let settings = {
|
|
333
|
-
project_id: project_id,
|
|
334
|
-
token: token,
|
|
335
|
-
subscriptionId: subscription._id,
|
|
336
|
-
secret: subscription.secret
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
db.set(CONTENT_KEY, settings);
|
|
340
|
-
|
|
341
|
-
let settings_data = await db.get(CONTENT_KEY)
|
|
342
|
-
resolve(subscription)
|
|
343
|
-
|
|
344
|
-
}).catch((error)=> {reject(error) })
|
|
345
|
-
|
|
346
|
-
})
|
|
347
|
-
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
async function unsubscribe(token, project_id, subscriptionId){
|
|
351
|
-
const tdClient = new TiledeskSubscriptionClient({ API_URL: API_URL, project_id: project_id, token: token })
|
|
352
|
-
|
|
353
|
-
return new Promise((resolve, reject)=> {
|
|
354
|
-
tdClient.unsubscribe(subscriptionId).then(async (data)=> {
|
|
355
|
-
winston.debug("(voice) Subscription: ", data)
|
|
356
|
-
resolve(data)
|
|
357
|
-
}).catch((error)=> {reject(error) })
|
|
358
|
-
|
|
359
|
-
})
|
|
360
|
-
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
async function startApp(settings, callback) {
|
|
364
|
-
winston.info("(voice) Starting Manage Route");
|
|
365
|
-
|
|
366
|
-
if (!settings.DB) {
|
|
367
|
-
winston.error("(voice)-MANAGE MONGODB_URL is mandatory. Exit...");
|
|
368
|
-
return callback('Missing parameter: MONGODB_URL');
|
|
369
|
-
}else {
|
|
370
|
-
db = settings.DB;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
if (!settings.API_URL) {
|
|
374
|
-
winston.error("(voice)-MANAGE API_URL is mandatory. Exit...");
|
|
375
|
-
return callback('Missing parameter: API_URL');
|
|
376
|
-
} else {
|
|
377
|
-
API_URL = settings.API_URL;
|
|
378
|
-
winston.info("(voice)-MANAGE API_URL: " + API_URL);
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
if (!settings.BASE_URL) {
|
|
382
|
-
winston.error("(voice)-MANAGE BASE_URL is mandatory. Exit...");
|
|
383
|
-
return callback('Missing parameter: BASE_URL');
|
|
384
|
-
} else {
|
|
385
|
-
BASE_URL = settings.BASE_URL;
|
|
386
|
-
winston.info("(voice)-MANAGE BASE_URL: " + BASE_URL);
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
if (!settings.redis_client) {
|
|
390
|
-
winston.error("(voice)-MANAGE redis_client is mandatory. Exit...");
|
|
391
|
-
return callback('Missing parameter: redis_client');
|
|
392
|
-
} else {
|
|
393
|
-
redis_client = settings.redis_client;
|
|
394
|
-
winston.info("(voice)-MANAGE redis_client: " + redis_client);
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
if (settings.BRAND_NAME) {
|
|
399
|
-
BRAND_NAME = settings.BRAND_NAME
|
|
400
|
-
winston.info("(voice)-MANAGE BRAND_NAME: " + BRAND_NAME);
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
function readHTMLFile(templateName, callback) {
|
|
408
|
-
fs.readFile(__dirname +'/..' + '/template' + templateName, { encoding: 'utf-8' },
|
|
409
|
-
function(err, html) {
|
|
410
|
-
if (err) {
|
|
411
|
-
throw err;
|
|
412
|
-
//callback(err);
|
|
413
|
-
} else {
|
|
414
|
-
callback(null, html)
|
|
415
|
-
}
|
|
416
|
-
})
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
module.exports = { router: router, startApp: startApp };
|
package/tiledesk/KVBaseMongo.js
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
const mongodb = require("mongodb");
|
|
2
|
-
var winston = require('../winston');
|
|
3
|
-
|
|
4
|
-
class KVBaseMongo {
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Constructor for KVBaseMongo object
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* const { KVBaseMongo } = require('./KVBaseMongo');
|
|
11
|
-
* let db = new KVBaseMongo("kvstore");
|
|
12
|
-
*
|
|
13
|
-
* @param {KVBASE_COLLECTION} The name of the Mongodb collection used as key-value store. Mandatory.
|
|
14
|
-
*/
|
|
15
|
-
constructor(KVBASE_COLLECTION) {
|
|
16
|
-
if (!KVBASE_COLLECTION) {
|
|
17
|
-
throw new Error('KVBASE_COLLECTION (the name of the Mongodb collection used as key-value store) is mandatory.');
|
|
18
|
-
}
|
|
19
|
-
this.KV_COLLECTION = KVBASE_COLLECTION;
|
|
20
|
-
winston.debug("KV_COLLECTION: " + this.KV_COLLECTION)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
connect(MONGODB_URI, callback) {
|
|
24
|
-
mongodb.MongoClient.connect(MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => {
|
|
25
|
-
if (err) {
|
|
26
|
-
winston.error(err);
|
|
27
|
-
process.exit(1);
|
|
28
|
-
} else {
|
|
29
|
-
this.db = client.db();
|
|
30
|
-
this.db.collection(this.KV_COLLECTION).createIndex(
|
|
31
|
-
{ "key": 1 }, { unique: true }
|
|
32
|
-
);
|
|
33
|
-
//winston.debug("[mongodb] db: ", this.db);
|
|
34
|
-
callback();
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
set(k, v) {
|
|
40
|
-
return new Promise((resolve, reject) => {
|
|
41
|
-
//this.db.set(k, v).then(() => {resolve();});
|
|
42
|
-
this.db.collection(this.KV_COLLECTION).updateOne({key: k}, { $set: { value: v, key: k } }, { upsert: true }, function(err, doc) {
|
|
43
|
-
if (err) {
|
|
44
|
-
reject(err);
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
resolve();
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
reuseConnection(db, callback) {
|
|
54
|
-
this.db = db;
|
|
55
|
-
this.db.collection(this.KV_COLLECTION).createIndex(
|
|
56
|
-
{ "key": 1 }, { unique: true }
|
|
57
|
-
)
|
|
58
|
-
callback();
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
get(k) {
|
|
63
|
-
return new Promise((resolve, reject) => {
|
|
64
|
-
//this.db.get(k).then(value => {resolve(value)});
|
|
65
|
-
|
|
66
|
-
winston.debug("Searching on Collection " + this.KV_COLLECTION + ' for key: '+ k)
|
|
67
|
-
|
|
68
|
-
this.db.collection(this.KV_COLLECTION).findOne({ key: k }, function(err, doc) {
|
|
69
|
-
if (err) {
|
|
70
|
-
winston.error("Error reading mongodb value", err);
|
|
71
|
-
reject(err);
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
if (doc) {
|
|
75
|
-
winston.debug("Doc found with key: " + doc.key);
|
|
76
|
-
resolve(doc.value);
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
winston.debug("No Doc found!");
|
|
80
|
-
resolve(null);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
remove(k) {
|
|
88
|
-
return new Promise((resolve, reject) => {
|
|
89
|
-
this.db.collection(this.KV_COLLECTION).deleteOne({key: k}, function(err) {
|
|
90
|
-
if (err) {
|
|
91
|
-
reject(err);
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
resolve();
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
module.exports = { KVBaseMongo };
|