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 + }