272 lines
11 KiB
JavaScript
272 lines
11 KiB
JavaScript
"use strict";
|
|
/**
|
|
* DerechohabienteController - REST API for INFONAVIT workers
|
|
*
|
|
* Endpoints para gestión de derechohabientes INFONAVIT.
|
|
*
|
|
* @module Infonavit
|
|
* @routes /api/derechohabientes
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.createDerechohabienteController = createDerechohabienteController;
|
|
const express_1 = require("express");
|
|
const derechohabiente_service_1 = require("../services/derechohabiente.service");
|
|
const derechohabiente_entity_1 = require("../entities/derechohabiente.entity");
|
|
const historico_puntos_entity_1 = require("../entities/historico-puntos.entity");
|
|
const auth_middleware_1 = require("../../auth/middleware/auth.middleware");
|
|
const auth_service_1 = require("../../auth/services/auth.service");
|
|
const user_entity_1 = require("../../core/entities/user.entity");
|
|
const tenant_entity_1 = require("../../core/entities/tenant.entity");
|
|
const refresh_token_entity_1 = require("../../auth/entities/refresh-token.entity");
|
|
function createDerechohabienteController(dataSource) {
|
|
const router = (0, express_1.Router)();
|
|
// Repositories
|
|
const derechohabienteRepo = dataSource.getRepository(derechohabiente_entity_1.Derechohabiente);
|
|
const historicoPuntosRepo = dataSource.getRepository(historico_puntos_entity_1.HistoricoPuntos);
|
|
const userRepository = dataSource.getRepository(user_entity_1.User);
|
|
const tenantRepository = dataSource.getRepository(tenant_entity_1.Tenant);
|
|
const refreshTokenRepository = dataSource.getRepository(refresh_token_entity_1.RefreshToken);
|
|
// Services
|
|
const service = new derechohabiente_service_1.DerechohabienteService(derechohabienteRepo, historicoPuntosRepo);
|
|
const authService = new auth_service_1.AuthService(userRepository, tenantRepository, refreshTokenRepository);
|
|
const authMiddleware = new auth_middleware_1.AuthMiddleware(authService, dataSource);
|
|
// Helper for service context
|
|
const getContext = (req) => {
|
|
if (!req.tenantId) {
|
|
throw new Error('Tenant ID is required');
|
|
}
|
|
return {
|
|
tenantId: req.tenantId,
|
|
userId: req.user?.sub,
|
|
};
|
|
};
|
|
/**
|
|
* GET /api/derechohabientes
|
|
* List workers with filters
|
|
*/
|
|
router.get('/', authMiddleware.authenticate, async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const filters = {};
|
|
if (req.query.nss)
|
|
filters.nss = req.query.nss;
|
|
if (req.query.curp)
|
|
filters.curp = req.query.curp;
|
|
if (req.query.status)
|
|
filters.status = req.query.status;
|
|
if (req.query.search)
|
|
filters.search = req.query.search;
|
|
if (req.query.minPoints)
|
|
filters.minPoints = parseInt(req.query.minPoints);
|
|
const page = parseInt(req.query.page) || 1;
|
|
const limit = Math.min(parseInt(req.query.limit) || 20, 100);
|
|
const result = await service.findWithFilters(getContext(req), filters, page, limit);
|
|
res.status(200).json({ success: true, data: result.data, pagination: result.meta });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* GET /api/derechohabientes/:id
|
|
* Get worker with details
|
|
*/
|
|
router.get('/:id', authMiddleware.authenticate, async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const derechohabiente = await service.findWithDetails(getContext(req), req.params.id);
|
|
if (!derechohabiente) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Worker not found' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: derechohabiente });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* GET /api/derechohabientes/nss/:nss
|
|
* Get worker by NSS
|
|
*/
|
|
router.get('/nss/:nss', authMiddleware.authenticate, async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const derechohabiente = await service.findByNss(getContext(req), req.params.nss);
|
|
if (!derechohabiente) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Worker not found' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: derechohabiente });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* POST /api/derechohabientes
|
|
* Create new worker
|
|
*/
|
|
router.post('/', authMiddleware.authenticate, authMiddleware.authorize('admin', 'infonavit'), async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const derechohabiente = await service.create(getContext(req), req.body);
|
|
res.status(201).json({ success: true, data: derechohabiente });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* PUT /api/derechohabientes/:id
|
|
* Update worker
|
|
*/
|
|
router.put('/:id', authMiddleware.authenticate, authMiddleware.authorize('admin', 'infonavit'), async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const derechohabiente = await service.update(getContext(req), req.params.id, req.body);
|
|
if (!derechohabiente) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Worker not found' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: derechohabiente });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* POST /api/derechohabientes/:id/points
|
|
* Update worker points
|
|
*/
|
|
router.post('/:id/points', authMiddleware.authenticate, authMiddleware.authorize('admin', 'infonavit'), async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const { points, source, notes } = req.body;
|
|
const derechohabiente = await service.updatePoints(getContext(req), req.params.id, points, source, notes);
|
|
if (!derechohabiente) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Worker not found' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: derechohabiente });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* POST /api/derechohabientes/:id/precalify
|
|
* Precalify worker
|
|
*/
|
|
router.post('/:id/precalify', authMiddleware.authenticate, authMiddleware.authorize('admin', 'infonavit'), async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const derechohabiente = await service.precalify(getContext(req), req.params.id, {
|
|
precalificationDate: new Date(req.body.precalificationDate),
|
|
precalificationAmount: req.body.precalificationAmount,
|
|
creditType: req.body.creditType,
|
|
creditPoints: req.body.creditPoints,
|
|
});
|
|
if (!derechohabiente) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Worker not found' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: derechohabiente });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* POST /api/derechohabientes/:id/qualify
|
|
* Qualify worker
|
|
*/
|
|
router.post('/:id/qualify', authMiddleware.authenticate, authMiddleware.authorize('admin', 'infonavit'), async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const derechohabiente = await service.qualify(getContext(req), req.params.id);
|
|
if (!derechohabiente) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Worker not found' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: derechohabiente });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* GET /api/derechohabientes/:id/points-history
|
|
* Get points history
|
|
*/
|
|
router.get('/:id/points-history', authMiddleware.authenticate, async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const history = await service.getPointsHistory(getContext(req), req.params.id);
|
|
res.status(200).json({ success: true, data: history });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* DELETE /api/derechohabientes/:id
|
|
* Soft delete worker
|
|
*/
|
|
router.delete('/:id', authMiddleware.authenticate, authMiddleware.authorize('admin'), async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const deleted = await service.softDelete(getContext(req), req.params.id);
|
|
if (!deleted) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Worker not found' });
|
|
return;
|
|
}
|
|
res.status(204).send();
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
return router;
|
|
}
|
|
//# sourceMappingURL=derechohabiente.controller.js.map
|