2019-01-21 16:27:59 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Plan and PartnerPlan.
|
|
|
|
# Plan are used to define subscription's characteristics.
|
|
|
|
# PartnerPlan is a special kind of plan which send notifications to an external user
|
|
|
|
class API::PlansController < API::ApiController
|
2016-03-23 18:39:41 +01:00
|
|
|
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
|
|
|
|
|
2019-01-21 16:27:59 +01:00
|
|
|
unless %w[PartnerPlan Plan].include? plan_params[:type]
|
|
|
|
render json: { error: 'unhandled plan type' }, status: :unprocessable_entity and return
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2019-01-21 16:27:59 +01:00
|
|
|
type = plan_params[:type]
|
|
|
|
partner = params[:plan][:partner_id].empty? ? nil : User.find(params[:plan][:partner_id])
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2019-01-21 16:27:59 +01:00
|
|
|
res = PlansService.create(type, partner, plan_params)
|
2021-03-30 15:56:36 +02:00
|
|
|
if res.errors
|
|
|
|
render json: res.errors, status: :unprocessable_entity
|
2019-01-21 16:27:59 +01:00
|
|
|
else
|
|
|
|
render json: res, status: :created
|
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
|
2019-01-21 16:27:59 +01:00
|
|
|
|
|
|
|
def plan_params
|
|
|
|
# parameters caching for performance
|
|
|
|
if @parameters
|
|
|
|
@parameters
|
|
|
|
else
|
|
|
|
@parameters = params
|
|
|
|
@parameters[:plan][:amount] = @parameters[:plan][:amount].to_f * 100.0 if @parameters[:plan][:amount]
|
|
|
|
if @parameters[:plan][:prices_attributes]
|
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] }
|
2019-01-21 16:27:59 +01:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
2019-01-21 16:27:59 +01:00
|
|
|
|
|
|
|
@parameters = @parameters.require(:plan)
|
|
|
|
.permit(:base_name, :type, :group_id, :amount, :interval, :interval_count, :is_rolling,
|
2021-03-03 17:21:06 +01:00
|
|
|
:training_credit_nb, :ui_weight, :disabled, :monthly_payment, :description,
|
2019-01-21 16:27:59 +01:00
|
|
|
plan_file_attributes: %i[id attachment _destroy],
|
|
|
|
prices_attributes: %i[id amount])
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
2019-01-21 16:27:59 +01:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|