Skip to content

Commit

Permalink
fix: Set ready_to_be_refreshed only when updating a plan
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe authored and jdenquin committed Jul 24, 2024
1 parent 3f06925 commit 35cef0e
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 39 deletions.
2 changes: 0 additions & 2 deletions app/services/charges/apply_taxes_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ def call
charge.applied_taxes.find_or_create_by!(tax: taxes.find_by(code: tax_code))
end

charge.plan.invoices.draft.update_all(ready_to_be_refreshed: true) # rubocop:disable Rails/SkipsModelValidations

result
rescue ActiveRecord::RecordInvalid => e
result.record_validation_failure!(record: e.record)
Expand Down
2 changes: 0 additions & 2 deletions app/services/plans/apply_taxes_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ def call
plan.applied_taxes.find_or_create_by!(tax: taxes.find_by(code: tax_code))
end

plan.invoices.draft.update_all(ready_to_be_refreshed: true) # rubocop:disable Rails/SkipsModelValidations

result
rescue ActiveRecord::RecordInvalid => e
result.record_validation_failure!(record: e.record)
Expand Down
7 changes: 2 additions & 5 deletions app/services/plans/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def call
end
end

plan.invoices.draft.update_all(ready_to_be_refreshed: true) # rubocop:disable Rails/SkipsModelValidations

result.plan = plan.reload
result
rescue ActiveRecord::RecordInvalid => e
Expand Down Expand Up @@ -216,16 +218,11 @@ def sanitize_charges(plan, args_charges, created_charges_ids)
end

def discard_charge!(charge)
draft_invoice_ids = Invoice.draft.joins(plans: [:charges])
.where(charges: {id: charge.id}).distinct.pluck(:id)

charge.discard!
charge.group_properties.discard_all

charge.filter_values.discard_all
charge.filters.discard_all

Invoice.where(id: draft_invoice_ids).update_all(ready_to_be_refreshed: true) # rubocop:disable Rails/SkipsModelValidations
end

# NOTE: We should remove pending subscriptions
Expand Down
8 changes: 0 additions & 8 deletions spec/services/charges/apply_taxes_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@
expect(Charge::AppliedTax.find_by(id: existing.id)).to be_nil
end

it 'marks invoices as ready to be refreshed' do
subscription = create(:subscription, plan:)
invoice = create(:invoice, :draft, organization: plan.organization)
create(:invoice_subscription, invoice:, subscription:)

expect { apply_service.call }.to change { invoice.reload.ready_to_be_refreshed }.to(true)
end

it 'returns applied taxes' do
result = apply_service.call
expect(result.applied_taxes.count).to eq(2)
Expand Down
8 changes: 0 additions & 8 deletions spec/services/plans/apply_taxes_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@
expect { apply_service.call }.to change { plan.applied_taxes.count }.from(0).to(2)
end

it 'marks invoices as ready to be refreshed' do
subscription = create(:subscription, organization:, plan:)
invoice = create(:invoice, :draft)
create(:invoice_subscription, invoice:, subscription:)

expect { apply_service.call }.to change { invoice.reload.ready_to_be_refreshed }.to(true)
end

it 'returns applied taxes' do
result = apply_service.call
expect(result.applied_taxes.count).to eq(2)
Expand Down
22 changes: 8 additions & 14 deletions spec/services/plans/update_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@
end
end

it 'marks invoices as ready to be refreshed' do
subscription = create(:subscription, organization:, plan:)
invoice = create(:invoice, :draft)
create(:invoice_subscription, invoice:, subscription:)

expect { plans_service.call }.to change { invoice.reload.ready_to_be_refreshed }.to(true)
end

context 'when charges are not passed' do
let(:charge) { create(:standard_charge, plan:) }
let(:update_args) do
Expand Down Expand Up @@ -692,20 +700,6 @@
.to change { charge.reload.deleted_at }.from(nil).to(Time.current)
end
end

it 'discards group properties related to the charge' do
freeze_time do
expect { plans_service.call }
.to change { group_property.reload.deleted_at }.from(nil).to(Time.current)
end
end

it 'marks invoices as ready to be refreshed' do
invoice = create(:invoice, :draft)
create(:invoice_subscription, subscription:, invoice:)

expect { plans_service.call }.to change { invoice.reload.ready_to_be_refreshed }.to(true)
end
end

context 'when attached to a subscription' do
Expand Down

0 comments on commit 35cef0e

Please sign in to comment.