hubxglpi/src/modules/watchdog/model/email.model.js

83 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// src/modules/watchdog/model/email.model.js
function prepareNotificationPayload(tickets) {
return {
subject: buildSubject(tickets),
bodyEmail: buildBodyEmail(tickets),
recipients: getRecipients(),
cc: getCc()
};
}
function buildSubject(tickets) {
if (tickets.length === 0) {
return '[GOLEIRO] Nenhum chamado foi agarrado';
}
return `🧤 [GOLEIRO] ${tickets.length} chamados foram agarrados`;
}
function buildBodyEmail(tickets) {
let body = `
<p>🚨 <strong>Atenção!</strong></p>
<p>O goleiro defendeu os seguintes chamados:</p>
<p>Esses chamados foram <strong>fechados no Hubsoft</strong>, mas ainda constam como <strong>abertos no GLPI</strong>.</p>
<br>
<ul>
`;
tickets.forEach(ticket => {
body += `
<li>
Protocolo Hubsoft: <strong>${ticket.protocolo_hub}</strong><br>
Mundiale ID: <strong>${ticket.ticket_mundiale}</strong><br>
GLPI ID: <strong>${ticket.glpi_ticket_id ?? 'não encontrado'}</strong><br>
Fechado em: ${formatDate(ticket.closed_at)}
</li>
<br>
`;
});
body += `
</ul>
<br>
<p>⚽ Favor verificar e alinhar os status no GLPI.</p>
<p><em>Watchdog Hub × GLPI</em></p>
`;
return body;
}
function formatDate(value) {
if (!value) return 'não informado';
const d = new Date(value);
if (Number.isNaN(d.getTime())) return String(value); // fallback seguro
return d.toLocaleString('pt-BR', { timeZone: 'America/Sao_Paulo' });
}
function formatDate(date) {
try {
return new Date(date).toLocaleString('pt-BR');
} catch {
return date;
}
}
function getRecipients() {
return process.env.WATCHDOG_RECIPIENT_EMAILS?.split(',') || [];
}
function getCc() {
return process.env.WATCHDOG_CC_EMAILS?.split(',') || [];
}
module.exports = {
prepareNotificationPayload
};