From 8f81aa2eeb61db173ec0a8e585452ae891284c5a Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 5 Jan 2023 14:28:35 +0100 Subject: [PATCH] fix: salta los tickets que estan bloqueados --- back/methods/osticket/closeTicket.js | 93 +++++++++++++--------------- 1 file changed, 44 insertions(+), 49 deletions(-) diff --git a/back/methods/osticket/closeTicket.js b/back/methods/osticket/closeTicket.js index e5cbc58f42..cd0a2b3a2b 100644 --- a/back/methods/osticket/closeTicket.js +++ b/back/methods/osticket/closeTicket.js @@ -25,10 +25,10 @@ module.exports = Self => { return false; const con = mysql.createConnection({ - host: config.hostDb, - user: config.userDb, - password: config.passwordDb, - port: config.portDb + host: `${config.hostDb}`, + user: `${config.userDb}`, + password: `${config.passwordDb}`, + port: `${config.portDb}` }); const sql = `SELECT ot.ticket_id, ot.number @@ -38,23 +38,23 @@ module.exports = Self => { JOIN ( SELECT ote.thread_id, MAX(ote.created) created, MAX(ote.updated) updated FROM osticket.ost_thread_entry ote - WHERE ote.staff_id AND ote.type = 'R' + WHERE ote.staff_id != 0 AND ote.type = 'R' GROUP BY ote.thread_id ) sub ON sub.thread_id = ot2.id - WHERE ot.isanswered - AND ots.state = ? - AND IF(sub.updated > sub.created, sub.updated, sub.created) < DATE_SUB(CURDATE(), INTERVAL ? DAY)`; + WHERE ot.isanswered = 1 + AND ots.state = '${config.oldStatus}' + AND IF(sub.updated > sub.created, sub.updated, sub.created) < DATE_SUB(CURDATE(), INTERVAL ${config.day} DAY)`; - const ticketsId = []; + let ticketsId = []; con.connect(err => { if (err) throw err; - con.query(sql, [config.oldStatus, config.day], - (err, results) => { - if (err) throw err; - for (const result of results) - ticketsId.push(result.ticket_id); - }); + con.query(sql, (err, results) => { + if (err) throw err; + for (const result of results) + ticketsId.push(result.ticket_id); + }); }); + await getRequestToken(); async function getRequestToken() { @@ -94,39 +94,6 @@ module.exports = Self => { await close(token, secondCookie); } - async function close(token, secondCookie) { - for (const ticketId of ticketsId) { - try { - const lockCode = await getLockCode(token, secondCookie, ticketId); - let form = new FormData(); - form.append('__CSRFToken__', token); - form.append('id', ticketId); - form.append('a', config.responseType); - form.append('lockCode', lockCode); - form.append('from_email_id', config.fromEmailId); - form.append('reply-to', config.replyTo); - form.append('cannedResp', 0); - form.append('response', config.comment); - form.append('signature', 'none'); - form.append('reply_status_id', config.newStatusId); - - const ostUri = `${config.host}/tickets.php?id=${ticketId}`; - const params = { - method: 'POST', - body: form, - headers: { - 'Cookie': secondCookie - } - }; - await fetch(ostUri, params); - } catch (e) { - const err = new Error(`${ticketId} Ticket close failed: ${e.message}`); - err.stack += e.stack; - throw err; - } - } - } - async function getLockCode(token, secondCookie, ticketId) { const ostUri = `${config.host}/ajax.php/lock/ticket/${ticketId}`; const params = { @@ -140,7 +107,35 @@ module.exports = Self => { const body = await response.text(); const json = JSON.parse(body); - return json.code; + return json.code || json.retry; + } + + async function close(token, secondCookie) { + for (const ticketId of ticketsId) { + const lockCode = await getLockCode(token, secondCookie, ticketId); + if (lockCode == false) continue; + let form = new FormData(); + form.append('__CSRFToken__', token); + form.append('id', ticketId); + form.append('a', config.responseType); + form.append('lockCode', lockCode); + form.append('from_email_id', config.fromEmailId); + form.append('reply-to', config.replyTo); + form.append('cannedResp', 0); + form.append('response', config.comment); + form.append('signature', 'none'); + form.append('reply_status_id', config.newStatusId); + + const ostUri = `${config.host}/tickets.php?id=${ticketId}`; + const params = { + method: 'POST', + body: form, + headers: { + 'Cookie': secondCookie + } + }; + return fetch(ostUri, params); + } } }; };