| | | 1 | | using FAU.Entidades; |
| | | 2 | | using FAU.Logica.DTOs; |
| | | 3 | | using FAU.Logica.Services; |
| | | 4 | | using Microsoft.AspNetCore.Authorization; |
| | | 5 | | using Microsoft.AspNetCore.Http; |
| | | 6 | | using Microsoft.AspNetCore.Mvc; |
| | | 7 | | |
| | | 8 | | namespace FAU.API.Controllers; |
| | | 9 | | |
| | | 10 | | [ApiController] |
| | | 11 | | [Route("api/novedades-periodo")] |
| | | 12 | | [Authorize(Roles = "Administrador,Supervisor,Usuario")] |
| | | 13 | | [Produces("application/json")] |
| | | 14 | | public class NovedadesPeriodoController : ControllerBase |
| | | 15 | | { |
| | | 16 | | private readonly INovedadPeriodoService _novedadPeriodoService; |
| | | 17 | | private readonly ILogger<NovedadesPeriodoController> _logger; |
| | | 18 | | |
| | 4 | 19 | | public NovedadesPeriodoController( |
| | 4 | 20 | | INovedadPeriodoService novedadPeriodoService, |
| | 4 | 21 | | ILogger<NovedadesPeriodoController> logger) |
| | | 22 | | { |
| | 4 | 23 | | _novedadPeriodoService = novedadPeriodoService; |
| | 4 | 24 | | _logger = logger; |
| | 4 | 25 | | } |
| | | 26 | | |
| | | 27 | | [HttpGet] |
| | | 28 | | [ProducesResponseType(typeof(ApiResponse<PagedResult<NovedadPeriodoDto>>), StatusCodes.Status200OK)] |
| | | 29 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 30 | | public async Task<IActionResult> GetNovedades( |
| | | 31 | | [FromQuery] int page = 1, |
| | | 32 | | [FromQuery] int pageSize = 10, |
| | | 33 | | [FromQuery] long? periodoId = null, |
| | | 34 | | [FromQuery] long? personaId = null, |
| | | 35 | | [FromQuery] string? tipo = null, |
| | | 36 | | [FromQuery] string? estado = null) |
| | | 37 | | { |
| | | 38 | | try |
| | | 39 | | { |
| | 0 | 40 | | var resultado = await _novedadPeriodoService.GetPaginatedAsync(page, pageSize, periodoId, personaId, tipo, e |
| | | 41 | | |
| | 0 | 42 | | return Ok(ApiResponse<PagedResult<NovedadPeriodoDto>>.SuccessResult(resultado)); |
| | | 43 | | } |
| | 0 | 44 | | catch (Exception ex) |
| | | 45 | | { |
| | 0 | 46 | | _logger.LogError(ex, "Error al obtener novedades de período"); |
| | 0 | 47 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 48 | | } |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | [HttpGet("{id}")] |
| | | 52 | | [ProducesResponseType(typeof(ApiResponse<NovedadPeriodoDto>), StatusCodes.Status200OK)] |
| | | 53 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 54 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 55 | | public async Task<IActionResult> GetNovedad(long id) |
| | | 56 | | { |
| | | 57 | | try |
| | | 58 | | { |
| | 0 | 59 | | var novedad = await _novedadPeriodoService.GetByIdAsync(id); |
| | 0 | 60 | | if (novedad == null) |
| | | 61 | | { |
| | 0 | 62 | | return NotFound(ApiResponse<string>.ErrorResult("Novedad de período no encontrada")); |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | var response = new NovedadPeriodoDto |
| | 0 | 66 | | { |
| | 0 | 67 | | Id = novedad.Id, |
| | 0 | 68 | | PeriodoId = novedad.PeriodoId, |
| | 0 | 69 | | PersonaId = novedad.PersonaId, |
| | 0 | 70 | | Cedula = novedad.Persona?.Cedula ?? string.Empty, |
| | 0 | 71 | | NombreCompleto = novedad.Persona?.NombreCompleto ?? string.Empty, |
| | 0 | 72 | | Tipo = novedad.Tipo, |
| | 0 | 73 | | DiasAfectados = novedad.DiasAfectados, |
| | 0 | 74 | | PorcentajeSubsidio = novedad.PorcentajeSubsidio, |
| | 0 | 75 | | FechaDesde = novedad.FechaDesde ?? DateTime.MinValue, |
| | 0 | 76 | | FechaHasta = novedad.FechaHasta, |
| | 0 | 77 | | Observaciones = novedad.Observaciones, |
| | 0 | 78 | | Estado = novedad.Estado, |
| | 0 | 79 | | CreadoPor = novedad.CreadoPor, |
| | 0 | 80 | | CreadoEn = novedad.CreadoEn, |
| | 0 | 81 | | ConfirmadoPor = novedad.ConfirmadoPor, |
| | 0 | 82 | | ConfirmadoEn = novedad.ConfirmadoEn |
| | 0 | 83 | | }; |
| | | 84 | | |
| | 0 | 85 | | return Ok(ApiResponse<NovedadPeriodoDto>.SuccessResult(response)); |
| | | 86 | | } |
| | 0 | 87 | | catch (Exception ex) |
| | | 88 | | { |
| | 0 | 89 | | _logger.LogError(ex, "Error al obtener novedad de período {Id}", id); |
| | 0 | 90 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 91 | | } |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | [HttpPost] |
| | | 95 | | [Authorize(Roles = "Administrador,Usuario")] |
| | | 96 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status201Created)] |
| | | 97 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 98 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 99 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 100 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 101 | | public async Task<IActionResult> CreateNovedad([FromBody] CrearNovedadPeriodoRequest request) |
| | | 102 | | { |
| | | 103 | | try |
| | | 104 | | { |
| | 2 | 105 | | var (success, novedad, error) = await _novedadPeriodoService.CreateAsync(request); |
| | 2 | 106 | | if (!success) |
| | | 107 | | { |
| | 1 | 108 | | if (error?.Contains("ya existe", StringComparison.OrdinalIgnoreCase) == true) |
| | | 109 | | { |
| | 1 | 110 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | | 111 | | } |
| | 0 | 112 | | if (error?.Contains("no encontrado", StringComparison.OrdinalIgnoreCase) == true || |
| | 0 | 113 | | error?.Contains("no existe", StringComparison.OrdinalIgnoreCase) == true) |
| | | 114 | | { |
| | 0 | 115 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al crear la novedad de período")); |
| | | 119 | | } |
| | | 120 | | |
| | 1 | 121 | | var response = new { id = novedad!.Id, creado = true }; |
| | 1 | 122 | | return CreatedAtAction(nameof(GetNovedad), new { id = novedad.Id }, ApiResponse<object>.SuccessResult(respon |
| | | 123 | | } |
| | 0 | 124 | | catch (Exception ex) |
| | | 125 | | { |
| | 0 | 126 | | _logger.LogError(ex, "Error al crear novedad de período"); |
| | 0 | 127 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 128 | | } |
| | 2 | 129 | | } |
| | | 130 | | |
| | | 131 | | [HttpPut("{id}")] |
| | | 132 | | [Authorize(Roles = "Administrador,Usuario")] |
| | | 133 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 134 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status400BadRequest)] |
| | | 135 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 136 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 137 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 138 | | public async Task<IActionResult> UpdateNovedad(long id, [FromBody] ActualizarNovedadPeriodoRequest request) |
| | | 139 | | { |
| | | 140 | | try |
| | | 141 | | { |
| | 0 | 142 | | var (success, novedad, error) = await _novedadPeriodoService.UpdateAsync(id, request); |
| | 0 | 143 | | if (!success) |
| | | 144 | | { |
| | 0 | 145 | | if (error?.Contains("no encontrada", StringComparison.OrdinalIgnoreCase) == true) |
| | | 146 | | { |
| | 0 | 147 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | | 148 | | } |
| | 0 | 149 | | if (error?.Contains("solo se pueden", StringComparison.OrdinalIgnoreCase) == true || |
| | 0 | 150 | | error?.Contains("debe proporcionar", StringComparison.OrdinalIgnoreCase) == true) |
| | | 151 | | { |
| | 0 | 152 | | return BadRequest(ApiResponse<string>.ErrorResult(error)); |
| | | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | return Ok(ApiResponse<object>.SuccessResult(new { actualizado = true }, "Novedad de período actualizada exit |
| | | 159 | | } |
| | 0 | 160 | | catch (Exception ex) |
| | | 161 | | { |
| | 0 | 162 | | _logger.LogError(ex, "Error al actualizar novedad de período {Id}", id); |
| | 0 | 163 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 164 | | } |
| | 0 | 165 | | } |
| | | 166 | | |
| | | 167 | | [HttpDelete("{id}")] |
| | | 168 | | [Authorize(Roles = "Administrador,Usuario")] |
| | | 169 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 170 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 171 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 172 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 173 | | public async Task<IActionResult> DeleteNovedad(long id) |
| | | 174 | | { |
| | | 175 | | try |
| | | 176 | | { |
| | 0 | 177 | | var (success, novedad, error) = await _novedadPeriodoService.DeleteAsync(id); |
| | 0 | 178 | | if (!success) |
| | | 179 | | { |
| | 0 | 180 | | if (error?.Contains("no encontrada", StringComparison.OrdinalIgnoreCase) == true) |
| | | 181 | | { |
| | 0 | 182 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | | 183 | | } |
| | 0 | 184 | | return Conflict(ApiResponse<string>.ErrorResult(error ?? "No se pudo eliminar la novedad de período")); |
| | | 185 | | } |
| | | 186 | | |
| | 0 | 187 | | return NoContent(); |
| | | 188 | | } |
| | 0 | 189 | | catch (Exception ex) |
| | | 190 | | { |
| | 0 | 191 | | _logger.LogError(ex, "Error al eliminar novedad de período {Id}", id); |
| | 0 | 192 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 193 | | } |
| | 0 | 194 | | } |
| | | 195 | | |
| | | 196 | | [HttpPost("{id}/confirmar")] |
| | | 197 | | [Authorize(Roles = "Administrador,Supervisor")] |
| | | 198 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 199 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 200 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 201 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 202 | | public async Task<IActionResult> ConfirmarNovedad(long id) |
| | | 203 | | { |
| | | 204 | | try |
| | | 205 | | { |
| | 1 | 206 | | var (success, novedad, error) = await _novedadPeriodoService.ChangeStateToConfirmedAsync(id); |
| | 1 | 207 | | if (!success) |
| | | 208 | | { |
| | 0 | 209 | | if (error?.Contains("no encontrada", StringComparison.OrdinalIgnoreCase) == true) |
| | | 210 | | { |
| | 0 | 211 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | | 212 | | } |
| | 0 | 213 | | return Conflict(ApiResponse<string>.ErrorResult(error ?? "No se pudo confirmar la novedad de período")); |
| | | 214 | | } |
| | | 215 | | |
| | 1 | 216 | | return Ok(ApiResponse<object>.SuccessResult(new { confirmado = true }, "Novedad de período confirmada exitos |
| | | 217 | | } |
| | 0 | 218 | | catch (Exception ex) |
| | | 219 | | { |
| | 0 | 220 | | _logger.LogError(ex, "Error al confirmar novedad de período {Id}", id); |
| | 0 | 221 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 222 | | } |
| | 1 | 223 | | } |
| | | 224 | | |
| | | 225 | | [HttpPost("{id}/anular")] |
| | | 226 | | [Authorize(Roles = "Administrador,Supervisor")] |
| | | 227 | | [ProducesResponseType(typeof(ApiResponse<object>), StatusCodes.Status200OK)] |
| | | 228 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status404NotFound)] |
| | | 229 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status409Conflict)] |
| | | 230 | | [ProducesResponseType(typeof(ApiResponse<string>), StatusCodes.Status500InternalServerError)] |
| | | 231 | | public async Task<IActionResult> AnularNovedad(long id) |
| | | 232 | | { |
| | | 233 | | try |
| | | 234 | | { |
| | 1 | 235 | | var (success, novedad, error) = await _novedadPeriodoService.AnularAsync(id); |
| | 1 | 236 | | if (!success) |
| | | 237 | | { |
| | 1 | 238 | | if (error?.Contains("no encontrada", StringComparison.OrdinalIgnoreCase) == true) |
| | | 239 | | { |
| | 0 | 240 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | | 241 | | } |
| | 1 | 242 | | return Conflict(ApiResponse<string>.ErrorResult(error ?? "No se pudo anular la novedad de período")); |
| | | 243 | | } |
| | | 244 | | |
| | 0 | 245 | | return Ok(ApiResponse<object>.SuccessResult(new { anulada = true }, "Novedad de período anulada exitosamente |
| | | 246 | | } |
| | 0 | 247 | | catch (Exception ex) |
| | | 248 | | { |
| | 0 | 249 | | _logger.LogError(ex, "Error al anular novedad de período {Id}", id); |
| | 0 | 250 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 251 | | } |
| | 1 | 252 | | } |
| | | 253 | | } |