This commit is contained in:
Rodrigo Bertoldo de Sousa Martins committed 2026-10-14 16:26:40 -03:00
1 parent c3e262309d
commit 01164f909b
1 file changed
+17 -1
+17 -1
View File
@@ -159,6 +159,16 @@ class SgpAPI {
return onu.olt_id !== undefined ? String(onu.olt_id) : ''; return onu.olt_id !== undefined ? String(onu.olt_id) : '';
} }
/**
* Debug: imprime no log a estrutura bruta do cliente retornada pelo SGP
* (senhas/tokens mascarados) para facilitar o mapeamento de campos.
*/
_logEstrutura(cliente) {
if (!cliente || process.env.SGP_DEBUG === 'false') return;
const mascarar = (k, v) => (/senha|password|token|secret/i.test(k) ? '***' : v);
console.log('🧪 [SGP] Estrutura bruta do cliente:\n' + JSON.stringify(cliente, mascarar, 2));
}
_contratoId(ct) { _contratoId(ct) {
const v = ct.contrato ?? ct.id ?? ct.contrato_id ?? ct.contratoId ?? ct.clientecontrato_id ?? ct.cliente_contrato_id; const v = ct.contrato ?? ct.id ?? ct.contrato_id ?? ct.contratoId ?? ct.clientecontrato_id ?? ct.cliente_contrato_id;
return v !== null && typeof v === 'object' ? (v.id ?? v.contrato) : v; return v !== null && typeof v === 'object' ? (v.id ?? v.contrato) : v;
@@ -205,13 +215,18 @@ class SgpAPI {
const out = []; const out = [];
(c.contratos || []).forEach(ct => { (c.contratos || []).forEach(ct => {
const idContrato = this._contratoId(ct); const primeiroSrv = (ct.servicos && ct.servicos[0]) || {};
const idContrato = this._contratoId(ct) ?? this._contratoId(primeiroSrv);
if (idContrato === undefined) { if (idContrato === undefined) {
console.warn('⚠️ [SGP] Contrato sem id reconhecido. Campos recebidos:', Object.keys(ct)); console.warn('⚠️ [SGP] Contrato sem id reconhecido. Campos recebidos:', Object.keys(ct));
} }
const idStatus = this._statusId(ct.status); const idStatus = this._statusId(ct.status);
(ct.servicos && ct.servicos.length ? ct.servicos : [{}]).forEach(srv => { (ct.servicos && ct.servicos.length ? ct.servicos : [{}]).forEach(srv => {
const onu = srv.onu || {}; const onu = srv.onu || {};
if (!(onu.serial || onu.phy_addr || onu.addr || onu.serial_onu || srv.serial)) {
console.warn('⚠️ [SGP] Serial da ONU não veio no cadastro. Campos do contrato:', Object.keys(ct),
'| do serviço:', Object.keys(srv), '| da onu:', Object.keys(onu));
}
const conexao = onu.conexao || srv.conexao || {}; const conexao = onu.conexao || srv.conexao || {};
const e = srv.endereco || end; const e = srv.endereco || end;
out.push({ out.push({
@@ -332,6 +347,7 @@ class SgpAPI {
const id = parseInt(idCliente, 10); const id = parseInt(idCliente, 10);
if (isNaN(id)) throw new Error(`ID do cliente inválido: ${idCliente}`); if (isNaN(id)) throw new Error(`ID do cliente inválido: ${idCliente}`);
const clientes = await this._listarClientes(credentials, { cliente_id: id }); const clientes = await this._listarClientes(credentials, { cliente_id: id });
this._logEstrutura(clientes[0]);
const dados = []; const dados = [];
clientes.forEach(c => dados.push(...this._mapServicos({ ...c, id: this._clienteId(c) ?? id }))); clientes.forEach(c => dados.push(...this._mapServicos({ ...c, id: this._clienteId(c) ?? id })));
await this._completarDadosOnu(credentials, dados); await this._completarDadosOnu(credentials, dados);