69 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
		
		
			
		
	
	
			69 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
|  | using GestionIntegral.Api.Dtos.Suscripciones; | ||
|  | using GestionIntegral.Api.Services.Suscripciones; | ||
|  | using Microsoft.AspNetCore.Authorization; | ||
|  | using Microsoft.AspNetCore.Mvc; | ||
|  | using System.Security.Claims; | ||
|  | 
 | ||
|  | namespace GestionIntegral.Api.Controllers.Suscripciones | ||
|  | { | ||
|  |     [Route("api/pagos")] | ||
|  |     [ApiController] | ||
|  |     [Authorize] | ||
|  |     public class PagosController : ControllerBase | ||
|  |     { | ||
|  |         private readonly IPagoService _pagoService; | ||
|  |         private readonly ILogger<PagosController> _logger; | ||
|  | 
 | ||
|  |         // Permiso para registrar pagos manuales (a crear en BD) | ||
|  |         private const string PermisoRegistrarPago = "SU008"; | ||
|  | 
 | ||
|  |         public PagosController(IPagoService pagoService, ILogger<PagosController> logger) | ||
|  |         { | ||
|  |             _pagoService = pagoService; | ||
|  |             _logger = logger; | ||
|  |         } | ||
|  | 
 | ||
|  |         private bool TienePermiso(string codAcc) => User.IsInRole("SuperAdmin") || User.HasClaim(c => c.Type == "permission" && c.Value == codAcc); | ||
|  |          | ||
|  |         private int? GetCurrentUserId() | ||
|  |         { | ||
|  |             if (int.TryParse(User.FindFirstValue(ClaimTypes.NameIdentifier) ?? User.FindFirstValue("sub"), out int userId)) return userId; | ||
|  |             return null; | ||
|  |         } | ||
|  | 
 | ||
|  |         // GET: api/facturas/{idFactura}/pagos | ||
|  |         [HttpGet("~/api/facturas/{idFactura:int}/pagos")] | ||
|  |         [ProducesResponseType(typeof(IEnumerable<PagoDto>), StatusCodes.Status200OK)] | ||
|  |         [ProducesResponseType(StatusCodes.Status403Forbidden)] | ||
|  |         public async Task<IActionResult> GetPagosPorFactura(int idFactura) | ||
|  |         { | ||
|  |             // Se podría usar un permiso de "Ver Facturación" | ||
|  |             if (!TienePermiso("SU006")) return Forbid(); | ||
|  | 
 | ||
|  |             var pagos = await _pagoService.ObtenerPagosPorFacturaId(idFactura); | ||
|  |             return Ok(pagos); | ||
|  |         } | ||
|  | 
 | ||
|  |         // POST: api/pagos | ||
|  |         [HttpPost] | ||
|  |         [ProducesResponseType(typeof(PagoDto), StatusCodes.Status201Created)] | ||
|  |         [ProducesResponseType(StatusCodes.Status400BadRequest)] | ||
|  |         [ProducesResponseType(StatusCodes.Status403Forbidden)] | ||
|  |         public async Task<IActionResult> RegistrarPago([FromBody] CreatePagoDto createDto) | ||
|  |         { | ||
|  |             if (!TienePermiso(PermisoRegistrarPago)) return Forbid(); | ||
|  |             if (!ModelState.IsValid) return BadRequest(ModelState); | ||
|  | 
 | ||
|  |             var userId = GetCurrentUserId(); | ||
|  |             if (userId == null) return Unauthorized(); | ||
|  | 
 | ||
|  |             var (dto, error) = await _pagoService.RegistrarPagoManual(createDto, userId.Value); | ||
|  | 
 | ||
|  |             if (error != null) return BadRequest(new { message = error }); | ||
|  |             if (dto == null) return StatusCode(StatusCodes.Status500InternalServerError, "Error al registrar el pago."); | ||
|  | 
 | ||
|  |             // No tenemos un "GetById" para pagos, así que devolvemos el objeto con un 201. | ||
|  |             return StatusCode(201, dto); | ||
|  |         } | ||
|  |     } | ||
|  | } |