2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Price
|
|
|
|
# Prices are used in reservations (Machine, Space)
|
2016-03-23 18:39:41 +01:00
|
|
|
class API::PricesController < API::ApiController
|
|
|
|
before_action :authenticate_user!
|
|
|
|
|
|
|
|
def index
|
|
|
|
authorize Price
|
|
|
|
@prices = Price.all
|
|
|
|
if params[:priceable_type]
|
|
|
|
@prices = @prices.where(priceable_type: params[:priceable_type])
|
2019-01-16 16:28:25 +01:00
|
|
|
|
|
|
|
@prices = @prices.where(priceable_id: params[:priceable_id]) if params[:priceable_id]
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
if params[:plan_id]
|
2020-12-02 10:06:18 +01:00
|
|
|
plan_id = if /no|nil|null|undefined/i.match?(params[:plan_id])
|
2019-01-16 16:28:25 +01:00
|
|
|
nil
|
|
|
|
else
|
|
|
|
params[:plan_id]
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
@prices = @prices.where(plan_id: plan_id)
|
|
|
|
end
|
2019-01-16 16:28:25 +01:00
|
|
|
@prices = @prices.where(group_id: params[:group_id]) if params[:group_id]
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
authorize Price
|
|
|
|
@price = Price.find(params[:id])
|
2019-01-16 16:28:25 +01:00
|
|
|
price_parameters = price_params
|
|
|
|
price_parameters[:amount] = price_parameters[:amount] * 100
|
|
|
|
if @price.update(price_parameters)
|
2016-03-23 18:39:41 +01:00
|
|
|
render status: :ok
|
|
|
|
else
|
|
|
|
render status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def compute
|
2021-04-23 12:52:06 +02:00
|
|
|
cs = CartService.new(current_user)
|
|
|
|
cart = cs.from_hash(params)
|
|
|
|
@amount = cart.total
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2019-01-16 16:28:25 +01:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def price_params
|
|
|
|
params.require(:price).permit(:amount)
|
|
|
|
end
|
|
|
|
|
2020-12-02 10:06:18 +01:00
|
|
|
def compute_reservation_price_params
|
2020-11-03 16:50:11 +01:00
|
|
|
params.require(:reservation).permit(:reservable_id, :reservable_type, :plan_id, :user_id, :nb_reserve_places, :payment_schedule,
|
2019-01-16 16:28:25 +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-09 16:36:14 +02:00
|
|
|
|
2020-12-02 10:06:18 +01:00
|
|
|
def compute_subscription_price_params
|
|
|
|
params.require(:subscription).permit(:plan_id, :user_id, :payment_schedule)
|
|
|
|
end
|
|
|
|
|
2016-08-09 16:36:14 +02:00
|
|
|
def coupon_params
|
2016-08-10 15:34:47 +02:00
|
|
|
params.permit(:coupon_code)
|
2016-08-09 16:36:14 +02:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|