Cleanup SIRET champ
This commit is contained in:
parent
1934fdfc4d
commit
55a9b31b93
13 changed files with 183 additions and 81 deletions
|
@ -12,7 +12,6 @@
|
|||
}
|
||||
|
||||
.right-spinner {
|
||||
display: none;
|
||||
position: absolute;
|
||||
top: 3.7em;
|
||||
right: 1.2em;
|
||||
|
|
|
@ -1,31 +1,51 @@
|
|||
class Champs::SiretController < ApplicationController
|
||||
def index
|
||||
siret, champ_id = params.required([:siret, :champ_id])
|
||||
@champ = Champs::SiretChamp.find(champ_id)
|
||||
@etablissement = @champ.etablissement
|
||||
if siret == 'blank'
|
||||
if @etablissement
|
||||
@etablissement.mark_for_destruction
|
||||
end
|
||||
@blank = true
|
||||
elsif siret == 'invalid'
|
||||
if @etablissement
|
||||
@etablissement.mark_for_destruction
|
||||
end
|
||||
@error = "Le numéro de SIRET doit comporter exactement 14 chiffres."
|
||||
else
|
||||
etablissement_attributes = ApiEntrepriseService.get_etablissement_params_for_siret(siret, @champ.dossier.procedure_id)
|
||||
if etablissement_attributes.present?
|
||||
@etablissement = @champ.build_etablissement(etablissement_attributes)
|
||||
@etablissement.champ = @champ
|
||||
before_action :authenticate_logged_user!
|
||||
|
||||
def show
|
||||
@position = params[:position]
|
||||
extract_siret
|
||||
find_etablisement
|
||||
|
||||
if @siret.empty?
|
||||
@etablissement&.mark_for_destruction
|
||||
elsif @siret.present? && @siret.length == 14
|
||||
etablissement = find_etablisement_with_siret
|
||||
if etablissement.present?
|
||||
@etablissement = etablissement
|
||||
else
|
||||
message = ['Nous n’avons pas trouvé d’établissement correspondant à ce numéro de SIRET.']
|
||||
message << helpers.link_to('Plus d’informations', "https://faq.demarches-simplifiees.fr/article/4-erreur-siret", target: '_blank')
|
||||
@error = helpers.safe_join(message, ' ')
|
||||
@etablissement&.mark_for_destruction
|
||||
@siret = :not_found
|
||||
end
|
||||
else
|
||||
@etablissement&.mark_for_destruction
|
||||
@siret = :invalid
|
||||
end
|
||||
respond_to do |format|
|
||||
format.js
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def extract_siret
|
||||
if params[:dossier].key?(:champs_attributes)
|
||||
@siret = params[:dossier][:champs_attributes][@position][:value]
|
||||
@attribute = "dossier[champs_attributes][#{@position}][etablissement_attributes]"
|
||||
else
|
||||
@siret = params[:dossier][:champs_private_attributes][@position][:value]
|
||||
@attribute = "dossier[champs_private_attributes][#{@position}][etablissement_attributes]"
|
||||
end
|
||||
end
|
||||
|
||||
def find_etablisement
|
||||
if params[:champ_id].present?
|
||||
champ = Champ.find_by(dossier_id: logged_user.dossiers, id: params[:champ_id])
|
||||
@etablissement = champ&.etablissement
|
||||
end
|
||||
@procedure_id = champ&.dossier&.procedure_id || 'aperçu'
|
||||
end
|
||||
|
||||
def find_etablisement_with_siret
|
||||
etablissement_attributes = ApiEntrepriseService.get_etablissement_params_for_siret(@siret, @procedure_id)
|
||||
if etablissement_attributes.present?
|
||||
Etablissement.new(etablissement_attributes)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
import $ from 'jquery';
|
||||
|
||||
$(document).on('input', '[data-siret]', evt => {
|
||||
const input = $(evt.target);
|
||||
const value = input.val();
|
||||
const url = input.data('siret');
|
||||
|
||||
switch (value.length) {
|
||||
case 0:
|
||||
input.removeData('invalid');
|
||||
$.get(url, { siret: 'blank' });
|
||||
break;
|
||||
case 14:
|
||||
input.attr('disabled', true);
|
||||
$('.spinner').show();
|
||||
$.get(url, { siret: value }).then(
|
||||
() => {
|
||||
input.removeData('invalid');
|
||||
input.removeAttr('disabled');
|
||||
$('.spinner').hide();
|
||||
},
|
||||
() => {
|
||||
input.removeAttr('disabled');
|
||||
input.data('invalid', true);
|
||||
$('.spinner').hide();
|
||||
}
|
||||
);
|
||||
break;
|
||||
default:
|
||||
if (!input.data('invalid')) {
|
||||
input.data('invalid', true);
|
||||
$.get(url, { siret: 'invalid' });
|
||||
}
|
||||
}
|
||||
});
|
16
app/javascript/new_design/spinner.js
Normal file
16
app/javascript/new_design/spinner.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
import Rails from 'rails-ujs';
|
||||
import { show, hide } from '../shared/utils';
|
||||
|
||||
const { delegate } = Rails;
|
||||
|
||||
function showSpinner() {
|
||||
[...document.querySelectorAll('.spinner')].forEach(show);
|
||||
}
|
||||
|
||||
function hideSpinner() {
|
||||
[...document.querySelectorAll('.spinner')].forEach(hide);
|
||||
}
|
||||
|
||||
delegate(document, '[data-spinner]', 'ajax:complete', hideSpinner);
|
||||
delegate(document, '[data-spinner]', 'ajax:stopped', hideSpinner);
|
||||
delegate(document, '[data-spinner]', 'ajax:send', showSpinner);
|
|
@ -12,13 +12,13 @@ import '../shared/safari-11-file-xhr-workaround';
|
|||
import '../shared/autocomplete';
|
||||
import '../shared/remote-input';
|
||||
|
||||
import '../new_design/spinner';
|
||||
import '../new_design/buttons';
|
||||
import '../new_design/form-validation';
|
||||
import '../new_design/carto';
|
||||
import '../new_design/select2';
|
||||
|
||||
import '../new_design/champs/linked-drop-down-list';
|
||||
import '../new_design/champs/siret';
|
||||
|
||||
import { toggleCondidentielExplanation } from '../new_design/avis';
|
||||
import { togglePrintMenu } from '../new_design/dossier';
|
||||
|
|
11
app/javascript/shared/utils.js
Normal file
11
app/javascript/shared/utils.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
export function show({ classList }) {
|
||||
classList.remove('hidden');
|
||||
}
|
||||
|
||||
export function hide({ classList }) {
|
||||
classList.add('hidden');
|
||||
}
|
||||
|
||||
export function toggle({ classList }) {
|
||||
classList.toggle('hidden');
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
<% if @blank || @error %>
|
||||
<%= render_to_element("#etablissement-for-#{@champ.id}", partial: 'shared/champs/siret/delete_etablissement', locals: { message: @error, position: @champ.order_place, etablissement: @etablissement }) %>
|
||||
<% else %>
|
||||
<%= render_to_element("#etablissement-for-#{@champ.id}", partial: 'shared/champs/siret/etablissement', locals: { position: @champ.order_place, etablissement: @etablissement }) %>
|
||||
<% end %>
|
6
app/views/champs/siret/show.js.erb
Normal file
6
app/views/champs/siret/show.js.erb
Normal file
|
@ -0,0 +1,6 @@
|
|||
<%= render_to_element(".siret-info-#{@position}",
|
||||
partial: 'shared/champs/siret/etablissement',
|
||||
locals: {
|
||||
siret: @siret,
|
||||
attribute: @attribute,
|
||||
etablissement: @etablissement }) %>
|
|
@ -1,5 +0,0 @@
|
|||
.mandatory= message
|
||||
- if etablissement.present?
|
||||
- champ_attributes = etablissement.champ.private? ? 'champs_private_attributes' : 'champs_attributes'
|
||||
= fields_for "dossier[#{champ_attributes}][#{position}][etablissement_attributes]", etablissement do |form|
|
||||
= form.hidden_field :_destroy
|
|
@ -1,4 +1,17 @@
|
|||
= render partial: 'shared/dossiers/editable_champs/etablissement_titre', locals: { etablissement: etablissement }
|
||||
- champ_attributes = etablissement.champ.private? ? 'champs_private_attributes' : 'champs_attributes'
|
||||
= fields_for "dossier[#{champ_attributes}][#{position}][etablissement_attributes]", etablissement do |form|
|
||||
= render partial: 'shared/dossiers/editable_champs/etablissement', locals: { form: form, signature: etablissement.sign }
|
||||
- case siret
|
||||
- when :invalid
|
||||
Le numéro de SIRET doit comporter exactement 14 chiffres.
|
||||
|
||||
- when :not_found
|
||||
Nous n’avons pas trouvé d’établissement correspondant à ce numéro de SIRET.
|
||||
= link_to('Plus d’informations', "https://faq.demarches-simplifiees.fr/article/4-erreur-siret", target: '_blank')
|
||||
|
||||
- else
|
||||
- if siret.present? && siret == etablissement&.siret
|
||||
= render partial: 'shared/dossiers/editable_champs/etablissement_titre', locals: { etablissement: etablissement }
|
||||
= fields_for attribute, etablissement do |form|
|
||||
= render partial: 'shared/dossiers/editable_champs/etablissement', locals: { form: form, signature: etablissement.sign }
|
||||
|
||||
- elsif etablissement.present?
|
||||
= fields_for attribute, etablissement do |form|
|
||||
= form.hidden_field :_destroy
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
= form.text_field :value,
|
||||
placeholder: champ.libelle,
|
||||
class: 'small-margin',
|
||||
data: { siret: champ.persisted? ? champs_siret_path(format: :js, champ_id: champ) : nil },
|
||||
data: { remote: true, debounce: true, url: champs_siret_path(form.index), params: { champ_id: champ&.id }.to_query, spinner: true },
|
||||
required: champ.mandatory?
|
||||
.spinner.right-spinner
|
||||
%div{ id: "etablissement-for-#{champ.id}" }
|
||||
.spinner.right-spinner.hidden
|
||||
%div{ class: "siret-info-#{form.index}" }
|
||||
- if champ.etablissement.present?
|
||||
= render partial: 'shared/dossiers/editable_champs/etablissement_titre', locals: { etablissement: champ.etablissement }
|
||||
= form.fields_for :etablissement do |form|
|
||||
= render partial: 'shared/dossiers/editable_champs/etablissement', locals: { form: form, signature: champ.etablissement.sign }
|
||||
= form.fields_for :etablissement do |form|
|
||||
= render partial: 'shared/dossiers/editable_champs/etablissement', locals: { form: form, signature: champ.etablissement.sign }
|
||||
|
|
|
@ -110,7 +110,7 @@ Rails.application.routes.draw do
|
|||
end
|
||||
|
||||
namespace :champs do
|
||||
get ':champ_id/siret' => 'siret#index', as: 'siret'
|
||||
get ':position/siret', to: 'siret#show', as: :siret
|
||||
get ':position/dossier_link', to: 'dossier_link#show', as: :dossier_link
|
||||
end
|
||||
|
||||
|
|
82
spec/controllers/champs/siret_controller_spec.rb
Normal file
82
spec/controllers/champs/siret_controller_spec.rb
Normal file
|
@ -0,0 +1,82 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Champs::SiretController, type: :controller do
|
||||
let(:user) { create(:user) }
|
||||
let(:procedure) { create(:procedure, :published) }
|
||||
|
||||
describe '#show' do
|
||||
let(:dossier) { create(:dossier, user: user, procedure: procedure) }
|
||||
let(:params) do
|
||||
{
|
||||
dossier: {
|
||||
champs_attributes: {
|
||||
'1' => { value: "#{siret}" }
|
||||
}
|
||||
},
|
||||
position: '1'
|
||||
}
|
||||
end
|
||||
let(:siret) { '' }
|
||||
|
||||
context 'when user is connected' do
|
||||
render_views
|
||||
before { sign_in user }
|
||||
|
||||
context 'when siret empty' do
|
||||
before {
|
||||
get :show, params: params, format: 'js'
|
||||
}
|
||||
|
||||
it 'empty info message' do
|
||||
expect(response.body).to include('.siret-info-1')
|
||||
expect(response.body).to include('innerHTML = ""')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when siret invalid' do
|
||||
let(:siret) { '1234' }
|
||||
before {
|
||||
get :show, params: params, format: 'js'
|
||||
}
|
||||
|
||||
it 'invalid error' do
|
||||
expect(response.body).to include('Le numéro de SIRET doit comporter exactement 14 chiffres.')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when siret not found' do
|
||||
let(:siret) { '0' * 14 }
|
||||
before {
|
||||
expect(subject).to receive(:find_etablisement_with_siret).and_return(false)
|
||||
get :show, params: params, format: 'js'
|
||||
}
|
||||
|
||||
it 'not found error' do
|
||||
expect(response.body).to include('Nous n’avons pas trouvé d’établissement correspondant à ce numéro de SIRET.')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when siret found' do
|
||||
let(:siret) { etablissement.siret }
|
||||
let(:etablissement) { build(:etablissement) }
|
||||
before {
|
||||
expect(subject).to receive(:find_etablisement_with_siret).and_return(etablissement)
|
||||
get :show, params: params, format: 'js'
|
||||
}
|
||||
|
||||
it 'etablissement info message' do
|
||||
expect(response.body).to include(etablissement.entreprise_raison_sociale)
|
||||
expect(response.body).to include(etablissement.entreprise_capital_social.to_s)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when user is not connected' do
|
||||
before {
|
||||
get :show, params: { position: '1' }, format: 'js'
|
||||
}
|
||||
|
||||
it { expect(response.code).to eq('401') }
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue