From 5eec01bdc365b2b670a7fa81bc4dafd8ee288094 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 19 Jul 2022 11:20:08 +0200 Subject: [PATCH] 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",