// src/modules/comments/useCases/syncGlpiCommentToHub.usecase.js const repository = require('../repositories/comment.repository') const { sanitizeGLPIComment } = require('../../../shared/utils/commentSanitizer') const { logInfo, logError, logWarning } = require('../../../shared/utils/logger') async function sync({ glpiTicketId, glpiMessageId, rawContent }) { logInfo(`[COMMENTS][GLPI->HUB] Ticket ${glpiTicketId}`) try { const syncRecord = await repository.getSyncByGlpiId(glpiTicketId) if (!syncRecord?.hubsoft_ticket_id) { logWarning('[COMMENTS][GLPI->HUB] Ticket sem vínculo com Hubsoft') return } const exists = await repository.commentExists({ source: 'glpi', sourceCommentId: glpiMessageId }) if (exists) { logInfo('[COMMENTS][GLPI->HUB] Comentário duplicado ignorado') return } const content = sanitizeGLPIComment({ content: rawContent }) const hubsoftMessageId = await repository.addHubsoftComment( syncRecord.hubsoft_ticket_id, content ) await repository.insertSyncComment({ syncDataId: syncRecord.id, sourceSystem: 'glpi', sourceCommentId: glpiMessageId, destinationCommentId: hubsoftMessageId, hubsoftTicketId: syncRecord.hubsoft_ticket_id, glpiTicketId, content, author: 'glpi-user', status: 'synced' }) logInfo('[COMMENTS][GLPI->HUB] Comentário sincronizado com sucesso') } catch (err) { logError('[COMMENTS][GLPI->HUB] Erro ao sincronizar comentário', err) } } module.exports = { sync }