// src/shared/repositories/glpi.repository.js const { glpi } = require("../../shared/infra/database"); const { logInfo, logError } = require('../../utils/logger.js'); async function insertTicket(ticket) { const sql = ` INSERT INTO glpi_tickets (entities_id, name, date, date_mod, status, users_id_recipient, content, urgency, impact, priority, type, itilcategories_id, date_creation, slas_id_ttr) VALUES (?, ?, ?, NOW(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `; const values = [ ticket.entities_id, ticket.name, ticket.date, ticket.status, ticket.users_id_recipient, ticket.content, ticket.urgency, ticket.impact, ticket.priority, ticket.type, ticket.itilcategories_id, ticket.date_creation, ticket.slas_id_ttr ]; const [result] = await glpi.execute(sql, values); return result.insertId; } async function getEntitiesByService(codigoCliente, codigoServico) { try { const sql = `SELECT id FROM glpi_entities WHERE name LIKE ? or name LIKE ? or name LIKE ? LIMIT 1;`; const values = [`${codigoCliente}-${codigoServico}-%`, `${codigoCliente} -${codigoServico}-%`, `${codigoCliente} - ${codigoServico} -%`]; const [rows] = await glpi.execute(sql, values); if (rows.length > 0) { return rows[0].id; } return null; } catch (error) { logError(`Erro ao buscar entidade por código de serviço: ${error}`); throw error; } } async function getEntitiesByClient(codigoCliente) { try { const sql = `SELECT id FROM glpi_entities WHERE name LIKE ? or name LIKE ? LIMIT 1;`; const values = [`${codigoCliente}-%`, `${codigoCliente} -%`]; const [rows] = await glpi.execute(sql, values); if (rows.length > 0) { return rows[0].id; } return null; } catch (error) { logError(`Erro ao buscar entidade por código de cliente: ${error}`); throw error; } } async function insertEntity(entity_name){ } async function insertGroupTicket(ticketId, type) { try { const groupId = 25 if (type !== 'IMPLANTACAO') { groupId = 36; // Implantacao } const sql = ` INSERT INTO glpi_groups_tickets (tickets_id, groups_id, type) VALUES (?, ?, 2) `; const values = [ticketId, groupId]; await glpi.execute(sql, values); logInfo(`Grupo associado ao ticket GLPI ID: ${ticketId}`); } catch (error) { logError(`Erro ao associar grupo ao ticket GLPI ID ${ticketId}: ${error}`); throw error; } } module.exports = { insertTicket, getEntitiesByService, getEntitiesByClient, insertGroupTicket };