220 lines
9.0 KiB
JavaScript
220 lines
9.0 KiB
JavaScript
"use strict";
|
|
/**
|
|
* AsignacionController - REST API for housing assignments
|
|
*
|
|
* Endpoints para gestión de asignaciones de vivienda INFONAVIT.
|
|
*
|
|
* @module Infonavit
|
|
* @routes /api/asignaciones
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.createAsignacionController = createAsignacionController;
|
|
const express_1 = require("express");
|
|
const asignacion_service_1 = require("../services/asignacion.service");
|
|
const asignacion_vivienda_entity_1 = require("../entities/asignacion-vivienda.entity");
|
|
const oferta_vivienda_entity_1 = require("../entities/oferta-vivienda.entity");
|
|
const derechohabiente_entity_1 = require("../entities/derechohabiente.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 createAsignacionController(dataSource) {
|
|
const router = (0, express_1.Router)();
|
|
// Repositories
|
|
const asignacionRepo = dataSource.getRepository(asignacion_vivienda_entity_1.AsignacionVivienda);
|
|
const ofertaRepo = dataSource.getRepository(oferta_vivienda_entity_1.OfertaVivienda);
|
|
const derechohabienteRepo = dataSource.getRepository(derechohabiente_entity_1.Derechohabiente);
|
|
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 asignacion_service_1.AsignacionService(asignacionRepo, ofertaRepo, derechohabienteRepo);
|
|
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/asignaciones
|
|
* List assignments 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.ofertaId)
|
|
filters.ofertaId = req.query.ofertaId;
|
|
if (req.query.derechohabienteId)
|
|
filters.derechohabienteId = req.query.derechohabienteId;
|
|
if (req.query.status)
|
|
filters.status = req.query.status;
|
|
if (req.query.dateFrom)
|
|
filters.dateFrom = new Date(req.query.dateFrom);
|
|
if (req.query.dateTo)
|
|
filters.dateTo = new Date(req.query.dateTo);
|
|
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/asignaciones/:id
|
|
* Get assignment 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 asignacion = await service.findWithDetails(getContext(req), req.params.id);
|
|
if (!asignacion) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Assignment not found' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: asignacion });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* POST /api/asignaciones
|
|
* Create new assignment
|
|
*/
|
|
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 asignacion = await service.create(getContext(req), {
|
|
ofertaId: req.body.ofertaId,
|
|
derechohabienteId: req.body.derechohabienteId,
|
|
assignmentDate: new Date(req.body.assignmentDate),
|
|
creditAmount: req.body.creditAmount,
|
|
subsidyAmount: req.body.subsidyAmount,
|
|
downPayment: req.body.downPayment,
|
|
notes: req.body.notes,
|
|
});
|
|
res.status(201).json({ success: true, data: asignacion });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* POST /api/asignaciones/:id/approve
|
|
* Approve assignment
|
|
*/
|
|
router.post('/:id/approve', 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 asignacion = await service.approve(getContext(req), req.params.id);
|
|
if (!asignacion) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Assignment not found' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: asignacion });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* POST /api/asignaciones/:id/formalize
|
|
* Formalize assignment
|
|
*/
|
|
router.post('/:id/formalize', 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 asignacion = await service.formalize(getContext(req), req.params.id, {
|
|
formalizationDate: new Date(req.body.formalizationDate),
|
|
notaryName: req.body.notaryName,
|
|
notaryNumber: req.body.notaryNumber,
|
|
deedNumber: req.body.deedNumber,
|
|
deedDate: new Date(req.body.deedDate),
|
|
});
|
|
if (!asignacion) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Assignment not found' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: asignacion });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* POST /api/asignaciones/:id/deliver
|
|
* Deliver housing
|
|
*/
|
|
router.post('/:id/deliver', 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 asignacion = await service.deliver(getContext(req), req.params.id, new Date(req.body.deliveryDate));
|
|
if (!asignacion) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Assignment not found' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: asignacion });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* POST /api/asignaciones/:id/cancel
|
|
* Cancel assignment
|
|
*/
|
|
router.post('/:id/cancel', 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 asignacion = await service.cancel(getContext(req), req.params.id, req.body.reason);
|
|
if (!asignacion) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Assignment not found' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: asignacion });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
return router;
|
|
}
|
|
//# sourceMappingURL=asignacion.controller.js.map
|