Actualizar server.js

This commit is contained in:
2026-02-07 16:13:51 +00:00
parent 21f473b18c
commit c40cca6b4b

View File

@@ -5,40 +5,39 @@ const app = express();
app.use(cors()); app.use(cors());
app.use(express.json()); app.use(express.json());
// --- “BD” en memoria (se borra cuando redeploy/restart) --- // --- “BD” en memoria (se borra al redeploy/restart) ---
const services = []; const services = [];
let nextId = 1; let nextId = 1;
// Raíz
app.get("/", (req, res) => res.send("IntegraRepara API OK")); app.get("/", (req, res) => res.send("IntegraRepara API OK"));
// Health (sin DB por ahora)
app.get("/health", (req, res) => { app.get("/health", (req, res) => {
res.json({ ok: true, db: false, now: new Date().toISOString() }); res.json({ ok: true, db: false, now: new Date().toISOString() });
}); });
// Listar servicios // GET /services
app.get("/services", (req, res) => { app.get("/services", (req, res) => {
// opcional: ordenar por id desc para ver lo último arriba
const list = [...services].sort((a, b) => b.id - a.id); const list = [...services].sort((a, b) => b.id - a.id);
res.json({ ok: true, count: list.length, services: list }); res.json({ ok: true, count: list.length, services: list });
}); });
// Crear servicio // POST /services
app.post("/services", (req, res) => { app.post("/services", (req, res) => {
const { title, client, phone, address, notes } = req.body || {}; // Tu HTML manda clientName, aquí lo acepto
const { title, client, clientName, phone, address, notes } = req.body || {};
const clientFinal = client ?? clientName;
if (!title || !client) { if (!title || !clientFinal) {
return res.status(400).json({ return res.status(400).json({
ok: false, ok: false,
error: "Faltan campos obligatorios: title y client", error: "Faltan campos obligatorios: title y client (o clientName)",
}); });
} }
const item = { const item = {
id: nextId++, id: nextId++,
title: String(title).trim(), title: String(title).trim(),
client: String(client).trim(), client: String(clientFinal).trim(),
phone: phone ? String(phone).trim() : "", phone: phone ? String(phone).trim() : "",
address: address ? String(address).trim() : "", address: address ? String(address).trim() : "",
notes: notes ? String(notes).trim() : "", notes: notes ? String(notes).trim() : "",