feat: close tickets
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Vicent Llopis 2022-07-19 11:20:08 +02:00
parent b0827bd98b
commit 5eec01bdc3
5 changed files with 127 additions and 0 deletions

View File

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

View File

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

View File

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

3
back/models/osticket.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = Self => {
require('../methods/osticket/closeTicket')(Self);
};

View File

@ -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",