2018-04-03 17:53:14 +02:00
|
|
|
class Champs::SiretController < ApplicationController
|
2018-09-06 19:23:27 +02:00
|
|
|
before_action :authenticate_logged_user!
|
|
|
|
|
|
|
|
def show
|
|
|
|
@position = params[:position]
|
|
|
|
extract_siret
|
|
|
|
find_etablisement
|
|
|
|
|
|
|
|
if @siret.empty?
|
2019-05-02 11:24:22 +02:00
|
|
|
return clear_siret_and_etablissement
|
|
|
|
end
|
|
|
|
|
2021-01-26 12:36:10 +01:00
|
|
|
if !Siret.new(siret: @siret).valid?
|
2021-02-02 15:48:53 +01:00
|
|
|
# i18n-tasks-use t('errors.messages.invalid_siret')
|
2019-05-02 11:24:22 +02:00
|
|
|
return siret_error(:invalid)
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
etablissement = find_etablissement_with_siret
|
2022-02-23 11:36:25 +01:00
|
|
|
rescue APIEntreprise::API::Error::RequestFailed, APIEntreprise::API::Error::BadGateway, APIEntreprise::API::Error::TimedOut, APIEntreprise::API::Error::ServiceUnavailable
|
2021-09-15 19:37:23 +02:00
|
|
|
# i18n-tasks-use t('errors.messages.siret_network_error')
|
2019-05-02 11:24:22 +02:00
|
|
|
return siret_error(:network_error)
|
|
|
|
end
|
2020-05-18 14:26:11 +02:00
|
|
|
if etablissement.nil?
|
2021-02-02 15:48:53 +01:00
|
|
|
# i18n-tasks-use t('errors.messages.siret_not_found')
|
2019-05-02 11:24:22 +02:00
|
|
|
return siret_error(:not_found)
|
|
|
|
end
|
|
|
|
|
|
|
|
@etablissement = etablissement
|
|
|
|
if !@champ.nil?
|
|
|
|
@champ.update!(value: etablissement.siret, etablissement: etablissement)
|
2018-09-06 19:23:27 +02:00
|
|
|
end
|
|
|
|
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]"
|
2018-04-03 17:53:14 +02:00
|
|
|
end
|
2018-09-06 19:23:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_etablisement
|
2022-04-07 11:42:00 +02:00
|
|
|
@champ = policy_scope(Champ).find(params[:champ_id])
|
|
|
|
@etablissement = @champ.etablissement
|
|
|
|
@procedure_id = @champ.dossier.procedure.id
|
2018-09-06 19:23:27 +02:00
|
|
|
end
|
|
|
|
|
2019-05-02 11:24:22 +02:00
|
|
|
def find_etablissement_with_siret
|
2020-08-05 18:40:47 +02:00
|
|
|
APIEntrepriseService.create_etablissement(@champ, @siret, current_user.id)
|
2018-04-03 17:53:14 +02:00
|
|
|
end
|
2019-05-02 11:24:22 +02:00
|
|
|
|
|
|
|
def clear_siret_and_etablissement
|
2022-04-07 11:42:00 +02:00
|
|
|
@champ.update!(value: '')
|
2019-05-02 11:24:22 +02:00
|
|
|
@etablissement&.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
def siret_error(error)
|
|
|
|
clear_siret_and_etablissement
|
|
|
|
@siret = error
|
|
|
|
end
|
2018-04-03 17:53:14 +02:00
|
|
|
end
|