2021-11-25 16:26:55 +01:00
module Administrateurs
2018-04-13 16:56:00 +02:00
class ProceduresController < AdministrateurController
2022-11-10 15:15:48 +01:00
layout 'all' , only : [ :all , :administrateurs ]
2022-11-25 19:55:59 +01:00
respond_to :html , :xlsx
2022-11-10 15:15:48 +01:00
2023-03-23 17:14:15 +01:00
before_action :retrieve_procedure , only : [ :champs , :annotations , :modifications , :edit , :zones , :monavis , :update_monavis , :jeton , :update_jeton , :publication , :publish , :transfert , :close , :allow_expert_review , :allow_expert_messaging , :experts_require_administrateur_invitation , :reset_draft ]
2022-09-02 10:51:56 +02:00
before_action :draft_valid? , only : [ :apercu ]
2022-12-14 09:15:40 +01:00
after_action :reset_procedure , only : [ :update ]
2019-01-17 15:15:46 +01:00
2020-07-28 15:15:56 +02:00
ITEMS_PER_PAGE = 25
def index
@procedures_publiees = paginated_published_procedures
@procedures_draft = paginated_draft_procedures
@procedures_closed = paginated_closed_procedures
2022-05-10 13:58:58 +02:00
@procedures_deleted = paginated_deleted_procedures
2020-07-28 15:15:56 +02:00
@procedures_publiees_count = current_administrateur . procedures . publiees . count
@procedures_draft_count = current_administrateur . procedures . brouillons . count
@procedures_closed_count = current_administrateur . procedures . closes . count
2022-05-10 13:58:58 +02:00
@procedures_deleted_count = current_administrateur . procedures . with_discarded . discarded . count
2020-07-28 15:15:56 +02:00
@statut = params [ :statut ]
@statut . blank? ? @statut = 'publiees' : @statut = params [ :statut ]
end
def paginated_published_procedures
current_administrateur
. procedures
. publiees
. page ( params [ :page ] )
. per ( ITEMS_PER_PAGE )
. order ( published_at : :desc )
end
def paginated_draft_procedures
current_administrateur
. procedures
. brouillons
. page ( params [ :page ] )
. per ( ITEMS_PER_PAGE )
. order ( created_at : :desc )
end
def paginated_closed_procedures
current_administrateur
. procedures
. closes
. page ( params [ :page ] )
. per ( ITEMS_PER_PAGE )
. order ( created_at : :desc )
end
2022-05-10 13:58:58 +02:00
def paginated_deleted_procedures
current_administrateur
. procedures
. with_discarded
. discarded
. page ( params [ :page ] )
. per ( ITEMS_PER_PAGE )
. order ( created_at : :desc )
end
2018-04-13 16:56:00 +02:00
def apercu
2022-04-07 11:40:53 +02:00
@dossier = procedure_without_control . draft_revision . dossier_for_preview ( current_user )
2018-04-13 17:29:34 +02:00
@tab = apercu_tab
2018-04-13 16:56:00 +02:00
end
2019-09-24 16:38:58 +02:00
def new
@procedure || = Procedure . new ( for_individual : true )
end
2021-08-31 23:44:23 +02:00
SIGNIFICANT_DOSSIERS_THRESHOLD = 30
def new_from_existing
2022-07-22 15:40:22 +02:00
@grouped_procedures = nil
end
def search
query = ActiveRecord :: Base . sanitize_sql_like ( params [ :query ] )
2021-08-31 23:44:23 +02:00
significant_procedure_ids = Procedure
. publiees_ou_closes
2022-07-22 15:40:22 +02:00
. where ( 'unaccent(libelle) ILIKE unaccent(?)' , " % #{ query } % " )
2021-08-31 23:44:23 +02:00
. joins ( :dossiers )
. group ( " procedures.id " )
. having ( " count(dossiers.id) >= ? " , SIGNIFICANT_DOSSIERS_THRESHOLD )
. pluck ( 'procedures.id' )
@grouped_procedures = Procedure
. includes ( :administrateurs , :service )
. where ( id : significant_procedure_ids )
. group_by ( & :organisation_name )
. sort_by { | _ , procedures | procedures . first . created_at }
end
2020-07-15 10:48:11 +02:00
def show
2022-07-18 14:12:43 +02:00
@procedure = current_administrateur
. procedures
. includes (
2023-02-09 14:39:33 +01:00
published_revision : {
types_de_champ : [ ] ,
revision_types_de_champ : { type_de_champ : { piece_justificative_template_attachment : :blob } }
} ,
draft_revision : {
types_de_champ : [ ] ,
revision_types_de_champ : { type_de_champ : { piece_justificative_template_attachment : :blob } }
} ,
attestation_template : [ ] ,
initiated_mail : [ ] ,
received_mail : [ ] ,
closed_mail : [ ] ,
refused_mail : [ ] ,
without_continuation_mail : [ ]
2022-07-18 14:12:43 +02:00
)
. find ( params [ :id ] )
2022-09-30 14:26:35 +02:00
@procedure . validate ( :publication )
2020-09-24 14:36:22 +02:00
@current_administrateur = current_administrateur
2021-06-24 11:57:05 +02:00
@procedure_lien = commencer_url ( path : @procedure . path )
@procedure_lien_test = commencer_test_url ( path : @procedure . path )
2020-07-15 10:48:11 +02:00
end
2019-09-24 16:38:58 +02:00
def edit
end
2022-09-26 19:02:39 +02:00
def zones
end
2019-09-24 16:38:58 +02:00
def create
2023-01-12 13:04:21 +01:00
new_procedure_params = { max_duree_conservation_dossiers_dans_ds : Procedure :: NEW_MAX_DUREE_CONSERVATION }
. merge ( procedure_params )
. merge ( administrateurs : [ current_administrateur ] )
@procedure = Procedure . new ( new_procedure_params )
2020-08-27 19:55:10 +02:00
@procedure . draft_revision = @procedure . revisions . build
2019-09-24 16:38:58 +02:00
if ! @procedure . save
flash . now . alert = @procedure . errors . full_messages
render 'new'
else
flash . notice = 'Démarche enregistrée.'
current_administrateur . instructeur . assign_to_procedure ( @procedure )
2020-07-07 12:03:32 +02:00
redirect_to champs_admin_procedure_path ( @procedure )
2019-09-24 16:38:58 +02:00
end
end
def update
@procedure = current_administrateur . procedures . find ( params [ :id ] )
if ! @procedure . update ( procedure_params )
flash . now . alert = @procedure . errors . full_messages
2022-09-27 19:22:42 +02:00
if @procedure . errors [ :zones ] . present?
render 'zones'
else
render 'edit'
end
2019-09-24 16:38:58 +02:00
elsif @procedure . brouillon?
flash . notice = 'Démarche modifiée. Tous les dossiers de cette démarche ont été supprimés.'
2022-02-22 16:09:00 +01:00
redirect_to admin_procedure_path ( id : @procedure . id )
2019-09-24 16:38:58 +02:00
else
flash . notice = 'Démarche modifiée.'
2022-02-22 16:09:00 +01:00
redirect_to admin_procedure_path ( id : @procedure . id )
2019-09-24 16:38:58 +02:00
end
end
2021-09-01 00:20:32 +02:00
def clone
procedure = Procedure . find ( params [ :procedure_id ] )
new_procedure = procedure . clone ( current_administrateur , cloned_from_library? )
if new_procedure . valid?
2022-08-17 09:06:04 +02:00
flash . notice = 'Démarche clonée, pensez a vérifier la Présentation et choisir le service a laquelle cette procédure est associé.'
redirect_to admin_procedure_path ( id : new_procedure . id )
2021-09-01 00:20:32 +02:00
else
if cloned_from_library?
flash . alert = new_procedure . errors . full_messages
redirect_to new_from_existing_admin_procedures_path
else
flash . alert = new_procedure . errors . full_messages
redirect_to admin_procedures_path
end
end
rescue ActiveRecord :: RecordNotFound
flash . alert = 'Démarche inexistante'
redirect_to admin_procedures_path
end
2021-09-01 00:25:00 +02:00
def archive
procedure = current_administrateur . procedures . find ( params [ :procedure_id ] )
2022-05-27 15:44:43 +02:00
if params [ :new_procedure ] . present?
new_procedure = current_administrateur . procedures . find ( params [ :new_procedure ] )
procedure . update! ( replaced_by_procedure_id : new_procedure . id )
end
2021-09-01 00:25:00 +02:00
procedure . close!
flash . notice = " Démarche close "
redirect_to admin_procedures_path
rescue ActiveRecord :: RecordNotFound
flash . alert = 'Démarche inexistante'
redirect_to admin_procedures_path
end
2020-07-28 15:15:56 +02:00
def destroy
procedure = current_administrateur . procedures . find ( params [ :id ] )
if procedure . can_be_deleted_by_administrateur?
procedure . discard_and_keep_track! ( current_administrateur )
flash . notice = 'Démarche supprimée'
redirect_to admin_procedures_draft_path
else
render json : { } , status : 403
end
end
2022-05-10 13:58:58 +02:00
def restore
procedure = current_administrateur . procedures . with_discarded . discarded . find ( params [ :id ] )
2023-04-19 13:00:37 +02:00
procedure . restore ( current_administrateur )
2022-05-10 13:58:58 +02:00
flash . notice = t ( 'administrateurs.index.restored' , procedure_id : procedure . id )
redirect_to admin_procedures_path
end
2019-09-24 16:38:58 +02:00
def monavis
end
def update_monavis
if ! @procedure . update ( procedure_params )
flash . now . alert = @procedure . errors . full_messages
else
flash . notice = 'le champ MonAvis a bien été mis à jour'
end
render 'monavis'
end
2020-04-27 14:58:12 +02:00
def jeton
end
2022-03-16 16:23:53 +01:00
def modifications
end
2020-04-27 14:58:12 +02:00
def update_jeton
2020-10-19 11:33:36 +02:00
token = params [ :procedure ] [ :api_entreprise_token ]
@procedure . api_entreprise_token = token
if @procedure . valid? &&
2020-08-05 18:40:47 +02:00
APIEntreprise :: PrivilegesAdapter . new ( token ) . valid? &&
2020-10-19 11:33:36 +02:00
@procedure . save
redirect_to jeton_admin_procedure_path ( procedure_id : params [ :procedure_id ] ) ,
notice : 'Le jeton a bien été mis à jour'
2020-04-27 14:58:12 +02:00
else
2020-10-19 11:33:36 +02:00
2021-05-26 15:16:30 +02:00
flash . now . alert = " Mise à jour impossible : le jeton n’ est pas valide "
2020-10-19 11:33:36 +02:00
render 'jeton'
2020-04-27 14:58:12 +02:00
end
end
2020-09-08 12:35:44 +02:00
def publication
2022-09-30 14:38:44 +02:00
@procedure = current_administrateur
. procedures
. includes (
published_revision : :types_de_champ ,
draft_revision : :types_de_champ
) . find ( params [ :procedure_id ] )
2021-06-24 11:57:05 +02:00
@procedure_lien = commencer_url ( path : @procedure . path )
@procedure_lien_test = commencer_test_url ( path : @procedure . path )
2020-09-08 12:35:44 +02:00
@procedure . path = @procedure . suggested_path ( current_administrateur )
@current_administrateur = current_administrateur
2022-05-23 18:57:50 +02:00
@closed_procedures = current_administrateur . procedures . with_discarded . closes . map { | p | [ " #{ p . libelle } ( #{ p . id } ) " , p . id ] } . to_h
2020-09-08 12:35:44 +02:00
end
def publish
@procedure . assign_attributes ( publish_params )
2022-07-06 12:37:08 +02:00
if @procedure . draft_changed?
if @procedure . close?
if @procedure . publish_or_reopen! ( current_administrateur )
@procedure . publish_revision!
flash . notice = " Démarche publiée "
else
flash . alert = @procedure . errors . full_messages
end
else
@procedure . publish_revision!
flash . notice = " Nouvelle version de la démarche publiée "
end
2021-04-28 15:08:48 +02:00
elsif @procedure . publish_or_reopen! ( current_administrateur )
2020-09-08 12:35:44 +02:00
flash . notice = " Démarche publiée "
2021-04-28 15:08:48 +02:00
else
2020-09-08 12:35:44 +02:00
flash . alert = @procedure . errors . full_messages
end
2022-11-01 15:53:58 +01:00
if params [ :old_procedure ] . present? && @procedure . errors . empty?
current_administrateur
. procedures
. with_discarded
. closes
. find ( params [ :old_procedure ] )
. update! ( replaced_by_procedure : @procedure )
end
2022-06-02 15:26:04 +02:00
redirect_to admin_procedure_path ( @procedure )
2020-09-08 12:35:44 +02:00
end
2022-07-06 12:37:08 +02:00
def reset_draft
@procedure . reset_draft_revision!
redirect_to admin_procedure_path ( @procedure )
end
2020-09-29 10:52:13 +02:00
def transfert
end
2022-05-27 15:44:43 +02:00
def close
end
2020-09-30 18:22:06 +02:00
def allow_expert_review
@procedure . update! ( allow_expert_review : ! @procedure . allow_expert_review )
flash . notice = @procedure . allow_expert_review? ? " Avis externes activés " : " Avis externes désactivés "
2021-04-16 11:11:39 +02:00
redirect_to admin_procedure_experts_path ( @procedure )
2020-09-30 18:22:06 +02:00
end
2023-03-23 17:14:15 +01:00
def allow_expert_messaging
@procedure . update! ( allow_expert_messaging : ! @procedure . allow_expert_messaging )
flash . notice = @procedure . allow_expert_messaging ? " Les experts ont accès à la messagerie " : " Les experts n'ont plus accès à la messagerie "
redirect_to admin_procedure_experts_path ( @procedure )
end
2020-09-08 12:35:44 +02:00
def transfer
admin = Administrateur . by_email ( params [ :email_admin ] . downcase )
if admin . nil?
2020-09-29 10:52:13 +02:00
redirect_to admin_procedure_transfert_path ( params [ :procedure_id ] )
2021-05-26 15:16:30 +02:00
flash . alert = " Envoi vers #{ params [ :email_admin ] } impossible : cet administrateur n’ existe pas "
2020-09-08 12:35:44 +02:00
else
procedure = current_administrateur . procedures . find ( params [ :procedure_id ] )
procedure . clone ( admin , false )
redirect_to admin_procedure_path ( params [ :procedure_id ] )
flash . notice = " La démarche a correctement été clonée vers le nouvel administrateur. "
end
end
2021-04-16 11:11:39 +02:00
def experts_require_administrateur_invitation
@procedure . update! ( experts_require_administrateur_invitation : ! @procedure . experts_require_administrateur_invitation )
flash . notice = @procedure . experts_require_administrateur_invitation? ? " Les experts sont gérés par les administrateurs de la démarche " : " Les experts sont gérés par les instructeurs "
redirect_to admin_procedure_experts_path ( @procedure )
end
2022-07-18 11:39:25 +02:00
def champs
2023-02-09 14:39:33 +01:00
@procedure = Procedure . includes ( draft_revision : {
revision_types_de_champ : {
type_de_champ : { piece_justificative_template_attachment : :blob , revision : [ ] , procedure : [ ] } ,
revision : [ ] ,
procedure : [ ]
} ,
revision_types_de_champ_public : {
type_de_champ : { piece_justificative_template_attachment : :blob , revision : [ ] , procedure : [ ] } ,
revision : [ ] ,
procedure : [ ]
} ,
procedure : [ ]
} ) . find ( @procedure . id )
end
def annotations
@procedure = Procedure . includes ( draft_revision : {
revision_types_de_champ : {
type_de_champ : { piece_justificative_template_attachment : :blob , revision : [ ] , procedure : [ ] } ,
revision : [ ] ,
procedure : [ ]
} ,
revision_types_de_champ_private : {
type_de_champ : { piece_justificative_template_attachment : :blob , revision : [ ] , procedure : [ ] } ,
revision : [ ] ,
procedure : [ ]
} ,
procedure : [ ]
} ) . find ( @procedure . id )
2022-07-18 11:39:25 +02:00
end
2022-10-24 16:43:18 +02:00
2022-12-01 19:59:34 +01:00
def detail
@procedure = Procedure . find ( params [ :id ] )
2023-01-06 17:53:20 +01:00
@show_detail = params [ :show_detail ]
respond_to do | format |
format . turbo_stream
end
2022-12-01 19:59:34 +01:00
end
2022-10-24 16:43:18 +02:00
def all
2022-11-04 09:24:41 +01:00
@filter = ProceduresFilter . new ( current_administrateur , params )
2022-12-16 15:24:05 +01:00
all_procedures = filter_procedures ( @filter ) . map { | p | ProcedureDetail . new ( p ) }
2022-11-25 19:55:59 +01:00
respond_to do | format |
format . html do
all_procedures = Kaminari . paginate_array ( all_procedures . to_a , offset : 0 , limit : ITEMS_PER_PAGE , total_count : all_procedures . count )
@procedures = all_procedures . page ( params [ :page ] ) . per ( 25 )
end
2022-12-05 12:17:35 +01:00
format . xlsx do
2022-12-16 15:24:05 +01:00
render xlsx : ProcedureDetail . to_xlsx ( instances : all_procedures ) ,
filename : " demarches- #{ @filter } "
2022-12-05 12:17:35 +01:00
end
2022-11-25 19:55:59 +01:00
end
2022-10-24 16:43:18 +02:00
end
2022-07-18 11:39:25 +02:00
2022-11-10 14:59:19 +01:00
def administrateurs
@filter = ProceduresFilter . new ( current_administrateur , params )
2022-11-25 19:51:24 +01:00
pids = AdministrateursProcedure . select ( :administrateur_id ) . where ( procedure : filter_procedures ( @filter ) . map { | p | p [ " id " ] } )
@admins = Administrateur . includes ( :user , :procedures ) . where ( id : pids )
2022-11-21 17:40:54 +01:00
@admins = @admins . where ( 'unaccent(users.email) ILIKE unaccent(?)' , " % #{ @filter . email } % " ) if @filter . email . present?
2022-11-10 15:35:01 +01:00
@admins = paginate ( @admins , 'users.email' )
2022-11-10 14:59:19 +01:00
end
2018-04-13 16:56:00 +02:00
private
2022-11-10 15:35:01 +01:00
def filter_procedures ( filter )
2023-02-24 17:27:28 +01:00
procedures_result = Procedure . select ( :id ) . left_joins ( :procedures_zones ) . distinct . publiees_ou_closes
2022-11-10 15:35:01 +01:00
procedures_result = procedures_result . where ( procedures_zones : { zone_id : filter . zone_ids } ) if filter . zone_ids . present?
procedures_result = procedures_result . where ( aasm_state : filter . statuses ) if filter . statuses . present?
2023-03-02 00:04:30 +01:00
procedures_result = procedures_result . where ( " tags @> ARRAY[?]::text[] " , filter . tags ) if filter . tags . present?
2022-11-10 15:35:01 +01:00
procedures_result = procedures_result . where ( 'published_at >= ?' , filter . from_publication_date ) if filter . from_publication_date . present?
2022-11-16 11:03:23 +01:00
procedures_result = procedures_result . where ( 'unaccent(libelle) ILIKE unaccent(?)' , " % #{ filter . libelle } % " ) if filter . libelle . present?
2022-11-25 19:51:24 +01:00
procedures_sql = procedures_result . to_sql
2023-01-03 14:58:46 +01:00
sql = " select id, libelle, published_at, aasm_state, estimated_dossiers_count, count(administrateurs_procedures.administrateur_id) as admin_count from administrateurs_procedures inner join procedures on procedures.id = administrateurs_procedures.procedure_id where procedures.id in ( #{ procedures_sql } ) group by procedures.id order by published_at desc "
2022-11-25 19:51:24 +01:00
ActiveRecord :: Base . connection . execute ( sql )
2022-11-10 15:35:01 +01:00
end
def paginate ( result , ordered_by )
result . page ( params [ :page ] ) . per ( ITEMS_PER_PAGE ) . order ( ordered_by )
end
2022-09-02 10:51:56 +02:00
def draft_valid?
if procedure_without_control . draft_revision . invalid?
flash . alert = t ( 'preview_unavailable' , scope : 'administrateurs.procedures' )
redirect_back ( fallback_location : champs_admin_procedure_path ( procedure_without_control ) )
end
end
2018-04-13 17:29:34 +02:00
def apercu_tab
params [ :tab ] || 'dossier'
end
2018-04-13 16:56:00 +02:00
def procedure_without_control
Procedure . find ( params [ :id ] )
end
2019-09-24 16:38:58 +02:00
def procedure_params
2022-02-22 16:09:00 +01:00
editable_params = [
:libelle ,
:description ,
:organisation ,
:direction ,
:lien_site_web ,
:cadre_juridique ,
:deliberation ,
:notice ,
:web_hook_url ,
:declarative_with_state ,
:logo ,
:auto_archive_on ,
:monavis_embed ,
:api_entreprise_token ,
:duree_conservation_dossiers_dans_ds ,
2022-08-24 15:16:58 +02:00
{ zone_ids : [ ] } ,
2022-02-22 16:09:00 +01:00
:lien_dpo ,
2022-06-22 15:49:23 +02:00
:opendata ,
2022-10-20 11:51:31 +02:00
:procedure_expires_when_termine_enabled ,
:tags
2022-02-22 16:09:00 +01:00
]
2022-11-08 11:39:34 +01:00
2022-11-08 12:33:01 +01:00
editable_params << :piece_justificative_multiple if @procedure && ! @procedure . piece_justificative_multiple?
2022-11-08 11:39:34 +01:00
2019-09-24 16:38:58 +02:00
permited_params = if @procedure & . locked?
params . require ( :procedure ) . permit ( * editable_params )
else
2021-11-11 10:58:13 +01:00
params . require ( :procedure ) . permit ( * editable_params , :for_individual , :path )
2019-09-24 16:38:58 +02:00
end
2020-02-29 03:49:31 +01:00
if permited_params [ :auto_archive_on ] . present?
permited_params [ :auto_archive_on ] = Date . parse ( permited_params [ :auto_archive_on ] ) + 1 . day
end
2022-10-20 11:51:31 +02:00
if permited_params [ :tags ] . present?
permited_params [ :tags ] = JSON . parse ( permited_params [ :tags ] )
end
2019-09-24 16:38:58 +02:00
permited_params
end
2020-09-08 12:35:44 +02:00
def publish_params
params . permit ( :path , :lien_site_web )
end
2021-02-11 20:19:15 +01:00
def allow_decision_access_params
params . require ( :experts_procedure ) . permit ( :allow_decision_access )
end
2021-09-01 00:20:32 +02:00
def cloned_from_library?
params [ :from_new_from_existing ] . present?
end
2018-04-13 16:56:00 +02:00
end
end