diff --git a/back/methods/osticket/closeTicket.js b/back/methods/osticket/closeTicket.js index 87d54d3b8..33fe5958b 100644 --- a/back/methods/osticket/closeTicket.js +++ b/back/methods/osticket/closeTicket.js @@ -1,12 +1,13 @@ const jsdom = require('jsdom'); const mysql = require('mysql'); +const FormData = require('form-data'); module.exports = Self => { Self.remoteMethodCtx('closeTicket', { description: 'Close tickets without response from the user', accessType: 'READ', returns: { - type: 'Object', + type: 'object', root: true }, http: { @@ -54,9 +55,9 @@ module.exports = Self => { }); }); - await requestToken(); + await getRequestToken(); - async function requestToken() { + async function getRequestToken() { const response = await fetch(ostUri); const result = response.headers.get('set-cookie'); @@ -93,24 +94,45 @@ module.exports = Self => { await close(token, secondCookie); } + async function getLockCode(token, secondCookie, ticketId) { + const ostUri = `${config.host}/ajax.php/lock/ticket/${ticketId}`; + const params = { + method: 'POST', + headers: { + 'X-CSRFToken': token, + 'Cookie': secondCookie + } + }; + const response = await fetch(ostUri, params); + const body = await response.text(); + const json = JSON.parse(body); + + return json.code; + } + async function close(token, secondCookie) { for (const ticketId of ticketsId) { - const ostUri = `${config.host}/ajax.php/tickets/${ticketId}/status`; - const data = { - status_id: config.newStatusId, - comments: config.comment, - undefined: config.action - }; + const lockCode = await getLockCode(token, secondCookie, ticketId); + let form = new FormData(); + form.append('__CSRFToken__', token); + form.append('id', ticketId); + form.append('a', config.responseType); + form.append('lockCode', lockCode); + form.append('from_email_id', config.fromEmailId); + form.append('reply-to', config.replyTo); + form.append('cannedResp', 0); + form.append('response', config.comment); + form.append('signature', 'none'); + form.append('reply_status_id', config.newStatusId); + + const ostUri = `${config.host}/tickets.php?id=${ticketId}`; const params = { method: 'POST', - body: new URLSearchParams(data), + body: form, headers: { - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', - 'X-CSRFToken': token, 'Cookie': secondCookie } }; - return fetch(ostUri, params); } } diff --git a/back/models/osticket-config.json b/back/models/osticket-config.json index d42632c6a..5a863e7bc 100644 --- a/back/models/osticket-config.json +++ b/back/models/osticket-config.json @@ -27,9 +27,6 @@ "newStatusId": { "type": "number" }, - "action": { - "type": "string" - }, "day": { "type": "number" }, @@ -47,6 +44,15 @@ }, "portDb": { "type": "number" + }, + "responseType": { + "type": "string" + }, + "fromEmailId": { + "type": "number" + }, + "replyTo": { + "type": "string" } } } \ No newline at end of file diff --git a/db/changes/10481-june/00-osTicketConfig.sql b/db/changes/10481-june/00-osTicketConfig.sql index ad6662715..8727c816d 100644 --- a/db/changes/10481-june/00-osTicketConfig.sql +++ b/db/changes/10481-june/00-osTicketConfig.sql @@ -13,8 +13,4 @@ CREATE TABLE `vn`.`osTicketConfig` ( `passwordDb` varchar(100) COLLATE utf8mb3_unicode_ci DEFAULT NULL, `portDb` int(11) DEFAULT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; - -INSERT INTO `vn`.`osTicketConfig`(`id`, `host`, `user`, `password`, `oldStatus`, `newStatusId`, `action`, `day`, `comment`, `hostDb`, `userDb`, `passwordDb`, `portDb`) - VALUES - (0, 'https://cau.verdnatura.es/scp', NULL, NULL, 'open', 3, 'Cerrar', 60, 'Este CAU se ha cerrado automáticamente', NULL, NULL, NULL, NULL); \ No newline at end of file +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; \ No newline at end of file diff --git a/db/changes/10491-august/00-osTicketConfig.sql b/db/changes/10491-august/00-osTicketConfig.sql new file mode 100644 index 000000000..10a58b6c8 --- /dev/null +++ b/db/changes/10491-august/00-osTicketConfig.sql @@ -0,0 +1,8 @@ +ALTER TABLE `vn`.`osTicketConfig` DROP COLUMN `action`; +ALTER TABLE `vn`.`osTicketConfig` ADD responseType varchar(100) NULL; +ALTER TABLE `vn`.`osTicketConfig` ADD fromEmailId INT NULL; +ALTER TABLE `vn`.`osTicketConfig` ADD replyTo varchar(100) NULL; + +UPDATE `vn`.`osTicketConfig` + SET responseType='reply', fromEmailId=5, replyTo='all' +WHERE id=0; \ No newline at end of file diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 5cadaf77c..a744b21c3 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2710,3 +2710,7 @@ INSERT INTO `vn`.`ticketCollection` (`ticketFk`, `collectionFk`, `created`, `lev UPDATE `account`.`user` SET `hasGrant` = 1 WHERE `id` = 66; + +INSERT INTO `vn`.`osTicketConfig` (`id`, `host`, `user`, `password`, `oldStatus`, `newStatusId`, `day`, `comment`, `hostDb`, `userDb`, `passwordDb`, `portDb`, `responseType`, `fromEmailId`, `replyTo`) + VALUES + (0, 'http://localhost:56596/scp', 'ostadmin', 'Admin1', 'open', 3, 60, 'Este CAU se ha cerrado automáticamente. Si el problema persiste responda a este mensaje.', 'localhost', 'osticket', 'osticket', 40003, 'reply', 1, 'all'); \ No newline at end of file diff --git a/package.json b/package.json index f85af904c..ae4817ab0 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "bcrypt": "^5.0.1", "bmp-js": "^0.1.0", "compression": "^1.7.3", + "form-data": "^4.0.0", "fs-extra": "^5.0.0", "ftps": "^1.2.0", "got": "^10.7.0",