// src/modules/tickets/respository.js const hubglpiTicketsRepo = require('../../../infra/db/repositories/hubglpi/tickets.repository.js') const hubsoftTicketsRepo = require('../../../infra/db/repositories/hubsoft/tickets.repository.js') const hubglpiSyncRepo = require('../../../infra/db/repositories/hubglpi/sync.repository.js') const hubglpiCommentsRepo = require('../../../infra/db/repositories/hubglpi/comments.repository.js') const glpiTicketsRepo = require('../../../infra/db/repositories/glpi/tickets.repository.js') const glpiEntitiesRepo = require('../../../infra/db/repositories/glpi/entities.repository.js') const glpiGroupsRepo = require('../../../infra/db/repositories/glpi/groups.repository.js') const hubsoftApiClient = require('../../../infra/api/hubsoft.client.js') const watermarkRepository = require('../../../infra/db/repositories/hubglpi/watermark.repository.js'); const JOB_NAME = 'hubsoft_tickets_sync'; async function getWaterMark() { return watermarkRepository.getJobWatermark(JOB_NAME); } async function updateWaterMark(newWatermark) { return watermarkRepository.updateJobWatermark(JOB_NAME, newWatermark); } const TYPES = Object.freeze({ MUNDIALE: 4, IMPLANTACAO: 21, CANCELAMENTO: 27, SAC: 41, TITULARIDADE: 60 }); async function getMundialeTickets(watermark) { return hubsoftTicketsRepo.getTicketsByTipo({ tipoAtendimento: TYPES.MUNDIALE, usuarioAbertura: 248, watermark }); } async function getImplantacaoTickets(watermark) { return hubsoftTicketsRepo.getTicketsByTipo({ tipoAtendimento: TYPES.IMPLANTACAO, watermark }); } async function getCancelamentoTickets(watermark) { return hubsoftTicketsRepo.getTicketsByTipo({ tipoAtendimento: TYPES.CANCELAMENTO, watermark }); } async function getSacTickets(watermark) { return hubsoftTicketsRepo.getTicketsByTipo({ tipoAtendimento: TYPES.SAC, watermark }); } async function getTrocaTTickets(watermark) { return hubsoftTicketsRepo.getTicketsByTipo({ tipoAtendimento: TYPES.TITULARIDADE, watermark }); } async function insertTicketsHubGlpi(tickets) { return hubglpiTicketsRepo.insertTickets(tickets) } async function insertSyncDataByIds(ids) { return hubglpiSyncRepo.insertSyncData(ids) } async function fetchPendingTickets() { return hubglpiTicketsRepo.fetchPendingTickets() } async function insertTicketGlpi(ticket) { return glpiTicketsRepo.insertTicket(ticket) } async function getEntitiesByService(codigoCliente, codigoServico) { return glpiEntitiesRepo.getEntitiesByService(codigoCliente, codigoServico) } async function getEntitiesByClient(codigoCliente) { return glpiEntitiesRepo.getEntitiesByClient(codigoCliente) } const GROUP_BY_TYPE = { IMPLANTACAO: 'IMPLANTACAO', TITULARIDADE: 'IMPLANTACAO', CANCELAMENTO: 'IMPLANTACAO', MUNDIALE: 'NOC', SAC: 'NOC' } async function insertGroupTicket(id, type) { const group = GROUP_BY_TYPE[type] || 'NOC' if (group === 'IMPLANTACAO') { return glpiGroupsRepo.insertGroupImplantacao(id) } return glpiGroupsRepo.insertGroupNOC(id) } async function updateSyncDataCreated(hubId, glpiId) { return hubglpiSyncRepo.updateSyncDataCreated(hubId, glpiId) } async function sendHubsoftMessage(hubId, message) { return hubsoftApiClient.sendHubsoftMessage(hubId, message) } async function sendHubglpiMessage(hubId, message) { const syncDataId = await hubglpiSyncRepo.getSyncIdByHubsoftId(hubId); if (!syncDataId) { throw new Error(`sync_data não encontrado para ticket ${hubId}`); } return hubglpiCommentsRepo.insertSyncComment({ syncDataId: syncDataId.id, content: message, author: 'hubsoft-sync' }); } module.exports = { // watermark getWaterMark, updateWaterMark, // hubsoft reads getMundialeTickets, getImplantacaoTickets, getCancelamentoTickets, getSacTickets, getTrocaTTickets, // hubglpi insertTicketsHubGlpi, insertSyncDataByIds, fetchPendingTickets, updateSyncDataCreated, sendHubglpiMessage, // glpi insertTicketGlpi, getEntitiesByService, getEntitiesByClient, insertGroupTicket, // hubsoft api sendHubsoftMessage, }