Feat: Implementación de módulos ABM de suscripciones por cliente
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
// Archivo: GestionIntegral.Api/Controllers/Suscripciones/SuscripcionesController.cs
|
||||
|
||||
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/suscripciones")] // Ruta base para acciones sobre una suscripción específica
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class SuscripcionesController : ControllerBase
|
||||
{
|
||||
private readonly ISuscripcionService _suscripcionService;
|
||||
private readonly ILogger<SuscripcionesController> _logger;
|
||||
|
||||
// Permisos (nuevos, a crear en la BD)
|
||||
private const string PermisoGestionarSuscripciones = "SU005";
|
||||
|
||||
public SuscripcionesController(ISuscripcionService suscripcionService, ILogger<SuscripcionesController> logger)
|
||||
{
|
||||
_suscripcionService = suscripcionService;
|
||||
_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;
|
||||
_logger.LogWarning("No se pudo obtener el UserId del token JWT en SuscripcionesController.");
|
||||
return null;
|
||||
}
|
||||
|
||||
// Endpoint anidado para obtener las suscripciones de un suscriptor
|
||||
// GET: api/suscriptores/{idSuscriptor}/suscripciones
|
||||
[HttpGet("~/api/suscriptores/{idSuscriptor:int}/suscripciones")]
|
||||
public async Task<IActionResult> GetBySuscriptor(int idSuscriptor)
|
||||
{
|
||||
// Se podría usar el permiso de ver suscriptores (SU001) o el de gestionar suscripciones (SU005)
|
||||
if (!TienePermiso("SU001")) return Forbid();
|
||||
|
||||
var suscripciones = await _suscripcionService.ObtenerPorSuscriptorId(idSuscriptor);
|
||||
return Ok(suscripciones);
|
||||
}
|
||||
|
||||
// GET: api/suscripciones/{id}
|
||||
[HttpGet("{id:int}", Name = "GetSuscripcionById")]
|
||||
public async Task<IActionResult> GetById(int id)
|
||||
{
|
||||
if (!TienePermiso(PermisoGestionarSuscripciones)) return Forbid();
|
||||
var suscripcion = await _suscripcionService.ObtenerPorId(id);
|
||||
if (suscripcion == null) return NotFound();
|
||||
return Ok(suscripcion);
|
||||
}
|
||||
|
||||
// POST: api/suscripciones
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Create([FromBody] CreateSuscripcionDto createDto)
|
||||
{
|
||||
if (!TienePermiso(PermisoGestionarSuscripciones)) return Forbid();
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId == null) return Unauthorized();
|
||||
|
||||
var (dto, error) = await _suscripcionService.Crear(createDto, userId.Value);
|
||||
|
||||
if (error != null) return BadRequest(new { message = error });
|
||||
if (dto == null) return StatusCode(StatusCodes.Status500InternalServerError, "Error al crear la suscripción.");
|
||||
|
||||
return CreatedAtRoute("GetSuscripcionById", new { id = dto.IdSuscripcion }, dto);
|
||||
}
|
||||
|
||||
// PUT: api/suscripciones/{id}
|
||||
[HttpPut("{id:int}")]
|
||||
public async Task<IActionResult> Update(int id, [FromBody] UpdateSuscripcionDto updateDto)
|
||||
{
|
||||
if (!TienePermiso(PermisoGestionarSuscripciones)) return Forbid();
|
||||
if (!ModelState.IsValid) return BadRequest(ModelState);
|
||||
|
||||
var userId = GetCurrentUserId();
|
||||
if (userId == null) return Unauthorized();
|
||||
|
||||
var (exito, error) = await _suscripcionService.Actualizar(id, updateDto, userId.Value);
|
||||
|
||||
if (!exito)
|
||||
{
|
||||
if (error != null && error.Contains("no encontrada")) return NotFound(new { message = error });
|
||||
return BadRequest(new { message = error });
|
||||
}
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace GestionIntegral.Api.Dtos.Suscripciones
|
||||
{
|
||||
public class UpdateSuscripcionDto
|
||||
{
|
||||
// No permitimos cambiar el suscriptor o la publicación una vez creada.
|
||||
// Se debe cancelar y crear una nueva.
|
||||
|
||||
[Required]
|
||||
public DateTime FechaInicio { get; set; }
|
||||
|
||||
public DateTime? FechaFin { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Estado { get; set; } = string.Empty;
|
||||
|
||||
[Required(ErrorMessage = "Debe especificar los días de entrega.")]
|
||||
public List<string> DiasEntrega { get; set; } = new List<string>();
|
||||
|
||||
[StringLength(250)]
|
||||
public string? Observaciones { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -114,6 +114,7 @@ builder.Services.AddScoped<IPagoRepository, PagoRepository>();
|
||||
// Servicios
|
||||
builder.Services.AddScoped<IFormaPagoService, FormaPagoService>();
|
||||
builder.Services.AddScoped<ISuscriptorService, SuscriptorService>();
|
||||
builder.Services.AddScoped<ISuscripcionService, SuscripcionService>();
|
||||
|
||||
// --- SERVICIO DE HEALTH CHECKS ---
|
||||
// Añadimos una comprobación específica para SQL Server.
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using GestionIntegral.Api.Dtos.Suscripciones;
|
||||
|
||||
namespace GestionIntegral.Api.Services.Suscripciones
|
||||
{
|
||||
public interface ISuscripcionService
|
||||
{
|
||||
Task<IEnumerable<SuscripcionDto>> ObtenerPorSuscriptorId(int idSuscriptor);
|
||||
Task<SuscripcionDto?> ObtenerPorId(int idSuscripcion);
|
||||
Task<(SuscripcionDto? Suscripcion, string? Error)> Crear(CreateSuscripcionDto createDto, int idUsuario);
|
||||
Task<(bool Exito, string? Error)> Actualizar(int idSuscripcion, UpdateSuscripcionDto updateDto, int idUsuario);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
using GestionIntegral.Api.Data;
|
||||
using GestionIntegral.Api.Data.Repositories.Distribucion;
|
||||
using GestionIntegral.Api.Data.Repositories.Suscripciones;
|
||||
using GestionIntegral.Api.Dtos.Suscripciones;
|
||||
using GestionIntegral.Api.Models.Suscripciones;
|
||||
using System.Data;
|
||||
|
||||
namespace GestionIntegral.Api.Services.Suscripciones
|
||||
{
|
||||
public class SuscripcionService : ISuscripcionService
|
||||
{
|
||||
private readonly ISuscripcionRepository _suscripcionRepository;
|
||||
private readonly ISuscriptorRepository _suscriptorRepository;
|
||||
private readonly IPublicacionRepository _publicacionRepository;
|
||||
private readonly DbConnectionFactory _connectionFactory;
|
||||
private readonly ILogger<SuscripcionService> _logger;
|
||||
|
||||
public SuscripcionService(
|
||||
ISuscripcionRepository suscripcionRepository,
|
||||
ISuscriptorRepository suscriptorRepository,
|
||||
IPublicacionRepository publicacionRepository,
|
||||
DbConnectionFactory connectionFactory,
|
||||
ILogger<SuscripcionService> logger)
|
||||
{
|
||||
_suscripcionRepository = suscripcionRepository;
|
||||
_suscriptorRepository = suscriptorRepository;
|
||||
_publicacionRepository = publicacionRepository;
|
||||
_connectionFactory = connectionFactory;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
private async Task<SuscripcionDto?> MapToDto(Suscripcion suscripcion)
|
||||
{
|
||||
if (suscripcion == null) return null;
|
||||
var publicacion = await _publicacionRepository.GetByIdSimpleAsync(suscripcion.IdPublicacion);
|
||||
return new SuscripcionDto
|
||||
{
|
||||
IdSuscripcion = suscripcion.IdSuscripcion,
|
||||
IdSuscriptor = suscripcion.IdSuscriptor,
|
||||
IdPublicacion = suscripcion.IdPublicacion,
|
||||
NombrePublicacion = publicacion?.Nombre ?? "Desconocida",
|
||||
FechaInicio = suscripcion.FechaInicio.ToString("yyyy-MM-dd"),
|
||||
FechaFin = suscripcion.FechaFin?.ToString("yyyy-MM-dd"),
|
||||
Estado = suscripcion.Estado,
|
||||
DiasEntrega = suscripcion.DiasEntrega,
|
||||
Observaciones = suscripcion.Observaciones
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<SuscripcionDto?> ObtenerPorId(int idSuscripcion)
|
||||
{
|
||||
var suscripcion = await _suscripcionRepository.GetByIdAsync(idSuscripcion);
|
||||
if (suscripcion == null)
|
||||
return null;
|
||||
return await MapToDto(suscripcion);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<SuscripcionDto>> ObtenerPorSuscriptorId(int idSuscriptor)
|
||||
{
|
||||
var suscripciones = await _suscripcionRepository.GetBySuscriptorIdAsync(idSuscriptor);
|
||||
var dtosTasks = suscripciones.Select(s => MapToDto(s));
|
||||
var dtos = await Task.WhenAll(dtosTasks);
|
||||
return dtos.Where(dto => dto != null)!;
|
||||
}
|
||||
|
||||
public async Task<(SuscripcionDto? Suscripcion, string? Error)> Crear(CreateSuscripcionDto createDto, int idUsuario)
|
||||
{
|
||||
if (await _suscriptorRepository.GetByIdAsync(createDto.IdSuscriptor) == null)
|
||||
return (null, "El suscriptor no existe.");
|
||||
if (await _publicacionRepository.GetByIdSimpleAsync(createDto.IdPublicacion) == null)
|
||||
return (null, "La publicación no existe.");
|
||||
if (createDto.FechaFin.HasValue && createDto.FechaFin.Value < createDto.FechaInicio)
|
||||
return (null, "La fecha de fin no puede ser anterior a la fecha de inicio.");
|
||||
|
||||
var nuevaSuscripcion = new Suscripcion
|
||||
{
|
||||
IdSuscriptor = createDto.IdSuscriptor,
|
||||
IdPublicacion = createDto.IdPublicacion,
|
||||
FechaInicio = createDto.FechaInicio,
|
||||
FechaFin = createDto.FechaFin,
|
||||
Estado = createDto.Estado,
|
||||
DiasEntrega = string.Join(",", createDto.DiasEntrega),
|
||||
Observaciones = createDto.Observaciones,
|
||||
IdUsuarioAlta = idUsuario
|
||||
};
|
||||
|
||||
using var connection = _connectionFactory.CreateConnection();
|
||||
await (connection as System.Data.Common.DbConnection)!.OpenAsync();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var creada = await _suscripcionRepository.CreateAsync(nuevaSuscripcion, transaction);
|
||||
if (creada == null) throw new DataException("Error al crear la suscripción.");
|
||||
|
||||
transaction.Commit();
|
||||
_logger.LogInformation("Suscripción ID {Id} creada por Usuario ID {UserId}.", creada.IdSuscripcion, idUsuario);
|
||||
return (await MapToDto(creada), null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
try { transaction.Rollback(); } catch { }
|
||||
_logger.LogError(ex, "Error al crear suscripción para suscriptor ID {IdSuscriptor}", createDto.IdSuscriptor);
|
||||
return (null, $"Error interno: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(bool Exito, string? Error)> Actualizar(int idSuscripcion, UpdateSuscripcionDto updateDto, int idUsuario)
|
||||
{
|
||||
var existente = await _suscripcionRepository.GetByIdAsync(idSuscripcion);
|
||||
if (existente == null) return (false, "Suscripción no encontrada.");
|
||||
|
||||
if (updateDto.FechaFin.HasValue && updateDto.FechaFin.Value < updateDto.FechaInicio)
|
||||
return (false, "La fecha de fin no puede ser anterior a la fecha de inicio.");
|
||||
|
||||
existente.FechaInicio = updateDto.FechaInicio;
|
||||
existente.FechaFin = updateDto.FechaFin;
|
||||
existente.Estado = updateDto.Estado;
|
||||
existente.DiasEntrega = string.Join(",", updateDto.DiasEntrega);
|
||||
existente.Observaciones = updateDto.Observaciones;
|
||||
existente.IdUsuarioMod = idUsuario;
|
||||
existente.FechaMod = DateTime.Now;
|
||||
|
||||
using var connection = _connectionFactory.CreateConnection();
|
||||
await (connection as System.Data.Common.DbConnection)!.OpenAsync();
|
||||
using var transaction = connection.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var actualizado = await _suscripcionRepository.UpdateAsync(existente, transaction);
|
||||
if (!actualizado) throw new DataException("Error al actualizar la suscripción.");
|
||||
|
||||
transaction.Commit();
|
||||
_logger.LogInformation("Suscripción ID {Id} actualizada por Usuario ID {UserId}.", idSuscripcion, idUsuario);
|
||||
return (true, null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
try { transaction.Rollback(); } catch { }
|
||||
_logger.LogError(ex, "Error al actualizar suscripción ID: {IdSuscripcion}", idSuscripcion);
|
||||
return (false, $"Error interno: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user