Skip to content

Commit

Permalink
reopen can transition to a class
Browse files Browse the repository at this point in the history
  • Loading branch information
JeSuisUnCaillou committed Jan 21, 2025
1 parent 8c1bead commit efe94dd
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion app/models/authorization_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/organizers/reopen_authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ class ReopenAuthorization < ApplicationOrganizer
context.authorization_request = context.authorization.request
end

organize ExecuteAuthorizationRequestTransitionWithCallbacks
organize TransitionAuthorizationRequestToPreviousStage, ExecuteAuthorizationRequestTransitionWithCallbacks
end
33 changes: 33 additions & 0 deletions spec/organizers/reopen_authorization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit efe94dd

Please sign in to comment.