@tiledesk/tiledesk-server 2.4.99 → 2.4.101
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +8 -0
- package/app.js +47 -9
- package/event/emailEvent.js +13 -0
- package/event/integrationEvent.js +13 -0
- package/models/integrations.js +23 -0
- package/models/kb_setting.js +34 -3
- package/package.json +2 -2
- package/pubmodules/cache/mongoose-cachegoose-fn.js +12 -0
- package/pubmodules/emailNotification/requestNotification.js +1 -0
- package/routes/auth.js +14 -5
- package/routes/email.js +4 -2
- package/routes/faq.js +1 -0
- package/routes/integration.js +199 -0
- package/routes/kb.js +353 -0
- package/routes/kbsettings.js +6 -4
- package/routes/openai.js +53 -15
- 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 +317 -0
- package/services/cacheEnabler.js +5 -0
- package/services/emailService.js +610 -586
- package/services/messageService.js +283 -202
- package/services/openaiService.js +198 -92
- package/services/requestService.js +1764 -1421
- package/services/trainingService.js +6 -2
- package/test/kbRoute.js +134 -0
- package/test/kbsettingsRoute.js +14 -12
- 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
@@ -1,254 +1,336 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
3
|
var Message = require("../models/message");
|
4
|
+
var Project = require("../models/project");
|
4
5
|
var MessageConstants = require("../models/messageConstants");
|
5
6
|
const messageEvent = require('../event/messageEvent');
|
6
7
|
const messagePromiseEvent = require('../event/messagePromiseEvent');
|
7
8
|
var winston = require('../config/winston');
|
9
|
+
var cacheUtil = require("../utils/cacheUtil");
|
10
|
+
var cacheEnabler = require("../services/cacheEnabler");
|
8
11
|
|
9
12
|
class MessageService {
|
10
13
|
|
11
14
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
+
send(sender, senderFullname, recipient, text, id_project, createdBy, attributes, type, metadata, language) {
|
16
|
+
return this.create(sender, senderFullname, recipient, text, id_project, createdBy, MessageConstants.CHAT_MESSAGE_STATUS.SENDING, attributes, type, metadata, language);
|
17
|
+
}
|
15
18
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
winston.debug("Message upsert changeStatus:"+ status);
|
19
|
+
upsert(id, sender, senderFullname, recipient, text, id_project, createdBy, status, attributes, type, metadata, language) {
|
20
|
+
if (!id) {
|
21
|
+
return this.create(sender, senderFullname, recipient, text, id_project, createdBy, status, attributes, type, metadata, language);
|
22
|
+
} else {
|
23
|
+
winston.debug("Message upsert changeStatus:" + status);
|
21
24
|
return this.changeStatus(id, status);
|
22
|
-
}
|
23
|
-
}
|
24
|
-
|
25
|
-
create(sender, senderFullname, recipient, text, id_project, createdBy, status, attributes, type, metadata, language, channel_type, channel) {
|
26
|
-
let message = {
|
27
|
-
sender: sender,
|
28
|
-
senderFullname: senderFullname,
|
29
|
-
recipient: recipient,
|
30
|
-
text: text,
|
31
|
-
id_project: id_project,
|
32
|
-
createdBy: createdBy,
|
33
|
-
status: status,
|
34
|
-
attributes: attributes,
|
35
|
-
type: type,
|
36
|
-
metadata: metadata,
|
37
|
-
language: language,
|
38
|
-
channel_type: channel_type,
|
39
|
-
channel: channel
|
40
|
-
};
|
41
|
-
return this.save(message);
|
42
|
-
}
|
43
|
-
|
44
|
-
save(message) {
|
45
|
-
var that = this;
|
46
|
-
winston.debug('message.save called');
|
47
|
-
|
48
|
-
let sender = message.sender;
|
49
|
-
let senderFullname = message.senderFullname;
|
50
|
-
let recipient = message.recipient;
|
51
|
-
let recipientFullname = message.recipientFullname;
|
52
|
-
let text = message.text;
|
53
|
-
let id_project = message.id_project;
|
54
|
-
let createdBy = message.createdBy;
|
55
|
-
let status = message.status;
|
56
|
-
let attributes = message.attributes;
|
57
|
-
let type = message.type;
|
58
|
-
let metadata = message.metadata;
|
59
|
-
let language = message.language;
|
60
|
-
let channel_type = message.channel_type;
|
61
|
-
let channel = message.channel;
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
return new Promise(function (resolve, reject) {
|
66
|
-
|
67
|
-
if (!createdBy) {
|
68
|
-
createdBy = sender;
|
69
25
|
}
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
26
|
+
}
|
27
|
+
|
28
|
+
create(sender, senderFullname, recipient, text, id_project, createdBy, status, attributes, type, metadata, language, channel_type, channel) {
|
29
|
+
let message = {
|
30
|
+
sender: sender,
|
31
|
+
senderFullname: senderFullname,
|
32
|
+
recipient: recipient,
|
33
|
+
text: text,
|
34
|
+
id_project: id_project,
|
35
|
+
createdBy: createdBy,
|
36
|
+
status: status,
|
37
|
+
attributes: attributes,
|
38
|
+
type: type,
|
39
|
+
metadata: metadata,
|
40
|
+
language: language,
|
41
|
+
channel_type: channel_type,
|
42
|
+
channel: channel
|
43
|
+
};
|
44
|
+
return this.save(message);
|
45
|
+
}
|
46
|
+
|
47
|
+
save(message) {
|
48
|
+
|
49
|
+
let total_1;
|
50
|
+
let total_e;
|
51
|
+
let total_1_end_e;
|
52
|
+
let total_2;
|
53
|
+
|
54
|
+
var that = this;
|
55
|
+
winston.debug('message.save called');
|
56
|
+
|
57
|
+
message.createdAt = new Date();
|
58
|
+
|
59
|
+
// let start1 = Date.now();
|
60
|
+
// let project;
|
61
|
+
// let payload;
|
62
|
+
// let q = Project.findOne({ _id: message.id_project, status: 100 });
|
63
|
+
// if (cacheEnabler.project) {
|
64
|
+
// q.cache(cacheUtil.longTTL, "projects:id:" + message.id_project) //project_cache
|
65
|
+
// winston.debug('project cache enabled for /project detail');
|
66
|
+
// }
|
67
|
+
// q.exec(async function (err, p) {
|
68
|
+
// if (err) {
|
69
|
+
// winston.error('Error getting project ', err);
|
70
|
+
// }
|
71
|
+
// if (!p) {
|
72
|
+
// winston.warn('Project not found ');
|
73
|
+
// }
|
74
|
+
// //TODO REMOVE settings from project
|
75
|
+
// project = p;
|
76
|
+
// payload = {
|
77
|
+
// project: project,
|
78
|
+
// message: message
|
79
|
+
// }
|
80
|
+
|
81
|
+
// let end1 = Date.now();
|
82
|
+
// total_1 = end1 - start1;
|
83
|
+
|
84
|
+
// let start_e = Date.now();
|
85
|
+
// let result = await messageEvent.emit('message.create.quote.before', payload);
|
86
|
+
// let end_e = Date.now();
|
87
|
+
// total_e = end_e - start_e;
|
88
|
+
// if (result === false) {
|
89
|
+
// winston.info("Messages limits reached for project " + project._id)
|
90
|
+
// // return false in the second phase
|
91
|
+
// }
|
92
|
+
// });
|
93
|
+
|
94
|
+
// let start2 = Date.now();
|
95
|
+
let sender = message.sender;
|
96
|
+
let senderFullname = message.senderFullname;
|
97
|
+
let recipient = message.recipient;
|
98
|
+
let recipientFullname = message.recipientFullname;
|
99
|
+
let text = message.text;
|
100
|
+
let id_project = message.id_project;
|
101
|
+
let createdBy = message.createdBy;
|
102
|
+
let status = message.status;
|
103
|
+
let attributes = message.attributes;
|
104
|
+
let type = message.type;
|
105
|
+
let metadata = message.metadata;
|
106
|
+
let language = message.language;
|
107
|
+
let channel_type = message.channel_type;
|
108
|
+
let channel = message.channel;
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
return new Promise(function (resolve, reject) {
|
113
|
+
|
114
|
+
if (!createdBy) {
|
115
|
+
createdBy = sender;
|
94
116
|
}
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
117
|
+
|
118
|
+
var beforeMessage = {
|
119
|
+
sender: sender, senderFullname: senderFullname
|
120
|
+
, recipient: recipient, recipientFullname: recipientFullname
|
121
|
+
, text: text, id_project: id_project, createdBy: createdBy, status: status, attributes: attributes,
|
122
|
+
type: type, metadata: metadata, language: language, channel_type: channel_type, channel: channel
|
123
|
+
};
|
124
|
+
|
125
|
+
var messageToCreate = beforeMessage;
|
126
|
+
winston.debug('messageToCreate before', messageToCreate);
|
127
|
+
// messageEvent.emit('message.create.simple.before', {beforeMessage:beforeMessage});
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
messagePromiseEvent.emit('message.create.simple.before', { beforeMessage: beforeMessage }).then(results => {
|
132
|
+
winston.debug('message.create.simple.before results', results);
|
133
|
+
winston.debug('message.create.simple.before results prototype: ' + Object.prototype.toString.call(results));
|
134
|
+
|
135
|
+
if (results) {
|
136
|
+
winston.debug('message.create.simple.before results.length: ' + results.length); //TODO ELIMINA DOPO CHE CREA CRASH
|
137
|
+
}
|
138
|
+
|
139
|
+
/*
|
140
|
+
if (results ) { //NN HA MAI FUNZIONATO. LA MADIFICA DEL VALORE AVVENIVA PER PUNTATORE
|
141
|
+
winston.info('message.create.simple.before results.beforeMessage', results[0].beforeMessage);
|
142
|
+
messageToCreate = results[0].beforeMessage;
|
143
|
+
}
|
144
|
+
*/
|
145
|
+
|
146
|
+
winston.debug('messageToCreate', messageToCreate);
|
147
|
+
|
148
|
+
|
149
|
+
// if (id_project) {
|
150
|
+
|
151
|
+
var newMessage = new Message({
|
152
|
+
sender: messageToCreate.sender,
|
153
|
+
senderFullname: messageToCreate.senderFullname,
|
154
|
+
recipient: messageToCreate.recipient,
|
155
|
+
recipientFullname: messageToCreate.recipientFullname, //for direct
|
156
|
+
type: messageToCreate.type,
|
157
|
+
text: messageToCreate.text,
|
158
|
+
id_project: messageToCreate.id_project,
|
159
|
+
createdBy: messageToCreate.createdBy,
|
160
|
+
updatedBy: messageToCreate.createdBy,
|
161
|
+
status: messageToCreate.status,
|
162
|
+
metadata: messageToCreate.metadata,
|
163
|
+
attributes: messageToCreate.attributes,
|
164
|
+
language: messageToCreate.language,
|
165
|
+
channel_type: messageToCreate.channel_type,
|
166
|
+
channel: messageToCreate.channel
|
167
|
+
});
|
168
|
+
|
169
|
+
// winston.debug("create new message", newMessage);
|
170
|
+
|
171
|
+
return newMessage.save(function (err, savedMessage) {
|
172
|
+
if (err) {
|
173
|
+
winston.error("Error saving the message", { err: err, message: message, newMessage: newMessage });
|
174
|
+
return reject(err);
|
175
|
+
}
|
176
|
+
winston.verbose("Message created", savedMessage.toObject());
|
177
|
+
|
178
|
+
messageEvent.emit('message.create.simple', savedMessage);
|
179
|
+
that.emitMessage(savedMessage);
|
180
|
+
|
181
|
+
let q = Project.findOne({ _id: message.id_project, status: 100 });
|
182
|
+
if (cacheEnabler.project) {
|
183
|
+
q.cache(cacheUtil.longTTL, "projects:id:" + message.id_project) //project_cache
|
184
|
+
winston.debug('project cache enabled for /project detail');
|
185
|
+
}
|
186
|
+
q.exec(async function (err, p) {
|
123
187
|
if (err) {
|
124
|
-
winston.error(
|
125
|
-
|
188
|
+
winston.error('Error getting project ', err);
|
189
|
+
}
|
190
|
+
if (!p) {
|
191
|
+
winston.warn('Project not found ');
|
192
|
+
}
|
193
|
+
//TODO REMOVE settings from project
|
194
|
+
let payload = {
|
195
|
+
project: p,
|
196
|
+
message: message
|
197
|
+
}
|
198
|
+
|
199
|
+
let result = await messageEvent.emit('message.create.quote', payload);
|
200
|
+
if (result === false) {
|
201
|
+
winston.info("Messages limits reached for project " + p._id)
|
202
|
+
// return false in the second phase
|
126
203
|
}
|
127
|
-
winston.verbose("Message created", savedMessage.toObject());
|
128
|
-
|
129
|
-
messageEvent.emit('message.create.simple', savedMessage);
|
130
|
-
|
131
|
-
that.emitMessage(savedMessage);
|
132
|
-
// if (savedMessage.status === MessageConstants.CHAT_MESSAGE_STATUS.RECEIVED) {
|
133
|
-
// messageEvent.emit('message.received.simple', savedMessage);
|
134
|
-
// }
|
135
|
-
|
136
|
-
// if (savedMessage.status === MessageConstants.CHAT_MESSAGE_STATUS.SENDING) {
|
137
|
-
// messageEvent.emit('message.sending.simple', savedMessage);
|
138
|
-
// }
|
139
|
-
|
140
|
-
|
141
|
-
return resolve(savedMessage);
|
142
204
|
});
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
});
|
147
205
|
|
148
|
-
|
149
|
-
|
150
206
|
|
151
|
-
|
207
|
+
// if (savedMessage.status === MessageConstants.CHAT_MESSAGE_STATUS.RECEIVED) {
|
208
|
+
// messageEvent.emit('message.received.simple', savedMessage);
|
209
|
+
// }
|
152
210
|
|
153
|
-
|
211
|
+
// if (savedMessage.status === MessageConstants.CHAT_MESSAGE_STATUS.SENDING) {
|
212
|
+
// messageEvent.emit('message.sending.simple', savedMessage);
|
213
|
+
// }
|
154
214
|
|
215
|
+
// console.log("total 1: ", total_1);
|
216
|
+
// console.log("total e: ", total_e);
|
217
|
+
// total_1_end_e = total_1 + total_e;
|
218
|
+
// console.log("total 1 + e: ", total_1_end_e);
|
219
|
+
// console.log("total 2: ", total_2);
|
155
220
|
|
156
221
|
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
222
|
+
return resolve(savedMessage);
|
223
|
+
});
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
});
|
161
228
|
|
162
|
-
if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.SENDING) {
|
163
|
-
messageEvent.emit('message.sending.simple', message);
|
164
|
-
}
|
165
229
|
|
166
|
-
|
167
|
-
|
230
|
+
|
231
|
+
|
232
|
+
});
|
233
|
+
|
234
|
+
};
|
235
|
+
|
236
|
+
|
237
|
+
|
238
|
+
emitMessage(message) {
|
239
|
+
if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.RECEIVED) {
|
240
|
+
messageEvent.emit('message.received.simple', message);
|
241
|
+
}
|
242
|
+
|
243
|
+
if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.SENDING) {
|
244
|
+
messageEvent.emit('message.sending.simple', message);
|
245
|
+
}
|
246
|
+
|
247
|
+
if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.SENT) {
|
248
|
+
messageEvent.emit('message.sent.simple', message);
|
249
|
+
}
|
250
|
+
|
251
|
+
if (message.status === MessageConstants.CHAT_MESSAGE_STATUS.DELIVERED) {
|
252
|
+
messageEvent.emit('message.delivered.simple', message);
|
253
|
+
}
|
168
254
|
}
|
169
255
|
|
170
|
-
|
171
|
-
|
256
|
+
// TODO must update also message.attributes from chat21
|
257
|
+
// attento già scatta su chat21handler
|
258
|
+
|
259
|
+
changeStatus(message_id, newstatus) {
|
260
|
+
winston.debug("changeStatus. " + message_id + " " + newstatus);
|
261
|
+
var that = this;
|
262
|
+
return new Promise(function (resolve, reject) {
|
263
|
+
// winston.debug("request_id", request_id);
|
264
|
+
// winston.debug("newstatus", newstatus);
|
265
|
+
|
266
|
+
return Message.findByIdAndUpdate(message_id, { status: newstatus }, { new: true, upsert: false }, function (err, updatedMessage) {
|
267
|
+
if (err) {
|
268
|
+
winston.error(err);
|
269
|
+
return reject(err);
|
270
|
+
}
|
271
|
+
messageEvent.emit('message.update.simple', updatedMessage);
|
272
|
+
// winston.debug("updatedMessage", updatedMessage);
|
273
|
+
|
274
|
+
that.emitMessage(updatedMessage);
|
275
|
+
return resolve(updatedMessage);
|
276
|
+
});
|
277
|
+
});
|
278
|
+
|
172
279
|
}
|
173
|
-
}
|
174
|
-
|
175
|
-
// TODO must update also message.attributes from chat21
|
176
|
-
// attento già scatta su chat21handler
|
177
|
-
|
178
|
-
changeStatus(message_id, newstatus) {
|
179
|
-
winston.debug("changeStatus. "+message_id + " "+ newstatus);
|
180
|
-
var that = this;
|
181
|
-
return new Promise(function (resolve, reject) {
|
182
|
-
// winston.debug("request_id", request_id);
|
183
|
-
// winston.debug("newstatus", newstatus);
|
184
|
-
|
185
|
-
return Message.findByIdAndUpdate(message_id, {status: newstatus}, {new: true, upsert:false}, function(err, updatedMessage) {
|
186
|
-
if (err) {
|
187
|
-
winston.error(err);
|
188
|
-
return reject(err);
|
189
|
-
}
|
190
|
-
messageEvent.emit('message.update.simple',updatedMessage);
|
191
|
-
// winston.debug("updatedMessage", updatedMessage);
|
192
280
|
|
193
|
-
that.emitMessage(updatedMessage);
|
194
|
-
return resolve(updatedMessage);
|
195
|
-
});
|
196
|
-
});
|
197
281
|
|
198
|
-
}
|
199
282
|
|
283
|
+
getTranscriptByRequestId(requestid, id_project) {
|
284
|
+
winston.debug("requestid", requestid);
|
285
|
+
winston.debug("id_project", id_project);
|
286
|
+
var that = this;
|
287
|
+
return new Promise(function (resolve, reject) {
|
288
|
+
return Message.find({ "recipient": requestid, id_project: id_project }).sort({ createdAt: 'asc' }).exec(function (err, messages) {
|
289
|
+
if (err) {
|
290
|
+
winston.error("Error getting the transcript", err);
|
291
|
+
return reject(err);
|
292
|
+
}
|
200
293
|
|
294
|
+
winston.debug("messages", messages);
|
201
295
|
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
var that = this;
|
206
|
-
return new Promise(function (resolve, reject) {
|
207
|
-
return Message.find({"recipient": requestid, id_project: id_project}).sort({createdAt: 'asc'}).exec(function(err, messages) {
|
208
|
-
if (err) {
|
209
|
-
winston.error("Error getting the transcript", err);
|
210
|
-
return reject(err);
|
211
|
-
}
|
212
|
-
|
213
|
-
winston.debug("messages", messages);
|
296
|
+
if (!messages) {
|
297
|
+
return resolve(messages);
|
298
|
+
}
|
214
299
|
|
215
|
-
if(!messages){
|
216
|
-
return resolve(messages);
|
217
|
-
}
|
218
300
|
|
219
|
-
|
220
301
|
|
221
|
-
|
222
|
-
|
302
|
+
var transcript = '';
|
303
|
+
// messages.forEach(message => {
|
223
304
|
for (var i = 0; i < messages.length; i++) {
|
224
305
|
var message = messages[i];
|
225
306
|
// winston.debug("message", message);
|
226
307
|
// winston.debug("message.createdAt", message.createdAt);
|
227
|
-
|
228
308
|
|
229
|
-
|
309
|
+
|
310
|
+
transcript = transcript +
|
230
311
|
message.createdAt.toLocaleString('it', { timeZone: 'UTC' }) +
|
231
|
-
' ' + message.senderFullname +
|
312
|
+
' ' + message.senderFullname +
|
232
313
|
': ' + message.text;
|
233
314
|
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
315
|
+
//not add line break for last message
|
316
|
+
if (i < messages.length - 1) {
|
317
|
+
transcript = transcript + '\r\n';
|
318
|
+
}
|
238
319
|
|
239
|
-
|
320
|
+
// winston.debug("transcript", transcript);
|
240
321
|
}
|
241
|
-
|
322
|
+
// });
|
242
323
|
|
243
|
-
|
324
|
+
// winston.debug("final transcript", transcript);
|
244
325
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
326
|
+
// each message in messages
|
327
|
+
// p [#{message.createdAt.toLocaleString('it', { timeZone: 'UTC' })}] #{message.senderFullname}: #{message.text}
|
328
|
+
resolve(transcript);
|
329
|
+
|
330
|
+
});
|
249
331
|
});
|
250
|
-
}
|
251
|
-
|
332
|
+
}
|
333
|
+
|
252
334
|
|
253
335
|
|
254
336
|
|
@@ -257,7 +339,6 @@ class MessageService {
|
|
257
339
|
|
258
340
|
|
259
341
|
|
260
|
-
|
261
342
|
}
|
262
343
|
|
263
344
|
|