2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Reservation
|
|
|
|
# Reservations are used for Training, Machine, Space and Event
|
2023-02-24 17:26:55 +01:00
|
|
|
class API::ReservationsController < API::APIController
|
2016-03-23 18:39:41 +01:00
|
|
|
before_action :authenticate_user!
|
2023-06-27 17:50:47 +02:00
|
|
|
before_action :set_reservation, only: %i[show update confirm_payment]
|
2016-03-23 18:39:41 +01:00
|
|
|
respond_to :json
|
|
|
|
|
|
|
|
def index
|
2022-06-28 14:59:51 +02:00
|
|
|
if params[:user_id]
|
2020-04-28 12:48:03 +02:00
|
|
|
params[:user_id] = current_user.id unless current_user.admin? || current_user.manager?
|
2018-12-11 15:07:21 +01:00
|
|
|
|
2022-06-28 14:59:51 +02:00
|
|
|
where_clause = { statistic_profile_id: StatisticProfile.find_by!(user_id: params[:user_id]) }
|
|
|
|
where_clause[:reservable_type] = params[:reservable_type] if params[:reservable_type]
|
|
|
|
where_clause[:reservable_id] = params[:reservable_id] if params[:reservable_id]
|
2019-09-20 15:23:53 +02:00
|
|
|
|
|
|
|
@reservations = Reservation.where(where_clause)
|
2023-09-28 15:57:30 +02:00
|
|
|
.preload(:reservable, :booking_users, :tickets, { statistic_profile: { user: :profile }, slots_reservations: :slot })
|
2020-04-28 12:48:03 +02:00
|
|
|
elsif params[:reservable_id] && params[:reservable_type] && (current_user.admin? || current_user.manager?)
|
2016-03-23 18:39:41 +01:00
|
|
|
@reservations = Reservation.where(params.permit(:reservable_id, :reservable_type))
|
2023-09-28 15:57:30 +02:00
|
|
|
.preload(:reservable, :booking_users, :tickets, { statistic_profile: { user: :profile }, slots_reservations: :slot })
|
2016-03-23 18:39:41 +01:00
|
|
|
else
|
|
|
|
@reservations = []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-11 15:07:21 +01:00
|
|
|
def show; end
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
def update
|
|
|
|
authorize @reservation
|
|
|
|
if @reservation.update(reservation_params)
|
|
|
|
render :show, status: :ok, location: @reservation
|
|
|
|
else
|
|
|
|
render json: @reservation.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-27 17:50:47 +02:00
|
|
|
def confirm_payment
|
|
|
|
authorize @reservation
|
|
|
|
invoice = ReservationConfirmPaymentService.new(@reservation, current_user, params[:coupon_code], params[:offered]).call
|
|
|
|
if invoice
|
|
|
|
render :show, status: :ok, location: @reservation
|
|
|
|
else
|
|
|
|
render json: @reservation.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
private
|
2019-01-16 16:28:25 +01:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def set_reservation
|
|
|
|
@reservation = Reservation.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def reservation_params
|
2021-04-26 18:27:35 +02:00
|
|
|
params.require(:reservation).permit(:message, :reservable_id, :reservable_type, :nb_reserve_places,
|
2018-12-11 15:07:21 +01:00
|
|
|
tickets_attributes: %i[event_price_category_id booked],
|
2022-07-13 16:28:43 +02:00
|
|
|
slots_reservations_attributes: %i[id slot_id offered])
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
end
|