const { fetchCommentsFromServiceNow } = require('../services/servicenowService'); const { syncCommentsGlpitoSN, syncCommentsSNtoGlpi } = require('../services/glpiCommentService'); const TicketSyncModel = require('../models/ticketSyncModel'); const TicketGlpiModel = require('../models/ticketGlpiModel'); const { logInfo } = require('../utils/logger'); const processCommentsController = async () => { const ticketsToMonitor = await TicketSyncModel.getTicketsToMonitor(); logInfo(`Tickets em monitoramento: ${ticketsToMonitor.length}`); for (const ticket of ticketsToMonitor) { const glpiStatus = await TicketGlpiModel.getTicketStatus(ticket.glpi_ticket_id); if (glpiStatus === 5) { // Regra de negocio: enquanto o GLPI estiver Solucionado, nao manda comentario novo // do SN pra dentro do chamado. A decisao de reabertura acontece em // processTicketLifecycleController.js, que roda antes deste controlador no ciclo. logInfo(`Ticket ${ticket.glpi_ticket_id} permanece Solucionado no GLPI. Comentarios SN -> GLPI bloqueados ate reabertura.`); await syncCommentsGlpitoSN(ticket); continue; } const comments = await fetchCommentsFromServiceNow(ticket); if (comments.length !== 0) { await syncCommentsSNtoGlpi(comments, ticket); } await syncCommentsGlpitoSN(ticket); } }; module.exports = { processCommentsController };