feat(becarios): aprobar (pendiente→activo) + ver checadas con ip/ua/concepto

Lista/detalle incluyen telefono. POST /:id/aprobar activa al auto-registrado
(bitacora). GET /:id/checadas expone ip/user_agent/concepto para el panel.
This commit is contained in:
Collina Systems committed 2026-07-04 22:58:27 -06:00
1 parent 9a5bd439ac
commit b03e31fb34
1 file changed
+69 -1
+69 -1
View File
@@ -2,7 +2,7 @@ import { requireSupervisora } from '../services/sessions.js';
import { registrarBitacora } from '../services/bitacora.js';
import { calcularHoras, calcularHorasIndex } from '../services/horas.js';
const COLS = 'id, nombre, matricula, institucion, meta_horas, estado, created_at';
const COLS = 'id, nombre, telefono, matricula, institucion, meta_horas, estado, created_at';
export default async function becariosRoutes(fastify) {
// Todo endpoint de este plugin exige sesión de supervisora (§10.1).
@@ -194,6 +194,74 @@ export default async function becariosRoutes(fastify) {
}
}
);
// ──────────────── POST /becarios/:id/aprobar ────────────────
// Auto-registrado 'pendiente' → 'activo' (§4, §10.2): ya puede checar.
fastify.post(
'/becarios/:id/aprobar',
{ 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 !== 'pendiente') {
await client.query('ROLLBACK');
return reply.code(409).send({
error: `El becario está '${antes.estado}', solo se aprueban los 'pendiente'`,
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: 'aprobar',
entidad: 'becario',
entidadId: despues.id,
antes,
despues,
});
await client.query('COMMIT');
req.log.info({ becarioId: despues.id }, 'Becario aprobado');
return despues;
} catch (err) {
await client.query('ROLLBACK');
throw err;
} finally {
client.release();
}
}
);
// ──────────────── GET /becarios/:id/checadas ────────────────
// Checadas del becario con ip/user_agent/concepto (señal secundaria, §3, §10.7).
fastify.get(
'/becarios/:id/checadas',
{ schema: { params: idParam } },
async (req) => {
const { rows } = await fastify.db.query(
`SELECT id, tipo, ts, origen,
meta->>'ip' AS ip, meta->>'user_agent' AS user_agent, meta->>'concepto' AS concepto
FROM checadas WHERE becario_id = $1
ORDER BY ts DESC`,
[req.params.id]
);
return rows;
}
);
}
const idParam = {