demarches-normaliennes/app/controllers/champs/siret_controller.rb

64 lines
1.7 KiB
Ruby
Raw Normal View History

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
2022-04-28 15:04:21 +02:00
@champ = policy_scope(Champ).find(params[:champ_id])
@siret = read_param_value(@champ.input_name, 'value')
@etablissement = @champ.etablissement
2018-09-06 19:23:27 +02:00
if @siret.empty?
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')
return siret_error(:invalid)
end
begin
etablissement = find_etablissement_with_siret
rescue => error
if error.try(:network_error?) && !APIEntrepriseService.api_up?
# TODO: notify ops
etablissement = APIEntrepriseService.create_etablissement_as_degraded_mode(@champ, @siret, current_user.id)
if !@champ.nil?
@champ.update!(value: etablissement.siret, etablissement: etablissement)
end
@siret = :api_entreprise_down
return
else
Sentry.capture_exception(error, extra: { dossier_id: @champ.dossier_id, siret: @siret })
# i18n-tasks-use t('errors.messages.siret_network_error')
return siret_error(:network_error)
end
end
if etablissement.nil?
2021-02-02 15:48:53 +01:00
# i18n-tasks-use t('errors.messages.siret_not_found')
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 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
def clear_siret_and_etablissement
@champ.update!(value: '')
@etablissement&.destroy
end
def siret_error(error)
clear_siret_and_etablissement
@siret = error
end
2018-04-03 17:53:14 +02:00
end