2016-03-23 18:39:41 +01:00
|
|
|
class API::PlansController < API::ApiController
|
|
|
|
before_action :authenticate_user!, except: [:index]
|
|
|
|
|
|
|
|
def index
|
2016-06-21 14:49:39 +02:00
|
|
|
@plans = Plan.includes(:plan_file)
|
2016-03-23 18:39:41 +01:00
|
|
|
@plans = @plans.where(group_id: params[:group_id]) if params[:group_id]
|
2016-06-21 14:49:39 +02:00
|
|
|
render :index
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@plan = Plan.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
authorize Plan
|
2016-03-24 15:39:24 +01:00
|
|
|
begin
|
|
|
|
if plan_params[:type] and plan_params[:type] == 'PartnerPlan'
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2016-03-24 15:39:24 +01:00
|
|
|
partner = User.find(params[:plan][:partner_id])
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2016-03-24 15:39:24 +01:00
|
|
|
if plan_params[:group_id] == 'all'
|
|
|
|
plans = PartnerPlan.create_for_all_groups(plan_params)
|
|
|
|
if plans
|
|
|
|
plans.each { |plan| partner.add_role :partner, plan }
|
|
|
|
render json: { plan_ids: plans.map(&:id) }, status: :created
|
|
|
|
else
|
|
|
|
render status: :unprocessable_entity
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
else
|
2016-03-24 15:39:24 +01:00
|
|
|
@plan = PartnerPlan.new(plan_params)
|
|
|
|
if @plan.save
|
|
|
|
partner.add_role :partner, @plan
|
|
|
|
render :show, status: :created
|
|
|
|
else
|
|
|
|
render json: @plan.errors, status: :unprocessable_entity
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
else
|
2016-03-24 15:39:24 +01:00
|
|
|
if plan_params[:group_id] == 'all'
|
|
|
|
plans = Plan.create_for_all_groups(plan_params)
|
|
|
|
if plans
|
|
|
|
render json: { plan_ids: plans.map(&:id) }, status: :created
|
|
|
|
else
|
|
|
|
render status: :unprocessable_entity
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
else
|
2016-03-24 15:39:24 +01:00
|
|
|
@plan = Plan.new(plan_params)
|
|
|
|
if @plan.save
|
|
|
|
render :show, status: :created, location: @plan
|
|
|
|
else
|
|
|
|
render json: @plan.errors, status: :unprocessable_entity
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
end
|
2016-03-24 15:39:24 +01:00
|
|
|
rescue Stripe::InvalidRequestError => e
|
|
|
|
render json: {error: e.message}, status: :unprocessable_entity
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@plan = Plan.find(params[:id])
|
|
|
|
authorize @plan
|
|
|
|
if @plan.update(plan_params)
|
|
|
|
render :show, status: :ok
|
|
|
|
else
|
|
|
|
render json: @plan.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@plan = Plan.find(params[:id])
|
|
|
|
authorize @plan
|
|
|
|
@plan.destroy
|
|
|
|
head :no_content
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def plan_params
|
|
|
|
if @parameters
|
|
|
|
@parameters
|
|
|
|
else
|
|
|
|
@parameters = params
|
2016-11-29 15:23:36 +01:00
|
|
|
@parameters[:plan][:amount] = @parameters[:plan][:amount].to_f * 100.0 if @parameters[:plan][:amount]
|
2016-03-23 18:39:41 +01:00
|
|
|
@parameters[:plan][:prices_attributes] = @parameters[:plan][:prices_attributes].map do |price|
|
2016-11-29 15:23:36 +01:00
|
|
|
{ amount: price[:amount].to_f * 100.0, id: price[:id] }
|
2016-03-23 18:39:41 +01:00
|
|
|
end if @parameters[:plan][:prices_attributes]
|
|
|
|
|
|
|
|
@parameters = @parameters.require(:plan).permit(:base_name, :type, :group_id, :amount, :interval, :interval_count, :is_rolling,
|
|
|
|
:training_credit_nb,
|
|
|
|
:ui_weight,
|
|
|
|
plan_file_attributes: [:id, :attachment, :_destroy],
|
|
|
|
prices_attributes: [:id, :amount]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|