Add record to track overridden limits
Before, we were relying on just changing the cards_count in account, but this could create problems where the system to calculate the next card number fails due to the unique constraint.
This commit is contained in:
@@ -5,8 +5,6 @@ module Account::Billing
|
||||
has_one :subscription, class_name: "Account::Subscription", dependent: :destroy
|
||||
end
|
||||
|
||||
NEAR_CARD_LIMIT_THRESHOLD = 100
|
||||
|
||||
def plan
|
||||
active_subscription&.plan || Plan.free
|
||||
end
|
||||
@@ -18,12 +16,4 @@ module Account::Billing
|
||||
def subscribed?
|
||||
subscription.present?
|
||||
end
|
||||
|
||||
def nearing_plan_cards_limit?
|
||||
plan.limit_cards? && (plan.card_limit - cards_count) < NEAR_CARD_LIMIT_THRESHOLD
|
||||
end
|
||||
|
||||
def exceeding_card_limit?
|
||||
cards_count > plan.card_limit
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
module Account::Limited
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
has_one :overridden_limits, class_name: "Account::OverriddenLimits", dependent: :destroy
|
||||
end
|
||||
|
||||
NEAR_CARD_LIMIT_THRESHOLD = 100
|
||||
|
||||
def override_limits(card_count:)
|
||||
if overridden_limits
|
||||
overridden_limits.update(card_count: card_count)
|
||||
else
|
||||
create_overridden_limits(card_count: card_count)
|
||||
end
|
||||
end
|
||||
|
||||
def billed_cards_count
|
||||
overridden_limits&.card_count || cards_count
|
||||
end
|
||||
|
||||
def nearing_plan_cards_limit?
|
||||
plan.limit_cards? && (plan.card_limit - billed_cards_count) < NEAR_CARD_LIMIT_THRESHOLD
|
||||
end
|
||||
|
||||
def exceeding_card_limit?
|
||||
plan.limit_cards? && billed_cards_count > plan.card_limit
|
||||
end
|
||||
|
||||
def reset_overridden_limits
|
||||
overridden_limits&.destroy
|
||||
reload_overridden_limits
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,4 @@
|
||||
# To ease testing of limits
|
||||
class Account::OverriddenLimits < SaasRecord
|
||||
belongs_to :account
|
||||
end
|
||||
Reference in New Issue
Block a user