Skip to content

Commit

Permalink
Let lexpol controller becomes a ChampController
Browse files Browse the repository at this point in the history
  • Loading branch information
maatinito committed Feb 11, 2025
1 parent a40ec3d commit 4b16f3d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 51 deletions.
20 changes: 20 additions & 0 deletions app/controllers/champs/lexpol_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Champs
class LexpolController < Champs::ChampController

def upsert
service = LexpolService.new(champ: @champ, dossier: @champ.dossier)

force_create = params[:force_create].present?
nor = service.upsert_dossier(force_create: force_create)
if nor.present?
msg = @champ.value.blank? ? "Dossier Lexpol créé avec succès" :
"Dossier Lexpol mis à jour avec succès"
flash[:notice] = msg
else
flash[:alert] = "Impossible de #{@champ.value.blank? ? "créer" : "mettre à jour"} le dossier Lexpol."
end

redirect_back fallback_location: root_path
end
end
end
29 changes: 0 additions & 29 deletions app/controllers/instructeurs/lexpol_controller.rb

This file was deleted.

3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@

# TODO remove this route after august 2025
get ':champ_id/piece_justificative/download/:h/(:i)', to: 'piece_justificative#download', as: :legacy_piece_justificative_download

post ':dossier_id/:stable_id/lexpol/upsert', to: 'lexpol#upsert', as: :lexpol_upsert_dossier
end

resources :attachments, only: [:show, :destroy]
Expand Down Expand Up @@ -528,7 +530,6 @@
get 'reaffectation'
get 'pieces_jointes'
post 'reaffecter'
post 'lexpol/upsert/:champ_id', to: 'lexpol#upsert', as: :lexpol_upsert_dossier
end
end

Expand Down
36 changes: 16 additions & 20 deletions spec/controllers/instructeurs/lexpol_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
require 'rails_helper'

RSpec.describe Instructeurs::LexpolController, type: :controller do
RSpec.describe Champs::LexpolController, type: :controller do
render_views

let(:instructeur) { create(:instructeur) }
let(:procedure) { create(:procedure, :published, instructeurs: [instructeur]) }
let(:dossier) { create(:dossier, procedure: procedure) }
let(:champ) { create(:champ, dossier: dossier) }
let(:value) { nil }
let(:champ) { create(:champ_lexpol, dossier:, value:) }

before do
allow(ENV).to receive(:fetch).and_call_original
Expand All @@ -21,72 +22,67 @@
describe 'POST #upsert' do
context 'when dossier creation is successful' do
before do
allow(champ).to receive(:value).and_return(nil)
allow_any_instance_of(LexpolService).to receive(:upsert_dossier).and_return('NOR-12345')

post :upsert, params: {
procedure_id: procedure.id,
dossier_id: dossier.id,
champ_id: champ.id
stable_id: champ.stable_id
}
end

it 'redirects to annotations page with success message for creation' do
expect(response).to redirect_to(annotations_privees_instructeur_dossier_path(procedure, dossier))
expect(flash[:notice]).to eq('Dossier Lexpol créé avec succès. NOR : NOR-12345')
expect(response).to redirect_to(root_path)
expect(flash[:notice]).to eq('Dossier Lexpol créé avec succès')
end
end

context 'when dossier update is successful' do
let(:value) { 'NOR-12345' }
before do
allow(champ).to receive(:value).and_return('NOR-12345')
allow_any_instance_of(LexpolService).to receive(:upsert_dossier).and_return('NOR-12345')

post :upsert, params: {
procedure_id: procedure.id,
dossier_id: dossier.id,
champ_id: champ.id
stable_id: champ.stable_id
}
end

it 'redirects to annotations page with success message for update' do
expect(response).to redirect_to(annotations_privees_instructeur_dossier_path(procedure, dossier))
expect(flash[:notice]).to eq('Dossier Lexpol mis à jour avec succès. NOR : NOR-12345')
expect(response).to redirect_to(root_path)
expect(flash[:notice]).to eq('Dossier Lexpol mis à jour avec succès')
end
end

context 'when dossier creation fails' do
before do
allow(champ).to receive(:value).and_return(nil) # Simule un NOR vide
allow_any_instance_of(LexpolService).to receive(:upsert_dossier).and_return(nil)

post :upsert, params: {
procedure_id: procedure.id,
dossier_id: dossier.id,
champ_id: champ.id
stable_id: champ.stable_id
}
end

it 'redirects to annotations page with error message for creation' do
expect(response).to redirect_to(annotations_privees_instructeur_dossier_path(procedure, dossier))
expect(response).to redirect_to(root_path)
expect(flash[:alert]).to eq('Impossible de créer le dossier Lexpol.')
end
end

context 'when dossier update fails' do
let(:value) { 'NOR-12345' }
before do
allow(champ).to receive(:value).and_return('NOR-12345')
champ
allow_any_instance_of(LexpolService).to receive(:upsert_dossier).and_return(nil)

post :upsert, params: {
procedure_id: procedure.id,
dossier_id: dossier.id,
champ_id: champ.id
stable_id: champ.stable_id
}
end

it 'redirects to annotations page with error message for update' do
expect(response).to redirect_to(annotations_privees_instructeur_dossier_path(procedure, dossier))
expect(response).to redirect_to(root_path)
expect(flash[:alert]).to eq('Impossible de mettre à jour le dossier Lexpol.')
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
end

factory :champ_lexpol, class: 'Champs::LexpolChamp' do
type_de_champ { assocation :type_de_champ_lexpol, procedure: dossier.procedure }
type_de_champ { association :type_de_champ_lexpol, procedure: dossier.procedure }
end

factory :champ_cnaf, class: 'Champs::CnafChamp' do
Expand Down

0 comments on commit 4b16f3d

Please sign in to comment.