salix/back/methods/osticket/closeTicket.js

94 lines
3.4 KiB
JavaScript
Raw Normal View History

2022-07-21 11:11:24 +00:00
const jsdom = require('jsdom');
2022-07-19 09:20:08 +00:00
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;
2022-07-21 11:11:24 +00:00
const config = await models.OsTicketConfig.findOne();
const ostUri = `${config.host}/login.php`;
2022-07-19 09:20:08 +00:00
// 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'
// `);
2022-07-21 11:11:24 +00:00
await requestToken();
async function requestToken() {
const response = await fetch(ostUri);
2022-07-19 09:20:08 +00:00
2022-07-21 11:11:24 +00:00
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);
}
2022-07-19 09:20:08 +00:00
2022-07-21 11:11:24 +00:00
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);
}
2022-07-19 09:20:08 +00:00
};
};