diff --git a/app/controllers/administrateurs/services_controller.rb b/app/controllers/administrateurs/services_controller.rb index 1126f29f7..19567336e 100644 --- a/app/controllers/administrateurs/services_controller.rb +++ b/app/controllers/administrateurs/services_controller.rb @@ -15,6 +15,8 @@ module Administrateurs @service.administrateur = current_administrateur if @service.save + @service.enqueue_api_entreprise + redirect_to admin_services_path(procedure_id: params[:procedure_id]), notice: "#{@service.nom} créé" else @@ -33,6 +35,10 @@ module Administrateurs @service = service if @service.update(service_params) + if @service.siret_previously_changed? + @service.enqueue_api_entreprise + end + redirect_to admin_services_path(procedure_id: params[:procedure_id]), notice: "#{@service.nom} modifié" else diff --git a/app/models/service.rb b/app/models/service.rb index bac400e41..5bd98d3a5 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -45,8 +45,6 @@ class Service < ApplicationRecord validates :adresse, presence: { message: 'doit être renseignée' }, allow_nil: false validates :administrateur, presence: { message: 'doit être renseigné' }, allow_nil: false - after_commit :enqueue_api_entreprise, if: -> { siret_previously_changed? } - def clone_and_assign_to_administrateur(administrateur) service_cloned = self.dup service_cloned.administrateur = administrateur @@ -67,8 +65,6 @@ class Service < ApplicationRecord [etablissement_lat, etablissement_lng] end - private - def enqueue_api_entreprise APIEntreprise::ServiceJob.perform_later(self.id) end diff --git a/spec/controllers/administrateurs/services_controller_spec.rb b/spec/controllers/administrateurs/services_controller_spec.rb index 71c075ae9..121188c1f 100644 --- a/spec/controllers/administrateurs/services_controller_spec.rb +++ b/spec/controllers/administrateurs/services_controller_spec.rb @@ -34,7 +34,8 @@ describe Administrateurs::ServicesController, type: :controller do it { expect(Service.last.telephone).to eq('1234') } it { expect(Service.last.horaires).to eq('horaires') } it { expect(Service.last.adresse).to eq('adresse') } - it { expect(Service.last.siret).to eq('35600082800018') } + it { expect(Service.last.siret).to eq('35600082800018') } + it { expect(APIEntreprise::ServiceJob).to have_been_enqueued.with(Service.last.id) } it { expect(response).to redirect_to(admin_services_path(procedure_id: procedure.id)) } end @@ -49,7 +50,7 @@ describe Administrateurs::ServicesController, type: :controller do describe '#update' do let!(:service) { create(:service, administrateur: admin) } - let(:service_params) { { nom: 'nom', type_organisme: Service.type_organismes.fetch(:association) } } + let(:service_params) { { nom: 'nom', type_organisme: Service.type_organismes.fetch(:association), siret: "13002526500013" } } before do sign_in(admin.user) @@ -67,6 +68,7 @@ describe Administrateurs::ServicesController, type: :controller do it { expect(Service.last.nom).to eq('nom') } it { expect(Service.last.type_organisme).to eq(Service.type_organismes.fetch(:association)) } it { expect(response).to redirect_to(admin_services_path(procedure_id: procedure.id)) } + it { expect(APIEntreprise::ServiceJob).to have_been_enqueued.with(service.id) } end context 'when updating a service with invalid data' do diff --git a/spec/models/service_spec.rb b/spec/models/service_spec.rb index 95abc24a6..c29313d1c 100644 --- a/spec/models/service_spec.rb +++ b/spec/models/service_spec.rb @@ -87,25 +87,6 @@ describe Service, type: :model do end end - describe "API Entreprise job" do - subject { create(:service) } - it "should enqueue a job when created" do - expect(APIEntreprise::ServiceJob).to have_been_enqueued.with(subject.id) - end - - it "should enqueue a job when siret changed" do - subject.update(siret: "35600082800018") - expect(APIEntreprise::ServiceJob).to have_been_enqueued.with(subject.id) - end - - it "should not enqueue a job when siret is unchanged" do - subject - clear_enqueued_jobs - subject.update(telephone: "09879789") - expect(APIEntreprise::ServiceJob).not_to have_been_enqueued - end - end - describe "etablissement adresse & geo coordinates" do subject { create(:service, etablissement_lat: latitude, etablissement_lng: longitude, etablissement_infos: etablissement_infos) }