Skip to content

Commit

Permalink
feat: lexpol integration
Browse files Browse the repository at this point in the history
  • Loading branch information
yhru authored and maatinito committed Feb 11, 2025
1 parent d16876f commit a9a7eba
Show file tree
Hide file tree
Showing 30 changed files with 462 additions and 8 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ gem 'prawn-qrcode' # to generate qrcode in pdfs
gem 'prawn-rails' # PDF Generation
gem 'prawn-table'
gem 'premailer-rails'
gem 'pry-byebug'
gem 'puma' # Use Puma as the app server
gem 'pundit'
gem 'rack-attack'
Expand Down
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ GEM
racc
browser (5.3.1)
builder (3.2.4)
byebug (11.1.3)
capybara (3.40.0)
addressable
matrix
Expand Down Expand Up @@ -189,6 +190,7 @@ GEM
chunky_png (1.4.0)
clamav-client (3.2.0)
cmdparse (3.0.7)
coderay (1.1.3)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
concurrent-ruby (1.3.1)
Expand Down Expand Up @@ -478,6 +480,8 @@ GEM
net-smtp (0.5.0)
net-protocol
nio4r (2.7.3)
nokogiri (1.16.5-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.16.5-x86_64-linux)
racc (~> 1.4)
openid_connect (2.3.0)
Expand Down Expand Up @@ -525,6 +529,12 @@ GEM
premailer (~> 1.7, >= 1.7.9)
prometheus-client (4.2.2)
promise.rb (0.7.4)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
pry-byebug (3.10.1)
byebug (~> 11.0)
pry (>= 0.13, < 0.15)
psych (5.1.2)
stringio
public_suffix (5.0.5)
Expand Down Expand Up @@ -906,6 +916,7 @@ GEM
zxcvbn-ruby (1.2.0)

PLATFORMS
x86_64-darwin-22
x86_64-linux

DEPENDENCIES
Expand Down Expand Up @@ -1002,6 +1013,7 @@ DEPENDENCIES
prawn-rails
prawn-table
premailer-rails
pry-byebug
puma
pundit
rack-attack
Expand Down
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 @@ -64,8 +64,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
53 changes: 53 additions & 0 deletions app/controllers/instructeurs/dossiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,55 @@ 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])

puts "Dossier trouvé : #{dossier.inspect}"
puts "Procedure associée au dossier : #{dossier.procedure.inspect}"
puts "Procedure attendue : #{params[:procedure_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 Down Expand Up @@ -419,6 +468,7 @@ def champs_private_params
:code_departement,
:accreditation_number,
:accreditation_birthdate,
:lexpol,
:feature,
value: []
] + TypeDeChamp::INSTANCE_CHAMPS_PARAMS
Expand Down Expand Up @@ -497,6 +547,7 @@ def aasm_error_message(exception, target_state:)

def redirect_on_dossier_not_found
if !dossier_scope.exists?(id: params[:dossier_id])
Rails.logger.debug { "Redirecting because dossier ID #{params[:dossier_id]} not found in scope." }
redirect_to instructeur_procedure_path(procedure)
end
end
Expand All @@ -505,9 +556,11 @@ def redirect_on_dossier_in_batch_operation
dossier_in_batch = begin
dossier
rescue ActiveRecord::RecordNotFound
Rails.logger.debug { "Dossier not found in batch operation for ID: #{params[:dossier_id]}" }
current_instructeur.dossiers.find(params[:dossier_id])
end
if dossier_in_batch.batch_operation.present?
Rails.logger.debug { "Redirecting because dossier ID #{params[:dossier_id]} is part of a batch operation." }
flash.alert = "Votre action n'a pas été effectuée, ce dossier fait parti d'un traitement de masse."
redirect_back(fallback_location: instructeur_dossier_path(procedure, dossier_in_batch))
end
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
13 changes: 9 additions & 4 deletions app/controllers/instructeurs/procedures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,15 @@ def procedure_id
end

def procedure
Procedure
.with_attached_logo
.find(procedure_id)
.tap { Sentry.set_tags(procedure: _1.id) }
if params[:dossier_id].present?
dossier = Dossier.find(params[:dossier_id])
dossier.procedure
else
Procedure
.with_attached_logo
.find(procedure_id)
.tap { Sentry.set_tags(procedure: _1.id) }
end
end

def ensure_ownership!
Expand Down
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
2 changes: 2 additions & 0 deletions app/graphql/types/champ_descriptor_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def resolve_type(object, context)
Types::Champs::Descriptor::TeFenuaChampDescriptorType
when TypeDeChamp.type_champs.fetch(:visa)
Types::Champs::Descriptor::VisaChampDescriptorType
when TypeDeChamp.type_champs.fetch(:lexpol)
Types::Champs::Descriptor::LexpolChampDescriptorType

# ----- DS champs
when TypeDeChamp.type_champs.fetch(:engagement_juridique)
Expand Down
2 changes: 2 additions & 0 deletions app/graphql/types/champ_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def resolve_type(object, context)
Types::Champs::TitreIdentiteChampType
when ::Champs::VisaChamp
Types::Champs::VisaChampType
when ::Champs::LexpolChamp
Types::Champs::LexpolChampType
when ::Champs::EpciChamp
Types::Champs::EpciChampType
when ::Champs::RNAChamp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Types::Champs::Descriptor
class LexpolChampDescriptorType < Types::BaseObject
implements Types::ChampDescriptorType
end
end
9 changes: 9 additions & 0 deletions app/graphql/types/champs/lexpol_champ_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Types::Champs
class LexpolChampType < Types::BaseObject
implements Types::ChampType

field :nor, String, null: true, description: "Numéro NOR du dossier Lexpol", method: :value
field :status, String, null: true, description: "Statut du dossier Lexpol", method: :lexpol_status
field :dossier_url, String, null: true, description: "Lien vers le dossier Lexpol", method: :lexpol_dossier_url
end
end
2 changes: 2 additions & 0 deletions app/models/champ.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Champ < ApplicationRecord
include ChampConditionalConcern
include ChampsValidateConcern
include LexpolConcern

belongs_to :dossier, inverse_of: false, touch: true, optional: false
belongs_to :type_de_champ, inverse_of: :champ, optional: false
Expand Down Expand Up @@ -53,6 +54,7 @@ class Champ < ApplicationRecord
:siret?,
:carte?,
:te_fenua?,
:lexpol?,
:datetime?,
:mandatory?,
:prefillable?,
Expand Down
11 changes: 11 additions & 0 deletions app/models/champs/lexpol_champ.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Champs::LexpolChamp < Champ
def generate_or_update_lexpol_dossier
if vale.blank?
nor_number = APILexpol.new.create_dossier(model_id: '598706')
self.value = nor_number
else
APILexpol.new.update_dossier(nor_number: value)
end
save!
end
end
Loading

0 comments on commit a9a7eba

Please sign in to comment.