Skip to content

Commit

Permalink
when dunning campaign is applied to organization on creation ...
Browse files Browse the repository at this point in the history
previous dunning campaign applied to organzation is "un-applied"
  • Loading branch information
ancorcruz authored and rsempe committed Nov 4, 2024
1 parent f03dbb4 commit fa98d20
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
9 changes: 7 additions & 2 deletions app/services/dunning_campaigns/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ def call
# TODO: At least one threshold currency/amount pair is needed

ActiveRecord::Base.transaction do
if params[:applied_to_organization]
organization
.dunning_campaigns
.applied_to_organization
.update_all(applied_to_organization: false) # rubocop:disable Rails/SkipsModelValidations
end

dunning_campaign = organization.dunning_campaigns.create!(
applied_to_organization: params[:applied_to_organization],
code: params[:code],
Expand All @@ -24,8 +31,6 @@ def call
thresholds_attributes: params[:thresholds].map(&:to_h)
)

# TODO: If the dunning campaign is applied to the organization, we need to remove the flag from all other dunning campaigns.

result.dunning_campaign = dunning_campaign
end

Expand Down
43 changes: 42 additions & 1 deletion spec/services/dunning_campaigns/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
days_between_attempts: 1,
max_attempts: 3,
description: "Dunning Campaign Description",
applied_to_organization: true,
applied_to_organization:,
thresholds:
}
end

let(:applied_to_organization) { false }

let(:thresholds) do
[
{amount_cents: 10000, currency: "USD"},
Expand Down Expand Up @@ -67,6 +69,45 @@
expect(result.dunning_campaign.thresholds.first).to be_a(DunningCampaignThreshold)
end

context "with a previous dunning campaign set as applied_to_organization" do
let(:dunning_campaign_2) do
create(:dunning_campaign, organization:, applied_to_organization: true)
end

before { dunning_campaign_2 }

it "does not change previous dunning campaign applied_to_organization" do
expect { create_service.call }
.not_to change(dunning_campaign_2.reload, :applied_to_organization)
end
end

context "with applied_to_organization true" do
let(:applied_to_organization) { true }

it "updates the dunning campaign" do
result = create_service.call

expect(result).to be_success
expect(result.dunning_campaign.applied_to_organization).to eq(true)
end

context "with a previous dunning campaign set as applied_to_organization" do
let(:dunning_campaign_2) do
create(:dunning_campaign, organization:, applied_to_organization: true)
end

before { dunning_campaign_2 }

it "removes applied_to_organization from previous dunning campaign" do
expect { create_service.call }
.to change { dunning_campaign_2.reload.applied_to_organization }
.from(true)
.to(false)
end
end
end

context "with validation error" do
before do
create(:dunning_campaign, organization:, code: "dunning-campaign")
Expand Down

0 comments on commit fa98d20

Please sign in to comment.