Skip to content

Commit

Permalink
feat(tresholds): Add GraphQL for usage tresholds, refactor services
Browse files Browse the repository at this point in the history
  • Loading branch information
ivannovosad committed Aug 16, 2024
1 parent e764937 commit 6915bf8
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
module Types
module Subscriptions
class UsageThresholdOverridesInput < Types::BaseInputObject
argument :id, ID, required: true

argument :amount_cents, GraphQL::Types::BigInt, required: false
argument :amount_cents, GraphQL::Types::BigInt, required: true
argument :recurring, Boolean, required: false
argument :threshold_display_name, String, required: false
end
Expand Down
13 changes: 5 additions & 8 deletions app/services/plans/override_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,11 @@ def call
Charges::OverrideService.call(charge:, params: charge_params)
end

if License.premium? && plan.organization.premium_integrations.include?('progressive_billing')
plan.usage_thresholds.find_each do |threshold|
threshold_params = (
params[:usage_thresholds]&.find { |p| p[:id] == threshold.id } || {}
).merge(plan_id: new_plan.id)

UsageThresholds::OverrideService.call(threshold:, params: threshold_params)
end
if params[:usage_thresholds].present? &&
License.premium? &&
plan.organization.premium_integrations.include?('progressive_billing')

UsageThresholds::OverrideService.call(usage_thresholds_params: params[:usage_thresholds], new_plan: new_plan)
end

if params[:minimum_commitment].present? && License.premium?
Expand Down
29 changes: 15 additions & 14 deletions app/services/usage_thresholds/override_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,36 @@

module UsageThresholds
class OverrideService < BaseService
def initialize(threshold:, params:)
@threshold = threshold
@params = params
def initialize(usage_thresholds_params:, new_plan:)
@usage_thresholds_params = usage_thresholds_params
@new_plan = new_plan

super
end

def call
ActiveRecord::Base.transaction do
new_threshold = threshold.dup.tap do |c|
c.amount_cents = params[:amount_cents] if params.key?(:amount_cents)
c.recurring = params[:recurring] if params.key?(:recurring)
c.threshold_display_name = params[:threshold_display_name] if params.key?(:threshold_display_name)
c.plan_id = params[:plan_id]
end
new_threshold.save!
usage_thresholds_params.each do |params|
usage_threshold = new_plan.usage_thresholds.new(
plan_id: new_plan.id,
threshold_display_name: params[:threshold_display_name],
amount_cents: params[:amount_cents],
recurring: params[:recurring] || false
)

result.usage_threshold = new_threshold
usage_threshold.save!
end
end

result.usage_thresholds = new_plan.usage_thresholds
result
rescue ActiveRecord::RecordInvalid => e
result.record_validation_failure!(record: e.record)
rescue BaseService::FailedResult => e
e.result
result.fail_with_error!
end

private

attr_reader :threshold, :params
attr_reader :usage_thresholds_params, :new_plan
end
end
3 changes: 1 addition & 2 deletions schema.graphql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 2 additions & 14 deletions schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions spec/graphql/mutations/subscriptions/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
id
amountCents
usageThresholds {
id
amountCents
thresholdDisplayName
}
Expand Down Expand Up @@ -70,7 +69,7 @@
invoiceDisplayName: 'invoice display name'
],
usageThresholds: [
id: threshold.id,
amountCents: 100,
thresholdDisplayName: 'threshold display name'
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
RSpec.describe Types::Subscriptions::UsageThresholdOverridesInput do
subject { described_class }

it { is_expected.to accept_argument(:id).of_type('ID!') }
it { is_expected.to accept_argument(:amount_cents).of_type('BigInt') }
it { is_expected.to accept_argument(:amount_cents).of_type('BigInt!') }
it { is_expected.to accept_argument(:recurring).of_type('Boolean') }
it { is_expected.to accept_argument(:threshold_display_name).of_type('String') }
end
4 changes: 0 additions & 4 deletions spec/requests/api/v1/subscriptions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
amount_cents: commitment_amount_cents
},
usage_thresholds: [
id: usage_threshold.id,
amount_cents: override_amount_cents,
threshold_display_name: override_display_name
]
Expand All @@ -43,9 +42,6 @@

let(:override_amount_cents) { 777 }
let(:override_display_name) { 'Overriden Threshold 12' }
let(:usage_threshold) { create(:usage_threshold, plan:) }

before { usage_threshold }

it 'returns a success', :aggregate_failures do
create(:plan, code: plan.code, parent_id: plan.id, organization:, description: 'foo')
Expand Down
17 changes: 9 additions & 8 deletions spec/services/usage_thresholds/override_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'rails_helper'

RSpec.describe UsageThresholds::OverrideService, type: :service do
subject(:override_service) { described_class.new(threshold:, params:) }
subject(:override_service) { described_class.new(usage_thresholds_params:, new_plan: plan) }

let(:membership) { create(:membership) }
let(:organization) { membership.organization }
Expand All @@ -12,13 +12,14 @@
let(:threshold) { create(:usage_threshold, plan:) }
let(:plan) { create(:plan, organization:) }

let(:params) do
{
id: threshold.id,
plan_id: plan.id,
threshold_display_name: 'Overriden threshold',
amount_cents: 1000
}
let(:usage_thresholds_params) do
[
{
plan_id: plan.id,
threshold_display_name: 'Overriden threshold',
amount_cents: 1000
}
]
end

before { threshold }
Expand Down

0 comments on commit 6915bf8

Please sign in to comment.