From 8b3634ea7658810b6a9a1c64bac3a7fffa51cefa Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Mon, 4 Nov 2024 12:47:42 +0100 Subject: [PATCH] refactor(service): dedicated route for prefill from siret --- .../administrateurs/services_controller.rb | 25 ++++++++++++------- .../administrateurs/services/_form.html.haml | 6 +++-- config/routes.rb | 1 + .../services_controller_spec.rb | 22 +++++++++------- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/app/controllers/administrateurs/services_controller.rb b/app/controllers/administrateurs/services_controller.rb index e3439307e..7fc329efd 100644 --- a/app/controllers/administrateurs/services_controller.rb +++ b/app/controllers/administrateurs/services_controller.rb @@ -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) } diff --git a/app/views/administrateurs/services/_form.html.haml b/app/views/administrateurs/services/_form.html.haml index f9791c198..8a463a3d7 100644 --- a/app/views/administrateurs/services/_form.html.haml +++ b/app/views/administrateurs/services/_form.html.haml @@ -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 l’organisme dont ce service dépend. Rechercher le SIRET sur " diff --git a/config/routes.rb b/config/routes.rb index d194eb920..170ce0b4f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/controllers/administrateurs/services_controller_spec.rb b/spec/controllers/administrateurs/services_controller_spec.rb index 77ff1a3ba..e8bc20c5d 100644 --- a/spec/controllers/administrateurs/services_controller_spec.rb +++ b/spec/controllers/administrateurs/services_controller_spec.rb @@ -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