public async Task <IActionResult> ActiveReservation( [FromBody] ActiveReservationDto activeReservationDto ) { try { var handler = new JwtSecurityTokenHandler(); string authHeader = Request.Headers["Authorization"]; authHeader = authHeader.Replace("Bearer ", ""); var jsonToken = handler.ReadToken(authHeader); var tokenS = handler.ReadToken(authHeader) as JwtSecurityToken; var nameId = int.Parse(tokenS.Claims.First(claim => claim.Type == "nameid").Value); var reservation = await _reservationService.ActiveReservation(activeReservationDto); return(Ok(reservation)); } catch (Exception e) { return(HttpExceptionMapper.ToHttpActionResult(e)); } }
public async Task <ReservationResponseDto> ActiveReservation(ActiveReservationDto activeReservationDto) { // Se valida que exista la reserva var reservation = await _reservationRepository.FindOneByIdAsync(activeReservationDto.ReservationId); if (reservation == null) { throw new BadRequestException( $"Reservation with id {activeReservationDto.ReservationId} was not found"); } // Se valida que la reserva no pueda ser activada por segunda vez if (reservation.ReservationStateId == ReservationStates.Active) { throw new BadRequestException($"Reservation with id {reservation.Id} is already active"); } // Se valida que el activador exista var activator = await _userRepository.FindOneByStudentCodeAsync(activeReservationDto.ActivatorCode); if (activator == null) { throw new BadRequestException($"User with id {activeReservationDto.ActivatorCode} was not found"); } /* TODO: Se debe preguntar a los bibliotecólogos cuantos * cubiculos puede activar una persona por día [activador] */ // Se valida que el activador no se encuentre en otra reserva var temp = reservation.StartTime; var startTime = new DateTime(temp.Year, temp.Month, temp.Day); var isActivatorInAnotherReservation = await _userReservationRepository.IsActivatorInReservation( activator.Id, startTime, startTime.AddDays(1)); if (isActivatorInAnotherReservation) { throw new BadRequestException($"User with id {activator.Id} has already active in another reservation"); } // Se valida que el activador no sea el mismo host var host = await _userReservationRepository.GetHostByReservationId(reservation.Id); if (host == null) { throw new BadRequestException("Host must be present in reservation"); } if (host.Id == activator.Id) { throw new BadRequestException("Host cannot be activator"); } // TODO: Agregar timer para que se cambie el estado de la reserva // y agregar validacion por estado // Se valida que la reserva no se pueda activar hasta su hora de inicio // TODO: uncomment // if (DateTime.Now < reservation.StartTime) // throw new BadRequestException($"Reservation cannot be activated until {reservation.StartTime}"); // Se valida que la reserva no haya expirado // TODO: uncomment // var reservationStartTimeWithFiveMinutesAdded = reservation.StartTime.AddMinutes(5); // if (reservationStartTimeWithFiveMinutesAdded < DateTime.Now) // throw new BadRequestException( // $"Reservation time for activation has expired, your reservation has been canceled"); // Se actualiza la información de la reserva reservation.UpdatedAt = DateTime.Now; reservation.ReservationStateId = ReservationStates.Active; // Se coloca el activador como parte de la reserva var userReservations = new List <UserReservation>(); userReservations.Add(new UserReservation { UserId = activator.Id, UserRoleId = UserReservationRoles.Activator, IsActive = true, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now }); reservation.UserReservations = userReservations; // Se actualiza la reserva await _reservationRepository.UpdateOneAsync(reservation); var cubicle = await _cubicleRepository.FindOneByIdAsync(reservation.CubicleId); return(new ReservationResponseDto { CubicleCode = cubicle.Code, CubicleDescription = cubicle.Description, StartTime = reservation.StartTime, EndTime = reservation.EndTime, CubicleTotalSeats = cubicle.TotalSeats }); }