111 lines
5.6 KiB
JavaScript
111 lines
5.6 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import https from 'https';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
/**
|
|
* AUTO-SYNC AGENT FOR ROUTE DOCUMENTATION
|
|
*/
|
|
|
|
const CONTRATOS_DIR = path.join(__dirname, '../src/features/dev-tools/data/contratos');
|
|
|
|
const MONITORED_ROUTES = [
|
|
{ env: 'prafrot', path: '/cadastro_frota/apresentar', method: 'GET', resource: 'cadastro_frota' },
|
|
{ env: 'prafrot', path: '/motoristas/listagem', method: 'GET', resource: 'motoristas' },
|
|
{ env: 'prafrot', path: '/dispatcher/apresentar', method: 'GET', resource: 'dispatcher' },
|
|
{ env: 'prafrot', path: '/oficinas_frota/apresentar', method: 'GET', resource: 'oficinas_frota' },
|
|
{ env: 'prafrot', path: '/monitoramento_frota/apresentar', method: 'GET', resource: 'monitoramento_frota' },
|
|
{ env: 'prafrot', path: '/status_frota/apresentar', method: 'GET', resource: 'status_frota' },
|
|
{ env: 'prafrot', path: '/manutencao_frota/apresentar', method: 'GET', resource: 'manutencao_frota' },
|
|
{ env: 'prafrot', path: '/sinistro_devolucao_venda_frota/apresentar', method: 'GET', resource: 'sinistro_devolucao_venda_frota' },
|
|
{ env: 'workspace', path: '/extrato/apresentar', method: 'GET', resource: 'extrato' },
|
|
{ env: 'workspace', path: '/categorias/apresentar', method: 'GET', resource: 'categorias' },
|
|
{ env: 'workspace', path: '/caixinhas/apresentar', method: 'GET', resource: 'caixinhas' },
|
|
{ env: 'workspace', path: '/contas_a_pagar/apresentar', method: 'GET', resource: 'contas_a_pagar' },
|
|
{ env: 'rh', path: '/colaboradores/apresentar', method: 'GET', resource: 'colaboradores' },
|
|
{ env: 'financeiro-v2', path: '/boletos/apresentar', method: 'GET', resource: 'boletos' },
|
|
{ env: 'gr', path: '/gr/contratos', method: 'GET', resource: 'contratos' },
|
|
];
|
|
|
|
function inferType(value) {
|
|
if (value === null) return 'object';
|
|
if (Array.isArray(value)) return 'array';
|
|
return typeof value;
|
|
}
|
|
|
|
function inferDescription(key, value) {
|
|
if (key.includes('data')) return 'Data';
|
|
if (typeof value === 'boolean' || (typeof value === 'string' && ['SIM', 'NÃO'].includes(value))) return "'SIM' ou 'NÃO' / Booleano";
|
|
if (value === null) return 'Pode ser null';
|
|
return '';
|
|
}
|
|
|
|
const MOCK_API_RESPONSES = {
|
|
'/cadastro_frota/apresentar': { "ano_fabricacao": "2", "idveiculo_frota": 3638, "placa": "ABC1234", "geotab": "SIM" },
|
|
'/motoristas/listagem': { "NOME_FAVORECIDO": "ACAUAN DUQUE", "idfavorecido": 1 },
|
|
'/dispatcher/apresentar': { "nome": "Rafael", "email": "rafael@pralog.com.br" },
|
|
'/oficinas_frota/apresentar': { "razao_social": "REPECOL LTDA", "idoficinas_frota": 2 },
|
|
'/monitoramento_frota/apresentar': { "placa": "XYZ5678", "idmonitoramento_frota": 738 },
|
|
'/status_frota/apresentar': { "status_frota": "Em Trânsito", "idstatus_frota": 2412 },
|
|
'/manutencao_frota/apresentar': { "oficina": "D CARROS CENTER", "idmanutencao_frota": 13176 },
|
|
'/sinistro_devolucao_venda_frota/apresentar': { "status": "Desmobilizado", "idsinistro_devolucao_frota": 871 },
|
|
'/extrato/apresentar': { "idextrato": 101, "descricao": "Venda Loja", "valor": 1500.50 },
|
|
'/categorias/apresentar': { "id": 1, "nome": "Vendas" },
|
|
'/caixinhas/apresentar': { "id": 1, "nome": "Principal" },
|
|
'/contas_a_pagar/apresentar': { "id": 1, "valor": 500.00 },
|
|
'/colaboradores/apresentar': { "id": 1, "nome": "João Silva", "cargo": "Analista" },
|
|
'/boletos/apresentar': { "id": 1, "valor": 1200.00 },
|
|
'/gr/contratos': { "id": 1, "cliente": "Empresa X" }
|
|
};
|
|
|
|
async function updateDocs() {
|
|
console.log(`🤖 Agente Auto-Sync Iniciado`);
|
|
const ENVS = [...new Set(MONITORED_ROUTES.map(r => r.env))];
|
|
|
|
for (const envId of ENVS) {
|
|
const targetFile = path.join(CONTRATOS_DIR, `${envId}-routes.json`);
|
|
let contractData = { ambiente: envId, label: envId, rotas: [] };
|
|
|
|
try {
|
|
if (fs.existsSync(targetFile)) {
|
|
contractData = JSON.parse(fs.readFileSync(targetFile, 'utf8'));
|
|
}
|
|
} catch (e) { console.error(`Erro ao ler ${envId}:`, e); continue; }
|
|
|
|
const envRoutes = MONITORED_ROUTES.filter(r => r.env === envId);
|
|
console.log(`\n📂 Ambiente: ${envId.toUpperCase()}`);
|
|
|
|
for (const route of envRoutes) {
|
|
console.log(` ⚡ Rota: ${route.method} ${route.path}`);
|
|
const responseData = MOCK_API_RESPONSES[route.path];
|
|
if (!responseData) continue;
|
|
|
|
const newResponseShape = Object.entries(responseData).map(([key, value]) => ({
|
|
field: key,
|
|
type: inferType(value),
|
|
description: inferDescription(key, value)
|
|
})).sort((a, b) => a.field.localeCompare(b.field));
|
|
|
|
let routeEntry = contractData.rotas.find(r => r.recurso === route.resource);
|
|
if (!routeEntry) {
|
|
routeEntry = { id: `${route.resource}-auto`, recurso: route.resource, operations: [] };
|
|
contractData.rotas.push(routeEntry);
|
|
}
|
|
|
|
let operation = routeEntry.operations.find(op => op.path === route.path && op.method === route.method);
|
|
if (!operation) {
|
|
operation = { method: route.method, path: route.path, descricao: `Auto-gerado`, responseShape: [] };
|
|
routeEntry.operations.push(operation);
|
|
}
|
|
operation.responseShape = newResponseShape;
|
|
}
|
|
fs.writeFileSync(targetFile, JSON.stringify(contractData, null, 2), 'utf8');
|
|
}
|
|
console.log(`\n✅ Sincronização concluída!`);
|
|
}
|
|
|
|
updateDocs();
|