refactor(service): dedicated route for prefill from siret
This commit is contained in:
parent
384d089cb3
commit
8b3634ea76
4 changed files with 34 additions and 20 deletions
|
@ -24,15 +24,7 @@ module Administrateurs
|
||||||
@service = Service.new(service_params)
|
@service = Service.new(service_params)
|
||||||
@service.administrateur = current_administrateur
|
@service.administrateur = current_administrateur
|
||||||
|
|
||||||
if request.xhr? && params[:service][:siret].present?
|
if @service.save
|
||||||
@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
|
@service.enqueue_api_entreprise
|
||||||
|
|
||||||
redirect_to admin_services_path(procedure_id: params[:procedure_id]),
|
redirect_to admin_services_path(procedure_id: params[:procedure_id]),
|
||||||
|
@ -66,6 +58,19 @@ module Administrateurs
|
||||||
end
|
end
|
||||||
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
|
def add_to_procedure
|
||||||
procedure = current_administrateur.procedures.find(procedure_params[:id])
|
procedure = current_administrateur.procedures.find(procedure_params[:id])
|
||||||
service = services.find(procedure_params[:service_id])
|
service = services.find(procedure_params[:service_id])
|
||||||
|
@ -137,6 +142,8 @@ module Administrateurs
|
||||||
end
|
end
|
||||||
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)
|
siret_errors = @service.errors.where(:siret)
|
||||||
@service.errors.clear
|
@service.errors.clear
|
||||||
siret_errors.each { @service.errors.import(_1) }
|
siret_errors.each { @service.errors.import(_1) }
|
||||||
|
|
|
@ -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?
|
- if service.etablissement_infos.blank? && local_assigns[:prefilled].nil?
|
||||||
- c.with_hint do
|
- c.with_hint do
|
||||||
= "Indiquez le numéro de SIRET de l’organisme dont ce service dépend. Rechercher le SIRET sur "
|
= "Indiquez le numéro de SIRET de l’organisme dont ce service dépend. Rechercher le SIRET sur "
|
||||||
|
|
|
@ -734,6 +734,7 @@ Rails.application.routes.draw do
|
||||||
resources :services, except: [:show] do
|
resources :services, except: [:show] do
|
||||||
collection do
|
collection do
|
||||||
patch 'add_to_procedure'
|
patch 'add_to_procedure'
|
||||||
|
get ':procedure_id/prefill' => :prefill, as: :prefill
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -42,20 +42,18 @@ describe Administrateurs::ServicesController, type: :controller do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#create' do
|
describe '#prefill' do
|
||||||
before do
|
before do
|
||||||
sign_in(admin.user)
|
sign_in(admin.user)
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:xhr) { false }
|
subject { get :prefill, params:, xhr: true }
|
||||||
subject { post :create, params:, xhr: }
|
|
||||||
|
|
||||||
context 'when prefilling from a SIRET' do
|
context 'when prefilling from a SIRET' do
|
||||||
let(:xhr) { true }
|
|
||||||
let(:params) do
|
let(:params) do
|
||||||
{
|
{
|
||||||
procedure_id: procedure.id,
|
procedure_id: procedure.id,
|
||||||
service: { siret: "20004021000060" }
|
siret: "20004021000060"
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -70,11 +68,10 @@ describe Administrateurs::ServicesController, type: :controller do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when attempting to prefilling from invalid SIRET' do
|
context 'when attempting to prefilling from invalid SIRET' do
|
||||||
let(:xhr) { true }
|
|
||||||
let(:params) do
|
let(:params) do
|
||||||
{
|
{
|
||||||
procedure_id: procedure.id,
|
procedure_id: procedure.id,
|
||||||
service: { siret: "20004021000000" }
|
siret: "20004021000000"
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -87,11 +84,10 @@ describe Administrateurs::ServicesController, type: :controller do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when attempting to prefilling from not service public SIRET' do
|
context 'when attempting to prefilling from not service public SIRET' do
|
||||||
let(:xhr) { true }
|
|
||||||
let(:params) do
|
let(:params) do
|
||||||
{
|
{
|
||||||
procedure_id: procedure.id,
|
procedure_id: procedure.id,
|
||||||
service: { siret: "41816609600051" }
|
siret: "41816609600051"
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -105,6 +101,14 @@ describe Administrateurs::ServicesController, type: :controller do
|
||||||
end
|
end
|
||||||
end
|
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
|
context 'when submitting a new service' do
|
||||||
let(:params) do
|
let(:params) do
|
||||||
|
|
Loading…
Reference in a new issue