erp-construccion-backend/dist/modules/construction/controllers/manzana.controller.js

167 lines
6.5 KiB
JavaScript

"use strict";
/**
* ManzanaController - Controller de manzanas
*
* Endpoints REST para gestión de manzanas (bloques).
*
* @module Construction
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.createManzanaController = createManzanaController;
const express_1 = require("express");
const manzana_service_1 = require("../services/manzana.service");
const auth_middleware_1 = require("../../auth/middleware/auth.middleware");
const auth_service_1 = require("../../auth/services/auth.service");
const manzana_entity_1 = require("../entities/manzana.entity");
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");
/**
* Crear router de manzanas
*/
function createManzanaController(dataSource) {
const router = (0, express_1.Router)();
// Repositorios
const manzanaRepository = dataSource.getRepository(manzana_entity_1.Manzana);
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);
// Servicios
const manzanaService = new manzana_service_1.ManzanaService(manzanaRepository);
const authService = new auth_service_1.AuthService(userRepository, tenantRepository, refreshTokenRepository);
const authMiddleware = new auth_middleware_1.AuthMiddleware(authService, dataSource);
/**
* GET /manzanas
* Listar manzanas
*/
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 page = parseInt(req.query.page) || 1;
const limit = Math.min(parseInt(req.query.limit) || 20, 100);
const search = req.query.search;
const etapaId = req.query.etapaId;
const result = await manzanaService.findAll({ tenantId, page, limit, search, etapaId });
res.status(200).json({
success: true,
data: result.items,
pagination: {
page,
limit,
total: result.total,
totalPages: Math.ceil(result.total / limit),
},
});
}
catch (error) {
next(error);
}
});
/**
* GET /manzanas/:id
* Obtener manzana por ID
*/
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 manzana = await manzanaService.findById(req.params.id, tenantId);
if (!manzana) {
res.status(404).json({ error: 'Not Found', message: 'Block not found' });
return;
}
res.status(200).json({ success: true, data: manzana });
}
catch (error) {
next(error);
}
});
/**
* POST /manzanas
* Crear manzana
*/
router.post('/', authMiddleware.authenticate, authMiddleware.authorize('admin', 'engineer', 'director'), async (req, res, next) => {
try {
const tenantId = req.tenantId;
if (!tenantId) {
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
return;
}
const dto = req.body;
if (!dto.etapaId || !dto.code) {
res.status(400).json({ error: 'Bad Request', message: 'etapaId and code are required' });
return;
}
const manzana = await manzanaService.create(tenantId, dto, req.user?.sub);
res.status(201).json({ success: true, data: manzana });
}
catch (error) {
if (error instanceof Error && error.message.includes('already exists')) {
res.status(409).json({ error: 'Conflict', message: error.message });
return;
}
next(error);
}
});
/**
* PATCH /manzanas/:id
* Actualizar manzana
*/
router.patch('/:id', authMiddleware.authenticate, authMiddleware.authorize('admin', 'engineer', 'director'), async (req, res, next) => {
try {
const tenantId = req.tenantId;
if (!tenantId) {
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
return;
}
const dto = req.body;
const manzana = await manzanaService.update(req.params.id, tenantId, dto, req.user?.sub);
res.status(200).json({ success: true, data: manzana });
}
catch (error) {
if (error instanceof Error) {
if (error.message === 'Block not found') {
res.status(404).json({ error: 'Not Found', message: error.message });
return;
}
if (error.message.includes('already exists')) {
res.status(409).json({ error: 'Conflict', message: error.message });
return;
}
}
next(error);
}
});
/**
* DELETE /manzanas/:id
* Eliminar manzana
*/
router.delete('/:id', authMiddleware.authenticate, authMiddleware.authorize('admin', 'director'), async (req, res, next) => {
try {
const tenantId = req.tenantId;
if (!tenantId) {
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
return;
}
await manzanaService.delete(req.params.id, tenantId, req.user?.sub);
res.status(200).json({ success: true, message: 'Block deleted' });
}
catch (error) {
if (error instanceof Error && error.message === 'Block not found') {
res.status(404).json({ error: 'Not Found', message: error.message });
return;
}
next(error);
}
});
return router;
}
exports.default = createManzanaController;
//# sourceMappingURL=manzana.controller.js.map