2026-03-04 16:37:04 -03:00
|
|
|
const { fetchCommentsFromServiceNow, fetchTicketLiveStatusFromServiceNow } = require('../services/servicenowService');
|
2025-10-17 16:58:14 -03:00
|
|
|
const { syncCommentsGlpitoSN, syncCommentsSNtoGlpi } = require('../services/glpiCommentService');
|
|
|
|
|
const TicketSyncModel = require('../models/ticketSyncModel');
|
2026-03-04 16:37:04 -03:00
|
|
|
const TicketSnModel = require('../models/ticketSnModel');
|
|
|
|
|
const TicketGlpiModel = require('../models/ticketGlpiModel');
|
2025-11-19 16:01:37 -03:00
|
|
|
const { logInfo } = require('../utils/logger');
|
2025-10-13 06:06:52 -03:00
|
|
|
|
2026-03-04 16:37:04 -03:00
|
|
|
const ensureReopenFromSNWhenGlpiSolved = async (ticket) => {
|
2026-03-04 18:15:53 -03:00
|
|
|
const glpiStatus = await TicketGlpiModel.getTicketStatus(ticket.glpi_ticket_id);
|
|
|
|
|
const glpiIsSolved = glpiStatus === 5;
|
|
|
|
|
if (!glpiIsSolved) {
|
|
|
|
|
return { reopened: false, glpiSolved: false };
|
2026-03-04 16:37:04 -03:00
|
|
|
}
|
|
|
|
|
|
2026-03-09 16:38:06 -03:00
|
|
|
// CORREÇÃO: Verificar se o ticket foi fechado recentemente.
|
|
|
|
|
// Se o status já é 'solved', significa que o processGlpiClosureController
|
|
|
|
|
// tentou fechar recentemente. O SN pode ainda estar processando o close,
|
|
|
|
|
// então ignoramos a detecção de reopen para evitar falsos positivos.
|
|
|
|
|
if (ticket.glpi_sync_status === 'solved' || ticket.sn_sync_status === 'solved') {
|
|
|
|
|
logInfo(`Ticket GLPI ${ticket.glpi_ticket_id} fechou recentemente (status: ${ticket.glpi_sync_status}/${ticket.sn_sync_status}). Ignorando verificacao de reopen.`);
|
|
|
|
|
return { reopened: false, glpiSolved: true };
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 16:37:04 -03:00
|
|
|
const ticketSn = await TicketSnModel.findById(ticket.sn_ticket_id);
|
|
|
|
|
if (!ticketSn) {
|
2026-03-04 18:15:53 -03:00
|
|
|
return { reopened: false, glpiSolved: true };
|
2026-03-04 16:37:04 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const liveSnStatus = await fetchTicketLiveStatusFromServiceNow(ticketSn.sys_id, ticketSn.tipo);
|
|
|
|
|
const snIsFinal = liveSnStatus === 'Resolvido' || liveSnStatus === 'Encerrado' || liveSnStatus === 'Encerrado - Omitido';
|
|
|
|
|
if (snIsFinal) {
|
2026-03-04 18:15:53 -03:00
|
|
|
return { reopened: false, glpiSolved: true };
|
2026-03-04 16:37:04 -03:00
|
|
|
}
|
|
|
|
|
|
2026-03-04 18:15:53 -03:00
|
|
|
await TicketGlpiModel.updateStatus(ticket.glpi_ticket_id, 'Em Atendimento');
|
|
|
|
|
await TicketSyncModel.updateStatus(ticket.sn_ticket_id, 'synced', 'synced');
|
|
|
|
|
logInfo(`Reabertura detectada no SN para ticket ${ticket.sn_ticket_id}. GLPI ${ticket.glpi_ticket_id} retornou para Em Atendimento.`);
|
|
|
|
|
return { reopened: true, glpiSolved: true };
|
2026-03-04 16:37:04 -03:00
|
|
|
};
|
2025-10-13 06:06:52 -03:00
|
|
|
|
|
|
|
|
const processCommentsController = async () => {
|
2025-10-23 17:30:03 -03:00
|
|
|
const ticketsToMonitor = await TicketSyncModel.getTicketsToMonitor();
|
2026-03-04 16:37:04 -03:00
|
|
|
logInfo(`Tickets em monitoramento: ${ticketsToMonitor.length}`);
|
2025-11-19 16:01:37 -03:00
|
|
|
|
2026-03-04 16:37:04 -03:00
|
|
|
for (const ticket of ticketsToMonitor) {
|
2026-03-04 18:15:53 -03:00
|
|
|
const reopenState = await ensureReopenFromSNWhenGlpiSolved(ticket);
|
|
|
|
|
const reopenedThisCycle = reopenState.reopened;
|
|
|
|
|
const glpiWasSolved = reopenState.glpiSolved;
|
|
|
|
|
if (glpiWasSolved && !reopenedThisCycle) {
|
|
|
|
|
logInfo(`Ticket ${ticket.glpi_ticket_id} permanece Solucionado no GLPI. Comentarios SN -> GLPI bloqueados ate reabertura.`);
|
|
|
|
|
await syncCommentsGlpitoSN(ticket);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-10-17 16:58:14 -03:00
|
|
|
|
|
|
|
|
const comments = await fetchCommentsFromServiceNow(ticket);
|
2026-03-04 16:37:04 -03:00
|
|
|
if (comments.length !== 0) {
|
2026-03-04 18:15:53 -03:00
|
|
|
await syncCommentsSNtoGlpi(comments, ticket, { highlightReopenContext: reopenedThisCycle });
|
2026-03-04 16:37:04 -03:00
|
|
|
}
|
|
|
|
|
|
2025-10-17 16:58:14 -03:00
|
|
|
await syncCommentsGlpitoSN(ticket);
|
2025-10-29 14:20:21 -03:00
|
|
|
}
|
2026-03-04 16:37:04 -03:00
|
|
|
};
|
2025-10-29 14:20:21 -03:00
|
|
|
|
2025-10-13 06:06:52 -03:00
|
|
|
module.exports = {
|
|
|
|
|
processCommentsController
|
2026-03-04 18:15:53 -03:00
|
|
|
};
|