Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ProgressiveBilling): Usage threshold should be soft deleted #2429

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 AddDeletedAtToUsageThresholds < 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