feat(service): prefill first service with AgentConnect siret

This commit is contained in:
Colin Darie 2024-10-16 16:49:00 +02:00
parent 7742703081
commit 384d089cb3
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
3 changed files with 54 additions and 9 deletions

View file

@ -12,6 +12,12 @@ module Administrateurs
def new def new
@procedure = procedure @procedure = procedure
@service = Service.new @service = Service.new
siret = current_administrateur.instructeur.last_agent_connect_information&.siret
if siret
@service.siret = siret
@prefilled = handle_siret_prefill
end
end end
def create def create
@ -19,7 +25,13 @@ module Administrateurs
@service.administrateur = current_administrateur @service.administrateur = current_administrateur
if request.xhr? && params[:service][:siret].present? 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 elsif @service.save
@service.enqueue_api_entreprise @service.enqueue_api_entreprise
@ -111,8 +123,7 @@ module Administrateurs
current_administrateur.procedures.find(params[:procedure_id]) current_administrateur.procedures.find(params[:procedure_id])
end end
def handle_siret_update def handle_siret_prefill
@service.assign_attributes(siret: params[:service][:siret])
@service.validate @service.validate
if !@service.errors.include?(:siret) if !@service.errors.include?(:siret)
@ -130,11 +141,7 @@ module Administrateurs
@service.errors.clear @service.errors.clear
siret_errors.each { @service.errors.import(_1) } siret_errors.each { @service.errors.import(_1) }
render turbo_stream: turbo_stream.replace( prefilled
"service_form",
partial: "administrateurs/services/form",
locals: { service: @service, prefilled:, procedure: @procedure }
)
end end
end end
end end

View file

@ -8,4 +8,4 @@
%h1 Nouveau Service %h1 Nouveau Service
= render partial: 'form', = render partial: 'form',
locals: { service: @service, procedure: @procedure } locals: { service: @service, procedure: @procedure, prefilled: @prefilled }

View file

@ -4,6 +4,44 @@ describe Administrateurs::ServicesController, type: :controller do
let(:admin) { administrateurs(:default_admin) } let(:admin) { administrateurs(:default_admin) }
let(:procedure) { create(:procedure, administrateur: 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 describe '#create' do
before do before do
sign_in(admin.user) sign_in(admin.user)