-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create dedicated service for updating thresholds
- Loading branch information
Showing
3 changed files
with
96 additions
and
76 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
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,93 @@ | ||
# frozen_string_literal: true | ||
|
||
module Plans | ||
class UpdateUsageThresholdsService < BaseService | ||
Result = BaseResult[:plan] | ||
|
||
def initialize(plan:, usage_threshold_params:) | ||
@plan = plan | ||
@usage_threshold_params = usage_threshold_params | ||
super | ||
end | ||
|
||
def call | ||
result.plan = plan | ||
return result unless plan.organization.progressive_billing_enabled? | ||
|
||
if usage_thresholds.empty? | ||
plan.usage_thresholds.discard_all | ||
else | ||
process_usage_thresholds | ||
end | ||
end | ||
|
||
private | ||
|
||
def process_usage_thresholds | ||
created_thresholds_ids = [] | ||
|
||
hash_thresholds = usage_threshold_params.map { |c| c.to_h.deep_symbolize_keys } | ||
hash_thresholds.each do |payload_threshold| | ||
usage_threshold = plan.usage_thresholds.find_by(id: payload_threshold[:id]) | ||
|
||
if usage_threshold | ||
if payload_threshold.key?(:threshold_display_name) | ||
usage_threshold.threshold_display_name = payload_threshold[:threshold_display_name] | ||
end | ||
|
||
if payload_threshold.key?(:amount_cents) | ||
usage_threshold.amount_cents = payload_threshold[:amount_cents] | ||
end | ||
|
||
if payload_threshold.key?(:recurring) | ||
usage_threshold.recurring = payload_threshold[:recurring] | ||
end | ||
|
||
# This means that in the UI we just removed an existing threshold | ||
# and then just re-added a threshold (which no longer has an id) with the same amount | ||
# so we discard the existing one and we're inserting a new one instead | ||
if !usage_threshold.valid? && usage_threshold.errors.where(:amount_cents, :taken).present? | ||
usage_threshold.discard! | ||
else | ||
usage_threshold.save! | ||
next | ||
end | ||
end | ||
|
||
plan = plan.reload | ||
|
||
created_threshold = create_usage_threshold(plan, payload_threshold) | ||
created_thresholds_ids.push(created_threshold.id) | ||
end | ||
# NOTE: Delete thresholds that are no more linked to the plan | ||
sanitize_thresholds(plan, hash_thresholds, created_thresholds_ids) | ||
end | ||
|
||
def sanitize_thresholds(plan, args_thresholds, created_thresholds_ids) | ||
args_thresholds_ids = args_thresholds.map { |c| c[:id] }.compact | ||
thresholds_ids = plan.usage_thresholds.pluck(:id) - args_thresholds_ids - created_thresholds_ids | ||
plan.usage_thresholds.where(id: thresholds_ids).discard_all | ||
end | ||
|
||
def create_usage_threshold(plan, params) | ||
usage_threshold = plan.usage_thresholds.find_or_initialize_by( | ||
recurring: params[:recurring] || false, | ||
amount_cents: params[:amount_cents] | ||
) | ||
|
||
existing_recurring_threshold = plan.usage_thresholds.recurring.first | ||
|
||
if params[:recurring] && existing_recurring_threshold | ||
usage_threshold = existing_recurring_threshold | ||
end | ||
|
||
usage_threshold.threshold_display_name = params[:threshold_display_name] | ||
usage_threshold.amount_cents = params[:amount_cents] | ||
|
||
usage_threshold.save! | ||
usage_threshold | ||
end | ||
|
||
attr_reader :plan, :usage_threshold_params | ||
end | ||
end |