diff --git a/app/services/waste_carriers_engine/registration_activation_service.rb b/app/services/waste_carriers_engine/registration_activation_service.rb index 192d91aee..af3800851 100644 --- a/app/services/waste_carriers_engine/registration_activation_service.rb +++ b/app/services/waste_carriers_engine/registration_activation_service.rb @@ -1,15 +1,14 @@ # frozen_string_literal: true module WasteCarriersEngine - class UnpaidBalanceError < StandardError; end - class PendingConvictionsError < StandardError; end - class RegistrationActivationService < BaseService def run(registration:) @registration = registration + return unless can_be_completed? + registration.with_lock do - activate_registration if can_be_completed? + activate_registration end send_confirmation_email @@ -23,19 +22,19 @@ def activate_registration end def can_be_completed? - balance_is_paid? && no_pending_conviction_check? + balance_is_paid? && no_pending_conviction_check? && correct_status? end def balance_is_paid? - raise UnpaidBalanceError if @registration.unpaid_balance? + !@registration.unpaid_balance? + end - true + def correct_status? + @registration.pending? end def no_pending_conviction_check? - raise PendingConvictionsError if @registration.pending_manual_conviction_check? - - true + !@registration.pending_manual_conviction_check? end def send_confirmation_email diff --git a/spec/services/waste_carriers_engine/registration_activation_service_spec.rb b/spec/services/waste_carriers_engine/registration_activation_service_spec.rb index cac8e12a1..80280fd8e 100644 --- a/spec/services/waste_carriers_engine/registration_activation_service_spec.rb +++ b/spec/services/waste_carriers_engine/registration_activation_service_spec.rb @@ -35,16 +35,16 @@ module WasteCarriersEngine context "when the balance is unpaid" do before { allow(registration).to receive(:unpaid_balance?).and_return(true) } - it "raises an error" do - expect { service }.to raise_error(UnpaidBalanceError) + it "does not activates the registration" do + expect { service }.to_not change { registration.active? } end end context "when the registration has a pending convictions check" do before { allow(registration).to receive(:pending_manual_conviction_check?).and_return(true) } - it "raises an error" do - expect { service }.to raise_error(PendingConvictionsError) + it "does not activates the registration" do + expect { service }.to_not change { registration.active? } end end