2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Subscription
|
2016-03-23 18:39:41 +01:00
|
|
|
class API::SubscriptionsController < API::ApiController
|
2018-12-06 18:26:01 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
def update
|
|
|
|
authorize @subscription
|
|
|
|
|
2018-12-10 13:24:00 +01:00
|
|
|
free_days = params[:subscription][:free] || false
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2019-06-12 12:22:38 +02: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
|
|
|
|
|
2018-12-06 18:26:01 +01:00
|
|
|
# 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
|
|
|
|
2018-12-06 18:26:01 +01:00
|
|
|
def subscription_update_params
|
|
|
|
params.require(:subscription).permit(:expired_at)
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|