2026-01-12 16:42:08 -03:00
|
|
|
// src/modules/watchdog/job/job.js
|
|
|
|
|
|
2026-01-14 17:51:49 -03:00
|
|
|
require('dotenv').config({ path: '.env.development' })
|
|
|
|
|
|
|
|
|
|
const { logError, logInfo } = require('../../../shared/utils/logger')
|
2026-01-12 16:42:08 -03:00
|
|
|
const repository = require('../repository/watchdog.repository.js')
|
|
|
|
|
const model = require('../model/email.model.js')
|
|
|
|
|
|
|
|
|
|
async function runWatchdog() {
|
|
|
|
|
|
2026-01-14 17:51:49 -03:00
|
|
|
logInfo('[WATCHDOG] [JOB] Coletando chamados fechados há 31 minutos')
|
2026-01-12 16:42:08 -03:00
|
|
|
|
|
|
|
|
const thresholdDate = new Date(Date.now() - 31 * 60 * 1000)
|
|
|
|
|
|
|
|
|
|
const closedTickets = await repository.getClosedTicketsSince(thresholdDate)
|
2026-01-14 17:51:49 -03:00
|
|
|
logInfo(`[WATCHDOG] [JOB] Encontrados ${closedTickets.length} chamados fechados`)
|
2026-01-12 16:42:08 -03:00
|
|
|
|
|
|
|
|
for (const ticket of closedTickets) {
|
|
|
|
|
|
|
|
|
|
const hubGlpiTicket = await repository.checkTicketInHubGlpi(ticket.id_atendimento)
|
|
|
|
|
|
|
|
|
|
if (!hubGlpiTicket.exists) {
|
2026-01-14 17:51:49 -03:00
|
|
|
logInfo(`[WATCHDOG] [JOB] Chamado ${ticket.id_atendimento} não encontrado no HubGlpi. Ignorando.`)
|
2026-01-12 16:42:08 -03:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hubGlpiTicket.status === 'closed') {
|
2026-01-14 17:51:49 -03:00
|
|
|
logInfo(`[WATCHDOG] [JOB] Chamado ${ticket.id_atendimento} já está fechado no HubGlpi. Ignorando.`)
|
2026-01-12 16:42:08 -03:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if (await repository.notificationAlreadySent(ticket.id_atendimento)) {
|
2026-01-14 17:51:49 -03:00
|
|
|
logInfo(`[WATCHDOG] [JOB] Notificação já enviada para o chamado ${ticket.id_atendimento}.`)
|
2026-01-12 16:42:08 -03:00
|
|
|
continue
|
|
|
|
|
}
|
2026-01-14 17:51:49 -03:00
|
|
|
await repository.markNotificationsAsPending([ticket.id_atendimento])
|
2026-01-12 16:42:08 -03:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
const ticketsToNotify = await repository.getPendingTicketsForNotification()
|
2026-01-14 17:51:49 -03:00
|
|
|
logInfo(`[WATCHDOG] [JOB] ${ticketsToNotify.length} chamados pendentes para notificação.`)
|
2026-01-12 16:42:08 -03:00
|
|
|
|
2026-01-14 17:51:49 -03:00
|
|
|
if (!ticketsToNotify.length) {
|
|
|
|
|
logInfo('[WATCHDOG] [JOB] Nenhum chamado pendente para notificação')
|
2026-01-12 16:42:08 -03:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-14 17:51:49 -03:00
|
|
|
const payload = await model.prepareNotificationPayload(ticketsToNotify)
|
|
|
|
|
const hubsoftTicketIds = ticketsToNotify.map(t => Number(t.hubsoft_ticket_id)).filter(Number.isFinite);
|
|
|
|
|
|
2026-01-12 16:42:08 -03:00
|
|
|
try {
|
|
|
|
|
await repository.sendClosureNotifications(payload)
|
2026-01-14 17:51:49 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
await repository.markNotificationsAsSent(hubsoftTicketIds)
|
2026-01-12 16:42:08 -03:00
|
|
|
} catch (err) {
|
2026-01-14 17:51:49 -03:00
|
|
|
logError('[WATCHDOG] Erro ao enviar notificações', err)
|
|
|
|
|
await repository.markNotificationsAsFailed(hubsoftTicketIds)
|
2026-01-12 16:42:08 -03:00
|
|
|
}
|
|
|
|
|
|
2026-01-14 17:51:49 -03:00
|
|
|
logInfo(`[WATCHDOG] [JOB] Enviadas ${ticketsToNotify.length} notificações`)
|
2026-01-12 16:42:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-01-14 17:51:49 -03:00
|
|
|
runWatchdog().catch((error) => {
|
|
|
|
|
logError('[WATCHDOG] [JOB] Erro ao executar o job do Watchdog', error)
|
|
|
|
|
})
|
|
|
|
|
|
2026-01-12 16:42:08 -03:00
|
|
|
module.exports = { runWatchdog}
|