2026-01-02 18:19:11 -03:00
|
|
|
// src/modules/comments/useCases/syncGlpiCommentToHub.usecase.js
|
|
|
|
|
|
|
|
|
|
const repository = require('../repositories/comment.repository')
|
|
|
|
|
const { sanitizeGLPIComment } = require('../../../shared/utils/commentSanitizer')
|
2026-01-06 11:20:57 -03:00
|
|
|
const { logInfo, logError, logWarning } = require('../../../shared/utils/logger')
|
2026-01-02 18:19:11 -03:00
|
|
|
|
|
|
|
|
async function sync({ glpiTicketId, glpiMessageId, rawContent }) {
|
|
|
|
|
logInfo(`[COMMENTS][GLPI->HUB] Ticket ${glpiTicketId}`)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const syncRecord = await repository.getSyncByGlpiId(glpiTicketId)
|
|
|
|
|
if (!syncRecord?.hubsoft_ticket_id) {
|
2026-01-06 11:20:57 -03:00
|
|
|
logWarning('[COMMENTS][GLPI->HUB] Ticket sem vínculo com Hubsoft')
|
2026-01-02 18:19:11 -03:00
|
|
|
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
|
|
|
|
|
)
|
|
|
|
|
|
2026-01-06 11:20:57 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-02 18:19:11 -03:00
|
|
|
await repository.insertSyncComment({
|
2026-01-06 11:20:57 -03:00
|
|
|
syncDataId: syncRecord.id,
|
|
|
|
|
sourceSystem: 'glpi',
|
2026-01-02 18:19:11 -03:00
|
|
|
sourceCommentId: glpiMessageId,
|
|
|
|
|
destinationCommentId: hubsoftMessageId,
|
|
|
|
|
hubsoftTicketId: syncRecord.hubsoft_ticket_id,
|
|
|
|
|
glpiTicketId,
|
|
|
|
|
content,
|
2026-01-06 11:20:57 -03:00
|
|
|
author: 'glpi-user',
|
2026-01-02 18:19:11 -03:00
|
|
|
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 }
|