1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/app/controllers/api/reservations_controller.rb

71 lines
2.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# API Controller for resources of type Reservation
# Reservations are used for Training, Machine, Space and Event
2016-03-23 18:39:41 +01:00
class API::ReservationsController < API::ApiController
before_action :authenticate_user!
2018-12-11 15:07:21 +01:00
before_action :set_reservation, only: %i[show update]
2016-03-23 18:39:41 +01:00
respond_to :json
def index
2018-12-11 15:07:21 +01:00
if params[:reservable_id] && params[:reservable_type] && params[:user_id]
params[:user_id] = current_user.id unless current_user.admin?
2018-12-11 15:07:21 +01:00
2016-03-23 18:39:41 +01:00
@reservations = Reservation.where(params.permit(:reservable_id, :reservable_type, :user_id))
elsif params[:reservable_id] && params[:reservable_type] && current_user.admin?
2016-03-23 18:39:41 +01:00
@reservations = Reservation.where(params.permit(:reservable_id, :reservable_type))
else
@reservations = []
end
end
2018-12-11 15:07:21 +01:00
def show; end
2016-03-23 18:39:41 +01:00
# Admins can create any reservations. Members can directly create reservations if total = 0,
# otherwise, they must use payments_controller#confirm_payment
2016-03-23 18:39:41 +01:00
def create
2019-09-09 17:37:54 +02:00
authorize Reservation
user_id = params[:reservation][:user_id]
2018-12-11 15:07:21 +01:00
@reservation = Reservation.new(reservation_params)
is_reserve = Reservations::Reserve.new(user_id, current_user.invoicing_profile.id)
.pay_and_save(@reservation, coupon: coupon_params[:coupon_code])
2018-12-11 15:07:21 +01:00
if is_reserve
SubscriptionExtensionAfterReservation.new(@reservation).extend_subscription_if_eligible
render :show, status: :created, location: @reservation
else
render json: @reservation.errors, status: :unprocessable_entity
2016-03-23 18:39:41 +01:00
end
2018-12-11 15:07:21 +01:00
rescue InvalidCouponError
render json: { coupon_code: 'wrong coupon code or expired' }, status: :unprocessable_entity
2016-03-23 18:39:41 +01:00
end
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
private
2016-03-23 18:39:41 +01:00
def set_reservation
@reservation = Reservation.find(params[:id])
end
def reservation_params
params.require(:reservation).permit(:message, :reservable_id, :reservable_type, :plan_id, :nb_reserve_places,
2018-12-11 15:07:21 +01:00
tickets_attributes: %i[event_price_category_id booked],
slots_attributes: %i[id start_at end_at availability_id offered])
2016-03-23 18:39:41 +01:00
end
def coupon_params
params.permit(:coupon_code)
end
2016-03-23 18:39:41 +01:00
end