2024-04-29 00:17:15 +02:00
# frozen_string_literal: true
2021-11-25 16:26:55 +01:00
module Administrateurs
2018-04-19 16:15:44 +02:00
class ServicesController < AdministrateurController
2023-07-24 11:05:02 +02:00
skip_before_action :alert_for_missing_siret_service , only : :edit
2023-07-24 19:01:31 +02:00
skip_before_action :alert_for_missing_service , only : :edit
2018-04-19 16:15:44 +02:00
def index
2018-04-20 12:03:02 +02:00
@procedure = procedure
2024-11-25 14:59:54 +01:00
@services = ( [ procedure . service ] . compact + services . ordered ) . uniq
2018-04-19 16:15:44 +02:00
end
2018-04-19 10:44:14 +02:00
def new
2018-04-24 10:07:20 +02:00
@procedure = procedure
2018-10-30 15:16:47 +01:00
@service = Service . new
2024-10-16 16:49:00 +02:00
siret = current_administrateur . instructeur . last_agent_connect_information & . siret
if siret
@service . siret = siret
@prefilled = handle_siret_prefill
end
2018-04-19 10:44:14 +02:00
end
def create
2018-10-30 15:16:47 +01:00
@service = Service . new ( service_params )
@service . administrateur = current_administrateur
2018-04-19 10:44:14 +02:00
2024-11-04 12:47:42 +01:00
if @service . save
2022-10-25 14:35:36 +02:00
@service . enqueue_api_entreprise
2020-07-07 12:31:52 +02:00
redirect_to admin_services_path ( procedure_id : params [ :procedure_id ] ) ,
2018-10-30 15:16:47 +01:00
notice : " #{ @service . nom } créé "
2018-04-19 10:44:14 +02:00
else
2018-07-19 15:29:06 +02:00
@procedure = procedure
2018-10-30 15:16:47 +01:00
flash [ :alert ] = @service . errors . full_messages
2018-04-19 10:44:14 +02:00
render :new
end
end
2018-04-19 18:12:48 +02:00
def edit
@service = service
2018-04-24 10:07:20 +02:00
@procedure = procedure
2018-04-19 18:12:48 +02:00
end
def update
@service = service
if @service . update ( service_params )
2022-10-25 14:35:36 +02:00
if @service . siret_previously_changed?
@service . enqueue_api_entreprise
end
2020-07-07 12:31:52 +02:00
redirect_to admin_services_path ( procedure_id : params [ :procedure_id ] ) ,
2018-04-19 15:59:11 +02:00
notice : " #{ @service . nom } modifié "
2018-04-19 18:12:48 +02:00
else
2018-07-19 15:29:06 +02:00
@procedure = procedure
2018-04-19 18:12:48 +02:00
flash [ :alert ] = @service . errors . full_messages
render :edit
end
end
2024-11-04 12:47:42 +01:00
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
2018-04-20 12:03:02 +02:00
def add_to_procedure
procedure = current_administrateur . procedures . find ( procedure_params [ :id ] )
service = services . find ( procedure_params [ :service_id ] )
procedure . update ( service : service )
redirect_to admin_procedure_path ( procedure . id ) ,
notice : " service affecté : #{ procedure . service . nom } "
end
2018-04-23 15:32:27 +02:00
def destroy
service_to_destroy = service
if service_to_destroy . procedures . present?
if service_to_destroy . procedures . count == 1
2018-09-05 14:48:42 +02:00
message = " la démarche #{ service_to_destroy . procedures . first . libelle } utilise encore le service #{ service_to_destroy . nom } . Veuillez l'affecter à un autre service avant de pouvoir le supprimer "
2018-04-23 15:32:27 +02:00
else
2018-09-05 14:48:42 +02:00
message = " les démarches #{ service_to_destroy . procedures . map ( & :libelle ) . join ( ', ' ) } utilisent encore le service #{ service . nom } . Veuillez les affecter à un autre service avant de pouvoir le supprimer "
2018-04-23 15:32:27 +02:00
end
flash [ :alert ] = message
2020-07-07 12:31:52 +02:00
redirect_to admin_services_path ( procedure_id : params [ :procedure_id ] )
2018-04-23 15:32:27 +02:00
else
2022-08-02 11:44:47 +02:00
service_to_destroy . procedures . with_discarded . discarded . update ( service : nil )
2018-04-23 15:32:27 +02:00
service_to_destroy . destroy
2020-07-07 12:31:52 +02:00
redirect_to admin_services_path ( procedure_id : params [ :procedure_id ] ) ,
2018-04-23 15:32:27 +02:00
notice : " #{ service_to_destroy . nom } est supprimé "
end
end
2018-04-19 16:15:44 +02:00
private
2018-04-19 10:44:14 +02:00
def service_params
2022-07-19 12:41:10 +02:00
params . require ( :service ) . permit ( :nom , :organisme , :type_organisme , :email , :telephone , :horaires , :adresse , :siret )
2018-04-19 10:44:14 +02:00
end
2018-04-19 18:12:48 +02:00
def service
services . find ( params [ :id ] )
end
2018-04-19 16:15:44 +02:00
def services
2019-03-06 17:06:54 +01:00
service_ids = current_administrateur . service_ids
service_ids << maybe_procedure & . service_id
Service . where ( id : service_ids . compact . uniq )
2018-04-19 16:15:44 +02:00
end
2018-04-20 12:03:02 +02:00
def procedure_params
params . require ( :procedure ) . permit ( :id , :service_id )
end
2019-03-06 17:06:54 +01:00
def maybe_procedure
current_administrateur . procedures . find_by ( id : params [ :procedure_id ] )
end
2018-04-20 12:03:02 +02:00
def procedure
current_administrateur . procedures . find ( params [ :procedure_id ] )
end
2024-10-14 22:25:08 +02:00
2024-10-16 16:49:00 +02:00
def handle_siret_prefill
2024-10-14 22:25:08 +02:00
@service . validate
if ! @service . errors . include? ( :siret )
2024-10-15 18:26:19 +02:00
prefilled = case @service . prefill_from_siret
in [ Dry :: Monads :: Result :: Success , Dry :: Monads :: Result :: Success ]
:success
in [ Dry :: Monads :: Result :: Failure , Dry :: Monads :: Result :: Success ] | [ Dry :: Monads :: Result :: Success , Dry :: Monads :: Result :: Failure ]
:partial
else
:failure
end
2024-10-14 22:25:08 +02:00
end
2024-11-04 12:47:42 +01:00
# 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)
2024-10-14 22:25:08 +02:00
siret_errors = @service . errors . where ( :siret )
@service . errors . clear
siret_errors . each { @service . errors . import ( _1 ) }
2024-10-16 16:49:00 +02:00
prefilled
2024-10-14 22:25:08 +02:00
end
2018-04-19 16:15:44 +02:00
end
end