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
|
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]
|
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
|
|
|
|
2019-09-20 15:23:53 +02:00
|
|
|
where_clause = params.permit(:reservable_id, :reservable_type).to_h
|
|
|
|
where_clause[:statistic_profile_id] = StatisticProfile.find_by!(user_id: params[:user_id])
|
|
|
|
|
|
|
|
@reservations = Reservation.where(where_clause)
|
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))
|
|
|
|
else
|
|
|
|
@reservations = []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-11 15:07:21 +01:00
|
|
|
def show; end
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2019-09-11 10:31:22 +02:00
|
|
|
# Admins can create any reservations. Members can directly create reservations if total = 0,
|
2020-04-27 12:12:29 +02:00
|
|
|
# otherwise, they must use payments_controller#confirm_payment.
|
|
|
|
# Managers can create reservations for other users
|
2016-03-23 18:39:41 +01:00
|
|
|
def create
|
2020-04-27 12:12:29 +02:00
|
|
|
user_id = current_user.admin? || current_user.manager? ? params[:reservation][:user_id] : current_user.id
|
2020-05-06 12:43:47 +02:00
|
|
|
price = transaction_amount(current_user.admin? || (current_user.manager? && current_user.id != user_id), user_id)
|
2019-09-09 18:04:31 +02:00
|
|
|
|
2020-05-06 12:43:47 +02:00
|
|
|
authorize ReservationContext.new(Reservation, price[:amount], user_id)
|
2018-12-11 15:07:21 +01:00
|
|
|
|
|
|
|
@reservation = Reservation.new(reservation_params)
|
2019-06-12 12:22:38 +02:00
|
|
|
is_reserve = Reservations::Reserve.new(user_id, current_user.invoicing_profile.id)
|
2020-12-14 17:42:23 +01:00
|
|
|
.pay_and_save(@reservation,
|
|
|
|
payment_details: price[:price_details],
|
2020-12-16 18:33:43 +01:00
|
|
|
schedule: params[:reservation][:payment_schedule],
|
|
|
|
payment_method: params[:reservation][:payment_method])
|
2016-04-07 17:57:48 +02:00
|
|
|
|
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
|
2019-01-16 16:28:25 +01:00
|
|
|
|
2019-09-11 11:51:04 +02:00
|
|
|
def transaction_amount(is_admin, user_id)
|
|
|
|
user = User.find(user_id)
|
|
|
|
price_details = Price.compute(is_admin,
|
|
|
|
user,
|
|
|
|
reservation_params[:reservable_type].constantize.find(reservation_params[:reservable_id]),
|
|
|
|
reservation_params[:slots_attributes] || [],
|
2020-11-03 16:50:11 +01:00
|
|
|
plan_id: reservation_params[:plan_id],
|
|
|
|
nb_places: reservation_params[:nb_reserve_places],
|
|
|
|
tickets: reservation_params[:tickets_attributes],
|
|
|
|
coupon_code: coupon_params[:coupon_code])
|
2019-09-11 11:51:04 +02:00
|
|
|
|
|
|
|
# Subtract wallet amount from total
|
|
|
|
total = price_details[:total]
|
|
|
|
wallet_debit = get_wallet_debit(user, total)
|
2020-05-06 12:43:47 +02:00
|
|
|
|
|
|
|
{ price_details: price_details, amount: (total - wallet_debit) }
|
2019-09-11 11:51:04 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_wallet_debit(user, total_amount)
|
|
|
|
wallet_amount = (user.wallet.amount * 100).to_i
|
|
|
|
wallet_amount >= total_amount ? total_amount : wallet_amount
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def set_reservation
|
|
|
|
@reservation = Reservation.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def reservation_params
|
2019-09-10 17:57:46 +02:00
|
|
|
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
|
2016-08-10 15:34:47 +02:00
|
|
|
|
|
|
|
def coupon_params
|
|
|
|
params.permit(:coupon_code)
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|