From 384d089cb33d028c6826704c7d873f5da2a9250b Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Wed, 16 Oct 2024 16:49:00 +0200 Subject: [PATCH] feat(service): prefill first service with AgentConnect siret --- .../administrateurs/services_controller.rb | 23 +++++++---- .../administrateurs/services/new.html.haml | 2 +- .../services_controller_spec.rb | 38 +++++++++++++++++++ 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/app/controllers/administrateurs/services_controller.rb b/app/controllers/administrateurs/services_controller.rb index c025a0ff1..e3439307e 100644 --- a/app/controllers/administrateurs/services_controller.rb +++ b/app/controllers/administrateurs/services_controller.rb @@ -12,6 +12,12 @@ module Administrateurs def new @procedure = procedure @service = Service.new + + siret = current_administrateur.instructeur.last_agent_connect_information&.siret + if siret + @service.siret = siret + @prefilled = handle_siret_prefill + end end def create @@ -19,7 +25,13 @@ module Administrateurs @service.administrateur = current_administrateur if request.xhr? && params[:service][:siret].present? - handle_siret_update + @service.siret = params[:service][:siret] + prefilled = handle_siret_prefill + render turbo_stream: turbo_stream.replace( + "service_form", + partial: "administrateurs/services/form", + locals: { service: @service, prefilled:, procedure: @procedure } + ) elsif @service.save @service.enqueue_api_entreprise @@ -111,8 +123,7 @@ module Administrateurs current_administrateur.procedures.find(params[:procedure_id]) end - def handle_siret_update - @service.assign_attributes(siret: params[:service][:siret]) + def handle_siret_prefill @service.validate if !@service.errors.include?(:siret) @@ -130,11 +141,7 @@ module Administrateurs @service.errors.clear siret_errors.each { @service.errors.import(_1) } - render turbo_stream: turbo_stream.replace( - "service_form", - partial: "administrateurs/services/form", - locals: { service: @service, prefilled:, procedure: @procedure } - ) + prefilled end end end diff --git a/app/views/administrateurs/services/new.html.haml b/app/views/administrateurs/services/new.html.haml index 27a39f2b5..7477ad9b2 100644 --- a/app/views/administrateurs/services/new.html.haml +++ b/app/views/administrateurs/services/new.html.haml @@ -8,4 +8,4 @@ %h1 Nouveau Service = render partial: 'form', - locals: { service: @service, procedure: @procedure } + locals: { service: @service, procedure: @procedure, prefilled: @prefilled } diff --git a/spec/controllers/administrateurs/services_controller_spec.rb b/spec/controllers/administrateurs/services_controller_spec.rb index 510086688..77ff1a3ba 100644 --- a/spec/controllers/administrateurs/services_controller_spec.rb +++ b/spec/controllers/administrateurs/services_controller_spec.rb @@ -4,6 +4,44 @@ describe Administrateurs::ServicesController, type: :controller do let(:admin) { administrateurs(:default_admin) } let(:procedure) { create(:procedure, administrateur: admin) } + describe '#new' do + let(:admin) { administrateurs(:default_admin) } + let(:procedure) { create(:procedure, administrateur: admin) } + + before do + sign_in(admin.user) + end + + subject { get :new, params: { procedure_id: procedure.id } } + + context 'when admin has a SIRET from AgentConnect' do + let(:siret) { "20004021000060" } + + before do + agi = build(:agent_connect_information, siret:) + admin.instructeur.agent_connect_information << agi + end + + it 'prefills the SIRET and fetches service information' do + VCR.use_cassette("annuaire_service_public_success_#{siret}") do + subject + expect(assigns[:service].siret).to eq(siret) + expect(assigns[:service].nom).to eq("Communauté de communes - Lacs et Gorges du Verdon") + expect(assigns[:service].adresse).to eq("242 avenue Albert-1er 83630 Aups") + expect(assigns[:prefilled]).to eq(:success) + end + end + end + + context 'when admin has no SIRET from AgentConnect' do + it 'does not prefill the SIRET' do + subject + expect(assigns[:service].siret).to be_nil + expect(assigns[:prefilled]).to be_nil + end + end + end + describe '#create' do before do sign_in(admin.user)