Skip to content

Commit

Permalink
Merge pull request #3759 from 3scale/service-subscription-api
Browse files Browse the repository at this point in the history
Service Subscription API: create, show, change plan, approve
  • Loading branch information
mayorova committed May 9, 2024
2 parents 7c23d04 + c739ec6 commit 52b8d36
Show file tree
Hide file tree
Showing 12 changed files with 623 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/controllers/admin/api/service_contracts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class Admin::Api::ServiceContractsController < Admin::Api::ServiceBaseController
before_action :deny_on_premises_for_master
before_action :authorize_service_plans!

# Service Subscription List
# [DEPRECATED] Service Subscription List
# GET /admin/api/accounts/{account_id}/service_contracts.xml
def index
respond_with account.bought_service_contracts
end

# Service Subscription Delete
# [DEPRECATED] Service Subscription Delete
# DELETE /admin/api/accounts/{account_id}/service_contracts/{id}.xml
def destroy
service_subscription = ServiceSubscriptionService.new(account)
Expand Down
71 changes: 71 additions & 0 deletions app/controllers/admin/api/service_subscriptions_controller.rb
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
7 changes: 7 additions & 0 deletions app/models/service_contract.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class ServiceContract < Contract
include Logic::Contracting::ServiceContract

validate :same_service_plan_update, on: :update, if: :plan_id_changed?

before_create :accept_on_create, :unless => :live?

before_create :set_service_id
Expand Down Expand Up @@ -57,4 +59,9 @@ def legal_terms_acceptance_on?
@legal_terms_acceptance
end

def same_service_plan_update
return if plan.blank?

errors.add(:plan, :service_conflict) if Plan.find_by(id: plan_id_was)&.issuer_id != plan.issuer_id
end
end
21 changes: 21 additions & 0 deletions app/representers/service_subscription_representer.rb
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
7 changes: 7 additions & 0 deletions app/representers/service_subscriptions_representer.rb
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
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,9 @@ en:
unsuspended_applications:
one: 'There is 1 unsuspended application subscribed to the service'
other: 'There are %{count} unsuspended applications subscribed to the service'
plan:
service_conflict: 'must belong to the same product'

invoice:
attributes:
state:
Expand Down
7 changes: 7 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,13 @@

resources :service_contracts, :only => [:index, :destroy]

resources :service_subscriptions, constraints: { format: :json }, defaults: { format: :json }, except: %i[new edit update] do
member do
put :change_plan
put :approve
end
end

resources :messages, :only => [:create]
end

Expand Down

0 comments on commit 52b8d36

Please sign in to comment.