hubxglpi/src/modules/comments/useCases/syncGlpiCommentToHub.usecase.js

51 lines
1.5 KiB
JavaScript
Raw Normal View History

// src/modules/comments/useCases/syncGlpiCommentToHub.usecase.js
const repository = require('../repositories/comment.repository')
const { sanitizeGLPIComment } = require('../../../shared/utils/commentSanitizer')
const { logInfo, logError, logWarn } = 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) {
logWarn('[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({
source: 'glpi',
sourceCommentId: glpiMessageId,
destinationCommentId: hubsoftMessageId,
hubsoftTicketId: syncRecord.hubsoft_ticket_id,
glpiTicketId,
content,
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 }