feat(routing): add routing engine
This commit is contained in:
parent
b93e3776c4
commit
3cede55d41
3 changed files with 58 additions and 0 deletions
|
@ -455,6 +455,8 @@ module Users
|
|||
@dossier.assign_to_groupe_instructeur(groupe_instructeur_from_params)
|
||||
end
|
||||
|
||||
RoutingEngine.compute(@dossier)
|
||||
|
||||
if dossier.en_construction?
|
||||
errors += @dossier.check_mandatory_and_visible_champs
|
||||
end
|
||||
|
|
9
app/models/routing_engine.rb
Normal file
9
app/models/routing_engine.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
module RoutingEngine
|
||||
def self.compute(dossier)
|
||||
matching_groupe = dossier.procedure.groupe_instructeurs.active.find do |gi|
|
||||
gi.routing_rule&.compute(dossier.champs)
|
||||
end
|
||||
matching_groupe ||= dossier.procedure.defaut_groupe_instructeur
|
||||
dossier.update!(groupe_instructeur: matching_groupe)
|
||||
end
|
||||
end
|
47
spec/models/routing_engine_spec.rb
Normal file
47
spec/models/routing_engine_spec.rb
Normal file
|
@ -0,0 +1,47 @@
|
|||
describe RoutingEngine, type: :model do
|
||||
include Logic
|
||||
|
||||
describe '.compute' do
|
||||
let(:procedure) do
|
||||
create(:procedure).tap do |p|
|
||||
p.groupe_instructeurs.create(label: 'a second group')
|
||||
p.groupe_instructeurs.create(label: 'a third group')
|
||||
end
|
||||
end
|
||||
|
||||
let(:dossier) { create(:dossier, procedure:) }
|
||||
let(:defaut_groupe) { procedure.defaut_groupe_instructeur }
|
||||
let(:gi_2) { procedure.groupe_instructeurs.find_by(label: 'a second group') }
|
||||
|
||||
subject do
|
||||
RoutingEngine.compute(dossier)
|
||||
dossier.groupe_instructeur
|
||||
end
|
||||
|
||||
context 'without any rules' do
|
||||
it { is_expected.to eq(defaut_groupe) }
|
||||
end
|
||||
|
||||
context 'without any matching rules' do
|
||||
before do
|
||||
procedure.groupe_instructeurs.each do |gi|
|
||||
gi.update(routing_rule: constant(false))
|
||||
end
|
||||
end
|
||||
|
||||
it { is_expected.to eq(defaut_groupe) }
|
||||
end
|
||||
|
||||
context 'with a matching rules' do
|
||||
before { gi_2.update(routing_rule: constant(true)) }
|
||||
|
||||
it { is_expected.to eq(gi_2) }
|
||||
end
|
||||
|
||||
context 'with a closed gi with a matching rules' do
|
||||
before { gi_2.update(routing_rule: constant(true), closed: true) }
|
||||
|
||||
it { is_expected.to eq(defaut_groupe) }
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue