2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-03-29 10:47:43 +02:00
|
|
|
describe RoutingEngine, type: :model do
|
|
|
|
include Logic
|
|
|
|
|
|
|
|
describe '.compute' do
|
|
|
|
let(:dossier) { create(:dossier, procedure:) }
|
|
|
|
let(:defaut_groupe) { procedure.defaut_groupe_instructeur }
|
2023-09-04 14:58:59 +02:00
|
|
|
let(:gi_2) { procedure.groupe_instructeurs.create(label: 'a second group') }
|
2023-03-29 10:47:43 +02:00
|
|
|
|
|
|
|
subject do
|
|
|
|
RoutingEngine.compute(dossier)
|
|
|
|
dossier.groupe_instructeur
|
|
|
|
end
|
|
|
|
|
2023-08-22 14:06:58 +02:00
|
|
|
context 'with a drop down list type de champ' do
|
|
|
|
let(:procedure) do
|
|
|
|
create(:procedure,
|
|
|
|
types_de_champ_public: [{ type: :drop_down_list, libelle: 'Votre ville', options: ['Paris', 'Lyon', 'Marseille'] }]).tap do |p|
|
|
|
|
p.groupe_instructeurs.create(label: 'a third group')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:drop_down_tdc) { procedure.draft_revision.types_de_champ.first }
|
2023-03-29 10:47:43 +02:00
|
|
|
|
2023-08-22 14:06:58 +02:00
|
|
|
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|
|
2023-11-22 12:04:25 +01:00
|
|
|
gi.update(routing_rule: ds_eq(constant(false), constant(true)))
|
2023-08-22 14:06:58 +02:00
|
|
|
end
|
2023-03-29 10:47:43 +02:00
|
|
|
end
|
2023-08-22 14:06:58 +02:00
|
|
|
|
|
|
|
it { is_expected.to eq(defaut_groupe) }
|
2023-03-29 10:47:43 +02:00
|
|
|
end
|
|
|
|
|
2023-08-22 14:06:58 +02:00
|
|
|
context 'with rules not configured yet' do
|
|
|
|
before do
|
|
|
|
procedure.groupe_instructeurs.each do |gi|
|
|
|
|
gi.update(routing_rule: ds_eq(empty, empty))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq(defaut_groupe) }
|
|
|
|
end
|
2023-03-29 10:47:43 +02:00
|
|
|
|
2023-08-22 14:06:58 +02:00
|
|
|
context 'with a matching rule' do
|
|
|
|
before do
|
|
|
|
gi_2.update(routing_rule: ds_eq(champ_value(drop_down_tdc.stable_id), constant('Lyon')))
|
|
|
|
dossier.champs.first.update(value: 'Lyon')
|
2023-05-25 14:53:51 +02:00
|
|
|
end
|
2023-08-22 14:06:58 +02:00
|
|
|
|
|
|
|
it { is_expected.to eq(gi_2) }
|
2023-05-25 14:53:51 +02:00
|
|
|
end
|
|
|
|
|
2023-08-22 14:06:58 +02:00
|
|
|
context 'with a closed gi with a matching rule' do
|
|
|
|
before { gi_2.update(routing_rule: constant(true), closed: true) }
|
|
|
|
|
|
|
|
it { is_expected.to eq(defaut_groupe) }
|
|
|
|
end
|
2023-08-28 16:01:31 +02:00
|
|
|
|
|
|
|
context 'with a non equals rule' do
|
|
|
|
before do
|
|
|
|
gi_2.update(routing_rule: ds_not_eq(champ_value(drop_down_tdc.stable_id), constant('Lyon')))
|
|
|
|
dossier.champs.first.update(value: 'Paris')
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
|
|
|
is_expected.not_to eq(defaut_groupe)
|
|
|
|
is_expected.to eq(gi_2)
|
|
|
|
end
|
|
|
|
end
|
2023-05-25 14:53:51 +02:00
|
|
|
end
|
|
|
|
|
2023-08-22 14:06:58 +02:00
|
|
|
context 'with a departements type de champ' do
|
|
|
|
let(:procedure) do
|
|
|
|
create(:procedure, types_de_champ_public: [{ type: :departements }]).tap do |p|
|
|
|
|
p.groupe_instructeurs.create(label: 'a third group')
|
|
|
|
end
|
2023-05-25 14:53:51 +02:00
|
|
|
end
|
2023-03-29 10:47:43 +02:00
|
|
|
|
2023-08-22 14:06:58 +02:00
|
|
|
let(:departements_tdc) { procedure.draft_revision.types_de_champ.first }
|
2023-03-29 10:47:43 +02:00
|
|
|
|
2023-08-22 14:06:58 +02:00
|
|
|
context 'with a matching rule' do
|
|
|
|
before do
|
2023-08-29 11:35:39 +02:00
|
|
|
gi_2.update(routing_rule: ds_eq(champ_value(departements_tdc.stable_id), constant('43')))
|
2023-08-22 14:06:58 +02:00
|
|
|
dossier.champs.first.update(value: 'Haute-Loire')
|
|
|
|
end
|
2023-03-29 10:47:43 +02:00
|
|
|
|
2023-09-08 15:49:50 +02:00
|
|
|
it { is_expected.to eq(gi_2) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a regions type de champ' do
|
|
|
|
let(:procedure) do
|
|
|
|
create(:procedure, types_de_champ_public: [{ type: :regions }]).tap do |p|
|
|
|
|
p.groupe_instructeurs.create(label: 'a third group')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:regions_tdc) { procedure.draft_revision.types_de_champ.first }
|
|
|
|
|
|
|
|
context 'with a matching rule' do
|
|
|
|
before do
|
|
|
|
gi_2.update(routing_rule: ds_eq(champ_value(regions_tdc.stable_id), constant('04')))
|
|
|
|
dossier.champs.first.update(value: 'La Réunion')
|
|
|
|
end
|
|
|
|
|
2023-09-11 17:34:04 +02:00
|
|
|
it { is_expected.to eq(gi_2) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a communes type de champ' do
|
|
|
|
let(:procedure) do
|
|
|
|
create(:procedure, types_de_champ_public: [{ type: :communes }]).tap do |p|
|
|
|
|
p.groupe_instructeurs.create(label: 'a third group')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:communes_tdc) { procedure.draft_revision.types_de_champ.first }
|
|
|
|
|
|
|
|
context 'with a matching rule' do
|
|
|
|
before do
|
2023-12-21 14:20:54 +01:00
|
|
|
gi_2.update(routing_rule: ds_in_departement(champ_value(communes_tdc.stable_id), constant('92')))
|
2023-10-13 16:06:26 +02:00
|
|
|
dossier.champs.first.update(code_postal: '92500', external_id: '92063')
|
2023-09-11 17:34:04 +02:00
|
|
|
end
|
|
|
|
|
2023-08-22 14:06:58 +02:00
|
|
|
it { is_expected.to eq(gi_2) }
|
|
|
|
end
|
2023-03-29 10:47:43 +02:00
|
|
|
end
|
2023-09-04 14:58:59 +02:00
|
|
|
|
2023-09-11 20:34:16 +02:00
|
|
|
context 'with an epci type de champ' do
|
|
|
|
let(:procedure) do
|
|
|
|
create(:procedure, types_de_champ_public: [{ type: :epci }]).tap do |p|
|
|
|
|
p.groupe_instructeurs.create(label: 'a third group')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:epci_tdc) { procedure.draft_revision.types_de_champ.first }
|
|
|
|
|
|
|
|
context 'with a matching rule' do
|
|
|
|
before do
|
2023-12-21 14:20:54 +01:00
|
|
|
gi_2.update(routing_rule: ds_in_departement(champ_value(epci_tdc.stable_id), constant('42')))
|
2023-09-11 20:34:16 +02:00
|
|
|
dossier.champs.first.update_columns(
|
|
|
|
external_id: 244200895,
|
|
|
|
value: 'CC du Pilat Rhodanien',
|
|
|
|
value_json: { code_departement: '42' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
|
|
|
is_expected.to eq(gi_2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-04 14:58:59 +02:00
|
|
|
context 'routing rules priorities' do
|
|
|
|
let(:procedure) do
|
|
|
|
create(:procedure,
|
|
|
|
types_de_champ_public: [{ type: :drop_down_list, libelle: 'Ville', options: ['Paris', 'Lyon', 'Marseille'] }]).tap do |p|
|
|
|
|
p.groupe_instructeurs.create(label: 'c')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:drop_down_tdc) { procedure.draft_revision.types_de_champ.first }
|
|
|
|
|
|
|
|
let!(:gi_3) { procedure.groupe_instructeurs.find_by(label: 'c') }
|
|
|
|
|
|
|
|
context 'not eq rule coming first' do
|
|
|
|
before do
|
|
|
|
defaut_groupe.update(label: 'a')
|
|
|
|
gi_2.update(label: 'b', routing_rule: ds_not_eq(champ_value(drop_down_tdc.stable_id), constant('Lyon')))
|
|
|
|
gi_3.update(routing_rule: ds_eq(champ_value(drop_down_tdc.stable_id), constant('Marseille')))
|
|
|
|
dossier.champs.first.update(value: 'Marseille')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'computes by groups label order' do
|
|
|
|
is_expected.to eq(gi_2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-03-29 10:47:43 +02:00
|
|
|
end
|
|
|
|
end
|