| | | 1 | | using FAU.DataAccess.Repositories; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | using FAU.Logica.Exceptions; |
| | | 4 | | |
| | | 5 | | namespace FAU.Logica.Services; |
| | | 6 | | |
| | | 7 | | public class PeriodoGuard : IPeriodoGuard |
| | | 8 | | { |
| | | 9 | | private readonly IPeriodosRepository _periodosRepository; |
| | | 10 | | |
| | 6 | 11 | | public PeriodoGuard(IPeriodosRepository periodosRepository) |
| | | 12 | | { |
| | 6 | 13 | | _periodosRepository = periodosRepository; |
| | 6 | 14 | | } |
| | | 15 | | |
| | | 16 | | public async Task VerificarPeriodoAbiertoAsync() |
| | | 17 | | { |
| | 5 | 18 | | var periodo = await _periodosRepository.ObtenerEnCursoAsync(); |
| | 5 | 19 | | if (periodo is not null && periodo.Estado != Periodo.EstadoAbierta) |
| | | 20 | | { |
| | 3 | 21 | | throw new PeriodoBloqueadoException(periodo.Estado); |
| | | 22 | | } |
| | 2 | 23 | | } |
| | | 24 | | |
| | | 25 | | public async Task<DateTime> ObtenerPrimerDiaPeriodoAbiertoAsync() |
| | | 26 | | { |
| | 0 | 27 | | var periodo = await _periodosRepository.ObtenerEnCursoAsync(); |
| | 0 | 28 | | if (periodo is not null && periodo.Estado == Periodo.EstadoAbierta) |
| | 0 | 29 | | return new DateTime(periodo.Anio, periodo.Mes, 1); |
| | | 30 | | |
| | | 31 | | // Fallback: si no hay período abierto, usar el mes actual |
| | 0 | 32 | | return new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1); |
| | 0 | 33 | | } |
| | | 34 | | } |