@tiledesk/tiledesk-server 2.27.0 → 2.27.2
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 +7 -0
- package/channels/chat21/chat21Handler.js +569 -873
- package/channels/chat21/package-lock.json +70 -32
- package/channels/chat21/package.json +1 -1
- package/models/chat21SyncTrace.js +53 -0
- package/package.json +1 -1
- package/services/chat21SyncTracer.js +157 -0
- package/services/requestService.js +58 -1
|
@@ -7,1015 +7,711 @@ 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);
|
|
108
|
+
});
|
|
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);
|
|
112
127
|
});
|
|
113
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
|
+
});
|
|
114
150
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
var lastName = userData.updatedUser.lastname;
|
|
118
|
-
var current_user = userData.updatedUser.id;
|
|
119
|
-
|
|
120
|
-
setImmediate(() => {
|
|
121
|
-
winston.debug("Chat21Handler on user.update ", userData);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
122
153
|
|
|
123
|
-
|
|
154
|
+
botEvent.on('faqbot.update', function (bot) {
|
|
155
|
+
const firstName = bot.name;
|
|
156
|
+
const lastName = "";
|
|
157
|
+
const current_user = "bot_" + bot.id;
|
|
124
158
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
winston.verbose("Chat21 contact updated: " + JSON.stringify(data));
|
|
128
|
-
chat21Event.emit('contact.update', data);
|
|
129
|
-
}).catch(function(err) {
|
|
130
|
-
winston.error("Error updating chat21 contact ", err);
|
|
131
|
-
chat21Event.emit('contact.update.error', err);
|
|
132
|
-
});
|
|
159
|
+
setImmediate(() => {
|
|
160
|
+
winston.debug("[Chat21Handler] - faqbot.update - Bot update: ", bot);
|
|
133
161
|
|
|
134
|
-
|
|
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);
|
|
135
169
|
});
|
|
136
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
|
+
}
|
|
137
191
|
|
|
138
|
-
|
|
139
|
-
var firstName = bot.name;
|
|
140
|
-
var lastName = "";
|
|
141
|
-
var email = "";
|
|
142
|
-
// botprefix
|
|
143
|
-
var current_user = "bot_"+bot.id;
|
|
192
|
+
chat21.auth.setAdminToken(adminToken);
|
|
144
193
|
|
|
145
|
-
|
|
146
|
-
|
|
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);
|
|
147
200
|
|
|
148
|
-
|
|
201
|
+
const groupName = request.subject || lead.fullname;
|
|
149
202
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
});
|
|
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
|
+
});
|
|
158
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);
|
|
159
219
|
});
|
|
220
|
+
} catch (error) {
|
|
221
|
+
winston.error("[Chat21Handler] - lead.update - Error processing request " + request.request_id + ": ", error);
|
|
222
|
+
}
|
|
160
223
|
});
|
|
224
|
+
});
|
|
225
|
+
});
|
|
161
226
|
|
|
227
|
+
/** MESSAGE EVENTS */
|
|
228
|
+
messageEvent.on('message.test', function (message) {
|
|
162
229
|
|
|
230
|
+
winston.info("Chat21Sender message.test");
|
|
163
231
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
setImmediate(() => {
|
|
171
|
-
winston.debug("Chat21Handler on faqbot.create ", bot);
|
|
172
|
-
|
|
173
|
-
chat21.auth.setAdminToken(adminToken);
|
|
174
|
-
|
|
175
|
-
// update: function(firstname, lastname, current_user){
|
|
176
|
-
chat21.contacts.update(firstName, lastName, current_user).then(function(data) {
|
|
177
|
-
winston.verbose("Chat21 contact updated: " + JSON.stringify(data));
|
|
178
|
-
chat21Event.emit('contact.update', data);
|
|
179
|
-
}).catch(function(err) {
|
|
180
|
-
winston.error("Error updating chat21 contact ", err);
|
|
181
|
-
chat21Event.emit('contact.update.error', err);
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
});
|
|
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));
|
|
185
237
|
});
|
|
238
|
+
});
|
|
186
239
|
|
|
240
|
+
messageEvent.on('message.sending', (message) => {
|
|
187
241
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
// non sembra funzionare chiedi a Dario dove prende le info
|
|
191
|
-
setImmediate(() => {
|
|
192
|
-
winston.debug("Chat21Handler on lead.update ", lead);
|
|
193
|
-
|
|
194
|
-
// TODO AGGIORNA SOLO SE PASSA DA GUEST A ALTRO??
|
|
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
|
-
});
|
|
247
|
-
});
|
|
242
|
+
winston.verbose("[Chat21Handler] - message.sending - Message sending: ", message);
|
|
243
|
+
winston.debug("[Chat21Handler] - message.sending - Message sending: ", message);
|
|
248
244
|
|
|
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) {
|
|
249
250
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
winston.info("Chat21Sender message.test");
|
|
251
|
+
chat21.auth.setAdminToken(adminToken);
|
|
253
252
|
|
|
254
|
-
|
|
253
|
+
return this.typing(message, new Date()).then(function () {
|
|
255
254
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
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
|
-
});
|
|
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
|
+
}
|
|
471
261
|
|
|
262
|
+
winston.verbose("[Chat21Handler] - message.sending - Chat21 sending message: " + message.text);
|
|
263
|
+
winston.debug("[Chat21Handler] - message.sending - Chat21 sending message: ", message);
|
|
472
264
|
|
|
473
|
-
|
|
265
|
+
let timestamp = message.attributes?.clienttimestamp || Date.now();
|
|
266
|
+
let recipient_fullname = message.request?.subject || message.request?.lead?.fullname || "Guest";
|
|
474
267
|
|
|
475
|
-
|
|
476
|
-
|
|
268
|
+
if (message.request?.channel?.name) {
|
|
269
|
+
attributes['request_channel'] = message.request.channel.name;
|
|
270
|
+
}
|
|
477
271
|
|
|
478
|
-
|
|
272
|
+
return chat21.messages.sendToGroup(message.senderFullname, message.recipient, recipient_fullname, message.text, message.sender, attributes, message.type, message.metadata, timestamp).then(function (data) {
|
|
479
273
|
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
chat21.groups.updateAttributes(gattributes, request.request_id).then(function(data) {
|
|
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
|
-
});
|
|
274
|
+
winston.verbose("[Chat21Handler] - message.sending - Chat21 sent message: " + JSON.stringify(data) + " for text message " + message.text);
|
|
275
|
+
chat21Event.emit('message.sent', data);
|
|
490
276
|
|
|
491
|
-
|
|
492
|
-
|
|
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);
|
|
493
281
|
});
|
|
494
282
|
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
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
|
-
|
|
283
|
+
}).catch(function (err) {
|
|
284
|
+
winston.error("Chat21 sendToGroup err", err);
|
|
285
|
+
chat21Event.emit('message.sent.error', err);
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
}
|
|
638
289
|
|
|
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
|
-
});
|
|
700
|
-
});
|
|
701
|
-
|
|
702
|
-
requestEvent.on('request.delete', function(request) {
|
|
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) {
|
|
703
294
|
|
|
704
|
-
|
|
295
|
+
chat21.auth.setAdminToken(adminToken);
|
|
705
296
|
|
|
706
|
-
|
|
707
|
-
if (request.channelOutbound.name === ChannelConstants.CHAT21) {
|
|
297
|
+
return chat21.messages.send(message.senderFullname, message.recipient, message.recipientFullname, message.text, message.sender, message.attributes, message.type, message.metadata).then(function (data) {
|
|
708
298
|
|
|
709
|
-
|
|
299
|
+
winston.verbose("Chat21Sender send sent: " + JSON.stringify(data));
|
|
300
|
+
chat21Event.emit('message.sent', data);
|
|
710
301
|
|
|
711
|
-
|
|
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
|
+
});
|
|
712
307
|
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
chat21Event.emit('conversation.deleted.error', err);
|
|
719
|
-
})
|
|
720
|
-
}
|
|
721
|
-
})
|
|
308
|
+
}).catch(function (err) {
|
|
309
|
+
winston.error("[Chat21Handler] - message.sending - Error Chat21 send direct: ", err);
|
|
310
|
+
chat21Event.emit('message.sent.error', err);
|
|
311
|
+
});
|
|
312
|
+
}
|
|
722
313
|
|
|
723
|
-
|
|
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) {
|
|
724
318
|
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
winston.debug("chat21Handler requestEvent request.participants.update called" , data);
|
|
319
|
+
chat21.auth.setAdminToken(adminToken);
|
|
728
320
|
|
|
729
|
-
|
|
730
|
-
let removedParticipants = data.removedParticipants;
|
|
731
|
-
|
|
321
|
+
let timestamp = message.attributes?.clienttimestamp || Date.now();
|
|
732
322
|
|
|
733
|
-
|
|
734
|
-
if (request.channelOutbound.name === ChannelConstants.CHAT21) {
|
|
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) {
|
|
735
324
|
|
|
736
|
-
|
|
325
|
+
winston.verbose("[Chat21Handler] - message.sending - Chat21 sent message: " + JSON.stringify(data));
|
|
326
|
+
chat21Event.emit('message.sent', data);
|
|
737
327
|
|
|
738
|
-
|
|
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
|
+
});
|
|
739
333
|
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
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
|
+
});
|
|
746
346
|
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
members.push("system");
|
|
347
|
+
/** REQUEST EVENTS */
|
|
348
|
+
requestEvent.on('request.attributes.update', function (request) {
|
|
750
349
|
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
members.push(participant);
|
|
754
|
-
});
|
|
755
|
-
// requestObj.participants;
|
|
756
|
-
// var members = reqParticipantArray;
|
|
350
|
+
setImmediate(() => {
|
|
351
|
+
if (request.channelOutbound.name === ChannelConstants.CHAT21) {
|
|
757
352
|
|
|
758
|
-
|
|
759
|
-
// lead_id used. Change it?
|
|
760
|
-
members.push(request.lead.lead_id);
|
|
761
|
-
}
|
|
762
|
-
winston.verbose("Chat21 group with members for request.participants.update: " , members);
|
|
353
|
+
chat21.auth.setAdminToken(adminToken);
|
|
763
354
|
|
|
764
|
-
|
|
765
|
-
//racecondition
|
|
766
|
-
chat21.groups.setMembers(members, groupId).then(function(data) {
|
|
767
|
-
winston.verbose("Chat21 group set for request.participants.update : " + JSON.stringify(data));
|
|
768
|
-
chat21Event.emit('group.setMembers', data);
|
|
769
|
-
}).catch(function(err) {
|
|
770
|
-
winston.error("Error setting chat21 group for request.participants.update ", err);
|
|
771
|
-
chat21Event.emit('group.setMembers.error', err);
|
|
772
|
-
});
|
|
355
|
+
const gattributes = request.attributes;
|
|
773
356
|
|
|
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
|
+
});
|
|
774
365
|
|
|
775
|
-
|
|
776
|
-
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
});
|
|
777
369
|
|
|
778
|
-
|
|
779
|
-
// winston.info("newParticipants ", newParticipants);
|
|
370
|
+
requestEvent.on('request.create', function (request) {
|
|
780
371
|
|
|
781
|
-
|
|
782
|
-
|
|
372
|
+
winston.debug("[Chat21Handler] - request.create - Request create: ", request);
|
|
373
|
+
|
|
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
|
+
}
|
|
783
435
|
|
|
784
|
-
|
|
436
|
+
chat21.auth.setAdminToken(adminToken);
|
|
437
|
+
winston.debug("[Chat21Handler] - request.close - Archiving conversations for ", request.participants);
|
|
785
438
|
|
|
786
|
-
|
|
787
|
-
|
|
439
|
+
request.participants.forEach(function (participant) {
|
|
440
|
+
archiveConversation(traceEvent, request, participant);
|
|
441
|
+
});
|
|
788
442
|
|
|
789
|
-
|
|
790
|
-
//racecondition?low it's not dangerous archive a conversation that doen't exist
|
|
443
|
+
archiveConversation(traceEvent, request, "system");
|
|
791
444
|
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
}).catch(function(err) {
|
|
799
|
-
winston.error("Chat21 archived err", err);
|
|
800
|
-
chat21Event.emit('conversation.archived.error', err);
|
|
801
|
-
});
|
|
445
|
+
if (request.lead) {
|
|
446
|
+
archiveConversation(traceEvent, request, request.lead.lead_id);
|
|
447
|
+
}
|
|
448
|
+
}));
|
|
449
|
+
});
|
|
802
450
|
|
|
803
|
-
|
|
804
|
-
|
|
451
|
+
requestEvent.on('request.delete', function (request) {
|
|
805
452
|
|
|
453
|
+
winston.debug("[Chat21Handler] - request.delete - Request delete: ", request);
|
|
806
454
|
|
|
455
|
+
setImmediate(() => {
|
|
807
456
|
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
requestEvent.on('request.participants.join', function(data) {
|
|
814
|
-
winston.debug("chat21Handler requestEvent request.participants.join called" , data);
|
|
457
|
+
if (request.channelOutbound.name !== ChannelConstants.CHAT21) {
|
|
458
|
+
winston.error("[Chat21Handler] - request.delete - Request is not a chat21 request: ", request);
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
815
461
|
|
|
816
|
-
|
|
817
|
-
|
|
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
|
+
}
|
|
818
495
|
|
|
819
|
-
|
|
820
|
-
|
|
496
|
+
chat21.auth.setAdminToken(adminToken);
|
|
497
|
+
let requestObj = request.toJSON();
|
|
821
498
|
|
|
822
|
-
|
|
499
|
+
winston.verbose("[Chat21Handler] - request.participants.update - Setting chat21 group for request with id: " + requestObj._id);
|
|
823
500
|
|
|
824
|
-
|
|
501
|
+
const groupId = request.request_id;
|
|
825
502
|
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
var groupId = request.request_id;
|
|
503
|
+
let members = [];
|
|
504
|
+
members.push("system");
|
|
830
505
|
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
506
|
+
request.participants.forEach(function (participant, index) {
|
|
507
|
+
members.push(participant);
|
|
508
|
+
});
|
|
834
509
|
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
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
|
+
});
|
|
843
524
|
|
|
525
|
+
removedParticipants.forEach(function (removedParticipant) {
|
|
526
|
+
archiveConversation(traceEvent, request, removedParticipant);
|
|
527
|
+
});
|
|
844
528
|
|
|
529
|
+
}));
|
|
530
|
+
});
|
|
845
531
|
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
requestEvent.on('request.participants.leave', function(data) {
|
|
852
|
-
winston.debug("chat21Handler requestEvent request.participants.leave called" , data);
|
|
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);
|
|
853
535
|
|
|
854
|
-
|
|
855
|
-
|
|
536
|
+
let request = data.request;
|
|
537
|
+
let member = data.member;
|
|
538
|
+
chat21SyncTracer.trace(traceEvent, request, 'handler.received', 'info', { member: member, ...chat21SyncTracer.describeRequest(request) });
|
|
856
539
|
|
|
857
|
-
|
|
858
|
-
if (request.channelOutbound.name === ChannelConstants.CHAT21) {
|
|
540
|
+
setImmediate(() => runTraced(traceEvent, request, () => {
|
|
859
541
|
|
|
860
|
-
|
|
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
|
+
}
|
|
861
547
|
|
|
862
|
-
|
|
548
|
+
chat21.auth.setAdminToken(adminToken);
|
|
863
549
|
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
var groupId = request.request_id;
|
|
550
|
+
const groupId = request.request_id;
|
|
551
|
+
winston.verbose("[Chat21Handler] - request.participants.join - Joining member " + member + " for chat21 group with request : " + groupId);
|
|
868
552
|
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
chat21Event.emit('group.leave.error', err);
|
|
879
|
-
});
|
|
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
|
+
});
|
|
880
562
|
|
|
563
|
+
}));
|
|
564
|
+
});
|
|
881
565
|
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
.then(function(data){
|
|
886
|
-
winston.verbose("Chat21 archived " + JSON.stringify(data));
|
|
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
|
-
});
|
|
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);
|
|
894
569
|
|
|
895
|
-
|
|
570
|
+
let request = data.request;
|
|
571
|
+
let member = data.member;
|
|
572
|
+
chat21SyncTracer.trace(traceEvent, request, 'handler.received', 'info', { member: member, ...chat21SyncTracer.describeRequest(request) });
|
|
896
573
|
|
|
574
|
+
setImmediate(() => runTraced(traceEvent, request, () => {
|
|
897
575
|
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
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
|
+
}
|
|
902
581
|
|
|
582
|
+
chat21.auth.setAdminToken(adminToken);
|
|
583
|
+
const groupId = request.request_id;
|
|
903
584
|
|
|
585
|
+
winston.verbose("[Chat21Handler] - request.participants.leave - Leaving " + member + " for chat21 group for request with id: " + groupId);
|
|
904
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
|
+
});
|
|
905
596
|
|
|
906
|
-
|
|
597
|
+
archiveConversation(traceEvent, request, member);
|
|
907
598
|
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
return;
|
|
911
|
-
}
|
|
599
|
+
}));
|
|
600
|
+
})
|
|
912
601
|
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
setImmediate(() => {
|
|
602
|
+
/** GROUP EVENTS */
|
|
603
|
+
groupEvent.on('group.create', function (group) {
|
|
916
604
|
|
|
917
|
-
|
|
605
|
+
if (process.env.SYNC_CHAT21_GROUPS !== "true") {
|
|
606
|
+
winston.debug("[Chat21Handler] - group.create - Sync Tiledesk to Chat21 groups disabled");
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
918
609
|
|
|
610
|
+
winston.verbose("[Chat21Handler] - group.create - Creating chat21 group: ", group);
|
|
919
611
|
|
|
920
|
-
|
|
921
|
-
winston.debug("groupMembers ", groupMembers);
|
|
922
|
-
|
|
923
|
-
var group_id = "group-" + group._id;
|
|
924
|
-
winston.debug("group_id :" + group_id);
|
|
612
|
+
setImmediate(() => {
|
|
925
613
|
|
|
926
|
-
|
|
927
|
-
winston.verbose("Chat21 group created: " + JSON.stringify(data));
|
|
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
|
-
});
|
|
614
|
+
chat21.auth.setAdminToken(adminToken);
|
|
935
615
|
|
|
936
|
-
|
|
616
|
+
const groupMembers = group.members;
|
|
617
|
+
const group_id = "group-" + group._id;
|
|
937
618
|
|
|
938
|
-
|
|
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
|
+
});
|
|
939
626
|
|
|
627
|
+
});
|
|
628
|
+
});
|
|
940
629
|
|
|
941
|
-
|
|
630
|
+
groupEvent.on('group.update', function (group) {
|
|
942
631
|
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
632
|
+
if (process.env.SYNC_CHAT21_GROUPS !== "true") {
|
|
633
|
+
winston.debug("[Chat21Handler] - group.update - Sync Tiledesk to Chat21 groups disabled");
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
947
636
|
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
setImmediate(() => {
|
|
637
|
+
winston.verbose("[Chat21Handler] - group.update - Updating chat21 group: ", group);
|
|
951
638
|
|
|
952
|
-
|
|
639
|
+
setImmediate(() => {
|
|
953
640
|
|
|
641
|
+
chat21.auth.setAdminToken(adminToken);
|
|
954
642
|
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
var group_id = "group-" + group._id;
|
|
959
|
-
winston.debug("group_id :" + group_id);
|
|
643
|
+
const groupMembers = group.members;
|
|
644
|
+
const group_id = "group-" + group._id;
|
|
960
645
|
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
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
|
+
});
|
|
657
|
+
|
|
658
|
+
}).catch(function (err) {
|
|
659
|
+
winston.error("[Chat21Handler] - group.update - Error setting chat21 group: ", err);
|
|
660
|
+
chat21Event.emit('group.update.error', err);
|
|
661
|
+
});
|
|
976
662
|
|
|
977
|
-
|
|
663
|
+
});
|
|
664
|
+
});
|
|
978
665
|
|
|
979
|
-
|
|
666
|
+
groupEvent.on('group.delete', function (group) {
|
|
980
667
|
|
|
668
|
+
if (process.env.SYNC_CHAT21_GROUPS !== "true") {
|
|
669
|
+
winston.debug("[Chat21Handler] - group.delete - Sync Tiledesk to Chat21 groups disabled");
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
981
672
|
|
|
673
|
+
winston.verbose("[Chat21Handler] - group.delete - Deleting chat21 group: ", group);
|
|
982
674
|
|
|
675
|
+
setImmediate(() => {
|
|
983
676
|
|
|
677
|
+
chat21.auth.setAdminToken(adminToken);
|
|
984
678
|
|
|
985
|
-
|
|
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
|
+
});
|
|
986
688
|
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
return;
|
|
990
|
-
}
|
|
689
|
+
});
|
|
690
|
+
});
|
|
991
691
|
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
setImmediate(() => {
|
|
692
|
+
groupEvent.on('group.deleted', (request_id) => {
|
|
995
693
|
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
var group_id = "group-" + group._id;
|
|
999
|
-
winston.debug("group_id :" + group_id);
|
|
694
|
+
winston.debug("Chat21Handler on group.deleted for request_id: " + request_id);
|
|
1000
695
|
|
|
1001
|
-
|
|
696
|
+
setImmediate(() => {
|
|
1002
697
|
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
698
|
+
chat21.auth.setAdminToken(adminToken);
|
|
699
|
+
|
|
700
|
+
return chat21.groups.delete(request_id).then(function (data) {
|
|
701
|
+
winston.verbose("[Chat21Handler] - group.deleted - Chat21 group deleted: " + JSON.stringify(data));
|
|
702
|
+
chat21Event.emit('group.deleted', data);
|
|
703
|
+
}).catch(function (err) {
|
|
704
|
+
winston.error("[Chat21Handler] - group.deleted - Error deleting chat21 group: ", err);
|
|
705
|
+
chat21Event.emit('group.deleted.error', err);
|
|
706
|
+
});
|
|
1010
707
|
|
|
1011
|
-
|
|
708
|
+
})
|
|
1012
709
|
|
|
1013
|
-
|
|
710
|
+
})
|
|
1014
711
|
|
|
1015
|
-
|
|
712
|
+
}
|
|
1016
713
|
|
|
1017
|
-
|
|
1018
714
|
}
|
|
1019
715
|
|
|
1020
|
-
|
|
716
|
+
const chat21Handler = new Chat21Handler();
|
|
1021
717
|
module.exports = chat21Handler;
|