Skip to content

Commit

Permalink
wip: lexpol integration
Browse files Browse the repository at this point in the history
wip: lexpol 01 review edit and lexpol component

chore: few fixes

feat: variable support

feat: allow administrator to select model for lexpol type field

feat: use Typhoeus instead of uri

feat: add mapping to variable to send to lexpol & fix dropdown to select lexpol modele

chore: rubocop

Graphql: add champ descriptor for lexpol

chore: dossier controller spec 1

chore: dossier controller spec 1

feat: add feature flag for lexpol

chore: graphql schema load

fix: some changes according to comments

chore: rubocop

feat: add feature flag rake

chore: upgrade vite
  • Loading branch information
yhru committed Dec 11, 2024
1 parent 845707d commit 5229f10
Show file tree
Hide file tree
Showing 33 changed files with 1,381 additions and 325 deletions.
658 changes: 348 additions & 310 deletions Gemfile.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions app/components/editable_champ/lexpol_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class EditableChamp::LexpolComponent < EditableChamp::EditableChampBaseComponent
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-# %div.lexpol-champ
-# - if @champ.value.blank?
-# = form_with url: lexpol_create_dossier_instructeur_dossier_path(@champ.dossier.revision.procedure.id, @champ.dossier.id, champ_id: @champ.id), method: :post do |form|
-# = form.submit 'Créer le dossier dans LexPol', class: 'fr-btn'
-# - else
-# %p
-# %strong Numéro NOR :
-# = @champ.value
-# %p
-# %strong Statut du dossier :
-# = @champ.lexpol_status
-# - if @champ.lexpol_dossier_url.present?
-# = link_to 'Aller sur LexPol', @champ.lexpol_dossier_url, target: '_blank', class: 'fr-btn fr-btn--secondary'
-# = form_with url: lexpol_update_dossier_instructeur_dossier_path(@champ.dossier.revision.procedure.id, @champ.dossier.id, champ_id: @champ.id), method: :post do |form|
-# = form.submit 'Mettre à jour le dossier', class: 'fr-btn fr-btn--secondary'
12 changes: 12 additions & 0 deletions app/components/types_de_champ_editor/champ_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ def initialize(coordinate:, upper_coordinates:, focused: false, errors: '')
@errors = errors
end

def lexpol_models
@lexpol_models ||= begin
api = APILexpol.new
models_response = api.get_models
models = models_response['modeles']
models.map { |model| [model['libelle'], model['modele']] }
rescue => e
Rails.logger.error("Erreur lors de la récupération des modèles LexPol : #{e.message}")
[]
end
end

private

delegate :type_de_champ, :revision, :procedure, to: :coordinate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
= form.label :expression_reguliere_error_message, for: dom_id(type_de_champ, :expression_reguliere_error_message) do
= t('.expression_reguliere.labels.error_message')
= form.text_field :expression_reguliere_error_message, class: "fr-input small-margin small", id: dom_id(type_de_champ, :expression_reguliere_error_message)

- if !type_de_champ.header_section? && !type_de_champ.titre_identite?
.cell.fr-mt-1w
= form.label :description, "Description du champ (optionnel)", for: dom_id(type_de_champ, :description)
Expand All @@ -64,8 +65,25 @@
.cell.fr-mt-1w
= form.label :notice_explicative, "Notice explicative", for: dom_id(type_de_champ, :notice_explicative)
= render Attachment::EditComponent.new(**notice_explicative_options)


