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
|
2023-02-24 17:26:55 +01:00
|
|
|
class API::PlansController < API::APIController
|
2022-12-01 15:43:16 +01:00
|
|
|
include ApplicationHelper
|
|
|
|
|
|
|
|
before_action :authenticate_user!, except: %i[index durations]
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
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]
|
2022-11-15 09:47:28 +01:00
|
|
|
partner = params[:plan][:partner_id].blank? ? nil : User.find(params[:plan][:partner_id])
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2021-06-14 16:53:52 +02:00
|
|
|
plan = PlansService.create(type, partner, plan_params)
|
2022-05-17 17:38:00 +02:00
|
|
|
if plan.key?(:errors)
|
2021-06-14 16:53:52 +02:00
|
|
|
render json: plan.errors, status: :unprocessable_entity
|
2019-01-21 16:27:59 +01:00
|
|
|
else
|
2021-06-14 16:53:52 +02:00
|
|
|
render json: plan, 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
|
|
|
|
|
2021-06-10 14:06:53 +02:00
|
|
|
def durations
|
|
|
|
grouped = Plan.all.map { |p| [p.human_readable_duration, p.id] }.group_by { |i| i[0] }
|
|
|
|
@durations = []
|
|
|
|
grouped.each_pair do |duration, plans|
|
|
|
|
@durations.push(
|
|
|
|
name: duration,
|
|
|
|
plans: plans.map { |p| p[1] }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
private
|
2019-01-21 16:27:59 +01:00
|
|
|
|
|
|
|
def plan_params
|
|
|
|
# parameters caching for performance
|
|
|
|
if @parameters
|
|
|
|
@parameters
|
|
|
|
else
|
|
|
|
@parameters = params
|
2022-12-01 15:43:16 +01:00
|
|
|
@parameters[:plan][:amount] = to_centimes(@parameters[:plan][:amount]) if @parameters[:plan][:amount]
|
2019-01-21 16:27:59 +01:00
|
|
|
if @parameters[:plan][:prices_attributes]
|
2016-03-23 18:39:41 +01:00
|
|
|
@parameters[:plan][:prices_attributes] = @parameters[:plan][:prices_attributes].map do |price|
|
2022-12-01 15:43:16 +01:00
|
|
|
{ amount: to_centimes(price[:amount]), 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)
|
2023-03-07 17:35:21 +01:00
|
|
|
.permit(:base_name, :type, :group_id, :amount, :interval, :interval_count, :is_rolling, :limiting,
|
2021-06-08 17:26:40 +02:00
|
|
|
:training_credit_nb, :ui_weight, :disabled, :monthly_payment, :description, :plan_category_id,
|
2023-03-15 14:17:06 +01:00
|
|
|
:machines_visibility,
|
2019-01-21 16:27:59 +01:00
|
|
|
plan_file_attributes: %i[id attachment _destroy],
|
2022-11-14 17:54:14 +01:00
|
|
|
prices_attributes: %i[id amount],
|
2023-03-07 17:35:21 +01:00
|
|
|
advanced_accounting_attributes: %i[code analytical_section],
|
2023-03-10 14:13:00 +01:00
|
|
|
plan_limitations_attributes: %i[id limitable_id limitable_type limit _destroy])
|
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
|