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

125 lines
3.3 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, type = 'func') {
const normalizedType = String(type || 'func').toLowerCase()
const isAdm = normalizedType === 'adm'
return {
subject: buildSubject(tickets, normalizedType),
bodyEmail: isAdm ? buildBodyEmailAdm(tickets) : buildBodyEmail(tickets),
recipients: getRecipients(normalizedType),
cc: getCc(normalizedType)
}
}
function buildSubject(tickets, type = 'func') {
if (type === 'adm') {
if (tickets.length === 0) {
return '🧤⚽ [GOLEIRO ADM] Nenhum chamado foi para os pênaltis'
}
return `🚨⚽ [GOLEIRO ADM] ${tickets.length} chamados foram para os pênaltis`
}
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, time!</strong></p>
<p>O goleiro entrou em ação e 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>
🧾 <strong>Protocolo Hubsoft:</strong> ${ticket.protocolo_hub}<br>
🧠 <strong>Mundiale ID:</strong> ${ticket.ticket_mundiale}<br>
🛠 <strong>GLPI ID:</strong> ${ticket.glpi_ticket_id ?? 'não encontrado'}<br>
⏱ <strong>Fechado em:</strong> ${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 buildBodyEmailAdm(tickets) {
let body = `
<p>🚨⚽ <strong>Pênalti!</strong></p>
<p>Os seguintes chamados passaram da defesa inicial:</p>
<p>
Esses chamados foram <strong>fechados no Hubsoft há mais de 1 hora</strong>
e ainda <strong>não tiveram atualização no GLPI</strong>.
</p>
<br>
<ul>
`
tickets.forEach(ticket => {
body += `
<li>
🧾 <strong>Protocolo Hubsoft:</strong> ${ticket.protocolo_hub}<br>
🧠 <strong>Mundiale ID:</strong> ${ticket.ticket_mundiale}<br>
🛠 <strong>GLPI ID:</strong> ${ticket.glpi_ticket_id ?? 'não encontrado'}<br>
⏱ <strong>Fechado em:</strong> ${formatDate(ticket.closed_at)}
</li>
<br>
`
})
body += `
</ul>
<br>
<p>📢 Favor acionar o time responsável 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)
return d.toLocaleString('pt-BR', { timeZone: 'America/Sao_Paulo' })
}
function getRecipients(type) {
if (type === 'adm') {
return process.env.WATCHDOG_ADM_RECIPIENT_EMAILS?.split(',') || []
}
return process.env.WATCHDOG_RECIPIENT_EMAILS?.split(',') || []
}
function getCc(type) {
if (type === 'adm') {
return process.env.WATCHDOG_ADM_CC_EMAILS?.split(',') || []
}
return process.env.WATCHDOG_CC_EMAILS?.split(',') || []
}
module.exports = {
prepareNotificationPayload
}