Esempio n. 1
0
        public async Task <IActionResult> ValidatePresence(
            [FromBody] BookingValidationDeserializer bookingValidation,
            [FromServices] IUserServices userServices)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (!CurrentUser.Role.Name.Equals("Admin") &&
                !userServices.IsModeratorFor(bookingValidation.EventId,
                                             CurrentUser.Id))
            {
                return(BadRequest("Not Allowed to moderate this event"));
            }

            var query = BookingServices.WithUserAndEvent(Context.Booking);

            Booking book =
                await BookingServices.GetByIdsAsync(bookingValidation.UserId, bookingValidation.EventId, query);

            if (book == null)
            {
                return(NotFound("Booking not found"));
            }
            else if (!book.Event.OnGoingWindow() ||
                     !book.Event.Status.Equals(Status.ONGOING))
            {
                return(BadRequest("Can't validate presence outside of open window"));
            }
            else if (book.Present)
            {
                return(BadRequest("Presence Already validated"));
            }
            else if (book.Event.ValidationRequired && (bool)!book.Validated)
            {
                return(BadRequest("User hasn't been validated"));
            }

            // VALIDATE THE PRESENCE
            book.Present = true;

            // CREATE THE PointJury ASSOCIATED
            if (book.Event.JuryPoint != null)
            {
                BookingServices.CreateJuryPoint((float)book.Event.JuryPoint, bookingValidation.UserId);
            }

            try {
                Context.Booking.Update(book);
                await Context.SaveChangesAsync();

                EmailServices.SendFor(book.User, book.Event,
                                      BookingTemplate.PRESENT);
            } catch (DbUpdateException e) {
                return(BadRequest(e.InnerException.Message));
            }

            return(NoContent());
        }