-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (cascade-plan-updates): add logic for cascading charge removal (#…
…2715) ## Context Currently when plan is updated, these changes are not cascaded to all the children plans. ## Description This PR handles the case for cascading charge removal case
- Loading branch information
1 parent
faa7518
commit fb27588
Showing
7 changed files
with
191 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module Charges | ||
class DestroyJob < ApplicationJob | ||
queue_as 'default' | ||
|
||
def perform(charge:) | ||
Charges::DestroyService.call(charge:).raise_if_error! | ||
end | ||
end | ||
end |
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module Charges | ||
class DestroyService < BaseService | ||
def initialize(charge:) | ||
@charge = charge | ||
|
||
super | ||
end | ||
|
||
def call | ||
return result.not_found_failure!(resource: 'charge') unless charge | ||
|
||
ActiveRecord::Base.transaction do | ||
charge.discard! | ||
charge.filter_values.discard_all | ||
charge.filters.discard_all | ||
|
||
result.charge = charge | ||
end | ||
|
||
result | ||
rescue ActiveRecord::RecordInvalid => e | ||
result.record_validation_failure!(record: e.record) | ||
rescue BaseService::FailedResult => e | ||
e.result | ||
end | ||
|
||
private | ||
|
||
attr_reader :charge | ||
end | ||
end |
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,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Charges::DestroyJob, type: :job do | ||
let(:charge) { create(:standard_charge) } | ||
|
||
before do | ||
allow(Charges::DestroyService).to receive(:call).with(charge:).and_return(BaseService::Result.new) | ||
end | ||
|
||
it 'calls the service' do | ||
described_class.perform_now(charge:) | ||
|
||
expect(Charges::DestroyService).to have_received(:call) | ||
end | ||
end |
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,54 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Charges::DestroyService, type: :service do | ||
subject(:destroy_service) { described_class.new(charge:) } | ||
|
||
let(:membership) { create(:membership) } | ||
let(:organization) { membership.organization } | ||
let(:billable_metric) { create(:billable_metric, organization:) } | ||
let(:subscription) { create(:subscription) } | ||
let(:charge) { create(:standard_charge, plan: subscription.plan, billable_metric:) } | ||
|
||
let(:filters) { create_list(:billable_metric_filter, 2, billable_metric:) } | ||
let(:charge_filter) { create(:charge_filter, charge:) } | ||
let(:filter_value) do | ||
create(:charge_filter_value, charge_filter:, billable_metric_filter: filters.first) | ||
end | ||
|
||
before do | ||
charge | ||
filter_value | ||
end | ||
|
||
describe '#call' do | ||
it 'soft deletes the charge' do | ||
freeze_time do | ||
expect { destroy_service.call }.to change(Charge, :count).by(-1) | ||
.and change { charge.reload.deleted_at }.from(nil).to(Time.current) | ||
end | ||
end | ||
|
||
it 'soft deletes all related filters' do | ||
freeze_time do | ||
expect { destroy_service.call }.to change { charge_filter.reload.deleted_at }.from(nil).to(Time.current) | ||
end | ||
end | ||
|
||
it 'soft deletes all related filter values' do | ||
freeze_time do | ||
expect { destroy_service.call }.to change { filter_value.reload.deleted_at }.from(nil).to(Time.current) | ||
end | ||
end | ||
|
||
context 'when charge is not found' do | ||
it 'returns an error' do | ||
result = described_class.new(charge: nil).call | ||
|
||
expect(result).not_to be_success | ||
expect(result.error.error_code).to eq('charge_not_found') | ||
end | ||
end | ||
end | ||
end |
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