2022-07-21 11:11:24 +00:00
|
|
|
const jsdom = require('jsdom');
|
2022-07-29 09:59:03 +00:00
|
|
|
const mysql = require('mysql');
|
|
|
|
|
2022-07-19 09:20:08 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('closeTicket', {
|
|
|
|
description: 'Close tickets without response from the user',
|
|
|
|
accessType: 'READ',
|
|
|
|
returns: {
|
|
|
|
type: 'Object',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/closeTicket`,
|
2022-09-16 08:31:02 +00:00
|
|
|
verb: 'POST'
|
2022-07-19 09:20:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.closeTicket = async ctx => {
|
|
|
|
const models = Self.app.models;
|
2022-07-21 11:11:24 +00:00
|
|
|
const config = await models.OsTicketConfig.findOne();
|
2022-09-16 09:46:55 +00:00
|
|
|
const ostUri = `${config.host}/login.php`;
|
2022-07-21 11:11:24 +00:00
|
|
|
|
2022-09-16 09:46:55 +00:00
|
|
|
if (!config.user || !config.password || !config.userDb || !config.passwordDb)
|
2022-09-16 08:31:02 +00:00
|
|
|
return false;
|
|
|
|
|
2022-07-29 09:59:03 +00:00
|
|
|
const con = mysql.createConnection({
|
2022-09-16 09:46:55 +00:00
|
|
|
host: `${config.hostDb}`,
|
|
|
|
user: `${config.userDb}`,
|
|
|
|
password: `${config.passwordDb}`,
|
|
|
|
port: `${config.portDb}`
|
2022-07-29 09:59:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const sql = `SELECT ot.ticket_id, ot.number
|
|
|
|
FROM osticket.ost_ticket ot
|
2022-09-15 06:34:42 +00:00
|
|
|
JOIN osticket.ost_ticket_status ots ON ots.id = ot.status_id
|
2022-07-29 09:59:03 +00:00
|
|
|
JOIN osticket.ost_thread ot2 ON ot2.object_id = ot.ticket_id AND ot2.object_type = 'T'
|
|
|
|
JOIN (
|
|
|
|
SELECT ote.thread_id, MAX(ote.created) created, MAX(ote.updated) updated
|
|
|
|
FROM osticket.ost_thread_entry ote
|
|
|
|
WHERE ote.staff_id != 0 AND ote.type = 'R'
|
|
|
|
GROUP BY ote.thread_id
|
|
|
|
) sub ON sub.thread_id = ot2.id
|
|
|
|
WHERE ot.isanswered = 1
|
2022-09-15 06:34:42 +00:00
|
|
|
AND ots.state = '${config.oldStatus}'
|
2022-07-29 09:59:03 +00:00
|
|
|
AND IF(sub.updated > sub.created, sub.updated, sub.created) < DATE_SUB(CURDATE(), INTERVAL ${config.day} DAY)`;
|
|
|
|
|
|
|
|
let ticketsId = [];
|
|
|
|
con.connect(err => {
|
|
|
|
if (err) throw err;
|
|
|
|
con.query(sql, (err, results) => {
|
|
|
|
if (err) throw err;
|
|
|
|
for (const result of results)
|
|
|
|
ticketsId.push(result.ticket_id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-07-21 11:11:24 +00:00
|
|
|
await requestToken();
|
|
|
|
|
|
|
|
async function requestToken() {
|
|
|
|
const response = await fetch(ostUri);
|
2022-07-19 09:20:08 +00:00
|
|
|
|
2022-07-21 11:11:24 +00:00
|
|
|
const result = response.headers.get('set-cookie');
|
|
|
|
const [firtHeader] = result.split(' ');
|
|
|
|
const firtCookie = firtHeader.substring(0, firtHeader.length - 1);
|
|
|
|
const body = await response.text();
|
|
|
|
const dom = new jsdom.JSDOM(body);
|
|
|
|
const token = dom.window.document.querySelector('[name="__CSRFToken__"]').value;
|
|
|
|
|
|
|
|
await login(token, firtCookie);
|
|
|
|
}
|
2022-07-19 09:20:08 +00:00
|
|
|
|
2022-07-21 11:11:24 +00:00
|
|
|
async function login(token, firtCookie) {
|
|
|
|
const data = {
|
|
|
|
__CSRFToken__: token,
|
|
|
|
do: 'scplogin',
|
|
|
|
userid: config.user,
|
|
|
|
passwd: config.password,
|
|
|
|
ajax: 1
|
|
|
|
};
|
|
|
|
const params = {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams(data),
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
|
|
|
'Cookie': firtCookie
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const response = await fetch(ostUri, params);
|
|
|
|
const result = response.headers.get('set-cookie');
|
|
|
|
const [firtHeader] = result.split(' ');
|
|
|
|
const secondCookie = firtHeader.substring(0, firtHeader.length - 1);
|
|
|
|
|
|
|
|
await close(token, secondCookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function close(token, secondCookie) {
|
2022-07-29 09:59:03 +00:00
|
|
|
for (const ticketId of ticketsId) {
|
|
|
|
const ostUri = `${config.host}/ajax.php/tickets/${ticketId}/status`;
|
|
|
|
const data = {
|
|
|
|
status_id: config.newStatusId,
|
2022-09-15 06:34:42 +00:00
|
|
|
comments: config.comment,
|
2022-07-29 09:59:03 +00:00
|
|
|
undefined: config.action
|
|
|
|
};
|
|
|
|
const params = {
|
|
|
|
method: 'POST',
|
|
|
|
body: new URLSearchParams(data),
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
|
|
|
'X-CSRFToken': token,
|
|
|
|
'Cookie': secondCookie
|
|
|
|
}
|
|
|
|
};
|
2022-09-15 06:38:00 +00:00
|
|
|
|
|
|
|
return fetch(ostUri, params);
|
2022-07-29 09:59:03 +00:00
|
|
|
}
|
2022-07-21 11:11:24 +00:00
|
|
|
}
|
2022-07-19 09:20:08 +00:00
|
|
|
};
|
|
|
|
};
|