refactor(service): dedicated route for prefill from siret

This commit is contained in:
Colin Darie 2024-11-04 12:47:42 +01:00
parent 384d089cb3
commit 8b3634ea76
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
4 changed files with 34 additions and 20 deletions

View file

@ -24,15 +24,7 @@ module Administrateurs
@service = Service.new(service_params)
@service.administrateur = current_administrateur
if request.xhr? && params[:service][:siret].present?
@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
if @service.save
@service.enqueue_api_entreprise
redirect_to admin_services_path(procedure_id: params[:procedure_id]),
@ -66,6 +58,19 @@ module Administrateurs
end
end
def prefill
@procedure = procedure
@service = Service.new(siret: params[:siret])
prefilled = handle_siret_prefill
render turbo_stream: turbo_stream.replace(
"service_form",
partial: "administrateurs/services/form",
locals: { service: @service, prefilled:, procedure: @procedure }
)
end
def add_to_procedure
procedure = current_administrateur.procedures.find(procedure_params[:id])
service = services.find(procedure_params[:service_id])
@ -137,6 +142,8 @@ module Administrateurs
end
end
# On prefill from SIRET, we only want to display errors for the SIRET input
# so we have to remove other errors (ie. required attributes not yet filled)
siret_errors = @service.errors.where(:siret)
@service.errors.clear
siret_errors.each { @service.errors.import(_1) }

View file

@ -1,6 +1,8 @@
= form_with model: [:admin, service], id: "service_form", data: { turbo: token_list('true' => service.new_record?), controller: token_list('autosave' => service.new_record?), turbo_method: 'post' } do |f|
= form_with model: [:admin, service], id: "service_form" do |f|
= render Dsfr::InputComponent.new(form: f, attribute: :siret, input_type: :text_field, opts: { placeholder: "14 chiffres, sans espace" }) do |c|
= render Dsfr::InputComponent.new(form: f, attribute: :siret, input_type: :text_field,
opts: { placeholder: "14 chiffres, sans espace",
onblur: token_list("Turbo.visit('#{prefill_admin_services_path(procedure_id: procedure.id)}?siret=' + this.value)" => service.new_record?) }) do |c|
- if service.etablissement_infos.blank? && local_assigns[:prefilled].nil?
- c.with_hint do
= "Indiquez le numéro de SIRET de lorganisme dont ce service dépend. Rechercher le SIRET sur "

View file

@ -734,6 +734,7 @@ Rails.application.routes.draw do
resources :services, except: [:show] do
collection do
patch 'add_to_procedure'
get ':procedure_id/prefill' => :prefill, as: :prefill
end
end

View file

@ -42,20 +42,18 @@ describe Administrateurs::ServicesController, type: :controller do
end
end
describe '#create' do
describe '#prefill' do
before do
sign_in(admin.user)
end
let(:xhr) { false }
subject { post :create, params:, xhr: }
subject { get :prefill, params:, xhr: true }
context 'when prefilling from a SIRET' do
let(:xhr) { true }
let(:params) do
{
procedure_id: procedure.id,
service: { siret: "20004021000060" }
siret: "20004021000060"
}
end
@ -70,11 +68,10 @@ describe Administrateurs::ServicesController, type: :controller do
end
context 'when attempting to prefilling from invalid SIRET' do
let(:xhr) { true }
let(:params) do
{
procedure_id: procedure.id,
service: { siret: "20004021000000" }
siret: "20004021000000"
}
end
@ -87,11 +84,10 @@ describe Administrateurs::ServicesController, type: :controller do
end
context 'when attempting to prefilling from not service public SIRET' do
let(:xhr) { true }
let(:params) do
{
procedure_id: procedure.id,
service: { siret: "41816609600051" }
siret: "41816609600051"
}
end
@ -105,6 +101,14 @@ describe Administrateurs::ServicesController, type: :controller do
end
end
end
end
describe '#create' do
before do
sign_in(admin.user)
end
subject { post :create, params: }
context 'when submitting a new service' do
let(:params) do