feat: implement AI-powered Human-in-the-Loop Norm Builder interface and standard CRUD endpoints

This commit is contained in:
2026-06-23 18:28:48 +00:00
parent a62c8d1cf4
commit e4b9cb8139
5 changed files with 447 additions and 2 deletions
+81
View File
@@ -137,6 +137,87 @@ const server = http.createServer((req, res) => {
}
// --- END CATEGORIES CRUD ---
// --- STANDARDS CRUD ---
const STANDARDS_FILE = path.join(CONFIG_DIR, 'standards.json');
const readStandards = () => {
if (fs.existsSync(STANDARDS_FILE)) {
try { return JSON.parse(fs.readFileSync(STANDARDS_FILE, 'utf-8')); } catch (e) {}
}
return [];
};
if (pathname === '/api/standards') {
if (req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
const allStandards = readStandards();
const catId = parsedUrl.searchParams.get('categoryId');
if (catId) {
res.end(JSON.stringify(allStandards.filter(s => s.categoria_id === catId)));
} else {
res.end(JSON.stringify(allStandards));
}
return;
}
if (req.method === 'POST' || req.method === 'PUT') {
let body = '';
req.on('data', chunk => body += chunk.toString());
req.on('end', () => {
try {
const standard = JSON.parse(body);
// Sanitização numérica para parametros_controle
if (standard.parametros_controle) {
for (const key of Object.keys(standard.parametros_controle)) {
const grupo = standard.parametros_controle[key];
if (typeof grupo === 'object' && grupo !== null) {
for (const prop of Object.keys(grupo)) {
const val = grupo[prop];
if (val !== null && val !== undefined && val !== '') {
const num = parseFloat(val);
if (!isNaN(num)) {
grupo[prop] = num;
}
}
}
}
}
}
let standards = readStandards();
if (req.method === 'POST') {
standard.id = Date.now().toString();
standards.push(standard);
} else {
const index = standards.findIndex(s => s.id === standard.id);
if (index !== -1) standards[index] = { ...standards[index], ...standard };
}
fs.writeFileSync(STANDARDS_FILE, JSON.stringify(standards, null, 2));
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(standard));
} catch (err) {
res.writeHead(400); res.end(JSON.stringify({ error: 'Invalid payload' }));
}
});
return;
}
if (req.method === 'DELETE') {
const id = parsedUrl.searchParams.get('id');
if (id) {
let standards = readStandards();
standards = standards.filter(s => s.id !== id);
fs.writeFileSync(STANDARDS_FILE, JSON.stringify(standards, null, 2));
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: true }));
} else {
res.writeHead(400); res.end(JSON.stringify({ error: 'Missing id' }));
}
return;
}
}
// --- END STANDARDS CRUD ---
// Serve static files
let filePath = path.join(DIST_DIR, req.url === '/' ? 'index.html' : req.url);