mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-21 15:54:22 +01:00
[bug] invalid translation of title in new partner modal + refactored plans controller
This commit is contained in:
parent
75eaf4d4d0
commit
da4c9cf26f
@ -9,6 +9,7 @@
|
|||||||
- Fix a bug: unable to delete an admin who has changed a setting
|
- Fix a bug: unable to delete an admin who has changed a setting
|
||||||
- Fix a bug: unable to create/edit a plan of 12 months or 52 weeks
|
- Fix a bug: unable to create/edit a plan of 12 months or 52 weeks
|
||||||
- Fix a bug: Unable to search in user autocomplete fields
|
- Fix a bug: Unable to search in user autocomplete fields
|
||||||
|
- Fix a bug: Invalid translation in new partner modal
|
||||||
- Improved user autocompletion when using multiple words
|
- Improved user autocompletion when using multiple words
|
||||||
- Refactored frontend invoices translations
|
- Refactored frontend invoices translations
|
||||||
- Updated RailRoady 1.4.0 to 1.5.3
|
- Updated RailRoady 1.4.0 to 1.5.3
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<div class="modal-header">
|
<div class="modal-header">
|
||||||
<h3 class="modal-title" translate>{{ 'new_partner' }}</h3>
|
<h3 class="modal-title" translate>{{ 'plan_form.new_partner' }}</h3>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-body m-lg">
|
<div class="modal-body m-lg">
|
||||||
<form name="partnerForm">
|
<form name="partnerForm">
|
||||||
|
@ -1,4 +1,9 @@
|
|||||||
class API::PlansController < API::ApiController
|
# 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
|
||||||
before_action :authenticate_user!, except: [:index]
|
before_action :authenticate_user!, except: [:index]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@ -13,48 +18,19 @@
|
|||||||
|
|
||||||
def create
|
def create
|
||||||
authorize Plan
|
authorize Plan
|
||||||
begin
|
|
||||||
if plan_params[:type] and plan_params[:type] == 'PartnerPlan'
|
|
||||||
|
|
||||||
partner = User.find(params[:plan][:partner_id])
|
unless %w[PartnerPlan Plan].include? plan_params[:type]
|
||||||
|
render json: { error: 'unhandled plan type' }, status: :unprocessable_entity and return
|
||||||
|
end
|
||||||
|
|
||||||
if plan_params[:group_id] == 'all'
|
type = plan_params[:type]
|
||||||
plans = PartnerPlan.create_for_all_groups(plan_params)
|
partner = params[:plan][:partner_id].empty? ? nil : User.find(params[:plan][:partner_id])
|
||||||
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
|
|
||||||
|
|
||||||
else
|
res = PlansService.create(type, partner, plan_params)
|
||||||
@plan = PartnerPlan.new(plan_params)
|
if res[:errors]
|
||||||
if @plan.save
|
render res[:errors], status: :unprocessable_entity
|
||||||
partner.add_role :partner, @plan
|
else
|
||||||
render :show, status: :created
|
render json: res, status: :created
|
||||||
else
|
|
||||||
render json: @plan.errors, status: :unprocessable_entity
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
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
|
|
||||||
else
|
|
||||||
@plan = Plan.new(plan_params)
|
|
||||||
if @plan.save
|
|
||||||
render :show, status: :created, location: @plan
|
|
||||||
else
|
|
||||||
render json: @plan.errors, status: :unprocessable_entity
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
rescue Stripe::InvalidRequestError => e
|
|
||||||
render json: {error: e.message}, status: :unprocessable_entity
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -76,21 +52,25 @@
|
|||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def plan_params
|
|
||||||
if @parameters
|
def plan_params
|
||||||
@parameters
|
# parameters caching for performance
|
||||||
else
|
if @parameters
|
||||||
@parameters = params
|
@parameters
|
||||||
@parameters[:plan][:amount] = @parameters[:plan][:amount].to_f * 100.0 if @parameters[:plan][:amount]
|
else
|
||||||
|
@parameters = params
|
||||||
|
@parameters[:plan][:amount] = @parameters[:plan][:amount].to_f * 100.0 if @parameters[:plan][:amount]
|
||||||
|
if @parameters[:plan][:prices_attributes]
|
||||||
@parameters[:plan][:prices_attributes] = @parameters[:plan][:prices_attributes].map do |price|
|
@parameters[:plan][:prices_attributes] = @parameters[:plan][:prices_attributes].map do |price|
|
||||||
{ amount: price[:amount].to_f * 100.0, id: price[:id] }
|
{ amount: price[:amount].to_f * 100.0, id: price[:id] }
|
||||||
end if @parameters[:plan][:prices_attributes]
|
end
|
||||||
|
|
||||||
@parameters = @parameters.require(:plan).permit(:base_name, :type, :group_id, :amount, :interval, :interval_count, :is_rolling,
|
|
||||||
:training_credit_nb, :ui_weight, :disabled,
|
|
||||||
plan_file_attributes: [:id, :attachment, :_destroy],
|
|
||||||
prices_attributes: [:id, :amount]
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@parameters = @parameters.require(:plan)
|
||||||
|
.permit(:base_name, :type, :group_id, :amount, :interval, :interval_count, :is_rolling,
|
||||||
|
:training_credit_nb, :ui_weight, :disabled,
|
||||||
|
plan_file_attributes: %i[id attachment _destroy],
|
||||||
|
prices_attributes: %i[id amount])
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
26
app/services/plans_service.rb
Normal file
26
app/services/plans_service.rb
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Provides methods for Plan & PartnerPlan actions
|
||||||
|
class PlansService
|
||||||
|
class << self
|
||||||
|
def create(type, partner, params)
|
||||||
|
if params[:group_id] == 'all'
|
||||||
|
plans = type.constantize.create_for_all_groups(params)
|
||||||
|
return false unless plans
|
||||||
|
|
||||||
|
plans.each { |plan| partner.add_role :partner, plan } unless partner.nil?
|
||||||
|
{ plan_ids: plans.map(&:id) }
|
||||||
|
else
|
||||||
|
plan = type.constantize.new(params)
|
||||||
|
if plan.save
|
||||||
|
partner&.add_role :partner, plan
|
||||||
|
plan
|
||||||
|
else
|
||||||
|
{ errors: @plan.errors }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
rescue Stripe::InvalidRequestError => e
|
||||||
|
{ errors: e.message }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user