Files
project.linkedcorp.net/backend/src/modules/clinic/routes.js
T
2026-10-29 17:06:10 -05:00

59 lines
4.3 KiB
JavaScript

import { Router } from 'express';
import { requireAuth } from '../../middleware/auth.js';
import { requireModule } from '../../middleware/modules.js';
import { asyncHandler, ok } from '../../utils/http.js';
import * as clinic from './service.js';
import * as integrations from '../integrations/service.js';
const router = Router();
router.use(requireAuth, requireModule('clinic'));
// Configuracion (solo admin/superadmin): no exige ser personal de la agenda, por eso va antes
// del middleware que resuelve clinicStaff.
router.get('/config/staff', asyncHandler(async (req, res) => ok(res, await clinic.configStaff(req.currentUser))));
router.put('/config/staff/:id', asyncHandler(async (req, res) => ok(res, await clinic.saveStaff(req.body, req.currentUser, req.params.id))));
router.get('/config/schedules/:id', asyncHandler(async (req, res) => {
clinic.requireConfig(req.currentUser);
ok(res, await clinic.schedules(req.params.id));
}));
router.put('/config/schedules/:id', asyncHandler(async (req, res) => ok(res, await clinic.saveSchedules(req.params.id, req.body, req.currentUser))));
router.get('/config/types', asyncHandler(async (req, res) => {
clinic.requireConfig(req.currentUser);
ok(res, await clinic.types());
}));
router.post('/config/types', asyncHandler(async (req, res) => ok(res, await clinic.saveType(req.body, req.currentUser), 201)));
router.put('/config/types/:id', asyncHandler(async (req, res) => ok(res, await clinic.saveType(req.body, req.currentUser, req.params.id))));
router.use(asyncHandler(async (req, res, next) => {
req.clinicStaff = await clinic.staffFor(req.currentUser);
next();
}));
const get = (path, fn) => router.get(path, asyncHandler(async (req, res) => ok(res, await fn(req.query, req.clinicStaff, req.currentUser))));
const write = (method, path, fn) => router[method](path, asyncHandler(async (req, res) => ok(res, await fn(req.body, req.currentUser, req.clinicStaff, req.params.id), method === 'post' && !req.params.id ? 201 : 200)));
get('/staff/me', async (q, s) => s);
get('/doctors', async (q, s) => Promise.all([clinic.doctors(s), clinic.types()]).then(async ([doctors, types]) => ({
doctors: await Promise.all(doctors.map(async d => ({
...d,
schedules: await clinic.schedules(d.id)
}))),
types: types.filter(t => t.is_active)
})));
get('/availability', (q, s) => clinic.availability(q, s));
get('/appointments', (q, s) => clinic.appointments(q, s));
write('post', '/appointments', (body, user, staff) => clinic.saveAppointment(body, user, staff));
write('put', '/appointments/:id', (body, user, staff, n) => clinic.saveAppointment(body, user, staff, n));
write('post', '/appointments/:id/cancel', (body, user, staff, n) => clinic.changeStatus(n, body, user, staff, true));
write('post', '/appointments/:id/delete', (body, user, staff, n) => clinic.deleteAppointment(n, body, user, staff));
write('post', '/appointments/:id/status', (body, user, staff, n) => clinic.changeStatus(n, body, user, staff));
get('/blocks', (q, s) => clinic.blocks(q, s));
write('post', '/blocks', (body, user, staff) => clinic.saveBlock(body, user, staff));
write('put', '/blocks/:id', (body, user, staff, n) => clinic.saveBlock(body, user, staff, n));
router.delete('/blocks/:id', asyncHandler(async (req, res) => ok(res, await clinic.deleteBlock(req.params.id, req.query, req.currentUser, req.clinicStaff))));
get('/patients', (q, s) => clinic.patients(q, s));
// Pacientes del sistema origen (Conexiones externas): estado de la fuente, búsqueda en vivo e
// importación del elegido. Solo recepción (gestiona pacientes).
get('/patients/source', async (q, s) => { clinic.requireReception(s); return integrations.patientSourceInfo(); });
get('/patients/external', async (q, s) => { clinic.requireReception(s); return integrations.searchExternalPatients(q.search); });
write('post', '/patients/external/:id/import', async (body, user, staff, n) => { clinic.requireReception(staff); return integrations.importExternalPatient(n, user); });
router.get('/patients/:id/appointments', asyncHandler(async (req, res) => ok(res, await clinic.patientHistory(req.params.id, req.clinicStaff))));
write('post', '/patients', (body, user, staff) => clinic.savePatient(body, user, staff));
write('put', '/patients/:id', (body, user, staff, n) => clinic.savePatient(body, user, staff, n));
export default router;