diff --git a/backend/src/routes/becarios.js b/backend/src/routes/becarios.js
index ca1b4be..499cbe5 100644
--- a/backend/src/routes/becarios.js
+++ b/backend/src/routes/becarios.js
@@ -195,6 +195,81 @@ export default async function becariosRoutes(fastify) {
}
);
+ // ──────────────── 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
+ // checó no se puede eliminar (permanece de baja, conserva su historial, §10.2).
+ // Si no tiene checadas, se limpian sus registros borrables (solicitudes, ajustes,
+ // jornadas, tokens) y se elimina. Queda constancia en bitacora (append-only).
+ fastify.delete(
+ '/becarios/:id',
+ { schema: { params: idParam } },
+ async (req, reply) => {
+ const client = await fastify.db.getClient();
+ try {
+ await client.query('BEGIN');
+ const {
+ rows: [becario],
+ } = await client.query(`SELECT ${COLS} FROM becarios WHERE id = $1 FOR UPDATE`, [
+ req.params.id,
+ ]);
+ if (!becario) {
+ await client.query('ROLLBACK');
+ return notFound(reply);
+ }
+ if (becario.estado !== 'baja') {
+ await client.query('ROLLBACK');
+ return reply.code(409).send({
+ error: 'Solo se elimina un becario dado de baja. Da de baja primero.',
+ code: 'SOLO_BAJA',
+ });
+ }
+ const { rows: conCheck } = await client.query(
+ 'SELECT 1 FROM checadas WHERE becario_id = $1 LIMIT 1',
+ [req.params.id]
+ );
+ if (conCheck.length > 0) {
+ await client.query('ROLLBACK');
+ return reply.code(409).send({
+ error:
+ 'Tiene historial de checadas (registros inmutables); no se puede eliminar. Permanece de baja.',
+ code: 'TIENE_HISTORIAL',
+ });
+ }
+ // Sin checadas: no hay jornadas ni tokens/qr_usos que dependan de ellas,
+ // pero pueden existir solicitudes o ajustes sueltos. Se limpian todos.
+ await client.query('DELETE FROM solicitudes WHERE becario_id = $1', [req.params.id]);
+ await client.query('DELETE FROM ajustes WHERE becario_id = $1', [req.params.id]);
+ await client.query('DELETE FROM qr_token_usos WHERE becario_id = $1', [req.params.id]);
+ await client.query('DELETE FROM tokens_checada WHERE becario_id = $1', [req.params.id]);
+ await client.query('DELETE FROM jornadas WHERE becario_id = $1', [req.params.id]);
+ await client.query('DELETE FROM becarios WHERE id = $1', [req.params.id]);
+ await registrarBitacora(client, {
+ actor: req.supervisora.usuario,
+ accion: 'eliminar',
+ entidad: 'becario',
+ entidadId: becario.id,
+ antes: becario,
+ });
+ await client.query('COMMIT');
+ req.log.info({ becarioId: becario.id }, 'Becario eliminado (físico)');
+ return { ok: true, id: becario.id };
+ } catch (err) {
+ await client.query('ROLLBACK');
+ if (err.code === '23503') {
+ // Alguna referencia inesperada bloqueó el borrado.
+ return reply.code(409).send({
+ error: 'No se puede eliminar: tiene registros asociados. Permanece de baja.',
+ code: 'TIENE_HISTORIAL',
+ });
+ }
+ throw err;
+ } finally {
+ client.release();
+ }
+ }
+ );
+
// ──────────────── POST /becarios/:id/aprobar ────────────────
// Auto-registrado 'pendiente' → 'activo' (§4, §10.2): ya puede checar.
fastify.post(
diff --git a/docs/02-API.md b/docs/02-API.md
index fa7019c..903a781 100644
--- a/docs/02-API.md
+++ b/docs/02-API.md
@@ -132,9 +132,16 @@ Respalda antes/después en `bitacora`. **200** con el becario · **404**.
### `POST /api/becarios/:id/baja`
-Baja lógica: `estado = 'baja'` (nunca DELETE físico). **200** · **409**
+Baja lógica: `estado = 'baja'` (conserva historial). **200** · **409**
`YA_DE_BAJA` · **404**. Escribe en `bitacora`.
+### `DELETE /api/becarios/:id`
+
+Borrado **físico**, solo para becarios ya de `baja` y **sin historial de checadas**
+(inmutables). Limpia sus registros borrables (solicitudes, ajustes, jornadas,
+tokens) y elimina el becario; deja constancia en `bitacora`. **200** `{ ok, id }` ·
+**409** `SOLO_BAJA` (no está de baja) / `TIENE_HISTORIAL` (tiene checadas) · **404**.
+
---
## Dispositivos (Fase B)
diff --git a/frontend/panel.html b/frontend/panel.html
index 8732054..57e0472 100644
--- a/frontend/panel.html
+++ b/frontend/panel.html
@@ -182,6 +182,7 @@ function VistaBecarios({ onOpen, onError }) {
const [becarios, setBecarios] = useState(null);
const [form, setForm] = useState(null); // null | {} (nuevo) | becario (editar)
const [confirmBaja, setConfirmBaja] = useState(null);
+ const [confirmEliminar, setConfirmEliminar] = useState(null);
const [q, setQ] = useState("");
const load = useCallback(async () => {
try { setBecarios(await apiFetch("/becarios")); } catch (e) { onError(e.message); }
@@ -191,6 +192,10 @@ function VistaBecarios({ onOpen, onError }) {
try { await apiFetch(`/becarios/${b.id}/baja`, { method: "POST" }); setConfirmBaja(null); await load(); }
catch (e) { onError(e.message); setConfirmBaja(null); }
};
+ const doEliminar = async (b) => {
+ try { await apiFetch(`/becarios/${b.id}`, { method: "DELETE" }); setConfirmEliminar(null); await load(); }
+ catch (e) { onError(e.message); setConfirmEliminar(null); }
+ };
const aprobar = async (b) => {
try { await apiFetch(`/becarios/${b.id}/aprobar`, { method: "POST" }); await load(); } catch (e) { onError(e.message); }
};
@@ -241,7 +246,9 @@ function VistaBecarios({ onOpen, onError }) {
{b.estado === "pendiente" && aprobar(b)}>Aprobar}
onOpen(b.id)}>Ver
setForm(b)}>Editar
- {b.estado !== "baja" && setConfirmBaja(b)}>Baja}
+ {b.estado !== "baja"
+ ? setConfirmBaja(b)}>Baja
+ : setConfirmEliminar(b)}>Eliminar}
@@ -253,6 +260,9 @@ function VistaBecarios({ onOpen, onError }) {
{confirmBaja && doBaja(confirmBaja)} onCancel={() => setConfirmBaja(null)} />}
+ {confirmEliminar && doEliminar(confirmEliminar)} onCancel={() => setConfirmEliminar(null)} />}
);
}