-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathq_a.js
318 lines (303 loc) · 10 KB
/
q_a.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
var fsPath = require('fs-path');
var logger = new (require("./logger"))(__filename);
var engineApi = require('./engineApi');
var redisClient = require('./redisClient');
var db = require('./db');
var zammad = require('./zammad');
var wss = require("./websocket");
var ZAMMAD_POLLING_INTERVAL = process.env.ZAMMAD_POLLING_INTERVAL || 15; // In Minutes
var ZAMMAD_POLLING_BREAK = process.env.ZAMMAD_POLLING_BREAK || 5;
var VALID_ATTACHMENTS_EXTENSIONS = process.env.VALID_ATTACHMENTS_EXTENSIONS || ['mp3', 'wav', 'webm'];
var FS_ATTACHEMENTS_PATH = process.env.FS_ATTACHEMENTS_PATH || '/srv/http/answers';
var cachedAgents = [];
var lastZammadPoll = null;
//logger.debug('Connecting to Zammad service');
function dateDifferenceInSeconds(date1, date2) {
var diff = Math.abs(date1 - date2);
return Math.floor(diff / 1000);
}
function getTicketsFromZammad() {
return new Promise((fnResolve, fnReject) => {
redisClient.get('lastZammadQuery', (err, reply) => {
let query = '(';
if (reply) {
query += 'updated_at:>now-' + dateDifferenceInSeconds(new Date(+reply), new Date()) + 's AND ';
}
query += 'state:closed)';
zammad.get('tickets/search', {
query: query,
expand: true,
})
.then(body => {
return new Promise((resolve, reject) => {
if (!body.length) return fnResolve(body);
let ticketCount = body.length;
body.map(ticket => ({
id: ticket.id,
title: ticket.title,
subscriber_phone: ticket.customer,
articles_count: ticket.article_count,
state_id: ticket.state_id,
closed_at: ticket.close_at
}))
.forEach((ticket, index, tickets) => {
zammad.get(['ticket_articles/by_ticket', ticket.id].join('/'))
.then(body => {
let isValidTicket = false;
ticket.articles = body.filter((article, index, articles) => {
article.attachments = article.attachments.filter(attachment => {
let filenameSplitted = attachment.filename.split('.');
let extension = filenameSplitted[filenameSplitted.length - 1];
return VALID_ATTACHMENTS_EXTENSIONS.includes(extension);
});
if (article.attachments.length && index === 0) isValidTicket = true;
return article.attachments.length && isValidTicket;
})
.map(article => ({
id: article.id,
from: article.from,
subject: article.subject,
attachments: article.attachments,
created_at: article.created_at
}));
if (--ticketCount === 0) {
tickets.filter(ticket => (ticket.articles.length >= 2)).forEach((ticket, index, tickets) => {
db.createTicket(ticket.id, ticket.title, ticket.subscriber_phone, ticket.articles_count, ticket.state_id, ticket.closed_at)
.then(() => {
zammad.get('tags', {
object: 'Ticket',
o_id: ticket.id
})
.then(body => {
let redisMulti = redisClient.multi();
let redisTicketKey = ['tickets', ticket.id].join(':');
if (body.tags) {
ticket.tags = body.tags;
redisMulti.set([redisTicketKey, 'tags'].join(':'), JSON.stringify(ticket.tags)); }
let articleCount = ticket.articles.length;
ticket.articles.forEach(article => {
let attachmentCount = article.attachments.length;
redisMulti.hset([redisTicketKey, 'articles'].join(':'), article.id, JSON.stringify(article));
article.attachments.forEach((attachment, attachmentIndex) => {
let path = [FS_ATTACHEMENTS_PATH, ticket.id, article.id, attachment.filename].join('/');
zammad.get(['ticket_attachment', ticket.id, article.id, attachment.id].join('/'), null, 'binary')
.then(body => {
try {
fsPath.writeFileSync(path, body, 'binary');
}
catch(error) {
logger.error('Failed to save attachment "' + attachment.filename + '" to the file system.');
return fnReject(error);
}
if (--attachmentCount === 0 && --articleCount === 0 && ++ticketCount === tickets.length) {
redisClient.set('lastZammadQuery', Date.now());
fnResolve(tickets);
}
})
.catch(error => {
logger.error('Failed to fetch attachment for article ' + article.id, error);
return fnReject(error)
});
});
})
redisMulti.exec();
})
.catch(error => {
logger.error('Failed to fetch tags for ticket ' + ticket.id, error);
return fnReject(error)
});
})
.catch(error => {
logger.error('Failed to insert ticket into SQLite database.', error);
return fnReject(error)
});
});
}
})
.catch(error => {
logger.error('Failed to fetch ticket articles for ticket ' + ticket.id, error);
return fnReject(error)
});
});
});
})
.catch(error => {
logger.error('Failed to fetch tickets from Zammad!', error);
return fnReject(error);
});
});
});
}
function getRedisDataForTicket(id) {
return new Promise((resolve, reject) => {
let ticket = {};
redisClient.hgetall(['tickets', id].join(':'), (err, data) => {
if (data) {
Object.assign(ticket, data);
}
redisClient.hgetall(['tickets', id, 'articles'].join(':'), (err, articles) => {
articles = Object.values(articles);
articles = articles.map(article => JSON.parse(article));
ticket.articles = articles;
redisClient.get(['tickets', id, 'tags'].join(':'), (err, tags) => {
if (tags) {
tags = JSON.parse(tags);
ticket.tags = tags;
}
resolve(ticket);
});
});
});
});
}
function getTickets(filter) {
return new Promise((resolve, reject) => {
db.getTickets(filter).then(tickets => {
let count = tickets.length;
tickets.forEach(ticket => {
getRedisDataForTicket(ticket.id).then(ticketData => {
Object.assign(ticket, ticketData);
if (--count === 0) {
resolve(tickets);
pollZammad();
}
});
});
if (!tickets.length) {
resolve([]);
pollZammad();
}
}).catch(error => {
logger.debug('Failed to get tickets from database!', error);
});
});
}
function getAgents(cb) {
logger.debug('Get agents from Zammad!');
if (!cb) return cachedAgents;
zammad.get('users/search', {
query: '(active:true AND role_ids:2 AND phone:>0)',
expand: true
})
.then(body => {
let agents = body.map(agent => ({
id: agent.id,
email: agent.email,
firstname: agent.firstname,
lastname: agent.lastname,
phone: agent.phone,
mobile: agent.mobile,
image: agent.image,
organization_id: agent.organization_id,
groups: agent.groups
}));
cachedAgents = agents;
redisClient.set('cachedAgents', JSON.stringify(agents));
cb(null, agents);
}).catch(() => {
//.. Some error happend get the cached values
cb('Connection Timeout', cachedAgents);
});
}
function pollZammad() {
if (lastZammadPoll && lastZammadPoll > Date.now() - ZAMMAD_POLLING_BREAK * 60 * 1000) {
return logger.debug('Zammad was already polled in the past ' + ZAMMAD_POLLING_BREAK + ' minutes', lastZammadPoll);
}
lastZammadPoll = Date.now();
logger.debug('Polling Zammad');
getTicketsFromZammad().then(newTickets => {
let count = newTickets.length;
let tickets = [];
newTickets.forEach(ticket => {
tickets.push({ id: ticket.id, data: ticket });
if (--count === 0) {
logger.debug('New tickets fetched from Zammad!', newTickets);
wss.broadcastEvent('questions:update', tickets);
}
});
})
.catch(error => {
logger.debug('Failed to fetch tickets from zammad!', error);
});
}
function setFavorite(ids, favorite) {
return new Promise((resolve, reject) => {
let filter = { ids };
db.getTickets(filter).then((tickets) => {
let ids = [];
let updated = [];
let count = tickets.length;
tickets.forEach(ticket => {
ids.push(ticket.id);
redisClient.hset(['tickets', ticket.id].join(':'), 'favorite', favorite, (err, reply) => {
getRedisDataForTicket(ticket.id).then(ticketData => {
Object.assign(ticket, ticketData);
updated.push({ id: ticket.id, data: ticket });
if (--count === 0) {
redisClient[favorite ? 'sadd' : 'srem']("ticketTags:favorite", ids);
resolve(updated);
}
});
});
});
})
.catch(error => {
logger.debug('Query Error!', error);
reject(error);
});
});
}
function deleteTickets(ids) {
logger.debug('Delete tickets..', ids);
return new Promise((resolve, reject) => {
let redisMulti = redisClient.multi();
let count = ids.length;
let deleted = [];
db.deleteTickets(ids).then((results) => {
resolve(deleted);
logger.debug('Tickets deleted..');
redisMulti.srem('ticketTags:favorite', ids);
ids.forEach(id => {
deleted.push({ id: id, data: null });
let ticketKey = ['tickets', id].join(':');
let ticketArticlesKey = [ticketKey, 'articles'].join(':');
let ticketTagsKey = [ticketKey, 'tags'].join(':');
redisMulti.del([ticketKey, ticketArticlesKey, ticketTagsKey]);
try {
fsPath.remove([FS_ATTACHEMENTS_PATH, id].join('/'));
logger.debug('Removed file..');
}
catch(error) {
logger.debug('Failed to delete ticket from file system!', error);
}
if (--count === 0) {
redisMulti.exec((err, reply) => {
logger.debug('Redis commands executed.');
});
}
});
}).catch(error => {
logger.debug('Failed to delete tickets!', error);
reject(error);
});
});
}
db.init()
.then(() => {
logger.notice('SQLite database initialized successfully!');
setInterval(pollZammad, ZAMMAD_POLLING_INTERVAL * 60 * 1000);
})
.catch(error => {
logger.error('SQLite database initialization failed!', error);
});
redisClient.get('cachedAgents', (err, reply) => {
if (!err && reply) {
cachedAgents = JSON.parse(reply);
} else {
getAgents((err, agents) => { if (!err) cachedAgents = agents });
}
});
module.exports.getAgents = getAgents;
module.exports.getTickets = getTickets;
module.exports.deleteTickets = deleteTickets;
module.exports.setFavorite = setFavorite;