From f0f854736be317c69c27d98edc3ba4d88d398b91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorem=20Ju=C3=A1rez?= Date: Tue, 7 Jul 2026 11:59:15 -0600 Subject: [PATCH] feat(becarios): reactivar (revertir baja) desde el panel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit POST /api/becarios/:id/reactivar: 'baja' → 'activo' (409 ESTADO_INVALIDO si no está de baja). En el panel, los becarios de baja muestran botón "Reactivar" (verde) junto a "Eliminar". Escribe en bitacora. Co-Authored-By: Claude Fable 5 --- backend/src/routes/becarios.js | 51 ++++++++++++++++++++++++++++++++++ docs/02-API.md | 5 ++++ frontend/panel.html | 8 +++++- 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/backend/src/routes/becarios.js b/backend/src/routes/becarios.js index 499cbe5..fc6cc1e 100644 --- a/backend/src/routes/becarios.js +++ b/backend/src/routes/becarios.js @@ -195,6 +195,57 @@ export default async function becariosRoutes(fastify) { } ); + // ──────────────── POST /becarios/:id/reactivar ──────────────── + // Revierte una baja: 'baja' → 'activo'. Escribe en bitacora. + fastify.post( + '/becarios/:id/reactivar', + { schema: { params: idParam } }, + async (req, reply) => { + const client = await fastify.db.getClient(); + try { + await client.query('BEGIN'); + const { + rows: [antes], + } = await client.query(`SELECT ${COLS} FROM becarios WHERE id = $1 FOR UPDATE`, [ + req.params.id, + ]); + if (!antes) { + await client.query('ROLLBACK'); + return notFound(reply); + } + if (antes.estado !== 'baja') { + await client.query('ROLLBACK'); + return reply.code(409).send({ + error: `El becario está '${antes.estado}', solo se reactivan los de 'baja'`, + code: 'ESTADO_INVALIDO', + }); + } + const { + rows: [despues], + } = await client.query( + `UPDATE becarios SET estado = 'activo' WHERE id = $1 RETURNING ${COLS}`, + [req.params.id] + ); + await registrarBitacora(client, { + actor: req.supervisora.usuario, + accion: 'reactivar', + entidad: 'becario', + entidadId: despues.id, + antes, + despues, + }); + await client.query('COMMIT'); + req.log.info({ becarioId: despues.id }, 'Becario reactivado'); + return despues; + } catch (err) { + await client.query('ROLLBACK'); + throw err; + } finally { + client.release(); + } + } + ); + // ──────────────── DELETE /becarios/:id ──────────────── // Borrado FÍSICO, solo para becarios ya de 'baja' y SIN historial de checadas. // checadas es inmutable (trigger prohíbe DELETE): un becario que alguna vez diff --git a/docs/02-API.md b/docs/02-API.md index 00d3659..a1998aa 100644 --- a/docs/02-API.md +++ b/docs/02-API.md @@ -135,6 +135,11 @@ Respalda antes/después en `bitacora`. **200** con el becario · **404**. Baja lógica: `estado = 'baja'` (conserva historial). **200** · **409** `YA_DE_BAJA` · **404**. Escribe en `bitacora`. +### `POST /api/becarios/:id/reactivar` + +Revierte una baja: `estado 'baja' → 'activo'`. **200** con el becario · **409** +`ESTADO_INVALIDO` (no está de baja) · **404**. Escribe en `bitacora`. + ### `DELETE /api/becarios/:id` Borrado **físico**, solo para becarios ya de `baja` y **sin historial de checadas** diff --git a/frontend/panel.html b/frontend/panel.html index ac252dd..1adb28f 100644 --- a/frontend/panel.html +++ b/frontend/panel.html @@ -202,6 +202,9 @@ function VistaBecarios({ onOpen, onError }) { try { await apiFetch(`/becarios/${b.id}`, { method: "DELETE" }); setConfirmEliminar(null); await load(); } catch (e) { onError(e.message); setConfirmEliminar(null); } }; + const reactivar = async (b) => { + try { await apiFetch(`/becarios/${b.id}/reactivar`, { method: "POST" }); await load(); } catch (e) { onError(e.message); } + }; const aprobar = async (b) => { try { await apiFetch(`/becarios/${b.id}/aprobar`, { method: "POST" }); await load(); } catch (e) { onError(e.message); } }; @@ -254,7 +257,10 @@ function VistaBecarios({ onOpen, onError }) { setForm(b)}>Editar {b.estado !== "baja" ? setConfirmBaja(b)}>Baja - : setConfirmEliminar(b)}>Eliminar} + : <> + reactivar(b)}>Reactivar + setConfirmEliminar(b)}>Eliminar + }