feat(benefits-reception): add institutional benefits, QR digital card & reception access control (SDD-012 v3.8.39)
This commit is contained in:
+10
-3
@@ -86,9 +86,9 @@ const env = nunjucks.configure(path.join(__dirname, '../views'), {
|
||||
});
|
||||
|
||||
// Versiones institucionales del sistema
|
||||
const APP_VERSION = '3.8.38';
|
||||
const FRONTEND_VERSION = '3.8.38';
|
||||
const BACKEND_VERSION = '3.8.38';
|
||||
const APP_VERSION = '3.8.39';
|
||||
const FRONTEND_VERSION = '3.8.39';
|
||||
const BACKEND_VERSION = '3.8.39';
|
||||
|
||||
env.addGlobal('app_version', APP_VERSION);
|
||||
env.addGlobal('frontend_version', FRONTEND_VERSION);
|
||||
@@ -311,6 +311,9 @@ const scheduleRoutes = require('./routes/schedule');
|
||||
const buildingsRoutes = require('./routes/buildings');
|
||||
const adminRoutes = require('./routes/admin');
|
||||
const mySubjectsRoutes = require('./routes/my_subjects');
|
||||
const benefitsRoutes = require('./routes/benefits');
|
||||
const receptionRoutes = require('./routes/reception');
|
||||
const credentialRoutes = require('./routes/credential');
|
||||
|
||||
// Proxy transparente hacia la API REST de FastAPI para llamadas AJAX frontend
|
||||
app.use('/api/v1', async (req, res) => {
|
||||
@@ -403,6 +406,10 @@ app.use('/buildings', buildingsRoutes);
|
||||
app.use('/admin', adminRoutes);
|
||||
app.use('/my_subjects', mySubjectsRoutes);
|
||||
app.use('/mis-materias', mySubjectsRoutes);
|
||||
app.use('/benefits', benefitsRoutes);
|
||||
app.use('/reception', receptionRoutes);
|
||||
app.use('/credential', credentialRoutes);
|
||||
app.use('/credencial', credentialRoutes);
|
||||
|
||||
// Redirección de enlaces heredados de Jinja /main/dashboard preservando query params
|
||||
app.get(['/main/dashboard', '/main'], (req, res) => {
|
||||
|
||||
@@ -7,6 +7,7 @@ const buildUserSession = (decoded) => {
|
||||
const isBedelia = roleStr === 'bedelia';
|
||||
const isDocente = roleStr === 'docente' || roleStr === 'profesor';
|
||||
const isAlumno = roleStr === 'alumno' || roleStr === 'estudiante';
|
||||
const isRecepcion = roleStr === 'recepcion' || roleStr === 'recepción' || roleStr === 'seguridad';
|
||||
const canManage = isAdmin || isBedelia;
|
||||
|
||||
let badgeClass = 'bg-primary text-white';
|
||||
@@ -25,6 +26,10 @@ const buildUserSession = (decoded) => {
|
||||
badgeClass = 'bg-success text-white';
|
||||
badgeIcon = 'bi-mortarboard';
|
||||
badgeLabel = 'Alumno';
|
||||
} else if (isRecepcion) {
|
||||
badgeClass = 'bg-secondary text-white';
|
||||
badgeIcon = 'bi-qr-code-scan';
|
||||
badgeLabel = 'Recepción';
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -39,6 +44,7 @@ const buildUserSession = (decoded) => {
|
||||
is_bedelia: () => isBedelia,
|
||||
is_docente: () => isDocente,
|
||||
is_alumno: () => isAlumno,
|
||||
is_recepcion: () => isRecepcion,
|
||||
can_manage: () => canManage,
|
||||
has_role: (r) => roleStr === (r || '').toLowerCase(),
|
||||
has_permission: (module, action) => {
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { requireAuth } = require('../middlewares/authMiddleware');
|
||||
|
||||
router.use(requireAuth);
|
||||
|
||||
const getNormalizedRole = (user) => {
|
||||
const roleStr = (user?.role || '').toLowerCase();
|
||||
if (roleStr.includes('admin')) return 'admin';
|
||||
if (roleStr.includes('bedel')) return 'bedelia';
|
||||
if (roleStr.includes('docent') || roleStr.includes('prof')) return 'docente';
|
||||
if (roleStr.includes('recep')) return 'recepcion';
|
||||
return 'alumno';
|
||||
};
|
||||
|
||||
// GET /benefits — Catálogo interactivo para Alumnos, Docentes y Staff
|
||||
router.get('/', async (req, res) => {
|
||||
const role = getNormalizedRole(req.user);
|
||||
const category = req.query.category || '';
|
||||
const search = req.query.search || '';
|
||||
|
||||
let benefits = [];
|
||||
let categories = [];
|
||||
try {
|
||||
const resp = await req.apiClient.get('/benefits', {
|
||||
params: {
|
||||
role,
|
||||
category: category || undefined,
|
||||
search: search || undefined,
|
||||
active_only: true
|
||||
}
|
||||
});
|
||||
if (resp.data) {
|
||||
benefits = resp.data.items || [];
|
||||
categories = resp.data.categories || [];
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('Benefits catalog fetch notice:', err.message);
|
||||
}
|
||||
|
||||
res.render('benefits/catalog', {
|
||||
title: 'Club de Beneficios & Convenios — Edu-Space',
|
||||
benefits,
|
||||
categories,
|
||||
currentCategory: category,
|
||||
searchQuery: search,
|
||||
userRole: role,
|
||||
canManage: role === 'admin' || role === 'bedelia'
|
||||
});
|
||||
});
|
||||
|
||||
// GET /benefits/manage — Panel de Gestión ABM para Bedelía y Administradores
|
||||
router.get('/manage', async (req, res) => {
|
||||
const role = getNormalizedRole(req.user);
|
||||
if (role !== 'admin' && role !== 'bedelia') {
|
||||
return res.status(403).redirect('/benefits');
|
||||
}
|
||||
|
||||
let benefits = [];
|
||||
let buildings = [];
|
||||
let categories = [];
|
||||
|
||||
try {
|
||||
const [respBenefits, respBuildings] = await Promise.all([
|
||||
req.apiClient.get('/benefits', { params: { active_only: false } }),
|
||||
req.apiClient.get('/buildings')
|
||||
]);
|
||||
|
||||
if (respBenefits.data) {
|
||||
benefits = respBenefits.data.items || [];
|
||||
categories = respBenefits.data.categories || [];
|
||||
}
|
||||
if (respBuildings.data) {
|
||||
buildings = respBuildings.data.buildings || respBuildings.data || [];
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('Benefits manage fetch notice:', err.message);
|
||||
}
|
||||
|
||||
res.render('benefits/manage', {
|
||||
title: 'Gestión de Convenios & Beneficios — Bedelía',
|
||||
benefits,
|
||||
buildings,
|
||||
categories,
|
||||
userRole: role
|
||||
});
|
||||
});
|
||||
|
||||
// POST /benefits — Creación de nuevo convenio
|
||||
router.post('/', async (req, res) => {
|
||||
const role = getNormalizedRole(req.user);
|
||||
if (role !== 'admin' && role !== 'bedelia') {
|
||||
return res.status(403).json({ error: 'Acceso no autorizado' });
|
||||
}
|
||||
|
||||
try {
|
||||
const payload = {
|
||||
title: req.body.title,
|
||||
partner_name: req.body.partner_name,
|
||||
category: req.body.category || 'GENERAL',
|
||||
discount_label: req.body.discount_label,
|
||||
description: req.body.description || null,
|
||||
terms_conditions: req.body.terms_conditions || null,
|
||||
target_roles: req.body.target_roles || 'ALL',
|
||||
building_id: req.body.building_id ? parseInt(req.body.building_id, 10) : null,
|
||||
coupon_code: req.body.coupon_code || null,
|
||||
valid_from: req.body.valid_from || new Date().toISOString().split('T')[0],
|
||||
valid_until: req.body.valid_until || null,
|
||||
is_active: req.body.is_active === 'true' || req.body.is_active === true || req.body.is_active === 'on',
|
||||
image_url: req.body.image_url || null
|
||||
};
|
||||
|
||||
const resp = await req.apiClient.post('/benefits', payload);
|
||||
if (req.xhr || req.headers.accept?.includes('application/json')) {
|
||||
return res.status(201).json(resp.data);
|
||||
}
|
||||
res.redirect('/benefits/manage?msg=created');
|
||||
} catch (err) {
|
||||
console.error('Error creating benefit:', err.response?.data || err.message);
|
||||
if (req.xhr || req.headers.accept?.includes('application/json')) {
|
||||
return res.status(err.response?.status || 500).json(err.response?.data || { error: err.message });
|
||||
}
|
||||
res.redirect('/benefits/manage?err=' + encodeURIComponent(err.response?.data?.detail || 'Error al guardar'));
|
||||
}
|
||||
});
|
||||
|
||||
// POST /benefits/:id/edit — Edición de convenio
|
||||
router.post('/:id/edit', async (req, res) => {
|
||||
const role = getNormalizedRole(req.user);
|
||||
if (role !== 'admin' && role !== 'bedelia') {
|
||||
return res.status(403).json({ error: 'Acceso no autorizado' });
|
||||
}
|
||||
|
||||
try {
|
||||
const benefitId = req.params.id;
|
||||
const payload = {
|
||||
title: req.body.title,
|
||||
partner_name: req.body.partner_name,
|
||||
category: req.body.category,
|
||||
discount_label: req.body.discount_label,
|
||||
description: req.body.description || null,
|
||||
terms_conditions: req.body.terms_conditions || null,
|
||||
target_roles: req.body.target_roles,
|
||||
building_id: req.body.building_id ? parseInt(req.body.building_id, 10) : null,
|
||||
coupon_code: req.body.coupon_code || null,
|
||||
valid_from: req.body.valid_from,
|
||||
valid_until: req.body.valid_until || null,
|
||||
is_active: req.body.is_active === 'true' || req.body.is_active === true || req.body.is_active === 'on',
|
||||
image_url: req.body.image_url || null
|
||||
};
|
||||
|
||||
const resp = await req.apiClient.put(`/benefits/${benefitId}`, payload);
|
||||
if (req.xhr || req.headers.accept?.includes('application/json')) {
|
||||
return res.json(resp.data);
|
||||
}
|
||||
res.redirect('/benefits/manage?msg=updated');
|
||||
} catch (err) {
|
||||
console.error('Error updating benefit:', err.response?.data || err.message);
|
||||
if (req.xhr || req.headers.accept?.includes('application/json')) {
|
||||
return res.status(err.response?.status || 500).json(err.response?.data || { error: err.message });
|
||||
}
|
||||
res.redirect('/benefits/manage?err=' + encodeURIComponent(err.response?.data?.detail || 'Error al actualizar'));
|
||||
}
|
||||
});
|
||||
|
||||
// POST /benefits/:id/toggle — Switch de publicación rápido
|
||||
router.post('/:id/toggle', async (req, res) => {
|
||||
const role = getNormalizedRole(req.user);
|
||||
if (role !== 'admin' && role !== 'bedelia') {
|
||||
return res.status(403).json({ error: 'Acceso no autorizado' });
|
||||
}
|
||||
|
||||
try {
|
||||
const resp = await req.apiClient.patch(`/benefits/${req.params.id}/toggle-status`);
|
||||
res.json(resp.data);
|
||||
} catch (err) {
|
||||
res.status(err.response?.status || 500).json(err.response?.data || { error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /benefits/:id/delete — Eliminación
|
||||
router.post('/:id/delete', async (req, res) => {
|
||||
const role = getNormalizedRole(req.user);
|
||||
if (role !== 'admin' && role !== 'bedelia') {
|
||||
return res.status(403).json({ error: 'Acceso no autorizado' });
|
||||
}
|
||||
|
||||
try {
|
||||
await req.apiClient.delete(`/benefits/${req.params.id}`);
|
||||
if (req.xhr || req.headers.accept?.includes('application/json')) {
|
||||
return res.json({ success: true });
|
||||
}
|
||||
res.redirect('/benefits/manage?msg=deleted');
|
||||
} catch (err) {
|
||||
if (req.xhr || req.headers.accept?.includes('application/json')) {
|
||||
return res.status(err.response?.status || 500).json({ error: err.message });
|
||||
}
|
||||
res.redirect('/benefits/manage?err=error_deleting');
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,29 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { requireAuth } = require('../middlewares/authMiddleware');
|
||||
|
||||
router.use(requireAuth);
|
||||
|
||||
router.get(['/my-card', '/', '/mi-credencial'], async (req, res) => {
|
||||
let qrToken = '';
|
||||
let expiresInHours = 72;
|
||||
|
||||
try {
|
||||
const resp = await req.apiClient.get('/reception/my-qr-token');
|
||||
if (resp.data) {
|
||||
qrToken = resp.data.qr_token || '';
|
||||
expiresInHours = resp.data.expires_in_hours || 72;
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('QR token generation notice:', err.message);
|
||||
}
|
||||
|
||||
res.render('credential/card', {
|
||||
title: 'Mi Credencial Digital — UniCABA',
|
||||
user: req.user,
|
||||
qrToken,
|
||||
expiresInHours
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,82 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { requireAuth } = require('../middlewares/authMiddleware');
|
||||
|
||||
router.use(requireAuth);
|
||||
|
||||
const getNormalizedRole = (user) => {
|
||||
const roleStr = (user?.role || '').toLowerCase();
|
||||
if (roleStr.includes('admin')) return 'admin';
|
||||
if (roleStr.includes('bedel')) return 'bedelia';
|
||||
if (roleStr.includes('recep')) return 'recepcion';
|
||||
return roleStr;
|
||||
};
|
||||
|
||||
// GET /reception/scanner — Consola de Escaneo y Control de Acceso
|
||||
router.get('/scanner', async (req, res) => {
|
||||
const role = getNormalizedRole(req.user);
|
||||
if (role !== 'admin' && role !== 'bedelia' && role !== 'recepcion') {
|
||||
return res.status(403).redirect('/dashboard');
|
||||
}
|
||||
|
||||
let buildings = [];
|
||||
let recentLogs = [];
|
||||
try {
|
||||
const [respBld, respLogs] = await Promise.all([
|
||||
req.apiClient.get('/buildings'),
|
||||
req.apiClient.get('/reception/recent-logs', { params: { limit: 15 } })
|
||||
]);
|
||||
buildings = respBld.data?.buildings || respBld.data || [];
|
||||
recentLogs = respLogs.data?.items || [];
|
||||
} catch (err) {
|
||||
console.warn('Reception scanner init notice:', err.message);
|
||||
}
|
||||
|
||||
res.render('reception/scanner', {
|
||||
title: 'Control de Acceso QR — Recepción & Seguridad',
|
||||
buildings,
|
||||
recentLogs,
|
||||
userRole: role
|
||||
});
|
||||
});
|
||||
|
||||
// POST /reception/verify — Endpoint AJAX de verificación de código QR
|
||||
router.post('/verify', async (req, res) => {
|
||||
const role = getNormalizedRole(req.user);
|
||||
if (role !== 'admin' && role !== 'bedelia' && role !== 'recepcion') {
|
||||
return res.status(403).json({ error: 'Acceso no autorizado' });
|
||||
}
|
||||
|
||||
try {
|
||||
const payload = {
|
||||
qr_token: req.body.qr_token,
|
||||
building_id: req.body.building_id ? parseInt(req.body.building_id, 10) : null,
|
||||
notes: req.body.notes || null
|
||||
};
|
||||
const resp = await req.apiClient.post('/reception/verify-qr', payload);
|
||||
return res.json(resp.data);
|
||||
} catch (err) {
|
||||
console.error('Error verifying QR in reception:', err.response?.data || err.message);
|
||||
return res.status(err.response?.status || 500).json(
|
||||
err.response?.data || { status: 'DENEGADO', message: 'Error de comunicación con el validador' }
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
// GET /reception/recent-logs — Consulta de historial en vivo
|
||||
router.get('/recent-logs', async (req, res) => {
|
||||
try {
|
||||
const buildingId = req.query.building_id;
|
||||
const resp = await req.apiClient.get('/reception/recent-logs', {
|
||||
params: {
|
||||
building_id: buildingId || undefined,
|
||||
limit: 25
|
||||
}
|
||||
});
|
||||
return res.json(resp.data);
|
||||
} catch (err) {
|
||||
return res.status(err.response?.status || 500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user