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
@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

View file

@ -8,4 +8,4 @@
%h1 Nouveau Service
= 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(: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)