Search in sources :

Example 1 with DuplicateBookingException

use of uk.ac.cam.cl.dtg.isaac.api.managers.DuplicateBookingException in project isaac-api by isaacphysics.

the class EventsFacade method addMeToWaitingList.

/**
 * Add current user to waiting list for the given event.
 *
 * @param request
 *            - so we can determine if the user is logged in
 * @param eventId
 *            - event id
 * @return the new booking
 */
@POST
@Path("{event_id}/waiting_list")
@Produces(MediaType.APPLICATION_JSON)
@GZIP
@ApiOperation(value = "Add the current user to an event waiting list.")
public final Response addMeToWaitingList(@Context final HttpServletRequest request, @PathParam("event_id") final String eventId, final Map<String, String> additionalInformation) {
    try {
        RegisteredUserDTO user = userManager.getCurrentRegisteredUser(request);
        IsaacEventPageDTO event = this.getAugmentedEventDTOById(request, eventId);
        EventBookingDTO eventBookingDTO = bookingManager.requestWaitingListBooking(event, user, additionalInformation);
        this.getLogManager().logEvent(userManager.getCurrentUser(request), request, SegueServerLogType.EVENT_WAITING_LIST_BOOKING, ImmutableMap.of(EVENT_ID_FKEY_FIELDNAME, event.getId()));
        return Response.ok(this.mapper.map(eventBookingDTO, EventBookingDTO.class)).build();
    } catch (NoUserLoggedInException e) {
        return SegueErrorResponse.getNotLoggedInResponse();
    } catch (SegueDatabaseException e) {
        String errorMsg = "Database error occurred while trying to book a user onto an event.";
        log.error(errorMsg, e);
        return new SegueErrorResponse(Status.INTERNAL_SERVER_ERROR, errorMsg).toResponse();
    } catch (ContentManagerException e) {
        return new SegueErrorResponse(Status.INTERNAL_SERVER_ERROR, "Content Database error occurred while trying to retrieve all event booking information.").toResponse();
    } catch (EmailMustBeVerifiedException e) {
        return new SegueErrorResponse(Status.BAD_REQUEST, "In order to book on this event your user account must have a verified email address. Please verify your address to make a booking.").toResponse();
    } catch (DuplicateBookingException e) {
        return new SegueErrorResponse(Status.BAD_REQUEST, "You have already been booked on this event. Unable to create a duplicate booking.").toResponse();
    } catch (EventDeadlineException e) {
        return new SegueErrorResponse(Status.BAD_REQUEST, "The booking deadline for this event has passed. No more bookings are being accepted.").toResponse();
    } catch (EventIsNotFullException e) {
        return new SegueErrorResponse(Status.CONFLICT, "There are spaces on this event and the deadline has not passed. Please use the request booking endpoint to book you on to it.").toResponse();
    }
}
Also used : EventIsNotFullException(uk.ac.cam.cl.dtg.isaac.api.managers.EventIsNotFullException) SegueErrorResponse(uk.ac.cam.cl.dtg.isaac.dto.SegueErrorResponse) DuplicateBookingException(uk.ac.cam.cl.dtg.isaac.api.managers.DuplicateBookingException) RegisteredUserDTO(uk.ac.cam.cl.dtg.isaac.dto.users.RegisteredUserDTO) EventDeadlineException(uk.ac.cam.cl.dtg.isaac.api.managers.EventDeadlineException) SegueDatabaseException(uk.ac.cam.cl.dtg.segue.dao.SegueDatabaseException) IsaacEventPageDTO(uk.ac.cam.cl.dtg.isaac.dto.IsaacEventPageDTO) EventBookingDTO(uk.ac.cam.cl.dtg.isaac.dto.eventbookings.EventBookingDTO) DetailedEventBookingDTO(uk.ac.cam.cl.dtg.isaac.dto.eventbookings.DetailedEventBookingDTO) EmailMustBeVerifiedException(uk.ac.cam.cl.dtg.segue.comm.EmailMustBeVerifiedException) ContentManagerException(uk.ac.cam.cl.dtg.segue.dao.content.ContentManagerException) NoUserLoggedInException(uk.ac.cam.cl.dtg.segue.auth.exceptions.NoUserLoggedInException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 2 with DuplicateBookingException

use of uk.ac.cam.cl.dtg.isaac.api.managers.DuplicateBookingException in project isaac-api by isaacphysics.

the class EventsFacade method createReservationsForGivenUsers.

/**
 * Add event reservations for the given users.
 *
 * @param request
 *            - so we can determine who is making the request
 * @param eventId
 *            - event id
 * @param userIds
 *            - the users to reserve spaces for
 * @return the list of bookings/reservations
 */
@POST
@Path("{event_id}/reservations")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Add event reservations for the given users.")
public final Response createReservationsForGivenUsers(@Context final HttpServletRequest request, @PathParam("event_id") final String eventId, final List<Long> userIds) {
    RegisteredUserDTO reservingUser;
    IsaacEventPageDTO event;
    try {
        event = this.getRawEventDTOById(eventId);
    } catch (SegueDatabaseException | ContentManagerException e) {
        event = null;
    }
    if (null == event) {
        return new SegueErrorResponse(Status.BAD_REQUEST, "No event found with this ID.").toResponse();
    }
    if (!EventBookingManager.eventAllowsGroupBookings(event)) {
        return new SegueErrorResponse(Status.FORBIDDEN, "This event does not accept group bookings.").toResponse();
    }
    List<RegisteredUserDTO> usersToReserve = Lists.newArrayList();
    try {
        reservingUser = userManager.getCurrentRegisteredUser(request);
        if (!Arrays.asList(Role.TEACHER, Role.EVENT_LEADER, Role.EVENT_MANAGER, Role.ADMIN).contains(reservingUser.getRole())) {
            return SegueErrorResponse.getIncorrectRoleResponse();
        }
        // Enforce permission
        for (Long userId : userIds) {
            RegisteredUserDTO userToReserve = userManager.getUserDTOById(userId);
            if (userAssociationManager.hasPermission(reservingUser, userToReserve)) {
                usersToReserve.add(userToReserve);
            } else {
                return new SegueErrorResponse(Status.FORBIDDEN, "You do not have permission to book or reserve some of these users onto this event.").toResponse();
            }
        }
        List<EventBookingDTO> bookings = bookingManager.requestReservations(event, usersToReserve, reservingUser);
        this.getLogManager().logEvent(reservingUser, request, SegueServerLogType.EVENT_RESERVATIONS_CREATED, ImmutableMap.of(EVENT_ID_FKEY_FIELDNAME, event.getId(), USER_ID_FKEY_FIELDNAME, reservingUser.getId(), USER_ID_LIST_FKEY_FIELDNAME, userIds.toArray(), BOOKING_STATUS_FIELDNAME, BookingStatus.RESERVED.toString()));
        return Response.ok(this.mapper.mapAsList(bookings, EventBookingDTO.class)).build();
    } catch (NoUserLoggedInException e) {
        return SegueErrorResponse.getNotLoggedInResponse();
    } catch (SegueDatabaseException e) {
        String errorMsg = "Database error occurred while trying to reserve space for a user onto an event.";
        log.error(errorMsg, e);
        return new SegueErrorResponse(Status.INTERNAL_SERVER_ERROR, errorMsg).toResponse();
    } catch (EventIsFullException e) {
        return new SegueErrorResponse(Status.CONFLICT, "There are not enough spaces available for this event. Please try again with fewer users.").toResponse();
    } catch (EventGroupReservationLimitException e) {
        return new SegueErrorResponse(Status.CONFLICT, String.format("You can only request a maximum of %d student reservations for this event.", event.getGroupReservationLimit())).toResponse();
    } catch (EventDeadlineException e) {
        return new SegueErrorResponse(Status.BAD_REQUEST, "The booking deadline for this event has passed. No more bookings or reservations are being accepted.").toResponse();
    } catch (DuplicateBookingException e) {
        return new SegueErrorResponse(Status.BAD_REQUEST, "One of the users requested is already booked or reserved on this event. Unable to create a duplicate booking.").toResponse();
    } catch (NoUserException e) {
        return SegueErrorResponse.getResourceNotFoundResponse("Unable to locate one of the users specified.");
    }
}
Also used : EventIsFullException(uk.ac.cam.cl.dtg.isaac.api.managers.EventIsFullException) DuplicateBookingException(uk.ac.cam.cl.dtg.isaac.api.managers.DuplicateBookingException) SegueDatabaseException(uk.ac.cam.cl.dtg.segue.dao.SegueDatabaseException) NoUserException(uk.ac.cam.cl.dtg.segue.auth.exceptions.NoUserException) EventBookingDTO(uk.ac.cam.cl.dtg.isaac.dto.eventbookings.EventBookingDTO) DetailedEventBookingDTO(uk.ac.cam.cl.dtg.isaac.dto.eventbookings.DetailedEventBookingDTO) NoUserLoggedInException(uk.ac.cam.cl.dtg.segue.auth.exceptions.NoUserLoggedInException) SegueErrorResponse(uk.ac.cam.cl.dtg.isaac.dto.SegueErrorResponse) RegisteredUserDTO(uk.ac.cam.cl.dtg.isaac.dto.users.RegisteredUserDTO) EventDeadlineException(uk.ac.cam.cl.dtg.isaac.api.managers.EventDeadlineException) IsaacEventPageDTO(uk.ac.cam.cl.dtg.isaac.dto.IsaacEventPageDTO) EventGroupReservationLimitException(uk.ac.cam.cl.dtg.isaac.api.managers.EventGroupReservationLimitException) ContentManagerException(uk.ac.cam.cl.dtg.segue.dao.content.ContentManagerException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation)

Example 3 with DuplicateBookingException

use of uk.ac.cam.cl.dtg.isaac.api.managers.DuplicateBookingException in project isaac-api by isaacphysics.

the class EventsFacade method createBookingForGivenUser.

/**
 * createBooking for a specific isaac user.
 * - Will attempt to create a waiting list booking if the event is already full.
 * - Must be a Staff user.
 *
 * @param request
 *            - so we can determine if the user is logged in
 * @param eventId
 *            - event id
 * @param userId
 *            - user id
 * @param additionalInformation
 *            - additional information to be stored with this booking e.g. dietary requirements.
 * @return the new booking
 */
@POST
@Path("{event_id}/bookings/{user_id}")
@Produces(MediaType.APPLICATION_JSON)
@GZIP
@ApiOperation(value = "Create an event booking for a user.")
public final Response createBookingForGivenUser(@Context final HttpServletRequest request, @PathParam("event_id") final String eventId, @PathParam("user_id") final Long userId, final Map<String, String> additionalInformation) {
    try {
        RegisteredUserDTO currentUser = userManager.getCurrentRegisteredUser(request);
        RegisteredUserDTO bookedUser = userManager.getUserDTOById(userId);
        IsaacEventPageDTO event = this.getAugmentedEventDTOById(request, eventId);
        if (!bookingManager.isUserAbleToManageEvent(currentUser, event)) {
            return SegueErrorResponse.getIncorrectRoleResponse();
        }
        if (bookingManager.isUserBooked(eventId, userId)) {
            return new SegueErrorResponse(Status.BAD_REQUEST, "User is already booked on this event.").toResponse();
        }
        EventBookingDTO booking = bookingManager.createBookingOrAddToWaitingList(event, bookedUser, additionalInformation);
        this.getLogManager().logEvent(currentUser, request, SegueServerLogType.ADMIN_EVENT_BOOKING_CREATED, ImmutableMap.of(EVENT_ID_FKEY_FIELDNAME, event.getId(), USER_ID_FKEY_FIELDNAME, userId, BOOKING_STATUS_FIELDNAME, booking.getBookingStatus().toString(), ADMIN_BOOKING_REASON_FIELDNAME, additionalInformation.get("authorisation") == null ? "NOT_PROVIDED" : additionalInformation.get("authorisation")));
        return Response.ok(this.mapper.map(booking, EventBookingDTO.class)).build();
    } catch (NoUserLoggedInException e) {
        return SegueErrorResponse.getNotLoggedInResponse();
    } catch (SegueDatabaseException e) {
        String errorMsg = "Database error occurred while trying to book a user onto an event.";
        log.error(errorMsg, e);
        return new SegueErrorResponse(Status.INTERNAL_SERVER_ERROR, errorMsg).toResponse();
    } catch (NoUserException e) {
        return new SegueErrorResponse(Status.NOT_FOUND, "Unable to locate the user requested").toResponse();
    } catch (ContentManagerException e) {
        return new SegueErrorResponse(Status.INTERNAL_SERVER_ERROR, "Content Database error occurred while trying to retrieve all event booking information.").toResponse();
    } catch (DuplicateBookingException e) {
        return new SegueErrorResponse(Status.BAD_REQUEST, "User already booked on this event. Unable to create a duplicate booking.").toResponse();
    }
}
Also used : SegueErrorResponse(uk.ac.cam.cl.dtg.isaac.dto.SegueErrorResponse) DuplicateBookingException(uk.ac.cam.cl.dtg.isaac.api.managers.DuplicateBookingException) RegisteredUserDTO(uk.ac.cam.cl.dtg.isaac.dto.users.RegisteredUserDTO) SegueDatabaseException(uk.ac.cam.cl.dtg.segue.dao.SegueDatabaseException) NoUserException(uk.ac.cam.cl.dtg.segue.auth.exceptions.NoUserException) IsaacEventPageDTO(uk.ac.cam.cl.dtg.isaac.dto.IsaacEventPageDTO) EventBookingDTO(uk.ac.cam.cl.dtg.isaac.dto.eventbookings.EventBookingDTO) DetailedEventBookingDTO(uk.ac.cam.cl.dtg.isaac.dto.eventbookings.DetailedEventBookingDTO) ContentManagerException(uk.ac.cam.cl.dtg.segue.dao.content.ContentManagerException) NoUserLoggedInException(uk.ac.cam.cl.dtg.segue.auth.exceptions.NoUserLoggedInException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 4 with DuplicateBookingException

use of uk.ac.cam.cl.dtg.isaac.api.managers.DuplicateBookingException in project isaac-api by isaacphysics.

the class EventsFacade method createBookingForMe.

/**
 * createBooking for the current user.
 *
 * @param request
 *            - so we can determine if the user is logged in
 * @param eventId
 *            - event id
 * @return the new booking if allowed to book.
 */
@POST
@Path("{event_id}/bookings")
@Produces(MediaType.APPLICATION_JSON)
@GZIP
@ApiOperation(value = "Create an event booking for the current user.")
public final Response createBookingForMe(@Context final HttpServletRequest request, @PathParam("event_id") final String eventId, final Map<String, String> additionalInformation) {
    try {
        RegisteredUserDTO user = userManager.getCurrentRegisteredUser(request);
        IsaacEventPageDTO event = this.getAugmentedEventDTOById(request, eventId);
        if (EventStatus.CLOSED.equals(event.getEventStatus())) {
            return new SegueErrorResponse(Status.BAD_REQUEST, "Sorry booking for this event is closed. Please try again later.").toResponse();
        }
        if (EventStatus.WAITING_LIST_ONLY.equals(event.getEventStatus())) {
            return new SegueErrorResponse(Status.BAD_REQUEST, "Sorry booking for this event is restricted. You can only be added to a waiting list.").toResponse();
        }
        if (bookingManager.isUserBooked(eventId, user.getId())) {
            return new SegueErrorResponse(Status.BAD_REQUEST, "You are already booked on this event.").toResponse();
        }
        // reservedBy is null. If there is a reservation for me, it will be updated to CONFIRMED.
        EventBookingDTO eventBookingDTO = bookingManager.requestBooking(event, user, additionalInformation);
        this.getLogManager().logEvent(userManager.getCurrentUser(request), request, SegueServerLogType.EVENT_BOOKING, ImmutableMap.of(EVENT_ID_FKEY_FIELDNAME, event.getId()));
        return Response.ok(this.mapper.map(eventBookingDTO, EventBookingDTO.class)).build();
    } catch (NoUserLoggedInException e) {
        return SegueErrorResponse.getNotLoggedInResponse();
    } catch (SegueDatabaseException e) {
        String errorMsg = "Database error occurred while trying to book a user onto an event.";
        log.error(errorMsg, e);
        return new SegueErrorResponse(Status.INTERNAL_SERVER_ERROR, errorMsg).toResponse();
    } catch (ContentManagerException e) {
        return new SegueErrorResponse(Status.INTERNAL_SERVER_ERROR, "Content Database error occurred while trying to retrieve all event booking information.").toResponse();
    } catch (EmailMustBeVerifiedException e) {
        return new SegueErrorResponse(Status.BAD_REQUEST, "In order to book on this event your user account must have a verified email address. Please verify your address to make a booking.").toResponse();
    } catch (DuplicateBookingException e) {
        return new SegueErrorResponse(Status.BAD_REQUEST, "You have already been booked on this event. Unable to create a duplicate booking.").toResponse();
    } catch (EventIsFullException e) {
        return new SegueErrorResponse(Status.CONFLICT, "This event is already full. Unable to book you on to it.").toResponse();
    } catch (EventDeadlineException e) {
        return new SegueErrorResponse(Status.BAD_REQUEST, "The booking deadline for this event has passed. No more bookings are being accepted.").toResponse();
    }
}
Also used : EventIsFullException(uk.ac.cam.cl.dtg.isaac.api.managers.EventIsFullException) SegueErrorResponse(uk.ac.cam.cl.dtg.isaac.dto.SegueErrorResponse) DuplicateBookingException(uk.ac.cam.cl.dtg.isaac.api.managers.DuplicateBookingException) RegisteredUserDTO(uk.ac.cam.cl.dtg.isaac.dto.users.RegisteredUserDTO) EventDeadlineException(uk.ac.cam.cl.dtg.isaac.api.managers.EventDeadlineException) SegueDatabaseException(uk.ac.cam.cl.dtg.segue.dao.SegueDatabaseException) IsaacEventPageDTO(uk.ac.cam.cl.dtg.isaac.dto.IsaacEventPageDTO) EventBookingDTO(uk.ac.cam.cl.dtg.isaac.dto.eventbookings.EventBookingDTO) DetailedEventBookingDTO(uk.ac.cam.cl.dtg.isaac.dto.eventbookings.DetailedEventBookingDTO) EmailMustBeVerifiedException(uk.ac.cam.cl.dtg.segue.comm.EmailMustBeVerifiedException) ContentManagerException(uk.ac.cam.cl.dtg.segue.dao.content.ContentManagerException) NoUserLoggedInException(uk.ac.cam.cl.dtg.segue.auth.exceptions.NoUserLoggedInException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) GZIP(org.jboss.resteasy.annotations.GZIP)

Aggregations

ApiOperation (io.swagger.annotations.ApiOperation)4 POST (javax.ws.rs.POST)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 DuplicateBookingException (uk.ac.cam.cl.dtg.isaac.api.managers.DuplicateBookingException)4 IsaacEventPageDTO (uk.ac.cam.cl.dtg.isaac.dto.IsaacEventPageDTO)4 SegueErrorResponse (uk.ac.cam.cl.dtg.isaac.dto.SegueErrorResponse)4 DetailedEventBookingDTO (uk.ac.cam.cl.dtg.isaac.dto.eventbookings.DetailedEventBookingDTO)4 EventBookingDTO (uk.ac.cam.cl.dtg.isaac.dto.eventbookings.EventBookingDTO)4 RegisteredUserDTO (uk.ac.cam.cl.dtg.isaac.dto.users.RegisteredUserDTO)4 NoUserLoggedInException (uk.ac.cam.cl.dtg.segue.auth.exceptions.NoUserLoggedInException)4 SegueDatabaseException (uk.ac.cam.cl.dtg.segue.dao.SegueDatabaseException)4 ContentManagerException (uk.ac.cam.cl.dtg.segue.dao.content.ContentManagerException)4 GZIP (org.jboss.resteasy.annotations.GZIP)3 EventDeadlineException (uk.ac.cam.cl.dtg.isaac.api.managers.EventDeadlineException)3 EventIsFullException (uk.ac.cam.cl.dtg.isaac.api.managers.EventIsFullException)2 NoUserException (uk.ac.cam.cl.dtg.segue.auth.exceptions.NoUserException)2 EmailMustBeVerifiedException (uk.ac.cam.cl.dtg.segue.comm.EmailMustBeVerifiedException)2 EventGroupReservationLimitException (uk.ac.cam.cl.dtg.isaac.api.managers.EventGroupReservationLimitException)1 EventIsNotFullException (uk.ac.cam.cl.dtg.isaac.api.managers.EventIsNotFullException)1