From b93bac6bb9ff5a6763625476f98cfd3cfee4d94c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Semp=C3=A9?= Date: Tue, 5 Nov 2024 16:41:08 +0100 Subject: [PATCH] feat(dunning): Validate dunning campaign has at least one threshold --- app/services/dunning_campaigns/create_service.rb | 2 +- .../dunning_campaigns/bulk_process_service_spec.rb | 3 +-- .../dunning_campaigns/create_service_spec.rb | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/app/services/dunning_campaigns/create_service.rb b/app/services/dunning_campaigns/create_service.rb index fe2bbee1a463..b0e8bde59b41 100644 --- a/app/services/dunning_campaigns/create_service.rb +++ b/app/services/dunning_campaigns/create_service.rb @@ -11,7 +11,7 @@ def initialize(organization:, params:) def call return result.forbidden_failure! unless organization.auto_dunning_enabled? - # TODO: At least one threshold currency/amount pair is needed + return result.validation_failure!(errors: { thresholds: ["can't be blank"] }) if params[:thresholds].blank? ActiveRecord::Base.transaction do if params[:applied_to_organization] diff --git a/spec/services/dunning_campaigns/bulk_process_service_spec.rb b/spec/services/dunning_campaigns/bulk_process_service_spec.rb index 09653a16c270..0733dc36fa17 100644 --- a/spec/services/dunning_campaigns/bulk_process_service_spec.rb +++ b/spec/services/dunning_campaigns/bulk_process_service_spec.rb @@ -390,7 +390,7 @@ end end - context "when neither organizaiton nor customer has an applied dunning campaign" do + context "when neither organization nor customer has an applied dunning campaign" do let(:dunning_campaign) { create :dunning_campaign, organization:, applied_to_organization: false } let(:dunning_campaign_threshold) do @@ -403,7 +403,6 @@ end before do - dunning_campaign dunning_campaign_threshold invoice_1 end diff --git a/spec/services/dunning_campaigns/create_service_spec.rb b/spec/services/dunning_campaigns/create_service_spec.rb index b85dc6052a9a..9acd08e38219 100644 --- a/spec/services/dunning_campaigns/create_service_spec.rb +++ b/spec/services/dunning_campaigns/create_service_spec.rb @@ -121,6 +121,20 @@ expect(result.error.messages[:code]).to eq(["value_already_exist"]) end end + + context "without thresholds" do + let(:thresholds) { [] } + + it "returns an error" do + result = create_service.call + + aggregate_failures do + expect(result).not_to be_success + expect(result.error).to be_a(BaseService::ValidationFailure) + expect(result.error.messages[:thresholds]).to eq(["can't be blank"]) + end + end + end end end end