diff --git a/app/controllers/new_user/dossiers_controller.rb b/app/controllers/new_user/dossiers_controller.rb index c478d14b1..90976d902 100644 --- a/app/controllers/new_user/dossiers_controller.rb +++ b/app/controllers/new_user/dossiers_controller.rb @@ -69,11 +69,13 @@ module NewUser render :modifier else if @dossier.brouillon? + @dossier.en_construction! + NotificationMailer.send_notification(@dossier, @dossier.procedure.initiated_mail_template).deliver_now! redirect_to merci_dossier_path(@dossier) else + @dossier.en_construction! redirect_to users_dossier_recapitulatif_path(@dossier) end - @dossier.en_construction! end end diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 719682931..121693b89 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -106,13 +106,16 @@ class Users::DossiersController < UsersController update_current_user_siret!(siret) etablissement_attributes = SIRETService.fetch(siret, @facade.dossier) + Rails.logger.info("etablissement_attributes for siret: #{siret}, present?: #{etablissement_attributes.present?}") if etablissement_attributes.present? etablissement_attributes = ActionController::Parameters.new(etablissement_attributes).permit! etablissement = @facade.dossier.create_etablissement(etablissement_attributes) if etablissement.save + Rails.logger.info("etablissement saved, siret: #{siret}, id: #{etablissement.id}") @facade.dossier.mandataire_social!(current_user.france_connect_information) else + Rails.logger.info("etablissement not saved, siret: #{siret}, errors: #{etablissement.errors.full_messages}") return errors_valid_siret end else diff --git a/spec/controllers/new_user/dossiers_controller_spec.rb b/spec/controllers/new_user/dossiers_controller_spec.rb index 3839bed55..68c68a9b1 100644 --- a/spec/controllers/new_user/dossiers_controller_spec.rb +++ b/spec/controllers/new_user/dossiers_controller_spec.rb @@ -167,6 +167,20 @@ describe NewUser::DossiersController, type: :controller do expect(dossier.reload.state).to eq('en_construction') end + it 'sends an email only on the first #update' do + delivery = double + expect(delivery).to receive(:deliver_now!).with(no_args) + + expect(NotificationMailer).to receive(:send_notification) + .and_return(delivery) + + subject + + expect(NotificationMailer).not_to receive(:send_notification) + + subject + end + context 'when the update fails' do before do expect_any_instance_of(Dossier).to receive(:update).and_return(false) @@ -178,6 +192,12 @@ describe NewUser::DossiersController, type: :controller do it { expect(response).to render_template(:modifier) } it { expect(flash.alert).to eq(['nop']) } + + it 'does not send an email' do + expect(NotificationMailer).not_to receive(:send_notification) + + subject + end end context 'when the pj service returns an error' do