- if type_de_champ.lexpol?
.cell.fr-mt-1w
= form.label :lexpol_modele, "Sélectionner un modèle Lexpol", class: 'flex-grow', for: dom_id(type_de_champ, :lexpol_modele)
- if lexpol_models.any?
= form.select :lexpol_modele,
lexpol_models.map { |model| [model.first, model.last] },
{ prompt: 'Sélectionnez un modèle' },
{ class: 'fr-select small-margin small width-100', id: dom_id(type_de_champ, :lexpol_modele) }
- else
.fr-message.fr-message--error
%p Aucune modèle disponible ou erreur lors de la récupération des modèles.
.cell.fr-mt-1w
= form.label :lexpol_mapping, "Variables Lexpol", class: 'flex-grow', for: dom_id(type_de_champ, :lexpol_mapping)
= form.text_area :lexpol_mapping,
value: type_de_champ.lexpol_mapping,
class: 'fr-input small-margin small width-100 resize-y',
rows: 3,
placeholder: "variables_mes_demarches=variable_lexpol\n...",
id: dom_id(type_de_champ, :lexpol_mapping)

.flex.justify-start.fr-mt-1w
-# ----- Polynesia options
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/administrateurs/procedures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -589,5 +589,16 @@ def allow_decision_access_params
def cloned_from_library?
params[:from_new_from_existing].present?
end

def update_lexpol(champ)
return unless champ.lexpol?

if champ.value.blank?
champ.value = LexpolAPI.new.create_dossier(champ)
else
LexpolAPI.new.update_dossier(champ)
end
champ.save
end
end
end
2 changes: 2 additions & 0 deletions app/controllers/administrateurs/types_de_champ_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ def type_de_champ_update_params
:expression_reguliere,
:expression_reguliere_exemple_text,
:expression_reguliere_error_message,
:lexpol_modele,
:lexpol_mapping,
editable_options: [
*INSTANCE_EDITABLE_OPTIONS,
*TypesDeChamp::CarteTypeDeChamp::LAYERS
Expand Down
50 changes: 50 additions & 0 deletions app/controllers/instructeurs/dossiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,51 @@ def pieces_jointes
.filter { _1.class.in?([Champs::PieceJustificativeChamp, Champs::TitreIdentiteChamp]) }
end

def create_lexpol_dossier
begin
api_lexpol = APILexpol.new

nor = api_lexpol.create_dossier(598706)

if nor.nil?
raise "Le numéro NOR n'a pas été trouvé dans la réponse de l'API."
end

champ = Champ.find(params[:champ_id])
champ.update!(value: nor)
flash[:notice] = "Dossier créé avec succès dans LexPol. Numéro NOR : #{nor}"
rescue => e
Rails.logger.error("Erreur lors de la création du dossier dans LexPol : #{e.message}")
flash[:alert] = "Erreur lors de la création du dossier dans LexPol : #{e.message}"
redirect_to request.referer || root_path
end
end

def update_lexpol_dossier
dossier = Dossier.find(params[:dossier_id])
champ = dossier.champs.find_by(id: params[:champ_id])

mapping = (champ.type_de_champ.lexpol_mapping || "")
.split(',')
.map { |pair| pair.split('=').map(&:strip) }
.to_h

variables = dossier.champs.each_with_object({}) do |champ, hash|
next unless champ.value.present? && champ.type_de_champ&.libelle.present?

mapped_key = mapping[champ.type_de_champ.libelle] || champ.type_de_champ.libelle
hash[mapped_key] = champ.value
end

if champ&.lexpol_update_dossier(variables)
flash[:notice] = "Dossier LexPol mis à jour avec succès."
else
flash[:alert] = champ&.errors&.full_messages&.join(', ') || "Erreur lors de la mise à jour."
end

redirect_to annotations_privees_instructeur_dossier_path(dossier.procedure, dossier.id)
end

private

def checked_visa?(c)
Expand All @@ -393,6 +438,10 @@ def dossier
end
end

def procedure
@procedure ||= dossier.procedure
end

def dossier_with_champs
@dossier ||= DossierPreloader.load_one(dossier_scope.find(params[:dossier_id]))
end
Expand All @@ -419,6 +468,7 @@ def champs_private_params
:code_departement,
:accreditation_number,
:accreditation_birthdate,
:lexpol,
:feature,
value: []
] + TypeDeChamp::INSTANCE_CHAMPS_PARAMS
Expand Down
29 changes: 29 additions & 0 deletions app/controllers/instructeurs/lexpol_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Instructeurs
class LexpolController < ApplicationController
before_action :authenticate_instructeur!
before_action :set_dossier_and_champ

