feat: can close a ticket
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Vicent Llopis 2022-07-21 13:11:24 +02:00
parent 0ff3a9d45a
commit eccbd942f7
8 changed files with 115 additions and 92 deletions

View File

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

View File

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

View File

@ -116,6 +116,9 @@
"OsTicket": {
"dataSource": "osticket"
},
"OsTicketConfig": {
"dataSource": "vn"
},
"Edi": {
"dataSource": "vn"
}

View File

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

View File

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

View File

@ -0,0 +1,3 @@
INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`)
VALUES
('OsTicket', '*', '*', 'ALLOW', 'ROLE', 'employee');

View File

@ -0,0 +1,3 @@
INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`)
VALUES
('OsTicketConfig', '*', 'READ', 'ALLOW', 'ROLE', 'employee');

View File

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