171 lines
3.8 KiB
JavaScript
171 lines
3.8 KiB
JavaScript
// src/infra/db/repositories/hubglpi/tickets.repository.js
|
|
|
|
const db = require('../../connections/hubglpi.pg.js');
|
|
const { logError } = require('../../../../shared/utils/logger.js');
|
|
|
|
|
|
/**
|
|
* Insere ou atualiza tickets vindos do HubSoft
|
|
* @param {Array<Object>} tickets
|
|
*/
|
|
async function insertTickets(tickets) {
|
|
if (!tickets?.length) return [];
|
|
|
|
const query = `
|
|
INSERT INTO hubsoft_tickets (
|
|
id_atendimento,
|
|
codigo_cliente,
|
|
status_atendimento,
|
|
codigo_servico,
|
|
servico_nome,
|
|
protocolo_hub,
|
|
ticket_mundiale,
|
|
cliente_nome,
|
|
|
|
cpf_cnpj,
|
|
telefone,
|
|
email,
|
|
tipo_pessoa,
|
|
nome_razaosocial,
|
|
endereco,
|
|
|
|
ticket_type,
|
|
descricao_abertura,
|
|
created_at,
|
|
vendedor
|
|
)
|
|
SELECT
|
|
t.id_atendimento,
|
|
t.codigo_cliente,
|
|
t.status_atendimento,
|
|
t.codigo_servico,
|
|
t.servico_nome,
|
|
t.protocolo_hub,
|
|
t.ticket_mundiale,
|
|
t.cliente_nome,
|
|
|
|
t.cpf_cnpj,
|
|
t.telefone,
|
|
t.email,
|
|
t.tipo_pessoa,
|
|
t.nome_razaosocial,
|
|
t.endereco,
|
|
|
|
t.ticket_type,
|
|
descricao_abertura,
|
|
t.created_at,
|
|
vendedor
|
|
FROM jsonb_to_recordset($1::jsonb) AS t(
|
|
id_atendimento BIGINT,
|
|
codigo_cliente INTEGER,
|
|
status_atendimento TEXT,
|
|
codigo_servico INTEGER,
|
|
servico_nome TEXT,
|
|
protocolo_hub TEXT,
|
|
ticket_mundiale INTEGER,
|
|
cliente_nome TEXT,
|
|
|
|
cpf_cnpj TEXT,
|
|
telefone TEXT,
|
|
email TEXT,
|
|
tipo_pessoa TEXT,
|
|
nome_razaosocial TEXT,
|
|
endereco TEXT,
|
|
|
|
ticket_type TEXT,
|
|
descricao_abertura TEXT,
|
|
created_at TIMESTAMP,
|
|
vendedor TEXT
|
|
)
|
|
ON CONFLICT (id_atendimento)
|
|
DO UPDATE SET
|
|
codigo_cliente = EXCLUDED.codigo_cliente,
|
|
status_atendimento = EXCLUDED.status_atendimento,
|
|
codigo_servico = EXCLUDED.codigo_servico,
|
|
servico_nome = EXCLUDED.servico_nome,
|
|
protocolo_hub = EXCLUDED.protocolo_hub,
|
|
ticket_mundiale = EXCLUDED.ticket_mundiale,
|
|
cliente_nome = EXCLUDED.cliente_nome,
|
|
|
|
cpf_cnpj = EXCLUDED.cpf_cnpj,
|
|
telefone = EXCLUDED.telefone,
|
|
email = EXCLUDED.email,
|
|
tipo_pessoa = EXCLUDED.tipo_pessoa,
|
|
nome_razaosocial = EXCLUDED.nome_razaosocial,
|
|
endereco = EXCLUDED.endereco,
|
|
|
|
ticket_type = EXCLUDED.ticket_type,
|
|
descricao_abertura = EXCLUDED.descricao_abertura,
|
|
vendedor = EXCLUDED.vendedor,
|
|
updated_at = NOW()
|
|
RETURNING id_atendimento;
|
|
`;
|
|
|
|
try {
|
|
const { rows } = await db.query(query, [JSON.stringify(tickets)]);
|
|
return rows.map(r => r.id_atendimento);
|
|
} catch (err) {
|
|
logError('Erro ao inserir tickets no hubglpi', err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
async function fetchPendingTickets() {
|
|
const query = `
|
|
SELECT
|
|
ht.*,
|
|
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 ht
|
|
LEFT JOIN sync_data 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 { rows } = await db.query(query);
|
|
return rows;
|
|
} catch (err) {
|
|
logError('Erro ao buscar tickets pendentes', err);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
async function updateCloseMessage(hubTicketId, message) {
|
|
const query = `
|
|
UPDATE hubsoft_tickets
|
|
SET
|
|
status_atendimento = 'Resolvido',
|
|
descricao_fechamento = $2,
|
|
data_fechamento = NOW(),
|
|
updated_at = NOW()
|
|
WHERE id_atendimento = $1
|
|
RETURNING *;
|
|
`
|
|
try {
|
|
const { rows } = await db.query(query, [hubTicketId, message])
|
|
return rows[0]
|
|
} catch (err) {
|
|
logError(`Erro ao atualizar fechamento do ticket ${hubTicketId}`, err)
|
|
throw err
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
insertTickets,
|
|
fetchPendingTickets,
|
|
updateCloseMessage
|
|
};
|
|
|