salix/back/methods/chat/closeTicket.js

84 lines
3.4 KiB
JavaScript

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);
}
};
};