def create_dossier
if @champ.lexpol_create_dossier
redirect_to annotations_instructeur_dossier_path(@dossier.procedure, @dossier), notice: 'Dossier LexPol créé avec succès.'
else
redirect_to annotations_instructeur_dossier_path(@dossier.procedure, @dossier), alert: @champ.errors.full_messages.join(', ')
end
end

def update_dossier
if @champ.lexpol_update_dossier
redirect_to annotations_instructeur_dossier_path(@dossier.procedure, @dossier), notice: 'Dossier LexPol mis à jour avec succès.'
else
redirect_to annotations_instructeur_dossier_path(@dossier.procedure, @dossier), alert: @champ.errors.full_messages.join(', ')
end
end

private

def set_dossier_and_champ
@dossier = Dossier.find(params[:dossier_id])
@champ = @dossier.champs.find(params[:champ_id])
end
end
end
2 changes: 2 additions & 0 deletions app/graphql/api/v2/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def self.resolve_type(type_definition, object, ctx)
Types::Champs::TextChampType,
Types::Champs::TitreIdentiteChampType,
Types::Champs::VisaChampType,
Types::Champs::LexpolChampType,
Types::Champs::EngagementJuridiqueChampType,
Types::GeoAreas::ParcelleCadastraleType,
Types::GeoAreas::SelectionUtilisateurType,
Expand Down Expand Up @@ -128,6 +129,7 @@ def self.resolve_type(type_definition, object, ctx)
Types::Champs::Descriptor::TextChampDescriptorType,
Types::Champs::Descriptor::TitreIdentiteChampDescriptorType,
Types::Champs::Descriptor::VisaChampDescriptorType,
Types::Champs::Descriptor::LexpolChampDescriptorType,
Types::Champs::Descriptor::YesNoChampDescriptorType,
Types::Champs::Descriptor::ExpressionReguliereChampDescriptorType,
Types::Champs::Descriptor::EngagementJuridiqueChampDescriptorType
Expand Down
72 changes: 72 additions & 0 deletions app/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3197,6 +3197,73 @@ type IntegerNumberChampDescriptor implements ChampDescriptor {
type: TypeDeChamp! @deprecated(reason: "Utilisez le champ `__typename` à la place.")
}

type LexpolChamp implements Champ {
"""
L'identifiant du champDescriptor de ce champ
"""
champDescriptorId: String!

"""
Lien vers le dossier Lexpol
"""
dossierURL: String
id: ID!

"""
Libellé du champ.
"""
label: String!

"""
Numéro NOR du dossier Lexpol
"""
nor: String
prefilled: Boolean!

"""
Statut du dossier Lexpol
"""
status: String

"""
La valeur du champ sous forme texte.
"""
stringValue: String

"""
Date de dernière modification du champ.
"""
updatedAt: ISO8601DateTime!
}

type LexpolChampDescriptor implements ChampDescriptor {
"""
Description des champs d’un bloc répétable.
"""
champDescriptors: [ChampDescriptor!] @deprecated(reason: "Utilisez le champ `RepetitionChampDescriptor.champ_descriptors` à la place.")

"""
Description du champ.
"""
description: String
id: ID!

"""
Libellé du champ.
"""
label: String!

"""
Est-ce que le champ est obligatoire ?
"""
required: Boolean!

"""
Type de la valeur du champ.
"""
type: TypeDeChamp! @deprecated(reason: "Utilisez le champ `__typename` à la place.")
}

type LinkedDropDownListChamp implements Champ {
"""
L'identifiant du champDescriptor de ce champ
Expand Down Expand Up @@ -4743,6 +4810,11 @@ enum TypeDeChamp {
"""
integer_number

"""
Lexpol
"""
lexpol

"""
Deux menus déroulants liés
"""
Expand Down
Loading

0 comments on commit 5229f10

Please sign in to comment.