From 5eec01bdc365b2b670a7fa81bc4dafd8ee288094 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 19 Jul 2022 11:20:08 +0200 Subject: [PATCH 01/11] feat: close tickets --- back/methods/chat/closeTicket.js | 83 ++++++++++++++++++++++++++++ back/methods/osticket/closeTicket.js | 39 +++++++++++++ back/models/chat.js | 1 + back/models/osticket.js | 3 + package.json | 1 + 5 files changed, 127 insertions(+) create mode 100644 back/methods/chat/closeTicket.js create mode 100644 back/methods/osticket/closeTicket.js create mode 100644 back/models/osticket.js diff --git a/back/methods/chat/closeTicket.js b/back/methods/chat/closeTicket.js new file mode 100644 index 000000000..c57879cf0 --- /dev/null +++ b/back/methods/chat/closeTicket.js @@ -0,0 +1,83 @@ +const jsdom = require('jsdom'); +module.exports = Self => { + Self.remoteMethodCtx('closeTicket', { + description: 'Close tickets without response from the user', + accessType: 'READ', + returns: { + type: 'Object', + root: true + }, + http: { + path: `/closeTicket`, + verb: 'GET' + } + }); + + Self.closeTicket = async ctx => { + const models = Self.app.models; + // 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' + // `); + await requestToken(); + + async function requestToken() { + const ostUri = 'https://cau.verdnatura.es/scp/login.php'; + const response = await fetch(ostUri, {credentials: 'same-origin'}); + + const result = response.headers.get('set-cookie'); + const firtCookie = result.substring(0, 36); // logitud 37 + + const body = await response.text(); // obtain response in text format + const dom = new jsdom.JSDOM(body); // parse to html + const token = dom.window.document.querySelector('[name="__CSRFToken__"]').value; // get CSRFToken value + + await login(token, firtCookie); + } + + async function login(token, firtCookie) { + const ostUri = 'https://cau.verdnatura.es/scp/login.php'; + const data = `_CSRFToken__=${token}&do=scplogin&userid=vicent&passwd=llopis.19263&ajax=1`; + const params = { + method: 'POST', + body: JSON.stringify(data), // data can be `string` or {object}! + headers: { + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', + 'Cookie': firtCookie + }, + credentials: 'same-origin' + }; + const response = await fetch(ostUri, params); + console.log(firtCookie); + console.log(response.headers); + const result = response.headers.get('set-cookie'); + const secondCookie = result.substring(0, 36); // logitud 37 + console.log(response.statusText); + await close(token, secondCookie); + } + + async function close(token, secondCookie) { + const ostUri = 'https://cau.verdnatura.es/scp/ajax.php/tickets/47460/status'; + const data = `status_id=3&comments=&undefined=Cerrar`; + const params = { + method: 'POST', + body: JSON.stringify(data), // data can be `string` or {object}! + headers: { + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', + 'X-CSRFToken': token, + 'Cookie': secondCookie + }, + credentials: 'same-origin' + }; + const response = await fetch(ostUri, params); + console.log(response.statusText); + } + }; +}; diff --git a/back/methods/osticket/closeTicket.js b/back/methods/osticket/closeTicket.js new file mode 100644 index 000000000..b25c8301a --- /dev/null +++ b/back/methods/osticket/closeTicket.js @@ -0,0 +1,39 @@ +const axios = require('axios'); +module.exports = Self => { + Self.remoteMethodCtx('closeTicket', { + description: 'Close tickets without response from the user', + accessType: 'READ', + returns: { + type: 'Object', + root: true + }, + http: { + path: `/closeTicket`, + verb: 'GET' + } + }); + + Self.closeTicket = async ctx => { + const models = Self.app.models; + // 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' + // `); + console.log('Llega'); + // await requestToken(); + + // async function requestToken() { + // const ostUri = 'https://cau.verdnatura.es/scp/login.php'; + // const {data} = await axios.get(ostUri); + + // console.log(data.data); + // } + }; +}; diff --git a/back/models/chat.js b/back/models/chat.js index 95a1e2c29..371f95503 100644 --- a/back/models/chat.js +++ b/back/models/chat.js @@ -4,4 +4,5 @@ module.exports = Self => { require('../methods/chat/sendCheckingPresence')(Self); require('../methods/chat/notifyIssues')(Self); require('../methods/chat/sendQueued')(Self); + require('../methods/chat/closeTicket')(Self); }; diff --git a/back/models/osticket.js b/back/models/osticket.js new file mode 100644 index 000000000..58923e08e --- /dev/null +++ b/back/models/osticket.js @@ -0,0 +1,3 @@ +module.exports = Self => { + require('../methods/osticket/closeTicket')(Self); +}; diff --git a/package.json b/package.json index a10e445ed..c4133b824 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "i18n": "^0.8.4", "image-type": "^4.1.0", "imap": "^0.8.19", + "jsdom": "^16.7.0", "jszip": "^3.10.0", "ldapjs": "^2.2.0", "loopback": "^3.26.0", -- 2.40.1 From eccbd942f7a83ce1ebb544d92556842ed9819623 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 21 Jul 2022 13:11:24 +0200 Subject: [PATCH 02/11] feat: can close a ticket --- back/methods/chat/closeTicket.js | 83 ------------------- back/methods/osticket/closeTicket.js | 70 ++++++++++++++-- back/model-config.json | 3 + back/models/chat.js | 1 - back/models/osticket-config.json | 31 +++++++ db/changes/10481-june/00-aclOsTicket.sql | 3 + .../10481-june/00-aclOsTicketConfig.sql | 3 + db/changes/10481-june/00-osTicketConfig.sql | 13 +++ 8 files changed, 115 insertions(+), 92 deletions(-) delete mode 100644 back/methods/chat/closeTicket.js create mode 100644 back/models/osticket-config.json create mode 100644 db/changes/10481-june/00-aclOsTicket.sql create mode 100644 db/changes/10481-june/00-aclOsTicketConfig.sql create mode 100644 db/changes/10481-june/00-osTicketConfig.sql diff --git a/back/methods/chat/closeTicket.js b/back/methods/chat/closeTicket.js deleted file mode 100644 index c57879cf0..000000000 --- a/back/methods/chat/closeTicket.js +++ /dev/null @@ -1,83 +0,0 @@ -const jsdom = require('jsdom'); -module.exports = Self => { - Self.remoteMethodCtx('closeTicket', { - description: 'Close tickets without response from the user', - accessType: 'READ', - returns: { - type: 'Object', - root: true - }, - http: { - path: `/closeTicket`, - verb: 'GET' - } - }); - - Self.closeTicket = async ctx => { - const models = Self.app.models; - // 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' - // `); - await requestToken(); - - async function requestToken() { - const ostUri = 'https://cau.verdnatura.es/scp/login.php'; - const response = await fetch(ostUri, {credentials: 'same-origin'}); - - const result = response.headers.get('set-cookie'); - const firtCookie = result.substring(0, 36); // logitud 37 - - const body = await response.text(); // obtain response in text format - const dom = new jsdom.JSDOM(body); // parse to html - const token = dom.window.document.querySelector('[name="__CSRFToken__"]').value; // get CSRFToken value - - await login(token, firtCookie); - } - - async function login(token, firtCookie) { - const ostUri = 'https://cau.verdnatura.es/scp/login.php'; - const data = `_CSRFToken__=${token}&do=scplogin&userid=vicent&passwd=llopis.19263&ajax=1`; - const params = { - method: 'POST', - body: JSON.stringify(data), // data can be `string` or {object}! - headers: { - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', - 'Cookie': firtCookie - }, - credentials: 'same-origin' - }; - const response = await fetch(ostUri, params); - console.log(firtCookie); - console.log(response.headers); - const result = response.headers.get('set-cookie'); - const secondCookie = result.substring(0, 36); // logitud 37 - console.log(response.statusText); - await close(token, secondCookie); - } - - async function close(token, secondCookie) { - const ostUri = 'https://cau.verdnatura.es/scp/ajax.php/tickets/47460/status'; - const data = `status_id=3&comments=&undefined=Cerrar`; - const params = { - method: 'POST', - body: JSON.stringify(data), // data can be `string` or {object}! - headers: { - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', - 'X-CSRFToken': token, - 'Cookie': secondCookie - }, - credentials: 'same-origin' - }; - const response = await fetch(ostUri, params); - console.log(response.statusText); - } - }; -}; diff --git a/back/methods/osticket/closeTicket.js b/back/methods/osticket/closeTicket.js index b25c8301a..06997dbe8 100644 --- a/back/methods/osticket/closeTicket.js +++ b/back/methods/osticket/closeTicket.js @@ -1,4 +1,4 @@ -const axios = require('axios'); +const jsdom = require('jsdom'); module.exports = Self => { Self.remoteMethodCtx('closeTicket', { description: 'Close tickets without response from the user', @@ -15,6 +15,10 @@ module.exports = Self => { Self.closeTicket = async ctx => { const models = Self.app.models; + const config = await models.OsTicketConfig.findOne(); + + 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 @@ -26,14 +30,64 @@ module.exports = Self => { // AND (t.staff_id = 0 AND t.team_id = 0) // AND dept.code = 'IT' // `); - console.log('Llega'); - // await requestToken(); + await requestToken(); - // async function requestToken() { - // const ostUri = 'https://cau.verdnatura.es/scp/login.php'; - // const {data} = await axios.get(ostUri); + async function requestToken() { + const response = await fetch(ostUri); - // console.log(data.data); - // } + 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); + } + + 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) { + 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); + } }; }; diff --git a/back/model-config.json b/back/model-config.json index 343210383..f4adc954b 100644 --- a/back/model-config.json +++ b/back/model-config.json @@ -116,6 +116,9 @@ "OsTicket": { "dataSource": "osticket" }, + "OsTicketConfig": { + "dataSource": "vn" + }, "Edi": { "dataSource": "vn" } diff --git a/back/models/chat.js b/back/models/chat.js index 371f95503..95a1e2c29 100644 --- a/back/models/chat.js +++ b/back/models/chat.js @@ -4,5 +4,4 @@ module.exports = Self => { require('../methods/chat/sendCheckingPresence')(Self); require('../methods/chat/notifyIssues')(Self); require('../methods/chat/sendQueued')(Self); - require('../methods/chat/closeTicket')(Self); }; diff --git a/back/models/osticket-config.json b/back/models/osticket-config.json new file mode 100644 index 000000000..fe67c10f5 --- /dev/null +++ b/back/models/osticket-config.json @@ -0,0 +1,31 @@ +{ + "name": "OsTicketConfig", + "base": "VnModel", + "options": { + "mysql": { + "table": "osTicketConfig" + } + }, + "properties": { + "id": { + "type": "number", + "id": true, + "description": "Identifier" + }, + "host": { + "type": "string" + }, + "user": { + "type": "string" + }, + "password": { + "type": "string" + }, + "statusId": { + "type": "number" + }, + "action": { + "type": "string" + } + } +} \ No newline at end of file diff --git a/db/changes/10481-june/00-aclOsTicket.sql b/db/changes/10481-june/00-aclOsTicket.sql new file mode 100644 index 000000000..ae2a121f5 --- /dev/null +++ b/db/changes/10481-june/00-aclOsTicket.sql @@ -0,0 +1,3 @@ +INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) + VALUES + ('OsTicket', '*', '*', 'ALLOW', 'ROLE', 'employee'); \ No newline at end of file diff --git a/db/changes/10481-june/00-aclOsTicketConfig.sql b/db/changes/10481-june/00-aclOsTicketConfig.sql new file mode 100644 index 000000000..842e7a113 --- /dev/null +++ b/db/changes/10481-june/00-aclOsTicketConfig.sql @@ -0,0 +1,3 @@ +INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) + VALUES + ('OsTicketConfig', '*', 'READ', 'ALLOW', 'ROLE', 'employee'); \ No newline at end of file diff --git a/db/changes/10481-june/00-osTicketConfig.sql b/db/changes/10481-june/00-osTicketConfig.sql new file mode 100644 index 000000000..6e476d25b --- /dev/null +++ b/db/changes/10481-june/00-osTicketConfig.sql @@ -0,0 +1,13 @@ +CREATE TABLE `vn`.`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, + `action` varchar(100) COLLATE utf8mb3_unicode_ci 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 -- 2.40.1 From 538c04e8c52d03639accf8a59cac280bd520f584 Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 29 Jul 2022 11:59:03 +0200 Subject: [PATCH 03/11] feat: db connection and query --- back/methods/osticket/closeTicket.js | 82 +++++++++++++-------- back/models/osticket-config.json | 8 +- db/changes/10481-june/00-osTicketConfig.sql | 8 +- 3 files changed, 65 insertions(+), 33 deletions(-) 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); -- 2.40.1 From cd78afb539657f7d9eed54fde5e6003d8a2eb8e2 Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 29 Jul 2022 12:03:59 +0200 Subject: [PATCH 04/11] fix: db changes --- db/changes/10481-june/00-osTicketConfig.sql | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/db/changes/10481-june/00-osTicketConfig.sql b/db/changes/10481-june/00-osTicketConfig.sql index 24538fa21..bfe85228e 100644 --- a/db/changes/10481-june/00-osTicketConfig.sql +++ b/db/changes/10481-june/00-osTicketConfig.sql @@ -1,4 +1,4 @@ -CREATE TABLE `osTicketConfig` ( +CREATE TABLE `vn`.`osTicketConfig` ( `id` int(11) NOT NULL, `host` varchar(100) COLLATE utf8mb3_unicode_ci DEFAULT NULL, `user` varchar(100) COLLATE utf8mb3_unicode_ci DEFAULT NULL, @@ -10,6 +10,6 @@ CREATE TABLE `osTicketConfig` ( PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; -INSERT INTO `vn`.`osTicketConfig`(id, host, `user`, password, statusId, `action`) - VALUES - (0, 'http://localhost:56596/scp', 'ostadmin', 'Admin1', 1, 3, 'Cerrar', 60); +INSERT INTO `vn`.`osTicketConfig`(id, host, `user`, password, oldStatusId, newStatusId, `action`, `day`) + VALUES + (0, 'http://localhost:56596/scp', 'ostadmin', 'Admin1', 1, 3, 'Cerrar', 60); -- 2.40.1 From 28bf7bd74722480b7ffe1431c07155e95cf19796 Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 15 Sep 2022 08:34:42 +0200 Subject: [PATCH 05/11] feat: check general status 'open' to close ticket --- back/methods/osticket/closeTicket.js | 5 +++-- back/models/osticket-config.json | 7 +++++-- db/changes/10481-june/00-osTicketConfig.sql | 7 ++++--- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/back/methods/osticket/closeTicket.js b/back/methods/osticket/closeTicket.js index 1b4eaedbd..17e8d6b91 100644 --- a/back/methods/osticket/closeTicket.js +++ b/back/methods/osticket/closeTicket.js @@ -30,6 +30,7 @@ module.exports = Self => { const sql = `SELECT ot.ticket_id, ot.number FROM osticket.ost_ticket ot + JOIN osticket.ost_ticket_status ots ON ots.id = ot.status_id 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 @@ -38,7 +39,7 @@ module.exports = Self => { GROUP BY ote.thread_id ) sub ON sub.thread_id = ot2.id WHERE ot.isanswered = 1 - AND status_id IN (${config.oldStatusId}) + AND ots.state = '${config.oldStatus}' AND IF(sub.updated > sub.created, sub.updated, sub.created) < DATE_SUB(CURDATE(), INTERVAL ${config.day} DAY)`; let ticketsId = []; @@ -96,7 +97,7 @@ module.exports = Self => { const ostUri = `${config.host}/ajax.php/tickets/${ticketId}/status`; const data = { status_id: config.newStatusId, - comments: undefined, + comments: config.comment, undefined: config.action }; const params = { diff --git a/back/models/osticket-config.json b/back/models/osticket-config.json index db4799e99..529d081c8 100644 --- a/back/models/osticket-config.json +++ b/back/models/osticket-config.json @@ -21,8 +21,8 @@ "password": { "type": "string" }, - "oldStatusId": { - "type": "number" + "oldStatus": { + "type": "string" }, "newStatusId": { "type": "number" @@ -32,6 +32,9 @@ }, "day": { "type": "number" + }, + "comment": { + "type": "string" } } } \ 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 bfe85228e..fa969ccb0 100644 --- a/db/changes/10481-june/00-osTicketConfig.sql +++ b/db/changes/10481-june/00-osTicketConfig.sql @@ -3,13 +3,14 @@ CREATE TABLE `vn`.`osTicketConfig` ( `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, - `oldStatusId` int(11) DEFAULT NULL, + `oldStatus` varchar(100) COLLATE utf8mb3_unicode_ci DEFAULT NULL, `newStatusId` int(11) DEFAULT NULL, `action` varchar(100) COLLATE utf8mb3_unicode_ci DEFAULT NULL, `day` int(11) DEFAULT NULL, + `comment` varchar(100) COLLATE utf8mb3_unicode_ci DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; -INSERT INTO `vn`.`osTicketConfig`(id, host, `user`, password, oldStatusId, newStatusId, `action`, `day`) +INSERT INTO `vn`.`osTicketConfig`(`id`, `host`, `user`, `password`, `oldStatus`, `newStatusId`, `action`, `day`, `comment`) VALUES - (0, 'http://localhost:56596/scp', 'ostadmin', 'Admin1', 1, 3, 'Cerrar', 60); + (0, 'http://localhost:56596/scp', 'ostadmin', 'Admin1', 'open', 3, 'Cerrar', 60, 'Este CAU se ha cerrado automáticamente'); -- 2.40.1 From 5fce5b3e4fdd031b4af6f2db13f5fb1623c9cb0e Mon Sep 17 00:00:00 2001 From: vicent Date: Thu, 15 Sep 2022 08:38:00 +0200 Subject: [PATCH 06/11] refator: delete console.log --- back/methods/osticket/closeTicket.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/back/methods/osticket/closeTicket.js b/back/methods/osticket/closeTicket.js index 17e8d6b91..19c9aff19 100644 --- a/back/methods/osticket/closeTicket.js +++ b/back/methods/osticket/closeTicket.js @@ -45,7 +45,6 @@ module.exports = Self => { 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) @@ -109,9 +108,8 @@ module.exports = Self => { 'Cookie': secondCookie } }; - const response = await fetch(ostUri, params); - console.log(response.statusText); - // return fetch(ostUri, params); + + return fetch(ostUri, params); } } }; -- 2.40.1 From 7480bf8a69bc35939a0a89a890dd8fd28be29a0a Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 16 Sep 2022 10:31:02 +0200 Subject: [PATCH 07/11] feat: do nothing if user and password are empty in osTicketConfig --- back/methods/osticket/closeTicket.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/back/methods/osticket/closeTicket.js b/back/methods/osticket/closeTicket.js index 19c9aff19..d742a265f 100644 --- a/back/methods/osticket/closeTicket.js +++ b/back/methods/osticket/closeTicket.js @@ -11,7 +11,7 @@ module.exports = Self => { }, http: { path: `/closeTicket`, - verb: 'GET' + verb: 'POST' } }); @@ -19,6 +19,9 @@ module.exports = Self => { const models = Self.app.models; const config = await models.OsTicketConfig.findOne(); + if (!config.user || !config.password) + return false; + const ostUri = `${config.host}/login.php`; const con = mysql.createConnection({ -- 2.40.1 From a695468bb7c80d011ed902c0ddb9c09ce1008650 Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 16 Sep 2022 10:32:37 +0200 Subject: [PATCH 08/11] refator: deleted sensitive information --- db/changes/10481-june/00-osTicketConfig.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/changes/10481-june/00-osTicketConfig.sql b/db/changes/10481-june/00-osTicketConfig.sql index fa969ccb0..36e55b003 100644 --- a/db/changes/10481-june/00-osTicketConfig.sql +++ b/db/changes/10481-june/00-osTicketConfig.sql @@ -13,4 +13,4 @@ CREATE TABLE `vn`.`osTicketConfig` ( INSERT INTO `vn`.`osTicketConfig`(`id`, `host`, `user`, `password`, `oldStatus`, `newStatusId`, `action`, `day`, `comment`) VALUES - (0, 'http://localhost:56596/scp', 'ostadmin', 'Admin1', 'open', 3, 'Cerrar', 60, 'Este CAU se ha cerrado automáticamente'); + (0, 'http://localhost:56596/scp', '', '', 'open', 3, 'Cerrar', 60, 'Este CAU se ha cerrado automáticamente'); -- 2.40.1 From afeed9757cfce52cbe110f7f078b21caa3601e52 Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 16 Sep 2022 10:39:59 +0200 Subject: [PATCH 09/11] refactor: change acl --- db/changes/10481-june/00-aclOsTicketConfig.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/changes/10481-june/00-aclOsTicketConfig.sql b/db/changes/10481-june/00-aclOsTicketConfig.sql index 842e7a113..0ce58b260 100644 --- a/db/changes/10481-june/00-aclOsTicketConfig.sql +++ b/db/changes/10481-june/00-aclOsTicketConfig.sql @@ -1,3 +1,3 @@ INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES - ('OsTicketConfig', '*', 'READ', 'ALLOW', 'ROLE', 'employee'); \ No newline at end of file + ('OsTicketConfig', '*', 'READ', 'ALLOW', 'ROLE', 'it'); \ No newline at end of file -- 2.40.1 From 6367f1ef052fc013715c8655604a3eda41803b31 Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 16 Sep 2022 11:46:55 +0200 Subject: [PATCH 10/11] refator: delete sensitive info --- back/methods/osticket/closeTicket.js | 15 +++++++-------- back/models/osticket-config.json | 12 ++++++++++++ db/changes/10481-june/00-osTicketConfig.sql | 8 ++++++-- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/back/methods/osticket/closeTicket.js b/back/methods/osticket/closeTicket.js index d742a265f..87d54d3b8 100644 --- a/back/methods/osticket/closeTicket.js +++ b/back/methods/osticket/closeTicket.js @@ -18,17 +18,16 @@ module.exports = Self => { Self.closeTicket = async ctx => { const models = Self.app.models; const config = await models.OsTicketConfig.findOne(); - - if (!config.user || !config.password) - return false; - const ostUri = `${config.host}/login.php`; + if (!config.user || !config.password || !config.userDb || !config.passwordDb) + return false; + const con = mysql.createConnection({ - host: 'localhost', - user: 'osticket', - password: 'osticket', - port: 40003 + host: `${config.hostDb}`, + user: `${config.userDb}`, + password: `${config.passwordDb}`, + port: `${config.portDb}` }); const sql = `SELECT ot.ticket_id, ot.number diff --git a/back/models/osticket-config.json b/back/models/osticket-config.json index 529d081c8..d42632c6a 100644 --- a/back/models/osticket-config.json +++ b/back/models/osticket-config.json @@ -35,6 +35,18 @@ }, "comment": { "type": "string" + }, + "hostDb": { + "type": "string" + }, + "userDb": { + "type": "string" + }, + "passwordDb": { + "type": "string" + }, + "portDb": { + "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 36e55b003..ad6662715 100644 --- a/db/changes/10481-june/00-osTicketConfig.sql +++ b/db/changes/10481-june/00-osTicketConfig.sql @@ -8,9 +8,13 @@ CREATE TABLE `vn`.`osTicketConfig` ( `action` varchar(100) COLLATE utf8mb3_unicode_ci DEFAULT NULL, `day` int(11) DEFAULT NULL, `comment` varchar(100) COLLATE utf8mb3_unicode_ci DEFAULT NULL, + `hostDb` varchar(100) COLLATE utf8mb3_unicode_ci DEFAULT NULL, + `userDb` varchar(100) COLLATE utf8mb3_unicode_ci DEFAULT NULL, + `passwordDb` varchar(100) COLLATE utf8mb3_unicode_ci DEFAULT NULL, + `portDb` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; -INSERT INTO `vn`.`osTicketConfig`(`id`, `host`, `user`, `password`, `oldStatus`, `newStatusId`, `action`, `day`, `comment`) +INSERT INTO `vn`.`osTicketConfig`(`id`, `host`, `user`, `password`, `oldStatus`, `newStatusId`, `action`, `day`, `comment`, `hostDb`, `userDb`, `passwordDb`, `portDb`) VALUES - (0, 'http://localhost:56596/scp', '', '', 'open', 3, 'Cerrar', 60, 'Este CAU se ha cerrado automáticamente'); + (0, 'https://cau.verdnatura.es/scp', NULL, NULL, 'open', 3, 'Cerrar', 60, 'Este CAU se ha cerrado automáticamente', NULL, NULL, NULL, NULL); \ No newline at end of file -- 2.40.1 From a330e9d64a81db74462ef4c61fcd47bd092a9b9c Mon Sep 17 00:00:00 2001 From: vicent Date: Fri, 16 Sep 2022 12:02:04 +0200 Subject: [PATCH 11/11] refactor: change acl --- db/changes/10481-june/00-aclOsTicketConfig.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/changes/10481-june/00-aclOsTicketConfig.sql b/db/changes/10481-june/00-aclOsTicketConfig.sql index 0ce58b260..ad53ea6ae 100644 --- a/db/changes/10481-june/00-aclOsTicketConfig.sql +++ b/db/changes/10481-june/00-aclOsTicketConfig.sql @@ -1,3 +1,3 @@ INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES - ('OsTicketConfig', '*', 'READ', 'ALLOW', 'ROLE', 'it'); \ No newline at end of file + ('OsTicketConfig', '*', '*', 'ALLOW', 'ROLE', 'it'); \ No newline at end of file -- 2.40.1