259 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			259 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| // src/Controllers/EmpresasController.cs
 | |
| 
 | |
| using GestionIntegral.Api.Dtos.Empresas;     // Para los DTOs (EmpresaDto, CreateEmpresaDto, UpdateEmpresaDto)
 | |
| using GestionIntegral.Api.Services.Distribucion; // Para IEmpresaService
 | |
| using Microsoft.AspNetCore.Authorization;    // Para [Authorize]
 | |
| using Microsoft.AspNetCore.Mvc;
 | |
| using System.Security.Claims;                 // Para obtener el IdUsuario del token
 | |
| using Microsoft.Extensions.Logging;           // Para ILogger
 | |
| 
 | |
| namespace GestionIntegral.Api.Controllers // Ajusta el namespace si es necesario
 | |
| {
 | |
|     [Route("api/[controller]")] // Ruta base: /api/empresas
 | |
|     [ApiController]
 | |
|     [Authorize] // Proteger todos los endpoints por defecto, requiere autenticación
 | |
|     public class EmpresasController : ControllerBase
 | |
|     {
 | |
|         private readonly IEmpresaService _empresaService;
 | |
|         private readonly ILogger<EmpresasController> _logger;
 | |
| 
 | |
|         public EmpresasController(IEmpresaService empresaService, ILogger<EmpresasController> logger)
 | |
|         {
 | |
|             _empresaService = empresaService ?? throw new ArgumentNullException(nameof(empresaService));
 | |
|             _logger = logger ?? throw new ArgumentNullException(nameof(logger));
 | |
|         }
 | |
| 
 | |
|         // --- Helper para verificar permisos ---
 | |
|         // (Similar al de ZonasController)
 | |
|         private bool TienePermiso(string codAccRequerido)
 | |
|         {
 | |
|             // SuperAdmin siempre tiene permiso
 | |
|             if (User.IsInRole("SuperAdmin")) return true;
 | |
|             // Busca si el claim 'permission' con el valor requerido existe
 | |
|             return User.HasClaim(c => c.Type == "permission" && c.Value == codAccRequerido);
 | |
|         }
 | |
| 
 | |
|         // --- Helper para obtener User ID ---
 | |
|         // (Similar al de otros controladores)
 | |
|         private int? GetCurrentUserId()
 | |
|         {
 | |
|             var userIdClaim = User.FindFirstValue(ClaimTypes.NameIdentifier) ?? User.FindFirstValue("sub");
 | |
|             if (int.TryParse(userIdClaim, out int userId))
 | |
|             {
 | |
|                 return userId;
 | |
|             }
 | |
|             _logger.LogWarning("No se pudo obtener el UserId del token JWT en EmpresasController.");
 | |
|             return null;
 | |
|         }
 | |
| 
 | |
|         // --- Endpoints CRUD ---
 | |
| 
 | |
|         // GET: api/empresas
 | |
|         // Permiso Requerido: DE001 (Ver Empresas)
 | |
|         [HttpGet]
 | |
|         [ProducesResponseType(typeof(IEnumerable<EmpresaDto>), StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         public async Task<IActionResult> GetAllEmpresas([FromQuery] string? nombre, [FromQuery] string? detalle)
 | |
|         {
 | |
|             if (!TienePermiso("DE001"))
 | |
|             {
 | |
|                 _logger.LogWarning("Acceso denegado a GetAllEmpresas para el usuario {UserId}", GetCurrentUserId() ?? 0);
 | |
|                 return Forbid(); // 403 Forbidden
 | |
|             }
 | |
| 
 | |
|             try
 | |
|             {
 | |
|                 var empresas = await _empresaService.ObtenerTodasAsync(nombre, detalle);
 | |
|                 return Ok(empresas);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 _logger.LogError(ex, "Error al obtener todas las Empresas. Filtros: Nombre={Nombre}, Detalle={Detalle}", nombre, detalle);
 | |
|                 return StatusCode(StatusCodes.Status500InternalServerError, "Error interno al obtener las empresas.");
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         // GET: api/empresas/dropdown
 | |
|         [HttpGet("dropdown")]
 | |
|         [ProducesResponseType(typeof(IEnumerable<EmpresaDropdownDto>), StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         public async Task<IActionResult> GetEmpresasDropdown()
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 var empresas = await _empresaService.ObtenerParaDropdown();
 | |
|                 return Ok(empresas);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 _logger.LogError(ex, "Error al obtener todas las Empresas.");
 | |
|                 return StatusCode(StatusCodes.Status500InternalServerError, "Error interno al obtener las empresas.");
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         // GET: api/empresas/{id}
 | |
|         // Permiso Requerido: DE001 (Ver Empresas)
 | |
|         [HttpGet("{id:int}", Name = "GetEmpresaById")]
 | |
|         [ProducesResponseType(typeof(EmpresaDto), StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         [ProducesResponseType(StatusCodes.Status404NotFound)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         public async Task<IActionResult> GetEmpresaById(int id)
 | |
|         {
 | |
|             if (!TienePermiso("DE001")) return Forbid();
 | |
| 
 | |
|             try
 | |
|             {
 | |
|                 var empresa = await _empresaService.ObtenerPorIdAsync(id);
 | |
|                 if (empresa == null)
 | |
|                 {
 | |
|                     return NotFound(new { message = $"Empresa con ID {id} no encontrada." });
 | |
|                 }
 | |
|                 return Ok(empresa);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 _logger.LogError(ex, "Error al obtener Empresa por ID: {Id}", id);
 | |
|                 return StatusCode(StatusCodes.Status500InternalServerError, "Error interno al obtener la empresa.");
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         [HttpGet("{id:int}/lookup", Name = "GetEmpresaLookupById")]
 | |
|         [ProducesResponseType(typeof(EmpresaDto), StatusCodes.Status200OK)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         [ProducesResponseType(StatusCodes.Status404NotFound)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         public async Task<IActionResult> ObtenerLookupPorIdAsync(int id)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 var empresa = await _empresaService.ObtenerLookupPorIdAsync(id);
 | |
|                 if (empresa == null)
 | |
|                 {
 | |
|                     return NotFound(new { message = $"Empresa con ID {id} no encontrada." });
 | |
|                 }
 | |
|                 return Ok(empresa);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 _logger.LogError(ex, "Error al obtener Empresa por ID: {Id}", id);
 | |
|                 return StatusCode(StatusCodes.Status500InternalServerError, "Error interno al obtener la empresa.");
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         // POST: api/empresas
 | |
|         // Permiso Requerido: DE002 (Agregar Empresas)
 | |
|         [HttpPost]
 | |
|         [ProducesResponseType(typeof(EmpresaDto), StatusCodes.Status201Created)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)] // Por validación DTO o de negocio
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         public async Task<IActionResult> CreateEmpresa([FromBody] CreateEmpresaDto createDto)
 | |
|         {
 | |
|             if (!TienePermiso("DE002")) return Forbid();
 | |
| 
 | |
|             if (!ModelState.IsValid) return BadRequest(ModelState);
 | |
| 
 | |
|             var idUsuario = GetCurrentUserId();
 | |
|             if (idUsuario == null) return Unauthorized("No se pudo obtener el ID del usuario del token.");
 | |
| 
 | |
|             try
 | |
|             {
 | |
|                 var (empresaCreada, error) = await _empresaService.CrearAsync(createDto, idUsuario.Value);
 | |
| 
 | |
|                 if (error != null)
 | |
|                 {
 | |
|                     // Error de lógica de negocio (ej: nombre duplicado)
 | |
|                     return BadRequest(new { message = error });
 | |
|                 }
 | |
|                 if (empresaCreada == null) // Fallo inesperado en el servicio
 | |
|                 {
 | |
|                     return StatusCode(StatusCodes.Status500InternalServerError, "Error interno al crear la empresa.");
 | |
|                 }
 | |
| 
 | |
|                 // Devuelve 201 Created con la ubicación del nuevo recurso y el recurso mismo
 | |
|                 return CreatedAtRoute("GetEmpresaById", new { id = empresaCreada.IdEmpresa }, empresaCreada);
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 _logger.LogError(ex, "Error al crear Empresa. Nombre: {Nombre} por Usuario ID: {UsuarioId}", createDto.Nombre, idUsuario);
 | |
|                 return StatusCode(StatusCodes.Status500InternalServerError, "Error interno al procesar la creación de la empresa.");
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         // PUT: api/empresas/{id}
 | |
|         // Permiso Requerido: DE003 (Editar Empresas)
 | |
|         [HttpPut("{id:int}")]
 | |
|         [ProducesResponseType(StatusCodes.Status204NoContent)] // Éxito sin contenido
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)]
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         [ProducesResponseType(StatusCodes.Status404NotFound)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         public async Task<IActionResult> UpdateEmpresa(int id, [FromBody] UpdateEmpresaDto updateDto)
 | |
|         {
 | |
|             if (!TienePermiso("DE003")) return Forbid();
 | |
| 
 | |
|             if (!ModelState.IsValid) return BadRequest(ModelState);
 | |
| 
 | |
|             var idUsuario = GetCurrentUserId();
 | |
|             if (idUsuario == null) return Unauthorized("No se pudo obtener el ID del usuario del token.");
 | |
| 
 | |
|             try
 | |
|             {
 | |
|                 var (exito, error) = await _empresaService.ActualizarAsync(id, updateDto, idUsuario.Value);
 | |
| 
 | |
|                 if (!exito)
 | |
|                 {
 | |
|                     if (error == "Empresa no encontrada.") return NotFound(new { message = error });
 | |
|                     // Otro error de lógica de negocio (ej: nombre duplicado)
 | |
|                     return BadRequest(new { message = error });
 | |
|                 }
 | |
|                 return NoContent(); // Éxito
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 _logger.LogError(ex, "Error al actualizar Empresa ID: {Id} por Usuario ID: {UsuarioId}", id, idUsuario);
 | |
|                 return StatusCode(StatusCodes.Status500InternalServerError, "Error interno al actualizar la empresa.");
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         // DELETE: api/empresas/{id}
 | |
|         // Permiso Requerido: DE004 (Eliminar Empresas)
 | |
|         [HttpDelete("{id:int}")]
 | |
|         [ProducesResponseType(StatusCodes.Status204NoContent)]
 | |
|         [ProducesResponseType(StatusCodes.Status400BadRequest)] // Si está en uso
 | |
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)]
 | |
|         [ProducesResponseType(StatusCodes.Status403Forbidden)]
 | |
|         [ProducesResponseType(StatusCodes.Status404NotFound)]
 | |
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)]
 | |
|         public async Task<IActionResult> DeleteEmpresa(int id)
 | |
|         {
 | |
|             if (!TienePermiso("DE004")) return Forbid();
 | |
| 
 | |
|             var idUsuario = GetCurrentUserId();
 | |
|             if (idUsuario == null) return Unauthorized("No se pudo obtener el ID del usuario del token.");
 | |
| 
 | |
|             try
 | |
|             {
 | |
|                 var (exito, error) = await _empresaService.EliminarAsync(id, idUsuario.Value);
 | |
| 
 | |
|                 if (!exito)
 | |
|                 {
 | |
|                     if (error == "Empresa no encontrada.") return NotFound(new { message = error });
 | |
|                     // Otro error de lógica de negocio (ej: "En uso")
 | |
|                     return BadRequest(new { message = error });
 | |
|                 }
 | |
|                 return NoContent(); // Éxito
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 _logger.LogError(ex, "Error al eliminar Empresa ID: {Id} por Usuario ID: {UsuarioId}", id, idUsuario);
 | |
|                 return StatusCode(StatusCodes.Status500InternalServerError, "Error interno al eliminar la empresa.");
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| } |