Skip to content

Commit

Permalink
feat(ProgressiveBilling): Usage threshold should be soft deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet committed Aug 19, 2024
1 parent e30357d commit 1dc366a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
4 changes: 4 additions & 0 deletions app/models/usage_threshold.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
class UsageThreshold < ApplicationRecord
include PaperTrailTraceable
include Currencies
include Discard::Model
self.discard_column = :deleted_at

belongs_to :plan

Expand All @@ -14,4 +16,6 @@ class UsageThreshold < ApplicationRecord

scope :recurring, -> { where(recurring: true) }
scope :not_recurring, -> { where(recurring: false) }

default_scope -> { kept }
end
2 changes: 1 addition & 1 deletion app/services/plans/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def sanitize_charges(plan, args_charges, created_charges_ids)
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).destroy_all
plan.usage_thresholds.where(id: thresholds_ids).discard_all
end

def discard_charge!(charge)
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20240819092354_add_deleted_at_to_usage_thresholds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class ¨ < ActiveRecord::Migration[7.1]
def change
add_column :usage_thresholds, :deleted_at, :datetime

remove_index :usage_thresholds, %i[amount_cents plan_id recurring], unique: true
remove_index :usage_thresholds, %i[plan_id recurring], unique: true, where: "recurring is true"

add_index :usage_thresholds, %i[amount_cents plan_id recurring], unique: true, where: 'deleted_at IS NULL'
add_index :usage_thresholds, %i[plan_id recurring], unique: true, where: "recurring is true and deleted_at IS NULL"
end
end
7 changes: 4 additions & 3 deletions db/schema.rb

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

9 changes: 9 additions & 0 deletions spec/models/usage_threshold_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@
subject(:usage_threshold) { build(:usage_threshold) }

it { is_expected.to validate_numericality_of(:amount_cents).is_greater_than(0) }

describe 'default scope' do
let!(:deleted_usage_threshold) { create(:usage_threshold, :deleted) }

it "only returns non-deleted usage_threshold objects" do
expect(described_class.all).to eq([])
expect(described_class.unscoped.discarded).to eq([deleted_usage_threshold])
end
end
end

0 comments on commit 1dc366a

Please sign in to comment.