module.exports = Self => { Self.remoteMethodCtx('notifyIssues', { description: 'Notifies new urgent issues', accessType: 'READ', returns: { type: 'Object', root: true }, http: { path: `/notifyIssues`, verb: 'POST' } }); Self.notifyIssues = async ctx => { const models = Self.app.models; const $t = ctx.req.__; // $translate 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' `); if (!tickets.length) return; 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({ where: {code: 'IT'} }); const channelName = department && department.chatName; if (channelName) return Self.send(ctx, `#${channelName}`, `@all ➔ ${message}`); }; };