From 6eba19a1b7564cb9f3b3c71933ff1110b94ea58c Mon Sep 17 00:00:00 2001 From: joan Date: Mon, 12 Apr 2021 07:48:29 +0200 Subject: [PATCH] OsTicket notifications --- back/methods/chat/notifyIssues.js | 20 +++++++++++++------- back/methods/chat/spec/notifyIssue.spec.js | 13 ++++++++++--- back/model-config.json | 3 +++ back/models/osticket.json | 12 ++++++++++++ loopback/locale/es.json | 2 +- loopback/server/datasources.json | 9 +++++++++ 6 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 back/models/osticket.json diff --git a/back/methods/chat/notifyIssues.js b/back/methods/chat/notifyIssues.js index 54eb41c89..ef3c2cf1f 100644 --- a/back/methods/chat/notifyIssues.js +++ b/back/methods/chat/notifyIssues.js @@ -15,15 +15,21 @@ module.exports = Self => { Self.notifyIssues = async ctx => { const models = Self.app.models; const $t = ctx.req.__; // $translate - const [urgentIssue] = await Self.rawSql(` - SELECT * FROM managedesktop.vn_workOrderInmediata LIMIT 1 - `); + 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 + WHERE tp.priority = 'emergency' AND t.staff_id = 0 + `); - if (!urgentIssue) return; + if (!tickets.length) return; - const message = $t(`There's a new urgent ticket`, { - title: urgentIssue.title, - issueId: urgentIssue.workOrderId + let message = $t(`There's a new urgent ticket:`); + const ostUri = 'https://cau.verdnatura.es/scp/tickets.php?id='; + tickets.forEach(ticket => { + message += `\r\n[ID: *${ticket.number}* - ${ticket.subject} (@${ticket.username})](${ostUri + ticket.id})`; }); const department = await models.Department.findOne({ diff --git a/back/methods/chat/spec/notifyIssue.spec.js b/back/methods/chat/spec/notifyIssue.spec.js index e23c33859..9e8ac9e52 100644 --- a/back/methods/chat/spec/notifyIssue.spec.js +++ b/back/methods/chat/spec/notifyIssue.spec.js @@ -6,11 +6,12 @@ describe('Chat notifyIssue()', () => { return value; }; const chatModel = app.models.Chat; + const osTicketModel = app.models.OsTicket; const departmentId = 31; it(`should not call to the send() method and neither return a response`, async() => { spyOn(chatModel, 'send').and.callThrough(); - spyOn(chatModel, 'rawSql').and.returnValue([]); + spyOn(osTicketModel, 'rawSql').and.returnValue([]); const response = await chatModel.notifyIssues(ctx); @@ -20,7 +21,13 @@ describe('Chat notifyIssue()', () => { it(`should return a response calling the send() method`, async() => { spyOn(chatModel, 'send').and.callThrough(); - spyOn(chatModel, 'rawSql').and.returnValue([{title: 'Issue title'}]); + spyOn(osTicketModel, 'rawSql').and.returnValue([{ + id: 1, + number: '00001', + username: 'batman', + subject: 'Issue title'} + ]); + const expectedMessage = `@all ➔ There's a new urgent ticket:\r\n[ID: *00001* - Issue title (@batman)](https://osticket.verdnatura.es/scp/tickets.php?id=1)`; const department = await app.models.Department.findById(departmentId); let orgChatName = department.chatName; @@ -30,7 +37,7 @@ describe('Chat notifyIssue()', () => { expect(response.statusCode).toEqual(200); expect(response.message).toEqual('Fake notification sent'); - expect(chatModel.send).toHaveBeenCalledWith(ctx, '#IT', `@all ➔ There's a new urgent ticket`); + expect(chatModel.send).toHaveBeenCalledWith(ctx, '#IT', expectedMessage); // restores await department.updateAttribute('chatName', orgChatName); diff --git a/back/model-config.json b/back/model-config.json index 418cd4d1f..bf473d7bc 100644 --- a/back/model-config.json +++ b/back/model-config.json @@ -94,6 +94,9 @@ }, "Warehouse": { "dataSource": "vn" + }, + "OsTicket": { + "dataSource": "osticket" } } diff --git a/back/models/osticket.json b/back/models/osticket.json new file mode 100644 index 000000000..0c673d004 --- /dev/null +++ b/back/models/osticket.json @@ -0,0 +1,12 @@ +{ + "name": "OsTicket", + "base": "VnModel", + "acls": [{ + "property": "validations", + "accessType": "EXECUTE", + "principalType": "ROLE", + "principalId": "$everyone", + "permission": "ALLOW" + }] +} + \ No newline at end of file diff --git a/loopback/locale/es.json b/loopback/locale/es.json index 9c80bb816..869821704 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -172,7 +172,7 @@ "Swift / BIC cannot be empty": "Swift / BIC no puede estar vacío", "This BIC already exist.": "Este BIC ya existe.", "That item doesn't exists": "Ese artículo no existe", - "There's a new urgent ticket": "Hay un nuevo ticket urgente: [{{title}}](https://cau.verdnatura.es/WorkOrder.do?woMode=viewWO&woID={{issueId}})", + "There's a new urgent ticket:": "Hay un nuevo ticket urgente:", "Invalid account": "Cuenta inválida", "Compensation account is empty": "La cuenta para compensar está vacia", "This genus already exist": "Este genus ya existe", diff --git a/loopback/server/datasources.json b/loopback/server/datasources.json index 343bcedd8..87fff60e1 100644 --- a/loopback/server/datasources.json +++ b/loopback/server/datasources.json @@ -17,6 +17,15 @@ "connectTimeout": 40000, "acquireTimeout": 20000 }, + "osticket": { + "connector": "vn-mysql", + "database": "vn", + "debug": false, + "host": "localhost", + "port": "3306", + "username": "root", + "password": "root" + }, "tempStorage": { "name": "tempStorage", "connector": "loopback-component-storage",