From fa98d200ca275e7b6337e1d091ba90bfb7451c31 Mon Sep 17 00:00:00 2001 From: Ancor Cruz Date: Thu, 31 Oct 2024 12:51:03 +0000 Subject: [PATCH] when dunning campaign is applied to organization on creation ... previous dunning campaign applied to organzation is "un-applied" --- .../dunning_campaigns/create_service.rb | 9 +++- .../dunning_campaigns/create_service_spec.rb | 43 ++++++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/app/services/dunning_campaigns/create_service.rb b/app/services/dunning_campaigns/create_service.rb index 2acf74c8db7..fe2bbee1a46 100644 --- a/app/services/dunning_campaigns/create_service.rb +++ b/app/services/dunning_campaigns/create_service.rb @@ -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], @@ -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 diff --git a/spec/services/dunning_campaigns/create_service_spec.rb b/spec/services/dunning_campaigns/create_service_spec.rb index e794b911581..b85dc6052a9 100644 --- a/spec/services/dunning_campaigns/create_service_spec.rb +++ b/spec/services/dunning_campaigns/create_service_spec.rb @@ -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"}, @@ -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")