@tiledesk/tiledesk-server 2.27.0 → 2.27.1
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/CHANGELOG.md +3 -0
- package/channels/chat21/chat21Handler.js +553 -877
- package/models/chat21SyncTrace.js +53 -0
- package/package.json +1 -1
- package/services/chat21SyncTracer.js +110 -0
- package/services/requestService.js +58 -1
|
@@ -7,1015 +7,691 @@ const groupEvent = require('../../event/groupEvent');
|
|
|
7
7
|
const chat21Event = require('./chat21Event');
|
|
8
8
|
const leadEvent = require('../../event/leadEvent');
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
10
|
+
const messageService = require('../../services/messageService');
|
|
11
|
+
const MessageConstants = require("../../models/messageConstants");
|
|
12
|
+
const ChannelConstants = require("../../models/channelConstants");
|
|
13
|
+
const winston = require('../../config/winston');
|
|
14
|
+
const Request = require("../../models/request");
|
|
15
|
+
const chat21Config = require('./chat21Config');
|
|
16
|
+
const chat21 = require('./chat21Client');
|
|
17
|
+
const chat21SyncTracer = require('../../services/chat21SyncTracer');
|
|
21
18
|
|
|
22
19
|
const MaskData = require("maskdata");
|
|
23
20
|
|
|
24
21
|
const maskPasswordOptions = {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
// var chat21Util = require('./chat21Util');
|
|
42
|
-
// var tiledeskUtil = require('./tiledesk-util');
|
|
43
|
-
|
|
44
|
-
var adminToken = process.env.CHAT21_ADMIN_TOKEN || chat21Config.adminToken;
|
|
45
|
-
|
|
22
|
+
// Character to mask the data. default value is '*'
|
|
23
|
+
maskWith: "*",
|
|
24
|
+
//Should be positive Integer
|
|
25
|
+
// If the starting 'n' digits needs to be unmasked
|
|
26
|
+
// Default value is 4
|
|
27
|
+
unmaskedStartDigits: 3,
|
|
28
|
+
// Should be positive Integer
|
|
29
|
+
//If the ending 'n' digits needs to be unmasked
|
|
30
|
+
// Default value is 1
|
|
31
|
+
unmaskedEndDigits: 2
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
const adminToken = process.env.CHAT21_ADMIN_TOKEN || chat21Config.adminToken;
|
|
46
36
|
const masked_adminToken = MaskData.maskPhone(adminToken, maskPasswordOptions);
|
|
37
|
+
winston.info('Chat21Handler adminToken: ' + masked_adminToken);
|
|
38
|
+
|
|
39
|
+
// chat21-node-sdk resolves on any HTTP status code: a resolved promise does not mean the conversation was archived
|
|
40
|
+
function archiveConversation(event, request, user_id) {
|
|
41
|
+
winston.debug("[Chat21Handler] - " + event + " - Archiving conversation: " + request.request_id + " for " + user_id);
|
|
42
|
+
|
|
43
|
+
return chat21SyncTracer.traceCall(event, request, 'archive', { user_id: user_id }, () => {
|
|
44
|
+
return chat21.conversations.archive(request.request_id, user_id);
|
|
45
|
+
}).then(function (data) {
|
|
46
|
+
winston.verbose("[Chat21Handler] - " + event + " - Chat21 conversation archived for " + user_id + " result " + JSON.stringify(data));
|
|
47
|
+
chat21Event.emit('conversation.archived', data);
|
|
48
|
+
}).catch(function (err) {
|
|
49
|
+
winston.error("[Chat21Handler] - " + event + " - Error archiving chat21 conversation for " + user_id + ": ", err);
|
|
50
|
+
chat21Event.emit('conversation.archived.error', err);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
47
53
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
54
|
+
// Traces the exception and rethrows it, so the handler fails exactly as it would without tracing
|
|
55
|
+
function runTraced(event, request, fn) {
|
|
56
|
+
try {
|
|
57
|
+
fn();
|
|
58
|
+
} catch (err) {
|
|
59
|
+
chat21SyncTracer.trace(event, request, 'handler.exception', 'error', { error: chat21SyncTracer.describeError(err) });
|
|
60
|
+
throw err;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
51
63
|
|
|
52
64
|
|
|
53
65
|
class Chat21Handler {
|
|
54
66
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
chat21.conversations.typing(message.recipient, message.sender, message.text, timestamp).finally(function() {
|
|
65
|
-
return resolve();
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
typing(message, timestamp) {
|
|
68
|
+
return new Promise(function (resolve, reject) {
|
|
69
|
+
if (message.attributes &&
|
|
70
|
+
message.attributes.subtype &&
|
|
71
|
+
(message.attributes.subtype === 'info' || message.attributes.subtype === 'private')) {
|
|
72
|
+
return resolve();
|
|
73
|
+
} else {
|
|
74
|
+
chat21.conversations.typing(message.recipient, message.sender, message.text, timestamp).finally(function () {
|
|
75
|
+
return resolve();
|
|
70
76
|
});
|
|
71
|
-
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
72
80
|
|
|
73
81
|
|
|
74
|
-
|
|
82
|
+
listen() {
|
|
75
83
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
winston.debug("Chat21Handler listener start ");
|
|
79
|
-
|
|
84
|
+
winston.debug("Chat21Handler listener start ");
|
|
80
85
|
|
|
81
|
-
if (process.env.SYNC_CHAT21_GROUPS !=="true") {
|
|
82
|
-
winston.info("Sync Tiledesk to Chat21 groups disabled");
|
|
83
|
-
// return; questo distrugge il tread. attento non lo mettere +
|
|
84
|
-
}
|
|
85
86
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
// su projectUser create e update
|
|
91
|
-
authEvent.on('user.signup', function(userData) {
|
|
92
|
-
var firstName = userData.savedUser.firstname;
|
|
93
|
-
var lastName = userData.savedUser.lastname;
|
|
94
|
-
var email = userData.savedUser.email;
|
|
95
|
-
var current_user = userData.savedUser.id;
|
|
96
|
-
|
|
97
|
-
setImmediate(() => {
|
|
98
|
-
winston.debug("Chat21Handler on user.signup ", userData);
|
|
99
|
-
|
|
100
|
-
chat21.auth.setAdminToken(adminToken);
|
|
101
|
-
|
|
102
|
-
// create: function(firstname, lastname, email, current_user){
|
|
103
|
-
chat21.contacts.create(firstName, lastName, email, current_user).then(function(data) {
|
|
104
|
-
winston.verbose("Chat21 contact created: " + JSON.stringify(data));
|
|
105
|
-
chat21Event.emit('contact.create', data);
|
|
106
|
-
}).catch(function(err) {
|
|
107
|
-
winston.error("Error creating chat21 contact ", err);
|
|
108
|
-
chat21Event.emit('contact.create.error', err);
|
|
109
|
-
});
|
|
87
|
+
if (process.env.SYNC_CHAT21_GROUPS !== "true") {
|
|
88
|
+
winston.info("Sync Tiledesk to Chat21 groups disabled");
|
|
89
|
+
}
|
|
110
90
|
|
|
111
|
-
|
|
91
|
+
/** AUTH EVENTS */
|
|
92
|
+
authEvent.on('user.signup', function (userData) {
|
|
93
|
+
const firstName = userData.savedUser.firstname;
|
|
94
|
+
const lastName = userData.savedUser.lastname;
|
|
95
|
+
const email = userData.savedUser.email;
|
|
96
|
+
const current_user = userData.savedUser.id;
|
|
97
|
+
|
|
98
|
+
setImmediate(() => {
|
|
99
|
+
winston.debug("Chat21Handler on user.signup ", userData);
|
|
100
|
+
|
|
101
|
+
chat21.auth.setAdminToken(adminToken);
|
|
102
|
+
chat21.contacts.create(firstName, lastName, email, current_user).then(function (data) {
|
|
103
|
+
winston.verbose("Chat21 contact created: " + JSON.stringify(data));
|
|
104
|
+
chat21Event.emit('contact.create', data);
|
|
105
|
+
}).catch(function (err) {
|
|
106
|
+
winston.error("Error creating chat21 contact ", err);
|
|
107
|
+
chat21Event.emit('contact.create.error', err);
|
|
112
108
|
});
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
chat21Event.emit('contact.update.error', err);
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
});
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
authEvent.on('user.update', function (userData) {
|
|
113
|
+
const firstName = userData.updatedUser.firstname;
|
|
114
|
+
const lastName = userData.updatedUser.lastname;
|
|
115
|
+
const current_user = userData.updatedUser.id;
|
|
116
|
+
|
|
117
|
+
setImmediate(() => {
|
|
118
|
+
winston.debug("Chat21Handler on user.update ", userData);
|
|
119
|
+
|
|
120
|
+
chat21.auth.setAdminToken(adminToken);
|
|
121
|
+
chat21.contacts.update(firstName, lastName, current_user).then(function (data) {
|
|
122
|
+
winston.verbose("Chat21 contact updated: " + JSON.stringify(data));
|
|
123
|
+
chat21Event.emit('contact.update', data);
|
|
124
|
+
}).catch(function (err) {
|
|
125
|
+
winston.error("Error updating chat21 contact ", err);
|
|
126
|
+
chat21Event.emit('contact.update.error', err);
|
|
135
127
|
});
|
|
136
128
|
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
/** BOT EVENTS */
|
|
133
|
+
botEvent.on('faqbot.create', function (bot) {
|
|
134
|
+
const firstName = bot.name;
|
|
135
|
+
const lastName = "";
|
|
136
|
+
const email = "";
|
|
137
|
+
const current_user = "bot_" + bot.id;
|
|
138
|
+
|
|
139
|
+
setImmediate(() => {
|
|
140
|
+
winston.debug("[Chat21Handler] - faqbot.create - Bot create: ", bot);
|
|
141
|
+
|
|
142
|
+
chat21.auth.setAdminToken(adminToken);
|
|
143
|
+
chat21.contacts.create(firstName, lastName, email, current_user).then(function (data) {
|
|
144
|
+
winston.verbose("[Chat21Handler] - faqbot.create - Chat21 contact created: " + JSON.stringify(data));
|
|
145
|
+
chat21Event.emit('contact.create', data);
|
|
146
|
+
}).catch(function (err) {
|
|
147
|
+
winston.error("[Chat21Handler] - faqbot.create - Error creating chat21 contact: ", err);
|
|
148
|
+
chat21Event.emit('contact.create.error', err);
|
|
149
|
+
});
|
|
137
150
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
var lastName = "";
|
|
141
|
-
var email = "";
|
|
142
|
-
// botprefix
|
|
143
|
-
var current_user = "bot_"+bot.id;
|
|
144
|
-
|
|
145
|
-
setImmediate(() => {
|
|
146
|
-
winston.debug("Chat21Handler on faqbot.create ", bot);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
147
153
|
|
|
148
|
-
|
|
154
|
+
botEvent.on('faqbot.update', function (bot) {
|
|
155
|
+
const firstName = bot.name;
|
|
156
|
+
const lastName = "";
|
|
157
|
+
const current_user = "bot_" + bot.id;
|
|
149
158
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
winston.verbose("Chat21 contact created: " + JSON.stringify(data));
|
|
153
|
-
chat21Event.emit('contact.create', data);
|
|
154
|
-
}).catch(function(err) {
|
|
155
|
-
winston.error("Error creating chat21 contact ", err);
|
|
156
|
-
chat21Event.emit('contact.create.error', err);
|
|
157
|
-
});
|
|
159
|
+
setImmediate(() => {
|
|
160
|
+
winston.debug("[Chat21Handler] - faqbot.update - Bot update: ", bot);
|
|
158
161
|
|
|
159
|
-
|
|
162
|
+
chat21.auth.setAdminToken(adminToken);
|
|
163
|
+
chat21.contacts.update(firstName, lastName, current_user).then(function (data) {
|
|
164
|
+
winston.verbose("[Chat21Handler] - faqbot.update - Chat21 contact updated: " + JSON.stringify(data));
|
|
165
|
+
chat21Event.emit('contact.update', data);
|
|
166
|
+
}).catch(function (err) {
|
|
167
|
+
winston.error("[Chat21Handler] - faqbot.update - Error updating chat21 contact: ", err);
|
|
168
|
+
chat21Event.emit('contact.update.error', err);
|
|
160
169
|
});
|
|
161
170
|
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
/** LEAD EVENTS */
|
|
175
|
+
leadEvent.on('lead.update', function (lead) {
|
|
176
|
+
|
|
177
|
+
setImmediate(async () => {
|
|
178
|
+
winston.debug("[Chat21Handler] - lead.update - Lead update: ", lead);
|
|
179
|
+
|
|
180
|
+
let requests = [];
|
|
181
|
+
try {
|
|
182
|
+
requests = await Request.find({ lead: lead._id, id_project: lead.id_project });
|
|
183
|
+
if (requests.length === 0) {
|
|
184
|
+
winston.verbose("[Chat21Handler] - lead.update - No request found for lead id " + lead._id);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
} catch (error) {
|
|
188
|
+
winston.error("[Chat21Handler] - lead.update - Error getting requests by lead: ", error);
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
162
191
|
|
|
192
|
+
chat21.auth.setAdminToken(adminToken);
|
|
163
193
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
setImmediate(() => {
|
|
171
|
-
winston.debug("Chat21Handler on faqbot.create ", bot);
|
|
194
|
+
requests.forEach((request) => {
|
|
195
|
+
try {
|
|
196
|
+
if (request.channelOutbound.name !== ChannelConstants.CHAT21) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
winston.verbose("[Chat21Handler] - lead.update - Chat21 group update for request: ", request);
|
|
172
200
|
|
|
173
|
-
|
|
201
|
+
const groupName = request.subject || lead.fullname;
|
|
174
202
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
});
|
|
203
|
+
chat21.groups.update(groupName, undefined, undefined, request.request_id).then(function (data) {
|
|
204
|
+
winston.verbose("[Chat21Handler] - lead.update - Chat21 group updated for request: " + JSON.stringify(data));
|
|
205
|
+
chat21Event.emit('group.update', data);
|
|
206
|
+
}).catch(function (err) {
|
|
207
|
+
winston.error("[Chat21Handler] - lead.update - Error updating chat21 group: ", err);
|
|
208
|
+
chat21Event.emit('group.update.error', err);
|
|
209
|
+
});
|
|
183
210
|
|
|
211
|
+
const gattributes = { userFullname: lead.fullname, userEmail: lead.email };
|
|
212
|
+
chat21.groups.updateAttributes(gattributes, request.request_id).then(function (data) {
|
|
213
|
+
winston.verbose("[Chat21Handler] - lead.update - Chat21 group attributes updated for request: " + JSON.stringify(data));
|
|
214
|
+
chat21Event.emit('group.update', data);
|
|
215
|
+
chat21Event.emit('group.attributes.update', data);
|
|
216
|
+
}).catch(function (err) {
|
|
217
|
+
winston.error("[Chat21Handler] - lead.update - Error updating chat21 group attributes: ", err);
|
|
218
|
+
chat21Event.emit('group.attributes.update.error', err);
|
|
184
219
|
});
|
|
220
|
+
} catch (error) {
|
|
221
|
+
winston.error("[Chat21Handler] - lead.update - Error processing request " + request.request_id + ": ", error);
|
|
222
|
+
}
|
|
185
223
|
});
|
|
224
|
+
});
|
|
225
|
+
});
|
|
186
226
|
|
|
227
|
+
/** MESSAGE EVENTS */
|
|
228
|
+
messageEvent.on('message.test', function (message) {
|
|
187
229
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
Request.find({lead: lead._id, id_project: lead.id_project}, function(err, requests) {
|
|
196
|
-
|
|
197
|
-
if (err) {
|
|
198
|
-
winston.error("Error getting request by lead", err);
|
|
199
|
-
return 0;
|
|
200
|
-
}
|
|
201
|
-
if (!requests || (requests && requests.length==0)) {
|
|
202
|
-
winston.verbose("No request found for lead id " +lead._id );
|
|
203
|
-
return 0;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
chat21.auth.setAdminToken(adminToken);
|
|
207
|
-
|
|
208
|
-
requests.forEach(function(request) {
|
|
209
|
-
if (request.channelOutbound.name === ChannelConstants.CHAT21) {
|
|
210
|
-
|
|
211
|
-
winston.verbose("Chat21Handler lead.update for request ", request);
|
|
212
|
-
|
|
213
|
-
var groupName = lead.fullname;
|
|
214
|
-
if (request.subject) {
|
|
215
|
-
groupName=request.subject;
|
|
216
|
-
}
|
|
217
|
-
// update: function(name, owner, attributes, group_id){
|
|
218
|
-
|
|
219
|
-
chat21.groups.update(groupName, undefined, undefined, request.request_id).then(function(data) {
|
|
220
|
-
winston.verbose("Chat21 group updated for lead.update: " + JSON.stringify(data));
|
|
221
|
-
chat21Event.emit('group.update', data);
|
|
222
|
-
}).catch(function(err) {
|
|
223
|
-
winston.error("Error updating chat21 group for lead.update", err);
|
|
224
|
-
chat21Event.emit('group.update.error', err);
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
// updateAttributes: function(attributes, group_id){
|
|
228
|
-
var gattributes = {userFullname:lead.fullname, userEmail: lead.email }
|
|
229
|
-
// qui1
|
|
230
|
-
chat21.groups.updateAttributes(gattributes, request.request_id).then(function(data) {
|
|
231
|
-
winston.verbose("Chat21 group gattributes for lead.update updated: " + JSON.stringify(data));
|
|
232
|
-
chat21Event.emit('group.update', data);
|
|
233
|
-
chat21Event.emit('group.attributes.update', data);
|
|
234
|
-
}).catch(function(err) {
|
|
235
|
-
winston.error("Error updating chat21 gattributes for lead.update group ", err);
|
|
236
|
-
chat21Event.emit('group.attributes.update.error', err);
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
}
|
|
241
|
-
})
|
|
242
|
-
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
});
|
|
230
|
+
winston.info("Chat21Sender message.test");
|
|
231
|
+
|
|
232
|
+
chat21.auth.setAdminToken(adminToken);
|
|
233
|
+
return chat21.messages.sendToGroup(message.senderFullname, message.recipient,
|
|
234
|
+
message.recipient_fullname, message.text, message.sender, message.attributes, message.type, message.metadata, message.timestamp, message.group)
|
|
235
|
+
.then(function (data) {
|
|
236
|
+
winston.info("Chat21Sender sendToGroup test: " + JSON.stringify(data));
|
|
247
237
|
});
|
|
238
|
+
});
|
|
248
239
|
|
|
240
|
+
messageEvent.on('message.sending', (message) => {
|
|
249
241
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
winston.info("Chat21Sender message.test");
|
|
242
|
+
winston.verbose("[Chat21Handler] - message.sending - Message sending: ", message);
|
|
243
|
+
winston.debug("[Chat21Handler] - message.sending - Message sending: ", message);
|
|
253
244
|
|
|
254
|
-
|
|
245
|
+
if (message &&
|
|
246
|
+
message.status === MessageConstants.CHAT_MESSAGE_STATUS.SENDING &&
|
|
247
|
+
message.channel_type == MessageConstants.CHANNEL_TYPE.GROUP &&
|
|
248
|
+
message.request &&
|
|
249
|
+
message.request.channelOutbound.name == ChannelConstants.CHAT21) {
|
|
255
250
|
|
|
256
|
-
|
|
257
|
-
message.recipient_fullname, message.text, message.sender, message.attributes, message.type, message.metadata, message.timestamp, message.group)
|
|
258
|
-
.then(function(data){
|
|
259
|
-
winston.info("Chat21Sender sendToGroup test: "+ JSON.stringify(data));
|
|
260
|
-
});
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
// chat21Handler on worker is loaded with stadard events like request.create and NOT request.create.queue because it is used internally by the worker when the request is closed by ChatUnhandledRequestScheduler
|
|
264
|
-
messageEvent.on('message.sending', function(message) {
|
|
265
|
-
|
|
266
|
-
// setImmediate(() => {
|
|
267
|
-
// TODO perche nn c'è setImmedite? per performace
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
winston.verbose("Chat21Sender on message.sending: "+ message.text);
|
|
271
|
-
winston.debug("Chat21Sender on message.sending ", message);
|
|
272
|
-
|
|
273
|
-
if (message &&
|
|
274
|
-
message.status === MessageConstants.CHAT_MESSAGE_STATUS.SENDING &&
|
|
275
|
-
message.channel_type == MessageConstants.CHANNEL_TYPE.GROUP &&
|
|
276
|
-
message.request &&
|
|
277
|
-
message.request.channelOutbound.name == ChannelConstants.CHAT21) { //here only request.channelOutbound is important because chat21handler is for outgoing messages( from Tiledesk to agents clients)
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
chat21.auth.setAdminToken(adminToken);
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
//chat21.conversations.typing(message.recipient, message.sender, message.text, new Date()).finally(function() {
|
|
284
|
-
return that.typing(message,new Date() ).then(function() {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
let attributes = message.attributes;
|
|
288
|
-
|
|
289
|
-
if (!attributes) attributes = {};
|
|
290
|
-
|
|
291
|
-
attributes['tiledesk_message_id'] = message._id;
|
|
292
|
-
|
|
293
|
-
attributes['projectId'] = message.id_project; //TODO not used. used by ionic to open request detail ???
|
|
294
|
-
|
|
295
|
-
if (message.channel && message.channel.name) {
|
|
296
|
-
attributes['channel'] = message.channel.name;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
winston.verbose("Chat21Sender sending message.sending: "+ message.text);
|
|
303
|
-
winston.debug("Chat21Sender sending message.sending ", message);
|
|
304
|
-
|
|
305
|
-
// chat21Util.getParsedMessage().then(function(messageData) {
|
|
306
|
-
// message = messageData;
|
|
307
|
-
|
|
308
|
-
// doent'work must merge older field with new message = chat21Util.parseReply(message.text);
|
|
309
|
-
|
|
310
|
-
// sendToGroup: function(sender_fullname, recipient_id, recipient_fullname, text, sender_id, attributes, type, metadata, timestamp){
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
var timestamp = Date.now();
|
|
314
|
-
// var timestamp = undefined;
|
|
315
|
-
if (message.attributes && message.attributes.clienttimestamp) {
|
|
316
|
-
timestamp = message.attributes.clienttimestamp;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
var recipient_fullname = "Guest";
|
|
320
|
-
// guest_here
|
|
321
|
-
|
|
322
|
-
if (message.request && message.request.lead && message.request.lead.fullname) {
|
|
323
|
-
recipient_fullname = message.request.lead.fullname;
|
|
324
|
-
}
|
|
325
|
-
if (message.request && message.request.subject ) {
|
|
326
|
-
recipient_fullname = message.request.subject;
|
|
327
|
-
}
|
|
328
|
-
if (message.request && message.request.channel && message.request.channel.name ) {
|
|
329
|
-
attributes['request_channel'] = message.request.channel.name;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/*
|
|
333
|
-
const parsedReply = tiledeskUtil.parseReply(message.text);
|
|
334
|
-
winston.info("Chat21 sendToGroup parsedMessage " + JSON.stringify(parsedReply));
|
|
335
|
-
|
|
336
|
-
// message = {...message, ...parsedReply.message };
|
|
337
|
-
// merge(message, parsedReply.message );
|
|
338
|
-
|
|
339
|
-
if (parsedReply.message.text) {
|
|
340
|
-
message.text = parsedReply.message.text;
|
|
341
|
-
}
|
|
342
|
-
if (parsedReply.message.type) {
|
|
343
|
-
message.type = parsedReply.message.type;
|
|
344
|
-
}
|
|
345
|
-
if (parsedReply.message.type) {
|
|
346
|
-
message.metadata = parsedReply.message.metadata;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
// var msg_attributes = {...message.attributes, ...parsedReply.message.attributes };
|
|
350
|
-
if (parsedReply.message && parsedReply.message.attributes) {
|
|
351
|
-
for(const [key, value] of Object.entries(parsedReply.message.attributes)) {
|
|
352
|
-
attributes[key] = value
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
*/
|
|
356
|
-
|
|
357
|
-
// performance console log
|
|
358
|
-
// console.log("************* send message chat21: "+new Date().toISOString(), message.text );
|
|
359
|
-
|
|
360
|
-
return chat21.messages.sendToGroup(message.senderFullname, message.recipient,
|
|
361
|
-
recipient_fullname, message.text, message.sender, attributes, message.type, message.metadata, timestamp)
|
|
362
|
-
.then(function(data){
|
|
363
|
-
winston.verbose("Chat21Sender sendToGroup sent: "+ JSON.stringify(data) + " for text message " + message.text);
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
// chat21.conversations.stopTyping(message.recipient,message.sender);
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
// performance console log
|
|
370
|
-
// console.log("************* senttt message chat21: "+new Date().toISOString(), message.text );
|
|
371
|
-
|
|
372
|
-
chat21Event.emit('message.sent', data);
|
|
373
|
-
|
|
374
|
-
winston.debug("Message changeStatus 1");
|
|
375
|
-
messageService.changeStatus(message._id, MessageConstants.CHAT_MESSAGE_STATUS.DELIVERED) .then(function(upMessage){
|
|
376
|
-
winston.debug("Chat21 message sent ", upMessage.toObject());
|
|
377
|
-
}).catch(function(err) {
|
|
378
|
-
winston.error("Error Chat21 message sent with id: "+message._id, err);
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
}).catch(function(err) {
|
|
382
|
-
winston.error("Chat21 sendToGroup err", err);
|
|
383
|
-
chat21Event.emit('message.sent.error', err);
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
});
|
|
387
|
-
|
|
388
|
-
// });
|
|
389
|
-
}
|
|
390
|
-
else if (message &&
|
|
391
|
-
message.status === MessageConstants.CHAT_MESSAGE_STATUS.SENDING &&
|
|
392
|
-
message.channel_type == MessageConstants.CHANNEL_TYPE.DIRECT &&
|
|
393
|
-
message.channel.name == ChannelConstants.CHAT21) {
|
|
394
|
-
|
|
395
|
-
// winston.warn("Chat21Sender this is a direct message. Unimplemented method", message);
|
|
396
|
-
|
|
397
|
-
chat21.auth.setAdminToken(adminToken);
|
|
398
|
-
|
|
399
|
-
winston.debug("Chat21Sender");
|
|
400
|
-
// send: function(sender_fullname, recipient_id, recipient_fullname, text, sender_id, attributes, type, metadata){
|
|
401
|
-
return chat21.messages.send(message.senderFullname, message.recipient,
|
|
402
|
-
message.recipientFullname, message.text, message.sender, message.attributes, message.type, message.metadata)
|
|
403
|
-
.then(function(data){
|
|
404
|
-
winston.verbose("Chat21Sender send sent: "+ JSON.stringify(data));
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
// chat21.conversations.stopTyping(message.recipient,message.sender);
|
|
408
|
-
|
|
409
|
-
chat21Event.emit('message.sent', data);
|
|
410
|
-
winston.debug("Message changeStatus 2");
|
|
411
|
-
messageService.changeStatus(message._id, MessageConstants.CHAT_MESSAGE_STATUS.DELIVERED) .then(function(upMessage){
|
|
412
|
-
winston.debug("Chat21 message sent ", upMessage.toObject());
|
|
413
|
-
}).catch(function(err) {
|
|
414
|
-
winston.error("Error Chat21 message sent with id: "+message._id, err);
|
|
415
|
-
});
|
|
416
|
-
|
|
417
|
-
}).catch(function(err) {
|
|
418
|
-
winston.error("Chat21 send direct err", err);
|
|
419
|
-
chat21Event.emit('message.sent.error', err);
|
|
420
|
-
});
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
else if (message &&
|
|
427
|
-
message.status === MessageConstants.CHAT_MESSAGE_STATUS.SENDING &&
|
|
428
|
-
message.channel_type == MessageConstants.CHANNEL_TYPE.GROUP &&
|
|
429
|
-
message.channel.name == ChannelConstants.CHAT21) {
|
|
430
|
-
|
|
431
|
-
// winston.warn("Chat21Sender this is a group message. Unimplemented method", message);
|
|
432
|
-
|
|
433
|
-
chat21.auth.setAdminToken(adminToken);
|
|
434
|
-
|
|
435
|
-
var timestamp = Date.now();
|
|
436
|
-
// var timestamp = undefined;
|
|
437
|
-
if (message.attributes && message.attributes.clienttimestamp) {
|
|
438
|
-
timestamp = message.attributes.clienttimestamp;
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
return chat21.messages.sendToGroup(message.senderFullname, message.recipient,
|
|
443
|
-
message.recipientFullname, message.text, message.sender, message.attributes, message.type, message.metadata, timestamp)
|
|
444
|
-
.then(function(data){
|
|
445
|
-
winston.verbose("Chat21Sender send sent: "+ JSON.stringify(data));
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
// chat21.conversations.stopTyping(message.recipient,message.sender);
|
|
449
|
-
|
|
450
|
-
chat21Event.emit('message.sent', data);
|
|
451
|
-
winston.debug("Message changeStatus 3");
|
|
452
|
-
messageService.changeStatus(message._id, MessageConstants.CHAT_MESSAGE_STATUS.DELIVERED) .then(function(upMessage){
|
|
453
|
-
winston.debug("Chat21 message sent ", upMessage.toObject());
|
|
454
|
-
}).catch(function(err) {
|
|
455
|
-
winston.error("Error Chat21 message sent with id: "+message._id, err);
|
|
456
|
-
});
|
|
457
|
-
|
|
458
|
-
}).catch(function(err) {
|
|
459
|
-
winston.error("Chat21 sendToGroup err", err);
|
|
460
|
-
chat21Event.emit('message.sent.error', err);
|
|
461
|
-
});
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
} else {
|
|
466
|
-
winston.error("Chat21Sender this is not a group o direct message", message);
|
|
467
|
-
return;
|
|
468
|
-
}
|
|
469
|
-
// });
|
|
470
|
-
});
|
|
251
|
+
chat21.auth.setAdminToken(adminToken);
|
|
471
252
|
|
|
253
|
+
return this.typing(message, new Date()).then(function () {
|
|
472
254
|
|
|
473
|
-
|
|
255
|
+
let attributes = message.attributes || {};
|
|
256
|
+
attributes['tiledesk_message_id'] = message._id;
|
|
257
|
+
attributes['projectId'] = message.id_project;
|
|
258
|
+
if (message.channel && message.channel.name) {
|
|
259
|
+
attributes['channel'] = message.channel.name;
|
|
260
|
+
}
|
|
474
261
|
|
|
475
|
-
|
|
476
|
-
|
|
262
|
+
winston.verbose("[Chat21Handler] - message.sending - Chat21 sending message: " + message.text);
|
|
263
|
+
winston.debug("[Chat21Handler] - message.sending - Chat21 sending message: ", message);
|
|
477
264
|
|
|
478
|
-
|
|
265
|
+
let timestamp = message.attributes?.clienttimestamp || Date.now();
|
|
266
|
+
let recipient_fullname = message.request?.subject || message.request?.lead?.fullname || "Guest";
|
|
479
267
|
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
winston.verbose("Chat21 group gattributes for request.attributes.update updated: " + JSON.stringify(data));
|
|
484
|
-
chat21Event.emit('group.update', data);
|
|
485
|
-
chat21Event.emit('group.attributes.update', data);
|
|
486
|
-
}).catch(function(err) {
|
|
487
|
-
winston.error("Error updating chat21 gattributes for request.attributes.update group ", err);
|
|
488
|
-
chat21Event.emit('group.attributes.update.error', err);
|
|
489
|
-
});
|
|
268
|
+
if (message.request?.channel?.name) {
|
|
269
|
+
attributes['request_channel'] = message.request.channel.name;
|
|
270
|
+
}
|
|
490
271
|
|
|
491
|
-
|
|
492
|
-
});
|
|
493
|
-
});
|
|
272
|
+
return chat21.messages.sendToGroup(message.senderFullname, message.recipient, recipient_fullname, message.text, message.sender, attributes, message.type, message.metadata, timestamp).then(function (data) {
|
|
494
273
|
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
// requestEvent.on('request.update', function(request) {
|
|
498
|
-
|
|
499
|
-
// setImmediate(() => {
|
|
500
|
-
// if (request.channelOutbound.name === ChannelConstants.CHAT21) {
|
|
501
|
-
|
|
502
|
-
// chat21.auth.setAdminToken(adminToken);
|
|
503
|
-
|
|
504
|
-
// // https://stackoverflow.com/questions/42310950/handling-undefined-values-with-firebase/42315610
|
|
505
|
-
// // var requestWithoutUndefined = JSON.parse(JSON.stringify(request, function(k, v) {
|
|
506
|
-
// // if (v === undefined) { return null; } return v;
|
|
507
|
-
// // }));
|
|
508
|
-
|
|
509
|
-
// // var gattributes = { "_request":requestWithoutUndefined};
|
|
510
|
-
|
|
511
|
-
// // qui1
|
|
512
|
-
// chat21.groups.updateAttributes(gattributes, request.request_id).then(function(data) {
|
|
513
|
-
// winston.verbose("Chat21 group gattributes for request.update updated: " + JSON.stringify(data));
|
|
514
|
-
// chat21Event.emit('group.update', data);
|
|
515
|
-
// chat21Event.emit('group.attributes.update', data);
|
|
516
|
-
// }).catch(function(err) {
|
|
517
|
-
// winston.error("Error updating chat21 gattributes for request.update group ", err);
|
|
518
|
-
// chat21Event.emit('group.attributes.update.error', err);
|
|
519
|
-
// });
|
|
520
|
-
|
|
521
|
-
// }
|
|
522
|
-
// });
|
|
523
|
-
// });
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
// chat21Handler on worker is loaded with stadard events like request.create and NOT request.create.queue because it is used internally by the worker when the request is closed by ChatUnhandledRequestScheduler
|
|
527
|
-
requestEvent.on('request.create', function(request) {
|
|
528
|
-
|
|
529
|
-
winston.debug("chat21Handler requestEvent request.create called" , request);
|
|
530
|
-
// setImmediate(() => {
|
|
531
|
-
// perche nn c'è setImmedite? per performace
|
|
532
|
-
if (request.channelOutbound.name === ChannelConstants.CHAT21) {
|
|
533
|
-
|
|
534
|
-
chat21.auth.setAdminToken(adminToken);
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
// let requestObj = request.toObject();
|
|
539
|
-
let requestObj = request.toJSON();
|
|
540
|
-
|
|
541
|
-
winston.verbose("creating chat21 group for request with id: " + requestObj._id);
|
|
542
|
-
|
|
543
|
-
// winston.info("requestObj.participants: "+ Object.prototype.toString.call(requestObj.participants));
|
|
544
|
-
winston.debug("requestObj.participants: "+ JSON.stringify(requestObj.participants));
|
|
545
|
-
|
|
546
|
-
let members = requestObj.participants;
|
|
547
|
-
// var members = reqParticipantArray;
|
|
548
|
-
|
|
549
|
-
members.push("system");
|
|
550
|
-
if (request.lead) {
|
|
551
|
-
// lead_id used. Change it?
|
|
552
|
-
members.push(request.lead.lead_id);
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
// let membersArray = JSON.parse(JSON.stringify(members));
|
|
557
|
-
// winston.info("membersArray", membersArray);
|
|
558
|
-
|
|
559
|
-
var gAttributes = requestObj.attributes || {};
|
|
560
|
-
// TODO ATTENTION change value by reference
|
|
561
|
-
// var gAttributes = request.attributes || {}; //BUG LINK TO event emmiter obiect
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
// problema requester_id
|
|
565
|
-
gAttributes["requester_id"] = request.requester_id;
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
gAttributes['projectId'] = request.id_project; //used by ionic to open request detail
|
|
569
|
-
|
|
570
|
-
if (request.lead) {
|
|
571
|
-
gAttributes['userFullname'] = request.lead.fullname; //used by ionic to open request detail
|
|
572
|
-
gAttributes['userEmail'] = request.lead.email; //used by ionic to open request detail
|
|
573
|
-
// TOOD is it necessary?
|
|
574
|
-
// lead_id used. Change it?
|
|
575
|
-
gAttributes['senderAuthInfo'] = {authType: "USER", authVar: {uid:request.lead.lead_id}}; //used by ionic otherwise ionic dont show userFullname in the participants panel
|
|
576
|
-
}
|
|
577
|
-
// TODO ionic dont show attributes panel if attributes.client is empty. bug?
|
|
578
|
-
gAttributes['client'] = request.userAgent || 'n.d.'; //used by ionic to open request detail
|
|
579
|
-
if (request.department) {
|
|
580
|
-
gAttributes['departmentId'] = request.department._id; //used by ionic to open request detail
|
|
581
|
-
gAttributes['departmentName'] = request.department.name; //used by ionic to open request detail
|
|
582
|
-
}
|
|
583
|
-
gAttributes['sourcePage'] = request.sourcePage; //used by ionic to open request detail
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
// https://stackoverflow.com/questions/42310950/handling-undefined-values-with-firebase/42315610
|
|
587
|
-
|
|
588
|
-
// not used now. Before used by ionic
|
|
589
|
-
// var requestWithoutUndefined = JSON.parse(JSON.stringify(request, function(k, v) {
|
|
590
|
-
// if (v === undefined) { return null; } return v;
|
|
591
|
-
// }));
|
|
592
|
-
// gAttributes['_request'] = requestWithoutUndefined; //used by ionic to open request detail
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
winston.debug("Chat21 group create gAttributes: ",gAttributes);
|
|
599
|
-
|
|
600
|
-
var groupId = request.request_id;
|
|
601
|
-
|
|
602
|
-
var group_name = "Guest";
|
|
603
|
-
// guest_here
|
|
604
|
-
|
|
605
|
-
if (request.lead && request.lead.fullname) {
|
|
606
|
-
group_name = request.lead.fullname;
|
|
607
|
-
}
|
|
608
|
-
if (request.subject) {
|
|
609
|
-
group_name = request.subject;
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
// performance console log
|
|
613
|
-
// console.log("************* before request.support_group.created: "+new Date().toISOString());
|
|
614
|
-
|
|
615
|
-
//TODO racecondition?
|
|
616
|
-
return chat21.groups.create(group_name, members, gAttributes, groupId).then(function(data) {
|
|
617
|
-
winston.verbose("Chat21 group created: " + JSON.stringify(data));
|
|
618
|
-
|
|
619
|
-
// performance console log
|
|
620
|
-
// console.log("************* after request.support_group.created: "+new Date().toISOString());
|
|
621
|
-
|
|
622
|
-
requestEvent.emit('request.support_group.created', request);
|
|
623
|
-
|
|
624
|
-
chat21Event.emit('group.create', data);
|
|
625
|
-
}).catch(function(err) {
|
|
626
|
-
winston.error("Error creating chat21 group ", err);
|
|
627
|
-
|
|
628
|
-
requestEvent.emit('request.support_group.created.error', request);
|
|
629
|
-
|
|
630
|
-
chat21Event.emit('group.create.error', err);
|
|
631
|
-
});
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
}
|
|
635
|
-
// });
|
|
636
|
-
});
|
|
637
|
-
|
|
274
|
+
winston.verbose("[Chat21Handler] - message.sending - Chat21 sent message: " + JSON.stringify(data) + " for text message " + message.text);
|
|
275
|
+
chat21Event.emit('message.sent', data);
|
|
638
276
|
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
winston.debug("chat21Handler requestEvent request.close called" , request);
|
|
644
|
-
|
|
645
|
-
setImmediate(() => {
|
|
646
|
-
if (request.channelOutbound.name === ChannelConstants.CHAT21) {
|
|
647
|
-
|
|
648
|
-
chat21.auth.setAdminToken(adminToken);
|
|
649
|
-
|
|
650
|
-
winston.debug("Chat21Sender archiving conversations for ",request.participants);
|
|
651
|
-
|
|
652
|
-
//iterate request.participant and archive conversation
|
|
653
|
-
request.participants.forEach(function(participant,index) {
|
|
654
|
-
|
|
655
|
-
winston.debug("Chat21Sender archiving conversation: " + request.request_id + "for " + participant);
|
|
656
|
-
|
|
657
|
-
chat21.conversations.archive(request.request_id, participant)
|
|
658
|
-
.then(function(data){
|
|
659
|
-
winston.verbose("Chat21 conversation archived result "+ JSON.stringify(data));
|
|
660
|
-
|
|
661
|
-
chat21Event.emit('conversation.archived', data);
|
|
662
|
-
|
|
663
|
-
}).catch(function(err) {
|
|
664
|
-
winston.error("Chat21 archived err", err);
|
|
665
|
-
chat21Event.emit('conversation.archived.error', err);
|
|
666
|
-
});
|
|
667
|
-
});
|
|
668
|
-
|
|
669
|
-
// archive: function(recipient_id, user_id){
|
|
670
|
-
chat21.conversations.archive(request.request_id, "system")
|
|
671
|
-
.then(function(data){
|
|
672
|
-
winston.verbose("Chat21 archived for system"+ JSON.stringify(data));
|
|
673
|
-
|
|
674
|
-
chat21Event.emit('conversation.archived', data);
|
|
675
|
-
|
|
676
|
-
}).catch(function(err) {
|
|
677
|
-
winston.error("Chat21 archived for system err", err);
|
|
678
|
-
chat21Event.emit('conversation.archived.error', err);
|
|
679
|
-
});
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
// request.lead can be undefined because some test case uses the old deprecated method requestService.createWithId.
|
|
683
|
-
if (request.lead) {
|
|
684
|
-
// lead_id used. Change it?
|
|
685
|
-
chat21.conversations.archive(request.request_id, request.lead.lead_id) // chat21.conversations.archive(request.request_id, request.requester_id)<-desnt'archive
|
|
686
|
-
|
|
687
|
-
.then(function(data){
|
|
688
|
-
winston.verbose("Chat21 archived for request.lead.lead_id"+ JSON.stringify(data));
|
|
689
|
-
|
|
690
|
-
chat21Event.emit('conversation.archived', data);
|
|
691
|
-
|
|
692
|
-
}).catch(function(err) {
|
|
693
|
-
winston.error("Chat21 archived for request.lead.lead_id err", err);
|
|
694
|
-
chat21Event.emit('conversation.archived.error', err);
|
|
695
|
-
});
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
}
|
|
699
|
-
});
|
|
277
|
+
messageService.changeStatus(message._id, MessageConstants.CHAT_MESSAGE_STATUS.DELIVERED).then(function (upMessage) {
|
|
278
|
+
winston.debug("[Chat21Handler] - message.sending - Chat21 message sent: ", upMessage.toObject());
|
|
279
|
+
}).catch(function (err) {
|
|
280
|
+
winston.error("[Chat21Handler] - message.sending - Error Chat21 message sent with id: " + message._id, err);
|
|
700
281
|
});
|
|
701
|
-
|
|
702
|
-
requestEvent.on('request.delete', function(request) {
|
|
703
|
-
|
|
704
|
-
winston.debug("chat21Handler requestEvent request.delete called" , request);
|
|
705
|
-
|
|
706
|
-
setImmediate(() => {
|
|
707
|
-
if (request.channelOutbound.name === ChannelConstants.CHAT21) {
|
|
708
282
|
|
|
709
|
-
|
|
283
|
+
}).catch(function (err) {
|
|
284
|
+
winston.error("Chat21 sendToGroup err", err);
|
|
285
|
+
chat21Event.emit('message.sent.error', err);
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
}
|
|
710
289
|
|
|
711
|
-
|
|
290
|
+
else if (message &&
|
|
291
|
+
message.status === MessageConstants.CHAT_MESSAGE_STATUS.SENDING &&
|
|
292
|
+
message.channel_type == MessageConstants.CHANNEL_TYPE.DIRECT &&
|
|
293
|
+
message.channel.name == ChannelConstants.CHAT21) {
|
|
712
294
|
|
|
713
|
-
|
|
714
|
-
winston.verbose("Chat21 conversation archived result "+ JSON.stringify(data));
|
|
715
|
-
chat21Event.emit('conversation.deleted', data);
|
|
716
|
-
}).catch((err) => {
|
|
717
|
-
winston.error("Chat21 deleted err", err);
|
|
718
|
-
chat21Event.emit('conversation.deleted.error', err);
|
|
719
|
-
})
|
|
720
|
-
}
|
|
721
|
-
})
|
|
295
|
+
chat21.auth.setAdminToken(adminToken);
|
|
722
296
|
|
|
723
|
-
|
|
297
|
+
return chat21.messages.send(message.senderFullname, message.recipient, message.recipientFullname, message.text, message.sender, message.attributes, message.type, message.metadata).then(function (data) {
|
|
724
298
|
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
winston.debug("chat21Handler requestEvent request.participants.update called" , data);
|
|
299
|
+
winston.verbose("Chat21Sender send sent: " + JSON.stringify(data));
|
|
300
|
+
chat21Event.emit('message.sent', data);
|
|
728
301
|
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
302
|
+
messageService.changeStatus(message._id, MessageConstants.CHAT_MESSAGE_STATUS.DELIVERED).then(function (upMessage) {
|
|
303
|
+
winston.debug("[Chat21Handler] - message.sending - Chat21 message sent: ", upMessage.toObject());
|
|
304
|
+
}).catch(function (err) {
|
|
305
|
+
winston.error("[Chat21Handler] - message.sending - Error Chat21 message sent with id: " + message._id, err);
|
|
306
|
+
});
|
|
732
307
|
|
|
733
|
-
|
|
734
|
-
|
|
308
|
+
}).catch(function (err) {
|
|
309
|
+
winston.error("[Chat21Handler] - message.sending - Error Chat21 send direct: ", err);
|
|
310
|
+
chat21Event.emit('message.sent.error', err);
|
|
311
|
+
});
|
|
312
|
+
}
|
|
735
313
|
|
|
736
|
-
|
|
314
|
+
else if (message &&
|
|
315
|
+
message.status === MessageConstants.CHAT_MESSAGE_STATUS.SENDING &&
|
|
316
|
+
message.channel_type == MessageConstants.CHANNEL_TYPE.GROUP &&
|
|
317
|
+
message.channel.name == ChannelConstants.CHAT21) {
|
|
737
318
|
|
|
738
|
-
|
|
319
|
+
chat21.auth.setAdminToken(adminToken);
|
|
739
320
|
|
|
740
|
-
|
|
741
|
-
let requestObj = request.toJSON();
|
|
742
|
-
|
|
743
|
-
winston.verbose("setting chat21 group for request.participants.update for request with id: " + requestObj._id);
|
|
744
|
-
|
|
745
|
-
var groupId = request.request_id;
|
|
321
|
+
let timestamp = message.attributes?.clienttimestamp || Date.now();
|
|
746
322
|
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
members.push("system");
|
|
323
|
+
return chat21.messages.sendToGroup(message.senderFullname, message.recipient, message.recipientFullname, message.text, message.sender, message.attributes, message.type, message.metadata, timestamp).then(function (data) {
|
|
750
324
|
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
members.push(participant);
|
|
754
|
-
});
|
|
755
|
-
// requestObj.participants;
|
|
756
|
-
// var members = reqParticipantArray;
|
|
325
|
+
winston.verbose("[Chat21Handler] - message.sending - Chat21 sent message: " + JSON.stringify(data));
|
|
326
|
+
chat21Event.emit('message.sent', data);
|
|
757
327
|
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
328
|
+
messageService.changeStatus(message._id, MessageConstants.CHAT_MESSAGE_STATUS.DELIVERED).then(function (upMessage) {
|
|
329
|
+
winston.debug("[Chat21Handler] - message.sending - Chat21 message sent: ", upMessage.toObject());
|
|
330
|
+
}).catch(function (err) {
|
|
331
|
+
winston.error("[Chat21Handler] - message.sending - Error Chat21 message sent with id: " + message._id, err);
|
|
332
|
+
});
|
|
763
333
|
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
334
|
+
}).catch(function (err) {
|
|
335
|
+
winston.error("Chat21 sendToGroup err", err);
|
|
336
|
+
chat21Event.emit('message.sent.error', err);
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
else {
|
|
341
|
+
winston.error("[Chat21Handler] - message.sending - This is not a group or direct message: ", message);
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
});
|
|
773
346
|
|
|
347
|
+
/** REQUEST EVENTS */
|
|
348
|
+
requestEvent.on('request.attributes.update', function (request) {
|
|
774
349
|
|
|
775
|
-
|
|
776
|
-
|
|
350
|
+
setImmediate(() => {
|
|
351
|
+
if (request.channelOutbound.name === ChannelConstants.CHAT21) {
|
|
777
352
|
|
|
778
|
-
|
|
779
|
-
// winston.info("newParticipants ", newParticipants);
|
|
353
|
+
chat21.auth.setAdminToken(adminToken);
|
|
780
354
|
|
|
781
|
-
|
|
782
|
-
// winston.info("removedParticipants ", removedParticipants);
|
|
355
|
+
const gattributes = request.attributes;
|
|
783
356
|
|
|
784
|
-
|
|
357
|
+
chat21.groups.updateAttributes(gattributes, request.request_id).then(function (data) {
|
|
358
|
+
winston.verbose("[Chat21Handler] - request.attributes.update - Chat21 group attributes updated: " + JSON.stringify(data));
|
|
359
|
+
chat21Event.emit('group.update', data);
|
|
360
|
+
chat21Event.emit('group.attributes.update', data);
|
|
361
|
+
}).catch(function (err) {
|
|
362
|
+
winston.error("[Chat21Handler] - request.attributes.update - Error updating chat21 group attributes: ", err);
|
|
363
|
+
chat21Event.emit('group.attributes.update.error', err);
|
|
364
|
+
});
|
|
785
365
|
|
|
786
|
-
|
|
787
|
-
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
});
|
|
788
369
|
|
|
789
|
-
|
|
790
|
-
//racecondition?low it's not dangerous archive a conversation that doen't exist
|
|
370
|
+
requestEvent.on('request.create', function (request) {
|
|
791
371
|
|
|
792
|
-
|
|
793
|
-
.then(function(data){
|
|
794
|
-
winston.verbose("Chat21 archived "+ JSON.stringify(data));
|
|
795
|
-
|
|
796
|
-
chat21Event.emit('conversation.archived', data);
|
|
797
|
-
|
|
798
|
-
}).catch(function(err) {
|
|
799
|
-
winston.error("Chat21 archived err", err);
|
|
800
|
-
chat21Event.emit('conversation.archived.error', err);
|
|
801
|
-
});
|
|
372
|
+
winston.debug("[Chat21Handler] - request.create - Request create: ", request);
|
|
802
373
|
|
|
803
|
-
|
|
804
|
-
|
|
374
|
+
if (request.channelOutbound.name !== ChannelConstants.CHAT21) {
|
|
375
|
+
winston.error("[Chat21Handler] - request.create - Request is not a chat21 request: ", request);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
chat21.auth.setAdminToken(adminToken);
|
|
380
|
+
|
|
381
|
+
let requestObj = request.toJSON();
|
|
382
|
+
winston.verbose("[Chat21Handler] - request.create - Creating chat21 group for request with id: " + requestObj._id);
|
|
383
|
+
|
|
384
|
+
let members = requestObj.participants;
|
|
385
|
+
|
|
386
|
+
members.push("system");
|
|
387
|
+
if (request.lead) {
|
|
388
|
+
members.push(request.lead.lead_id);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const gAttributes = requestObj.attributes || {};
|
|
392
|
+
gAttributes["requester_id"] = request.requester_id;
|
|
393
|
+
gAttributes['projectId'] = request.id_project;
|
|
394
|
+
gAttributes['client'] = request.userAgent || 'n.d.';
|
|
395
|
+
gAttributes['sourcePage'] = request.sourcePage;
|
|
396
|
+
if (request.lead) {
|
|
397
|
+
gAttributes['userFullname'] = request.lead.fullname;
|
|
398
|
+
gAttributes['userEmail'] = request.lead.email;
|
|
399
|
+
gAttributes['senderAuthInfo'] = { authType: "USER", authVar: { uid: request.lead.lead_id } }; //used by ionic otherwise ionic dont show userFullname in the participants panel
|
|
400
|
+
}
|
|
401
|
+
if (request.department) {
|
|
402
|
+
gAttributes['departmentId'] = request.department._id;
|
|
403
|
+
gAttributes['departmentName'] = request.department.name;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
winston.debug("[Chat21Handler] - request.create - Chat21 group create gAttributes: ", gAttributes);
|
|
407
|
+
|
|
408
|
+
const groupId = request.request_id;
|
|
409
|
+
let group_name = request.subject || request.lead?.fullname || "Guest";
|
|
410
|
+
|
|
411
|
+
return chat21.groups.create(group_name, members, gAttributes, groupId).then(function (data) {
|
|
412
|
+
winston.verbose("[Chat21Handler] - request.create - Chat21 group created: " + JSON.stringify(data));
|
|
413
|
+
requestEvent.emit('request.support_group.created', request);
|
|
414
|
+
chat21Event.emit('group.create', data);
|
|
415
|
+
}).catch(function (err) {
|
|
416
|
+
winston.error("[Chat21Handler] - request.create - Error creating chat21 group: ", err);
|
|
417
|
+
requestEvent.emit('request.support_group.created.error', request);
|
|
418
|
+
chat21Event.emit('group.create.error', err);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
requestEvent.on('request.close', function (request) {
|
|
424
|
+
|
|
425
|
+
const traceEvent = 'request.close';
|
|
426
|
+
winston.debug("[Chat21Handler] - request.close - Request close: ", request);
|
|
427
|
+
chat21SyncTracer.trace(traceEvent, request, 'handler.received', 'info', chat21SyncTracer.describeRequest(request));
|
|
428
|
+
|
|
429
|
+
setImmediate(() => runTraced(traceEvent, request, () => {
|
|
430
|
+
if (request.channelOutbound.name !== ChannelConstants.CHAT21) {
|
|
431
|
+
winston.error("[Chat21Handler] - request.close - Request is not a chat21 request: ", request);
|
|
432
|
+
chat21SyncTracer.trace(traceEvent, request, 'handler.skipped', 'skipped', { channelOutbound: request.channelOutbound.name });
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
805
435
|
|
|
436
|
+
chat21.auth.setAdminToken(adminToken);
|
|
437
|
+
winston.debug("[Chat21Handler] - request.close - Archiving conversations for ", request.participants);
|
|
806
438
|
|
|
439
|
+
request.participants.forEach(function (participant) {
|
|
440
|
+
archiveConversation(traceEvent, request, participant);
|
|
441
|
+
});
|
|
807
442
|
|
|
808
|
-
|
|
809
|
-
});
|
|
810
|
-
});
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
requestEvent.on('request.participants.join', function(data) {
|
|
814
|
-
winston.debug("chat21Handler requestEvent request.participants.join called" , data);
|
|
443
|
+
archiveConversation(traceEvent, request, "system");
|
|
815
444
|
|
|
816
|
-
|
|
817
|
-
|
|
445
|
+
if (request.lead) {
|
|
446
|
+
archiveConversation(traceEvent, request, request.lead.lead_id);
|
|
447
|
+
}
|
|
448
|
+
}));
|
|
449
|
+
});
|
|
818
450
|
|
|
819
|
-
|
|
820
|
-
if (request.channelOutbound.name === ChannelConstants.CHAT21) {
|
|
451
|
+
requestEvent.on('request.delete', function (request) {
|
|
821
452
|
|
|
822
|
-
|
|
453
|
+
winston.debug("[Chat21Handler] - request.delete - Request delete: ", request);
|
|
823
454
|
|
|
824
|
-
|
|
455
|
+
setImmediate(() => {
|
|
825
456
|
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
457
|
+
if (request.channelOutbound.name !== ChannelConstants.CHAT21) {
|
|
458
|
+
winston.error("[Chat21Handler] - request.delete - Request is not a chat21 request: ", request);
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
830
461
|
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
462
|
+
chat21.auth.setAdminToken(adminToken);
|
|
463
|
+
winston.debug("[Chat21Handler] - request.delete - Deleting conversations for ", request.participants);
|
|
464
|
+
|
|
465
|
+
chat21.conversations.delete(request.request_id).then((data) => {
|
|
466
|
+
winston.verbose("[Chat21Handler] - request.delete - Chat21 conversation deleted result " + JSON.stringify(data));
|
|
467
|
+
chat21Event.emit('conversation.deleted', data);
|
|
468
|
+
}).catch((err) => {
|
|
469
|
+
winston.error("[Chat21Handler] - request.delete - Error deleting chat21 conversation: ", err);
|
|
470
|
+
chat21Event.emit('conversation.deleted.error', err);
|
|
471
|
+
})
|
|
472
|
+
})
|
|
473
|
+
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
requestEvent.on('request.participants.update', function (data) {
|
|
477
|
+
|
|
478
|
+
const traceEvent = 'request.participants.update';
|
|
479
|
+
winston.debug("[Chat21Handler] - request.participants.update - Request participants update: ", data);
|
|
480
|
+
|
|
481
|
+
let request = data.request;
|
|
482
|
+
let removedParticipants = data.removedParticipants;
|
|
483
|
+
chat21SyncTracer.trace(traceEvent, request, 'handler.received', 'info', {
|
|
484
|
+
removedParticipants: removedParticipants,
|
|
485
|
+
addedParticipants: data.addedParticipants,
|
|
486
|
+
...chat21SyncTracer.describeRequest(request)
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
setImmediate(() => runTraced(traceEvent, request, () => {
|
|
490
|
+
if (request.channelOutbound.name != ChannelConstants.CHAT21) {
|
|
491
|
+
winston.error("[Chat21Handler] - request.participants.update - Request is not a chat21 request: ", request);
|
|
492
|
+
chat21SyncTracer.trace(traceEvent, request, 'handler.skipped', 'skipped', { channelOutbound: request.channelOutbound.name });
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
834
495
|
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
winston.verbose("Chat21 group joined: " + JSON.stringify(data));
|
|
838
|
-
chat21Event.emit('group.join', data);
|
|
839
|
-
}).catch(function(err) {
|
|
840
|
-
winston.error("Error joining chat21 group ", err);
|
|
841
|
-
chat21Event.emit('group.join.error', err);
|
|
842
|
-
});
|
|
496
|
+
chat21.auth.setAdminToken(adminToken);
|
|
497
|
+
let requestObj = request.toJSON();
|
|
843
498
|
|
|
499
|
+
winston.verbose("[Chat21Handler] - request.participants.update - Setting chat21 group for request with id: " + requestObj._id);
|
|
844
500
|
|
|
501
|
+
const groupId = request.request_id;
|
|
845
502
|
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
});
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
requestEvent.on('request.participants.leave', function(data) {
|
|
852
|
-
winston.debug("chat21Handler requestEvent request.participants.leave called" , data);
|
|
503
|
+
let members = [];
|
|
504
|
+
members.push("system");
|
|
853
505
|
|
|
854
|
-
|
|
855
|
-
|
|
506
|
+
request.participants.forEach(function (participant, index) {
|
|
507
|
+
members.push(participant);
|
|
508
|
+
});
|
|
856
509
|
|
|
857
|
-
|
|
858
|
-
|
|
510
|
+
if (request.lead) {
|
|
511
|
+
members.push(request.lead.lead_id);
|
|
512
|
+
}
|
|
513
|
+
winston.verbose("[Chat21Handler] - request.participants.update - Chat21 group with members for request.participants.update: ", members);
|
|
514
|
+
|
|
515
|
+
chat21SyncTracer.traceCall(traceEvent, request, 'group.setMembers', { members: members }, () => {
|
|
516
|
+
return chat21.groups.setMembers(members, groupId);
|
|
517
|
+
}).then(function (data) {
|
|
518
|
+
winston.verbose("[Chat21Handler] - request.participants.update - Chat21 group set for request.participants.update : " + JSON.stringify(data));
|
|
519
|
+
chat21Event.emit('group.setMembers', data);
|
|
520
|
+
}).catch(function (err) {
|
|
521
|
+
winston.error("[Chat21Handler] - request.participants.update - Error setting chat21 group for request.participants.update: ", err);
|
|
522
|
+
chat21Event.emit('group.setMembers.error', err);
|
|
523
|
+
});
|
|
859
524
|
|
|
860
|
-
|
|
525
|
+
removedParticipants.forEach(function (removedParticipant) {
|
|
526
|
+
archiveConversation(traceEvent, request, removedParticipant);
|
|
527
|
+
});
|
|
861
528
|
|
|
862
|
-
|
|
529
|
+
}));
|
|
530
|
+
});
|
|
863
531
|
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
var groupId = request.request_id;
|
|
532
|
+
requestEvent.on('request.participants.join', function (data) {
|
|
533
|
+
const traceEvent = 'request.participants.join';
|
|
534
|
+
winston.debug("[Chat21Handler] - request.participants.join - Request participants join: ", data);
|
|
868
535
|
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
//leave: function(member_id, group_id){
|
|
873
|
-
chat21.groups.leave(member, groupId).then(function(data) {
|
|
874
|
-
winston.verbose("Chat21 group leaved: " + JSON.stringify(data));
|
|
875
|
-
chat21Event.emit('group.leave', data);
|
|
876
|
-
}).catch(function(err) {
|
|
877
|
-
winston.error("Error leaving chat21 group ", err);
|
|
878
|
-
chat21Event.emit('group.leave.error', err);
|
|
879
|
-
});
|
|
536
|
+
let request = data.request;
|
|
537
|
+
let member = data.member;
|
|
538
|
+
chat21SyncTracer.trace(traceEvent, request, 'handler.received', 'info', { member: member, ...chat21SyncTracer.describeRequest(request) });
|
|
880
539
|
|
|
540
|
+
setImmediate(() => runTraced(traceEvent, request, () => {
|
|
881
541
|
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
chat21Event.emit('conversation.archived', data);
|
|
889
|
-
|
|
890
|
-
}).catch(function(err) {
|
|
891
|
-
winston.error("Chat21 archived err", err);
|
|
892
|
-
chat21Event.emit('conversation.archived.error', err);
|
|
893
|
-
});
|
|
542
|
+
if (request.channelOutbound.name !== ChannelConstants.CHAT21) {
|
|
543
|
+
winston.error("[Chat21Handler] - request.participants.join - Request is not a chat21 request: ", request);
|
|
544
|
+
chat21SyncTracer.trace(traceEvent, request, 'handler.skipped', 'skipped', { channelOutbound: request.channelOutbound.name });
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
894
547
|
|
|
895
|
-
|
|
548
|
+
chat21.auth.setAdminToken(adminToken);
|
|
896
549
|
|
|
550
|
+
const groupId = request.request_id;
|
|
551
|
+
winston.verbose("[Chat21Handler] - request.participants.join - Joining member " + member + " for chat21 group with request : " + groupId);
|
|
897
552
|
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
553
|
+
chat21SyncTracer.traceCall(traceEvent, request, 'group.join', { member: member }, () => {
|
|
554
|
+
return chat21.groups.join(member, groupId);
|
|
555
|
+
}).then(function (data) {
|
|
556
|
+
winston.verbose("[Chat21Handler] - request.participants.join - Chat21 group joined: " + JSON.stringify(data));
|
|
557
|
+
chat21Event.emit('group.join', data);
|
|
558
|
+
}).catch(function (err) {
|
|
559
|
+
winston.error("[Chat21Handler] - request.participants.join - Error joining chat21 group: ", err);
|
|
560
|
+
chat21Event.emit('group.join.error', err);
|
|
561
|
+
});
|
|
902
562
|
|
|
563
|
+
}));
|
|
564
|
+
});
|
|
903
565
|
|
|
566
|
+
requestEvent.on('request.participants.leave', function (data) {
|
|
567
|
+
const traceEvent = 'request.participants.leave';
|
|
568
|
+
winston.debug("[Chat21Handler] - request.participants.leave - Request participants leave: ", data);
|
|
904
569
|
|
|
570
|
+
let request = data.request;
|
|
571
|
+
let member = data.member;
|
|
572
|
+
chat21SyncTracer.trace(traceEvent, request, 'handler.received', 'info', { member: member, ...chat21SyncTracer.describeRequest(request) });
|
|
905
573
|
|
|
906
|
-
|
|
574
|
+
setImmediate(() => runTraced(traceEvent, request, () => {
|
|
907
575
|
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
576
|
+
if (request.channelOutbound.name !== ChannelConstants.CHAT21) {
|
|
577
|
+
winston.error("[Chat21Handler] - request.participants.leave - Request is not a chat21 request: ", request);
|
|
578
|
+
chat21SyncTracer.trace(traceEvent, request, 'handler.skipped', 'skipped', { channelOutbound: request.channelOutbound.name });
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
912
581
|
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
setImmediate(() => {
|
|
582
|
+
chat21.auth.setAdminToken(adminToken);
|
|
583
|
+
const groupId = request.request_id;
|
|
916
584
|
|
|
917
|
-
|
|
585
|
+
winston.verbose("[Chat21Handler] - request.participants.leave - Leaving " + member + " for chat21 group for request with id: " + groupId);
|
|
918
586
|
|
|
587
|
+
chat21SyncTracer.traceCall(traceEvent, request, 'group.leave', { member: member }, () => {
|
|
588
|
+
return chat21.groups.leave(member, groupId);
|
|
589
|
+
}).then(function (data) {
|
|
590
|
+
winston.verbose("[Chat21Handler] - request.participants.leave - Chat21 group left: " + JSON.stringify(data));
|
|
591
|
+
chat21Event.emit('group.leave', data);
|
|
592
|
+
}).catch(function (err) {
|
|
593
|
+
winston.error("[Chat21Handler] - request.participants.leave - Error leaving chat21 group: ", err);
|
|
594
|
+
chat21Event.emit('group.leave.error', err);
|
|
595
|
+
});
|
|
919
596
|
|
|
920
|
-
|
|
921
|
-
winston.debug("groupMembers ", groupMembers);
|
|
922
|
-
|
|
923
|
-
var group_id = "group-" + group._id;
|
|
924
|
-
winston.debug("group_id :" + group_id);
|
|
597
|
+
archiveConversation(traceEvent, request, member);
|
|
925
598
|
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
// TODO ritorna success anche se
|
|
929
|
-
// verbose: Chat21 group created: {"success":false,"err":{"message":"Channel closed","stack":"IllegalOperationError: Channel closed\n at ConfirmChannel.<anonymous> (/usr/src/app/node_modules/amqplib/lib/channel.js:160:11)\n at ConfirmChannel.Channel.publish (/usr/src/app/node_modules/amqplib/lib/callback_model.js:171:17)\n at ConfirmChannel.publish (/usr/src/app/node_modules/amqplib/lib/callback_model.js:301:36)\n at Chat21Api.publish (/usr/src/app/chat21Api/index.js:1028:29)\n at Chat21Api.sendMessageRaw (/usr/src/app/chat21Api/index.js:762:14)\n at Chat21Api.sendGroupWelcomeMessage (/usr/src/app/chat21Api/index.js:205:14)\n at /usr/src/app/chat21Api/index.js:99:22\n at /usr/src/app/chat21Api/index.js:234:17\n at /usr/src/app/chatdb/index.js:77:9\n at executeCallback (/usr/src/app/node_modules/mongodb/lib/operations/execute_operation.js:70:5)\n at updateCallback (/usr/src/app/node_modules/mongodb/lib/operations/update_one.js:41:3)\n at /usr/src/app/node_modules/mongodb/lib/operations/update_one.js:24:64\n at handleCallback (/usr/src/app/node_modules/mongodb/lib/utils.js:128:55)\n at /usr/src/app/node_modules/mongodb/lib/operations/common_functions.js:378:5\n at handler (/usr/src/app/node_modules/mongodb/lib/core/sdam/topology.js:913:24)","stackAtStateChange":"Stack capture: Socket error\n at Connection.C.onSocketError (/usr/src/app/node_modules/amqplib/lib/connection.js:354:13)\n at Connection.emit (events.js:314:20)\n at Socket.go (/usr/src/app/node_modules/amqplib/lib/connection.js:481:12)\n at Socket.emit (events.js:314:20)\n at emitReadable_ (_stream_readable.js:557:12)\n at processTicksAndRejections (internal/process/task_queues.js:83:21)"}}
|
|
930
|
-
chat21Event.emit('group.create', data);
|
|
931
|
-
}).catch(function(err) {
|
|
932
|
-
winston.error("Error creating chat21 group ", err);
|
|
933
|
-
chat21Event.emit('group.create.error', err);
|
|
934
|
-
});
|
|
599
|
+
}));
|
|
600
|
+
})
|
|
935
601
|
|
|
936
|
-
|
|
602
|
+
/** GROUP EVENTS */
|
|
603
|
+
groupEvent.on('group.create', function (group) {
|
|
937
604
|
|
|
938
|
-
|
|
605
|
+
if (process.env.SYNC_CHAT21_GROUPS !== "true") {
|
|
606
|
+
winston.debug("[Chat21Handler] - group.create - Sync Tiledesk to Chat21 groups disabled");
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
939
609
|
|
|
610
|
+
winston.verbose("[Chat21Handler] - group.create - Creating chat21 group: ", group);
|
|
940
611
|
|
|
941
|
-
|
|
612
|
+
setImmediate(() => {
|
|
942
613
|
|
|
943
|
-
|
|
944
|
-
winston.debug("Sync Tiledesk to Chat21 groups disabled");
|
|
945
|
-
return;
|
|
946
|
-
}
|
|
614
|
+
chat21.auth.setAdminToken(adminToken);
|
|
947
615
|
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
setImmediate(() => {
|
|
616
|
+
const groupMembers = group.members;
|
|
617
|
+
const group_id = "group-" + group._id;
|
|
951
618
|
|
|
952
|
-
|
|
619
|
+
return chat21.groups.create(group.name, groupMembers, undefined, group_id).then(function (data) {
|
|
620
|
+
winston.verbose("[Chat21Handler] - group.create - Chat21 group created: " + JSON.stringify(data));
|
|
621
|
+
chat21Event.emit('group.create', data);
|
|
622
|
+
}).catch(function (err) {
|
|
623
|
+
winston.error("[Chat21Handler] - group.create - Error creating chat21 group: ", err);
|
|
624
|
+
chat21Event.emit('group.create.error', err);
|
|
625
|
+
});
|
|
953
626
|
|
|
627
|
+
});
|
|
628
|
+
});
|
|
954
629
|
|
|
955
|
-
|
|
956
|
-
winston.debug("groupMembers ", groupMembers);
|
|
957
|
-
|
|
958
|
-
var group_id = "group-" + group._id;
|
|
959
|
-
winston.debug("group_id :" + group_id);
|
|
630
|
+
groupEvent.on('group.update', function (group) {
|
|
960
631
|
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
return chat21.groups.setMembers(groupMembers, group_id).then(function(data) {
|
|
966
|
-
winston.verbose("Chat21 group.update set: " + JSON.stringify(data));
|
|
967
|
-
chat21Event.emit('group.setMembers', data);
|
|
968
|
-
}).catch(function(err) {
|
|
969
|
-
winston.error("Error setMembers chat21 group.update ", err);
|
|
970
|
-
chat21Event.emit('group.setMembers.error', err);
|
|
971
|
-
});
|
|
972
|
-
}).catch(function(err) {
|
|
973
|
-
winston.error("Error setting chat21 group.update ", err);
|
|
974
|
-
chat21Event.emit('group.update.error', err);
|
|
975
|
-
});
|
|
632
|
+
if (process.env.SYNC_CHAT21_GROUPS !== "true") {
|
|
633
|
+
winston.debug("[Chat21Handler] - group.update - Sync Tiledesk to Chat21 groups disabled");
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
976
636
|
|
|
977
|
-
|
|
637
|
+
winston.verbose("[Chat21Handler] - group.update - Updating chat21 group: ", group);
|
|
978
638
|
|
|
979
|
-
|
|
639
|
+
setImmediate(() => {
|
|
980
640
|
|
|
641
|
+
chat21.auth.setAdminToken(adminToken);
|
|
981
642
|
|
|
643
|
+
const groupMembers = group.members;
|
|
644
|
+
const group_id = "group-" + group._id;
|
|
982
645
|
|
|
646
|
+
return chat21.groups.update(group.name, undefined, undefined, group_id).then(function (data) {
|
|
647
|
+
winston.verbose("[Chat21Handler] - group.update - Chat21 group updated: " + JSON.stringify(data));
|
|
648
|
+
chat21Event.emit('group.update', data);
|
|
649
|
+
|
|
650
|
+
return chat21.groups.setMembers(groupMembers, group_id).then(function (data) {
|
|
651
|
+
winston.verbose("[Chat21Handler] - group.update - Chat21 group set: " + JSON.stringify(data));
|
|
652
|
+
chat21Event.emit('group.setMembers', data);
|
|
653
|
+
}).catch(function (err) {
|
|
654
|
+
winston.error("[Chat21Handler] - group.update - Error setting chat21 group: ", err);
|
|
655
|
+
chat21Event.emit('group.setMembers.error', err);
|
|
656
|
+
});
|
|
983
657
|
|
|
658
|
+
}).catch(function (err) {
|
|
659
|
+
winston.error("[Chat21Handler] - group.update - Error setting chat21 group: ", err);
|
|
660
|
+
chat21Event.emit('group.update.error', err);
|
|
661
|
+
});
|
|
984
662
|
|
|
985
|
-
|
|
663
|
+
});
|
|
664
|
+
});
|
|
986
665
|
|
|
987
|
-
|
|
988
|
-
winston.debug("Sync Tiledesk to Chat21 groups disabled");
|
|
989
|
-
return;
|
|
990
|
-
}
|
|
666
|
+
groupEvent.on('group.delete', function (group) {
|
|
991
667
|
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
668
|
+
if (process.env.SYNC_CHAT21_GROUPS !== "true") {
|
|
669
|
+
winston.debug("[Chat21Handler] - group.delete - Sync Tiledesk to Chat21 groups disabled");
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
995
672
|
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
var group_id = "group-" + group._id;
|
|
999
|
-
winston.debug("group_id :" + group_id);
|
|
673
|
+
winston.verbose("[Chat21Handler] - group.delete - Deleting chat21 group: ", group);
|
|
1000
674
|
|
|
1001
|
-
|
|
675
|
+
setImmediate(() => {
|
|
1002
676
|
|
|
1003
|
-
|
|
1004
|
-
winston.verbose("Chat21 group set for group.delete : " + JSON.stringify(data));
|
|
1005
|
-
chat21Event.emit('group.setMembers', data);
|
|
1006
|
-
}).catch(function(err) {
|
|
1007
|
-
winston.error("Error setMembers chat21 group for group.delete", err);
|
|
1008
|
-
chat21Event.emit('group.setMembers.error', err);
|
|
1009
|
-
});
|
|
677
|
+
chat21.auth.setAdminToken(adminToken);
|
|
1010
678
|
|
|
1011
|
-
|
|
679
|
+
const group_id = "group-" + group._id;
|
|
680
|
+
|
|
681
|
+
return chat21.groups.setMembers(["system"], group_id).then(function (data) {
|
|
682
|
+
winston.verbose("[Chat21Handler] - group.delete - Chat21 group set for group.delete : " + JSON.stringify(data));
|
|
683
|
+
chat21Event.emit('group.setMembers', data);
|
|
684
|
+
}).catch(function (err) {
|
|
685
|
+
winston.error("[Chat21Handler] - group.delete - Error setting chat21 group for group.delete: ", err);
|
|
686
|
+
chat21Event.emit('group.setMembers.error', err);
|
|
687
|
+
});
|
|
1012
688
|
|
|
1013
|
-
|
|
689
|
+
});
|
|
690
|
+
});
|
|
1014
691
|
|
|
1015
|
-
|
|
692
|
+
}
|
|
1016
693
|
|
|
1017
|
-
|
|
1018
694
|
}
|
|
1019
695
|
|
|
1020
|
-
|
|
696
|
+
const chat21Handler = new Chat21Handler();
|
|
1021
697
|
module.exports = chat21Handler;
|