salix/back/methods/chat/notifyIssues.js

49 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-02-01 08:29:47 +00:00
module.exports = Self => {
Self.remoteMethodCtx('notifyIssues', {
description: 'Notifies new urgent issues',
accessType: 'READ',
returns: {
type: 'Object',
root: true
},
http: {
path: `/notifyIssues`,
2022-09-13 11:53:44 +00:00
verb: 'POST'
2021-02-01 08:29:47 +00:00
}
});
Self.notifyIssues = async ctx => {
const models = Self.app.models;
const $t = ctx.req.__; // $translate
2021-04-12 05:48:29 +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'
2021-04-12 05:48:29 +00:00
`);
2021-02-01 08:29:47 +00:00
2021-04-12 05:48:29 +00:00
if (!tickets.length) return;
2021-02-01 08:29:47 +00:00
2021-04-12 05:48:29 +00:00
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})`;
2021-02-01 08:29:47 +00:00
});
const department = await models.Department.findOne({
where: {code: 'IT'}
});
const channelName = department && department.chatName;
if (channelName)
return Self.send(ctx, `#${channelName}`, `@all ➔ ${message}`);
return;
};
};