From efe94ddb825b427d5fc0faa5e233aa078e730664 Mon Sep 17 00:00:00 2001 From: Caillou <6117264+JeSuisUnCaillou@users.noreply.github.com> Date: Tue, 21 Jan 2025 12:26:42 +0100 Subject: [PATCH] reopen can transition to a class --- ...authorization_request_to_previous_stage.rb | 20 +++++++++++ app/models/authorization_request.rb | 6 +++- app/organizers/reopen_authorization.rb | 2 +- spec/organizers/reopen_authorization_spec.rb | 33 +++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 app/interactors/transition_authorization_request_to_previous_stage.rb diff --git a/app/interactors/transition_authorization_request_to_previous_stage.rb b/app/interactors/transition_authorization_request_to_previous_stage.rb new file mode 100644 index 000000000..c6f760a3e --- /dev/null +++ b/app/interactors/transition_authorization_request_to_previous_stage.rb @@ -0,0 +1,20 @@ +class TransitionAuthorizationRequestToPreviousStage < ApplicationInteractor + def call + return if context.authorization_request_class.blank? + + context.fail! unless context.authorization_request.can_reopen_to_class?(context.authorization_request_class) + + return if context.authorization_request.is_a? context.authorization_request_class + + transition_to_previous_stage + end + + private + + def transition_to_previous_stage + context.authorization_request.update!( + type: context.authorization_request_class.to_s, + form_uid: context.authorization_request.definition.previous_stage_form(context.authorization_request_class).id + ) + end +end diff --git a/app/models/authorization_request.rb b/app/models/authorization_request.rb index f2c159ef8..fa1326953 100644 --- a/app/models/authorization_request.rb +++ b/app/models/authorization_request.rb @@ -96,7 +96,11 @@ def latest_authorization_of_class(authorization_request_class) end def available_classes_for_reopen - authorizations.distinct.pluck(:authorization_request_class) + authorizations.distinct.pluck(:authorization_request_class).map(&:constantize) + end + + def can_reopen_to_class?(authorization_request_class) + available_classes_for_reopen.include? authorization_request_class end def events diff --git a/app/organizers/reopen_authorization.rb b/app/organizers/reopen_authorization.rb index 800e232a2..fc22bfaba 100644 --- a/app/organizers/reopen_authorization.rb +++ b/app/organizers/reopen_authorization.rb @@ -5,5 +5,5 @@ class ReopenAuthorization < ApplicationOrganizer context.authorization_request = context.authorization.request end - organize ExecuteAuthorizationRequestTransitionWithCallbacks + organize TransitionAuthorizationRequestToPreviousStage, ExecuteAuthorizationRequestTransitionWithCallbacks end diff --git a/spec/organizers/reopen_authorization_spec.rb b/spec/organizers/reopen_authorization_spec.rb index e2c8341e9..63367277f 100644 --- a/spec/organizers/reopen_authorization_spec.rb +++ b/spec/organizers/reopen_authorization_spec.rb @@ -36,5 +36,38 @@ expect { reopen_authorization_request }.not_to change { authorization_request.reload.state } end end + + context 'with an authorization_request_class to transition to' do + subject(:reopen_authorization_request) { described_class.call(authorization:, user:, authorization_request_class:) } + + let(:authorization_request) { create(:authorization_request, :api_impot_particulier, :validated) } + let(:authorization_request_class) { AuthorizationRequest::APIImpotParticulierSandbox } + + it 'transitions the authorization request to a previous stage' do + expect { reopen_authorization_request }.to change { AuthorizationRequest.find(authorization_request.id).class }.from(AuthorizationRequest::APIImpotParticulier).to(AuthorizationRequest::APIImpotParticulierSandbox) + end + + it "transistions the authorization request's form to the previous stage's form" do + expect { reopen_authorization_request }.to change { AuthorizationRequest.find(authorization_request.id).form_uid }.from('api-impot-particulier-production').to('api-impot-particulier-sandbox') + end + + context 'when the authorization_request_class is from the same stage' do + let(:authorization_request_class) { AuthorizationRequest::APIImpotParticulier } + + it "doesn't transitions the authorization_request class" do + expect { reopen_authorization_request }.not_to change { AuthorizationRequest.find(authorization_request.id).class } + end + + it "doesn't transitions the authorization_request form" do + expect { reopen_authorization_request }.not_to change { AuthorizationRequest.find(authorization_request.id).form_uid } + end + end + + context 'when the authorization_request_class is not an available reopen class' do + let(:authorization_request_class) { AuthorizationRequest::APIHermes } + + it { is_expected.to be_failure } + end + end end end