// src/models/hubsoft_ticketsModel.js const { log } = require('winston'); const dbConfig = require('../config/dbConfig.js'); const { logError, logInfo} = require('../utils/logger'); const { Pool } = require('pg'); const pool = new Pool({ host: dbConfig.hubglpi.databaseHost, port: dbConfig.hubglpi.databasePort, database: dbConfig.hubglpi.databaseName, user: dbConfig.hubglpi.databaseUser, password: dbConfig.hubglpi.databasePassword }); class HubglpiModel { static async insertTicket(ticketData) { const query = ` INSERT INTO hubsoft_tickets ( id_atendimento, codigo_cliente, status_atendimento, codigo_servico, servico_nome, protocolo_hub, ticket_mundiale, cliente_nome, created_at ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) ON CONFLICT (id_atendimento) DO UPDATE SET codigo_cliente = $2, status_atendimento = $3, codigo_servico = $4, servico_nome = $5, protocolo_hub = $6, ticket_mundiale = $7, cliente_nome = $8, created_at = $9 RETURNING *; `; const values = [ ticketData.id_atendimento, ticketData.codigo_cliente, ticketData.status_atendimento, ticketData.codigo_servico, ticketData.servico_nome, ticketData.protocolo_hub, ticketData.ticket_mundiale, ticketData.cliente_nome, ticketData.data_cadastro ]; try { const res = await pool.query(query, values); return res.rows[0]; } catch (err) { logError(`Erro ao inserir/atualizar ticket na tabela hubsoft_tickets: ${err}`); throw err; } } static async insertSyncData(syncData) { const query = ` INSERT INTO sync_data ( hubsoft_ticket_id ) VALUES ($1) ON CONFLICT (hubsoft_ticket_id) DO UPDATE SET hubsoft_ticket_id = $1 RETURNING *; `; const values = [ syncData ]; try { const res = await pool.query(query, values); return res.rows[0]; } catch (err) { logError(`Erro ao inserir/atualizar dados na tabela sync_data: ${err}`); throw err; } } static async update_syncData(sync_update) { const query = ` UPDATE sync_data set glpi_ticket_id = $1, status_sync = $2, sync_metadata = $3, last_sync_attempt = $4, sync_error_message = $5, created_at = $6, updated_at = $7 WHERE id = $8 RETURNING *; `; const values = [ sync_update.glpi_ticket_id, sync_update.status_sync, sync_update.sync_metadata, new Date(), sync_update.sync_error_message, sync_update.created_at, sync_update.updated_at, sync_update.sync_data_id ]; //Todo colocar parametros dinamicos || null se não tiver try { const res = await pool.query(query, values); return res.rows[0]; } catch (err) { logError(`Erro ao atualizar dados na tabela sync_data: ${err}`); throw err; } } static async get_idSyncByHubsoftId(hubsoft_ticket_id) { const query = ` SELECT id FROM sync_data WHERE hubsoft_ticket_id = $1; `; const values = [hubsoft_ticket_id]; try { const res = await pool.query(query, values); logInfo('ID de sync_data obtido com sucesso:', res.rows[0]); return res.rows[0] ? res.rows[0].id : null; } catch (err) { logError('Erro ao obter ID de sync_data', err); throw err; } } static async get_idSyncByGlpiID(glpi_ticket_id) { const query = ` SELECT id FROM sync_data WHERE glpi_ticket_id = $1; `; const values = [glpi_ticket_id]; try { const res = await pool.query(query, values); logInfo('ID de sync_data obtido com sucesso:', res.rows[0]); return res.rows[0] ? res.rows[0].id : null; } catch (err) { logError('Erro ao obter ID de sync_data', err); throw err; } } static async getTicketDataPending() { const query = ` SELECT ht.id_atendimento, ht.codigo_cliente, ht.status_atendimento, ht.codigo_servico, ht.servico_nome, ht.protocolo_hub, ht.ticket_mundiale, ht.cliente_nome, ht.created_at, sd.id AS sync_data_id, sd.glpi_ticket_id, sd.status_sync, sd.sync_metadata, sd.last_sync_attempt, sd.sync_error_message, sd.created_at AS sync_created_at, sd.updated_at AS sync_updated_at FROM hubsoft_tickets AS ht LEFT JOIN sync_data AS sd ON ht.id_atendimento = sd.hubsoft_ticket_id WHERE sd.status_sync IS NULL OR sd.status_sync = 'pending_create' ORDER BY ht.created_at ASC; `; try { const res = await pool.query(query); return res.rows; } catch (err) { logError(`Erro ao obter tickets pendentes ${err}`); throw err; } } static async getGlpiTicketIdByAtendimentoId(id_atendimento) { const query = ` SELECT glpi_ticket_id FROM sync_data WHERE hubsoft_ticket_id = $1; `; const values = [id_atendimento]; try { const res = await pool.query(query, values); return res.rows[0] ? res.rows[0].glpi_ticket_id : null; } catch (err) { logError(`Erro ao obter glpi_ticket_id por id_atendimento, ${err}`); throw err; } } static async update_syncaDataError(sync_error_message, id_atendimento) { const query = ` UPDATE sync_data set sync_error_message = $1, status_sync = 'sync_error', last_sync_attempt = $2 WHERE hubsoft_ticket_id = $3 RETURNING *; `; const values = [ sync_error_message, new Date(), id_atendimento ]; try { const res = await pool.query(query, values); return res.rows[0]; } catch (err){ logError(`Erro ao inserir mensagem de erro na tabela sync_data: ${err}`); throw err; } } } module.exports = HubglpiModel;