From 8f81aa2eeb61db173ec0a8e585452ae891284c5a Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 5 Jan 2023 14:28:35 +0100 Subject: [PATCH 1/7] 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 e5cbc58f4..cd0a2b3a2 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); + } } }; }; From 1a74e84359cf7acc99d7927c467b4f622e347d04 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 5 Jan 2023 14:32:14 +0100 Subject: [PATCH 2/7] feat: salta los tickets bloqueados --- back/methods/osticket/closeTicket.js | 92 +++++++++++++++------------- 1 file changed, 49 insertions(+), 43 deletions(-) diff --git a/back/methods/osticket/closeTicket.js b/back/methods/osticket/closeTicket.js index cd0a2b3a2..ad2af75b7 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 != 0 AND ote.type = 'R' + WHERE ote.staff_id AND ote.type = 'R' GROUP BY ote.thread_id ) sub ON sub.thread_id = ot2.id - 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)`; + WHERE ot.isanswered + AND ots.state = ? + AND IF(sub.updated > sub.created, sub.updated, sub.created) < DATE_SUB(CURDATE(), INTERVAL ? DAY)`; - let ticketsId = []; + const 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); - }); + con.query(sql, [config.oldStatus, config.day], + (err, results) => { + if (err) throw err; + for (const result of results) + ticketsId.push(result.ticket_id); + }); }); - await getRequestToken(); async function getRequestToken() { @@ -94,6 +94,40 @@ 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); + 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 + } + }; + 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 = { @@ -109,33 +143,5 @@ module.exports = Self => { 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); - } - } }; }; From f356188491e5f4261199510465b93a0d38cc8763 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 5 Jan 2023 14:54:43 +0100 Subject: [PATCH 3/7] feat: salta los tickets bloqueados --- back/methods/osticket/closeTicket.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/back/methods/osticket/closeTicket.js b/back/methods/osticket/closeTicket.js index ad2af75b7..c2f6aab1e 100644 --- a/back/methods/osticket/closeTicket.js +++ b/back/methods/osticket/closeTicket.js @@ -98,7 +98,7 @@ module.exports = Self => { for (const ticketId of ticketsId) { try { const lockCode = await getLockCode(token, secondCookie, ticketId); - if (lockCode == false) continue; + if (!lockCode) throw new Error(`Can't get lock code`); let form = new FormData(); form.append('__CSRFToken__', token); form.append('id', ticketId); @@ -123,7 +123,7 @@ module.exports = Self => { } catch (e) { const err = new Error(`${ticketId} Ticket close failed: ${e.message}`); err.stack += e.stack; - throw err; + console.error(err); } } } @@ -141,7 +141,7 @@ module.exports = Self => { const body = await response.text(); const json = JSON.parse(body); - return json.code || json.retry; + return json.code || null; } }; }; From a68fe26952f5e2286232fcc2178431fc61303b1c Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 5 Jan 2023 15:01:20 +0100 Subject: [PATCH 4/7] refactor: machar stack --- back/methods/osticket/closeTicket.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/back/methods/osticket/closeTicket.js b/back/methods/osticket/closeTicket.js index c2f6aab1e..601470a02 100644 --- a/back/methods/osticket/closeTicket.js +++ b/back/methods/osticket/closeTicket.js @@ -121,8 +121,8 @@ module.exports = Self => { }; await fetch(ostUri, params); } catch (e) { - const err = new Error(`${ticketId} Ticket close failed: ${e.message}`); - err.stack += e.stack; + const err = new Error(`$s{ticketId} Ticket close failed: ${e.message}`); + err.stack = e.stack; console.error(err); } } From 0c6d335c4ac5fd5cdaf6b0689047c8dceefb5914 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 5 Jan 2023 15:01:29 +0100 Subject: [PATCH 5/7] refactor: machacar stack --- back/methods/osticket/closeTicket.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back/methods/osticket/closeTicket.js b/back/methods/osticket/closeTicket.js index 601470a02..98ef0aeef 100644 --- a/back/methods/osticket/closeTicket.js +++ b/back/methods/osticket/closeTicket.js @@ -121,7 +121,7 @@ module.exports = Self => { }; await fetch(ostUri, params); } catch (e) { - const err = new Error(`$s{ticketId} Ticket close failed: ${e.message}`); + const err = new Error(`${ticketId} Ticket close failed: ${e.message}`); err.stack = e.stack; console.error(err); } From 201987fb992ac35220eef9d366ece37ff71228c2 Mon Sep 17 00:00:00 2001 From: vicent Date: Mon, 9 Jan 2023 11:42:16 +0100 Subject: [PATCH 6/7] feat: muestra el error --- back/methods/osticket/closeTicket.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/back/methods/osticket/closeTicket.js b/back/methods/osticket/closeTicket.js index 98ef0aeef..40c0d860c 100644 --- a/back/methods/osticket/closeTicket.js +++ b/back/methods/osticket/closeTicket.js @@ -97,13 +97,17 @@ module.exports = Self => { async function close(token, secondCookie) { for (const ticketId of ticketsId) { try { - const lockCode = await getLockCode(token, secondCookie, ticketId); - if (!lockCode) throw new Error(`Can't get lock code`); + const lock = await getLockCode(token, secondCookie, ticketId); + if (!lock.code) { + let error = `Can't get lock code`; + if (lock.msg) error += `: ${lock.msg}`; + throw new Error(error); + } let form = new FormData(); form.append('__CSRFToken__', token); form.append('id', ticketId); form.append('a', config.responseType); - form.append('lockCode', lockCode); + form.append('lockCode', lock.code); form.append('from_email_id', config.fromEmailId); form.append('reply-to', config.replyTo); form.append('cannedResp', 0); @@ -141,7 +145,7 @@ module.exports = Self => { const body = await response.text(); const json = JSON.parse(body); - return json.code || null; + return json; } }; }; From f103931e503f69a834171d35c9af096e740eea98 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 10 Jan 2023 08:14:13 +0100 Subject: [PATCH 7/7] fix: se concatenan pq sino no muestra el id del ticket --- back/methods/osticket/closeTicket.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back/methods/osticket/closeTicket.js b/back/methods/osticket/closeTicket.js index 40c0d860c..178b09601 100644 --- a/back/methods/osticket/closeTicket.js +++ b/back/methods/osticket/closeTicket.js @@ -126,7 +126,7 @@ module.exports = Self => { await fetch(ostUri, params); } catch (e) { const err = new Error(`${ticketId} Ticket close failed: ${e.message}`); - err.stack = e.stack; + err.stack += e.stack; console.error(err); } }