-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3759 from 3scale/service-subscription-api
Service Subscription API: create, show, change plan, approve
- Loading branch information
Showing
12 changed files
with
623 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
app/controllers/admin/api/service_subscriptions_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
class Admin::Api::ServiceSubscriptionsController < Admin::Api::ServiceBaseController | ||
wrap_parameters ServiceContract, name: :service_subscription | ||
representer entity: ::ServiceSubscriptionRepresenter, collection: ::ServiceSubscriptionsRepresenter | ||
|
||
before_action :deny_on_premises_for_master | ||
before_action :authorize_service_plans! | ||
|
||
# Service Subscription Create | ||
# POST /admin/api/accounts/:account_id/service_subscriptions.json | ||
def create | ||
respond_with account.bought_service_contracts.create(plan: service_plan) | ||
end | ||
|
||
# Service Subscription List | ||
# GET /admin/api/accounts/{account_id}/service_subscriptions.json | ||
def index | ||
respond_with account.bought_service_contracts | ||
end | ||
|
||
# Service Subscription Delete | ||
# DELETE /admin/api/accounts/{account_id}/service_subscriptions/{id}.json | ||
def destroy | ||
service = ServiceSubscriptionService.new(account) | ||
|
||
respond_with(service.unsubscribe(service_subscription)) | ||
end | ||
|
||
# Service Subscription Change Plan | ||
# PUT /admin/api/accounts/{account_id}/service_subscriptions/{id}/change_plan.json | ||
def change_plan | ||
service_subscription.change_plan(service_plan) | ||
respond_with(service_subscription, serialize: service_plan, representer: ServicePlanRepresenter) | ||
end | ||
|
||
# Service Subscription Show | ||
# GET /admin/api/accounts/:account_id/service_subscriptions/:id.json | ||
def show | ||
respond_with service_subscription | ||
end | ||
|
||
# Service Subscription Approve | ||
# PUT /admin/api/accounts/{account_id}/service_subscriptions/{id}/approve.json | ||
def approve | ||
service_subscription.accept | ||
respond_with service_subscription | ||
end | ||
|
||
private | ||
|
||
def account | ||
@account ||= current_account.buyers.find params.require(:account_id) | ||
end | ||
|
||
def service_subscription | ||
@service_subscription ||= account.bought_service_contracts.find(params.require(:id)) | ||
end | ||
|
||
def service_plan | ||
@service_plan ||= ServicePlan.provided_by(current_account).find(service_subscription_plan_id) | ||
end | ||
|
||
def service_subscription_plan_id | ||
@service_subscription_plan_id ||= service_subscription_params[:plan_id] | ||
end | ||
|
||
def service_subscription_params | ||
@service_subscription_params ||= params.permit(service_subscription: [:plan_id]).fetch(:service_subscription) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module ServiceSubscriptionRepresenter | ||
include ThreeScale::JSONRepresenter | ||
wraps_resource :service_subscription | ||
|
||
property :id | ||
property :plan_id | ||
property :user_account_id | ||
property :created_at | ||
property :updated_at | ||
property :state | ||
property :paid_until | ||
property :trial_period_expires_at | ||
property :setup_fee | ||
property :type | ||
property :variable_cost_paid_until | ||
property :tenant_id | ||
property :service_id | ||
property :accepted_at | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class ServiceSubscriptionsRepresenter < ThreeScale::CollectionRepresenter | ||
include ThreeScale::JSONRepresenter | ||
wraps_resource :service_subscriptions | ||
items extend: ServiceSubscriptionRepresenter | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.