REFACTOR: IF das entidades refatorado.
This commit is contained in:
parent
c41c099e92
commit
9bc9dfb06f
@ -219,16 +219,19 @@ const processAtendimento = async (ticketData) => {
|
|||||||
const titulo = `Mundiale - Protocolo: ${ticketData.ticket_mundiale} - ${ticketData.cliente_nome}`;
|
const titulo = `Mundiale - Protocolo: ${ticketData.ticket_mundiale} - ${ticketData.cliente_nome}`;
|
||||||
ticketData.titulo = titulo;
|
ticketData.titulo = titulo;
|
||||||
|
|
||||||
const selectedEntityCodServico = await glpiModel.selectEntityId(ticketData.codigo_servico);
|
// Busca a entidade primeiro pelo código do serviço
|
||||||
// Prioriza a entidade do serviço; se não existir tenta pelo cliente; fallback para 0
|
// Se não encontrar, busca pelo código do cliente
|
||||||
if (selectedEntityCodServico.name.include(ticketData.codigo_cliente)) {
|
// Se ainda assim não encontrar, atribui 0 (contratos ativos)
|
||||||
ticketData.entidades_id = selectedEntityCodServico.id;
|
const selectedEntityCodServico = await glpiModel.selectEntityIdCodServico(ticketData.codigo_cliente, ticketData.codigo_servico);
|
||||||
logInfo(`Entidade encontrada por serviço: ${selectedEntityCodServico.name}`);
|
|
||||||
|
if (selectedEntityCodServico) {
|
||||||
|
ticketData.entidades_id = selectedEntityCodServico;
|
||||||
|
logInfo(`Entidade encontrada por serviço: ${selectedEntityCodServico}`);
|
||||||
} else {
|
} else {
|
||||||
const selectedEntityCodCliente = await glpiModel.selectEntityId(ticketData.codigo_cliente);
|
const selectedEntityCodCliente = await glpiModel.selectEntityIdCodCliente(ticketData.codigo_cliente);
|
||||||
if (selectedEntityCodCliente) {
|
if (selectedEntityCodCliente) {
|
||||||
ticketData.entidades_id = selectedEntityCodCliente.id;
|
ticketData.entidades_id = selectedEntityCodCliente;
|
||||||
logInfo(`Entidade encontrada por cliente: ${selectedEntityCodCliente.name}`);
|
logInfo(`Entidade encontrada por cliente: ${selectedEntityCodCliente}`);
|
||||||
} else {
|
} else {
|
||||||
ticketData.entidades_id = 0;
|
ticketData.entidades_id = 0;
|
||||||
logInfo(`Nenhuma entidade encontrada para serviço="${ticketData.codigo_servico}" ou cliente="${ticketData.codigo_cliente}", atribuindo contratos ativos`);
|
logInfo(`Nenhuma entidade encontrada para serviço="${ticketData.codigo_servico}" ou cliente="${ticketData.codigo_cliente}", atribuindo contratos ativos`);
|
||||||
|
|||||||
@ -64,24 +64,46 @@ class GlpiModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async selectEntityId(id) {
|
static async selectEntityIdCodCliente(id) {
|
||||||
|
|
||||||
const query = `SELECT id, name FROM glpi_entities WHERE name LIKE ? OR name LIKE ? LIMIT 1;`;
|
const query = `SELECT id FROM glpi_entities WHERE name LIKE ? OR name LIKE ? LIMIT 1;`;
|
||||||
const values = [`${id} -%`, `${id}-%`];
|
const values = [`${id} -%`, `${id}-%`];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const [rows] = await pool.execute(query, values);
|
const [rows] = await pool.execute(query, values);
|
||||||
if (!rows || rows.length === 0) {
|
if (!rows || rows.length === 0) {
|
||||||
logInfo(`Entidade não encontrada para: ${id} `);
|
logInfo(`Entidade não encontrada por código de cliente para: ${id} `);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
logInfo(`Entidade encontrada para: ${id}`);
|
logInfo(`Entidade encontrada por código de cliente para: ${id}`);
|
||||||
|
|
||||||
|
|
||||||
return { id: Number(rows[0].id), name: rows[0].name };
|
return Number(rows[0].id);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logError(`Erro ao buscar entidade: ${err}`);
|
logError(`Erro ao buscar entidade por código de cliente: ${err}`);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async selectEntityIdCodServico(idCliente, idServico) {
|
||||||
|
|
||||||
|
const query = `SELECT id FROM glpi_entities WHERE name LIKE ? LIMIT 1;`;
|
||||||
|
const values = [`${idCliente} - ${idServico} %`];
|
||||||
|
|
||||||
|
try {
|
||||||
|
const [rows] = await pool.execute(query, values);
|
||||||
|
if (!rows || rows.length === 0) {
|
||||||
|
logInfo(`Entidade não encontrada por código de serviço para: ${idServico} `);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
logInfo(`Entidade encontrada por código de serviço para: ${idServico}`);
|
||||||
|
|
||||||
|
|
||||||
|
return Number(rows[0].id);
|
||||||
|
} catch (err) {
|
||||||
|
logError(`Erro ao buscar entidade por código de serviço: ${err}`);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user