1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-05 16:24:21 +01:00
fab-manager/app/controllers/api/subscriptions_controller.rb

91 lines
2.9 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# API Controller for resources of type Subscription
2016-03-23 18:39:41 +01:00
class API::SubscriptionsController < API::ApiController
before_action :set_subscription, only: %i[show edit update destroy]
2016-03-23 18:39:41 +01:00
before_action :authenticate_user!
def show
authorize @subscription
end
# Admins can create any subscriptions. Members can directly create subscriptions if total = 0,
# otherwise, they must use payments_controller#confirm_payment.
# Managers can create subscriptions for other users
2016-03-23 18:39:41 +01:00
def create
user_id = current_user.admin? || current_user.manager? ? params[:subscription][:user_id] : current_user.id
amount = transaction_amount(current_user.admin?, user_id)
2019-09-10 11:46:14 +02:00
authorize SubscriptionContext.new(Subscription, amount, user_id)
2019-09-10 11:46:14 +02:00
@subscription = Subscription.new(subscription_params)
is_subscribe = Subscriptions::Subscribe.new(current_user.invoicing_profile.id, user_id)
.pay_and_save(@subscription, coupon: coupon_params[:coupon_code], invoice: true)
2019-09-10 11:46:14 +02:00
if is_subscribe
render :show, status: :created, location: @subscription
2016-03-23 18:39:41 +01:00
else
2019-09-10 11:46:14 +02:00
render json: @subscription.errors, status: :unprocessable_entity
2016-03-23 18:39:41 +01:00
end
end
def update
authorize @subscription
free_days = params[:subscription][:free] || false
2016-03-23 18:39:41 +01:00
res = Subscriptions::Subscribe.new(current_user.invoicing_profile.id)
2018-12-11 17:27:25 +01:00
.extend_subscription(@subscription, subscription_update_params[:expired_at], free_days)
if res.is_a?(Subscription)
@subscription = res
render status: :created
elsif res
2016-03-23 18:39:41 +01:00
render status: :ok
else
render status: :unprocessable_entity
end
end
private
def transaction_amount(is_admin, user_id)
user = User.find(user_id)
price_details = Price.compute(is_admin,
user,
nil,
[],
subscription_params[:plan_id],
nil,
nil,
coupon_params[:coupon_code])
# Subtract wallet amount from total
total = price_details[:total]
wallet_debit = get_wallet_debit(user, total)
total - wallet_debit
end
def get_wallet_debit(user, total_amount)
wallet_amount = (user.wallet.amount * 100).to_i
wallet_amount >= total_amount ? total_amount : wallet_amount
end
# Use callbacks to share common setup or constraints between actions.
def set_subscription
@subscription = Subscription.find(params[:id])
end
2016-03-23 18:39:41 +01:00
# Never trust parameters from the scary internet, only allow the white list through.
def subscription_params
params.require(:subscription).permit(:plan_id)
end
2016-08-11 18:17:28 +02:00
def coupon_params
params.permit(:coupon_code)
end
def subscription_update_params
params.require(:subscription).permit(:expired_at)
end
2016-03-23 18:39:41 +01:00
end