Skip to content

Commit d1c9d92

Browse files
committed
Gère proprement les erreurs de l'organisation dans l'API write
FindOrCreateOrganizationForAPI utilisait find_or_create_by! qui lève une exception ActiveRecord::RecordInvalid non rescue, remontant en 500. Remplacé par find_or_create_by + context.fail! avec les erreurs du modèle. Expose un rendu d'erreur 422 dédié (Organisation invalide) dans le controller. Refactorise render_interactor_errors via deux constantes de lookup (SIMPLE_ERRORS, MODEL_ERRORS) pour contenir la complexité.
1 parent 612665b commit d1c9d92

3 files changed

Lines changed: 49 additions & 18 deletions

File tree

app/controllers/api/v1/authorization_requests_controller.rb

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
class API::V1::AuthorizationRequestsController < API::V1Controller
2+
SIMPLE_ERRORS = {
3+
form_not_found: [422, 'Formulaire introuvable', 'Le formulaire demandé est introuvable.'],
4+
unauthorized_type: [403, 'Non autorisé', "Vous n'avez pas les droits pour créer ce type de demande."],
5+
dead_end_state: [422, 'État non modifiable', "La demande ne peut pas être modifiée dans l'état actuel."]
6+
}.freeze
7+
8+
MODEL_ERRORS = {
9+
applicant_errors: 'Demandeur invalide',
10+
organization_errors: 'Organisation invalide'
11+
}.freeze
12+
213
before_action -> { doorkeeper_authorize! :read_authorizations }, only: %i[index show]
314
before_action -> { doorkeeper_authorize! :write_authorizations }, only: %i[create update]
415
before_action :set_authorization_request, only: %i[show update]
@@ -96,19 +107,17 @@ def render_authorization_request(authorization_request, status: :ok)
96107
end
97108

98109
def render_interactor_errors(result)
99-
if result.form_not_found
100-
render_error(422, title: 'Formulaire introuvable', detail: 'Le formulaire demandé est introuvable.')
101-
elsif result.unauthorized_type
102-
render_error(403, title: 'Non autorisé', detail: "Vous n'avez pas les droits pour créer ce type de demande.")
103-
elsif result.dead_end_state
104-
render_error(422, title: 'État non modifiable', detail: "La demande ne peut pas être modifiée dans l'état actuel.")
105-
elsif result.invalid_keys.present?
106-
render_invalid_keys_errors(result.invalid_keys)
107-
elsif result.applicant_errors.present?
108-
render_applicant_errors(result.applicant_errors)
109-
else
110-
render_generic_error
110+
SIMPLE_ERRORS.each do |key, (status, title, detail)|
111+
return render_error(status, title:, detail:) if result.public_send(key)
112+
end
113+
114+
return render_invalid_keys_errors(result.invalid_keys) if result.invalid_keys.present?
115+
116+
MODEL_ERRORS.each do |key, title|
117+
return render_model_errors(title, result.public_send(key)) if result.public_send(key).present?
111118
end
119+
120+
render_generic_error
112121
end
113122

114123
def render_invalid_keys_errors(keys)
@@ -118,9 +127,9 @@ def render_invalid_keys_errors(keys)
118127
render json: { errors: }, status: :unprocessable_content
119128
end
120129

121-
def render_applicant_errors(applicant_errors)
122-
errors = applicant_errors.full_messages.map do |msg|
123-
{ status: '422', title: 'Demandeur invalide', detail: msg }
130+
def render_model_errors(title, model_errors)
131+
errors = model_errors.full_messages.map do |msg|
132+
{ status: '422', title:, detail: msg }
124133
end
125134
render json: { errors: }, status: :unprocessable_content
126135
end
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
class FindOrCreateOrganizationForAPI < ApplicationInteractor
22
def call
3-
context.organization = Organization.find_or_create_by!(
3+
context.organization = find_or_create_organization
4+
5+
return context.fail!(organization_errors: context.organization.errors) unless context.organization.persisted?
6+
7+
UpdateOrganizationINSEEPayloadJob.perform_later(context.organization.id)
8+
end
9+
10+
private
11+
12+
def find_or_create_organization
13+
Organization.find_or_create_by(
414
legal_entity_id: context.siret,
515
legal_entity_registry: 'insee_sirene'
616
)
7-
8-
UpdateOrganizationINSEEPayloadJob.perform_later(context.organization.id)
917
end
1018
end

spec/interactors/find_or_create_organization_for_api_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,19 @@
3838
expect { result }.to have_enqueued_job(UpdateOrganizationINSEEPayloadJob)
3939
end
4040
end
41+
42+
context 'with an invalid siret' do
43+
subject(:result) { described_class.call(siret: 'invalid') }
44+
45+
it { is_expected.to be_a_failure }
46+
47+
it 'exposes the organization errors' do
48+
expect(result.organization_errors).to be_present
49+
end
50+
51+
it 'does not schedule an INSEE payload update' do
52+
expect { result }.not_to have_enqueued_job(UpdateOrganizationINSEEPayloadJob)
53+
end
54+
end
4155
end
4256
end

0 commit comments

Comments
 (0)