diff --git a/back/methods/osticket/closeTicket.js b/back/methods/osticket/closeTicket.js index 06997dbe8..1b4eaedbd 100644 --- a/back/methods/osticket/closeTicket.js +++ b/back/methods/osticket/closeTicket.js @@ -1,4 +1,6 @@ const jsdom = require('jsdom'); +const mysql = require('mysql'); + module.exports = Self => { Self.remoteMethodCtx('closeTicket', { description: 'Close tickets without response from the user', @@ -19,17 +21,37 @@ module.exports = Self => { const ostUri = `${config.host}/login.php`; - // const tickets = await models.OsTicket.rawSql(` - // SELECT t.ticket_id AS id, t.number, ua.username, td.subject - // FROM ost_ticket t - // JOIN ost_user_account ua ON t.user_id = ua.user_id - // JOIN ost_ticket__cdata td ON t.ticket_id = td.ticket_id - // JOIN ost_ticket_priority tp ON td.priority = tp.priority_id - // LEFT JOIN ost_department dept ON dept.id = t.dept_id - // WHERE tp.priority = 'emergency' - // AND (t.staff_id = 0 AND t.team_id = 0) - // AND dept.code = 'IT' - // `); + const con = mysql.createConnection({ + host: 'localhost', + user: 'osticket', + password: 'osticket', + port: 40003 + }); + + const sql = `SELECT ot.ticket_id, ot.number + FROM osticket.ost_ticket ot + 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 + AND status_id IN (${config.oldStatusId}) + 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; + console.log('Connected!'); + con.query(sql, (err, results) => { + if (err) throw err; + for (const result of results) + ticketsId.push(result.ticket_id); + }); + }); + await requestToken(); async function requestToken() { @@ -70,24 +92,26 @@ module.exports = Self => { } async function close(token, secondCookie) { - const ostUri = `${config.host}/ajax.php/tickets/47460/status`; - const data = { - status_id: config.statusId, - comments: undefined, - 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 - } - }; - const response = await fetch(ostUri, params); - console.log(response.statusText); - // return fetch(ostUri, params); + for (const ticketId of ticketsId) { + const ostUri = `${config.host}/ajax.php/tickets/${ticketId}/status`; + const data = { + status_id: config.newStatusId, + comments: undefined, + 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 + } + }; + const response = await fetch(ostUri, params); + console.log(response.statusText); + // return fetch(ostUri, params); + } } }; }; diff --git a/back/models/osticket-config.json b/back/models/osticket-config.json index fe67c10f5..db4799e99 100644 --- a/back/models/osticket-config.json +++ b/back/models/osticket-config.json @@ -21,11 +21,17 @@ "password": { "type": "string" }, - "statusId": { + "oldStatusId": { + "type": "number" + }, + "newStatusId": { "type": "number" }, "action": { "type": "string" + }, + "day": { + "type": "number" } } } \ No newline at end of file diff --git a/db/changes/10481-june/00-osTicketConfig.sql b/db/changes/10481-june/00-osTicketConfig.sql index 6e476d25b..24538fa21 100644 --- a/db/changes/10481-june/00-osTicketConfig.sql +++ b/db/changes/10481-june/00-osTicketConfig.sql @@ -1,13 +1,15 @@ -CREATE TABLE `vn`.`osTicketConfig` ( +CREATE TABLE `osTicketConfig` ( `id` int(11) NOT NULL, `host` varchar(100) COLLATE utf8mb3_unicode_ci DEFAULT NULL, `user` varchar(100) COLLATE utf8mb3_unicode_ci DEFAULT NULL, `password` varchar(100) COLLATE utf8mb3_unicode_ci DEFAULT NULL, - `statusId` int(11) DEFAULT NULL, + `oldStatusId` int(11) DEFAULT NULL, + `newStatusId` int(11) DEFAULT NULL, `action` varchar(100) COLLATE utf8mb3_unicode_ci DEFAULT NULL, + `day` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; INSERT INTO `vn`.`osTicketConfig`(id, host, `user`, password, statusId, `action`) VALUES - (0, 'https://cau.verdnatura.es/scp', 'vicent', 'llopis.19263', 3, 'Cerrar'); \ No newline at end of file + (0, 'http://localhost:56596/scp', 'ostadmin', 'Admin1', 1, 3, 'Cerrar', 60);