2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-04-21 12:02:57 +02:00
|
|
|
describe DossierProjectionService do
|
|
|
|
describe '#project' do
|
2024-10-07 15:00:05 +02:00
|
|
|
subject { described_class.project(dossiers_ids, columns) }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
|
|
|
context 'with multiple dossier' do
|
2024-05-13 15:03:28 +02:00
|
|
|
let!(:procedure) { create(:procedure, types_de_champ_public: [{}, { type: :linked_drop_down_list }]) }
|
2021-04-21 12:02:57 +02:00
|
|
|
let!(:dossier_1) { create(:dossier, procedure: procedure) }
|
2021-04-26 10:35:22 +02:00
|
|
|
let!(:dossier_2) { create(:dossier, :en_construction, :archived, procedure: procedure) }
|
|
|
|
let!(:dossier_3) { create(:dossier, :en_instruction, procedure: procedure) }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
|
|
|
let(:dossiers_ids) { [dossier_3.id, dossier_1.id, dossier_2.id] }
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:columns) do
|
2024-05-13 15:03:28 +02:00
|
|
|
procedure.active_revision.types_de_champ_public.map do |type_de_champ|
|
2024-10-07 15:00:05 +02:00
|
|
|
procedure.find_column(label: type_de_champ.libelle)
|
2024-05-13 15:03:28 +02:00
|
|
|
end
|
2021-04-21 12:02:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2024-09-27 15:37:11 +02:00
|
|
|
dossier_1.project_champs_public.first.update(value: 'champ_1')
|
|
|
|
dossier_1.project_champs_public.second.update(value: '["test"]')
|
|
|
|
dossier_2.project_champs_public.first.update(value: 'champ_2')
|
|
|
|
dossier_3.project_champs_public.first.destroy
|
2021-04-21 12:02:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:result) { subject }
|
|
|
|
|
2021-04-26 10:35:22 +02:00
|
|
|
it 'respects the dossiers_ids order, returns state, archived and nil for empty result' do
|
2021-04-21 12:02:57 +02:00
|
|
|
expect(result.length).to eq(3)
|
2021-04-26 10:35:22 +02:00
|
|
|
|
|
|
|
expect(result[0].dossier_id).to eq(dossier_3.id)
|
|
|
|
expect(result[1].dossier_id).to eq(dossier_1.id)
|
|
|
|
expect(result[2].dossier_id).to eq(dossier_2.id)
|
|
|
|
|
|
|
|
expect(result[0].state).to eq('en_instruction')
|
|
|
|
expect(result[1].state).to eq('brouillon')
|
|
|
|
expect(result[2].state).to eq('en_construction')
|
|
|
|
|
|
|
|
expect(result[0].archived).to be false
|
|
|
|
expect(result[1].archived).to be false
|
|
|
|
expect(result[2].archived).to be true
|
2021-04-21 12:02:57 +02:00
|
|
|
|
|
|
|
expect(result[0].columns[0]).to be nil
|
|
|
|
expect(result[1].columns[0]).to eq('champ_1')
|
|
|
|
expect(result[2].columns[0]).to eq('champ_2')
|
|
|
|
end
|
2023-04-03 16:05:18 +02:00
|
|
|
end
|
|
|
|
|
2023-04-13 13:10:23 +02:00
|
|
|
context 'with commune champ' do
|
2023-04-03 16:05:18 +02:00
|
|
|
let!(:procedure) { create(:procedure, types_de_champ_public: [{ type: :communes }]) }
|
|
|
|
let!(:dossier) { create(:dossier, procedure:) }
|
|
|
|
|
|
|
|
let(:dossiers_ids) { [dossier.id] }
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:columns) do
|
2023-04-03 16:05:18 +02:00
|
|
|
[
|
2024-10-07 15:00:05 +02:00
|
|
|
procedure.find_column(label: procedure.active_revision.types_de_champ_public[0].libelle)
|
2023-04-03 16:05:18 +02:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2024-09-27 15:37:11 +02:00
|
|
|
dossier.project_champs_public.first.update(code_postal: '63290', external_id: '63102')
|
2023-04-03 16:05:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:result) { subject }
|
|
|
|
|
|
|
|
it 'returns champ value' do
|
|
|
|
expect(result.length).to eq(1)
|
|
|
|
expect(result[0].dossier_id).to eq(dossier.id)
|
|
|
|
expect(result[0].columns[0]).to eq('Châteldon (63290)')
|
|
|
|
end
|
2021-04-21 12:02:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'attributes by attributes' do
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:procedure) { create(:procedure) }
|
|
|
|
let(:columns) { [procedure.find_column(label:)] }
|
2021-04-21 12:02:57 +02:00
|
|
|
let(:dossiers_ids) { [dossier.id] }
|
|
|
|
|
|
|
|
subject { super()[0].columns[0] }
|
|
|
|
|
|
|
|
context 'for self table' do
|
|
|
|
context 'for created_at column' do
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:label) { 'Créé le' }
|
|
|
|
let(:dossier) { Timecop.freeze(Time.zone.local(1992, 3, 22)) { create(:dossier, procedure:) } }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
|
|
|
it { is_expected.to eq('22/03/1992') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for en_construction_at column' do
|
2024-10-25 14:09:14 +02:00
|
|
|
let(:label) { 'Passé en construction le' }
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:dossier) { create(:dossier, :en_construction, en_construction_at: Time.zone.local(2018, 10, 17), procedure:) }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
|
|
|
it { is_expected.to eq('17/10/2018') }
|
|
|
|
end
|
|
|
|
|
2021-12-06 15:49:17 +01:00
|
|
|
context 'for depose_at column' do
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:label) { 'Déposé le' }
|
|
|
|
let(:dossier) { create(:dossier, :en_construction, depose_at: Time.zone.local(2018, 10, 17), procedure:) }
|
2021-12-06 15:49:17 +01:00
|
|
|
|
|
|
|
it { is_expected.to eq('17/10/2018') }
|
|
|
|
end
|
|
|
|
|
2021-04-21 12:02:57 +02:00
|
|
|
context 'for updated_at column' do
|
2024-10-25 14:09:14 +02:00
|
|
|
let(:label) { 'Dernière mise à jour le' }
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:dossier) { create(:dossier, procedure:) }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
|
|
|
before { dossier.touch(time: Time.zone.local(2018, 9, 25)) }
|
|
|
|
|
|
|
|
it { is_expected.to eq('25/09/2018') }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for user table' do
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:label) { 'Demandeur' }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:dossier) { create(:dossier, user: create(:user, email: 'bla@yopmail.com'), procedure:) }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
|
|
|
it { is_expected.to eq('bla@yopmail.com') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for individual table' do
|
|
|
|
let(:procedure) { create(:procedure, :for_individual, :with_type_de_champ, :with_type_de_champ_private) }
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:dossier) { create(:dossier, procedure:, individual: build(:individual, nom: 'Martin', prenom: 'Jacques', gender: 'M.')) }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
|
|
|
context 'for prenom column' do
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:label) { 'Prénom' }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
|
|
|
it { is_expected.to eq('Jacques') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for nom column' do
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:label) { 'Nom' }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
|
|
|
it { is_expected.to eq('Martin') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for gender column' do
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:label) { 'Civilité' }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
|
|
|
it { is_expected.to eq('M.') }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for etablissement table' do
|
2024-10-25 14:09:14 +02:00
|
|
|
let(:label) { 'Établissement code postal' }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
2024-10-07 15:00:05 +02:00
|
|
|
let!(:dossier) { create(:dossier, procedure:, etablissement: create(:etablissement, code_postal: '75008')) }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
|
|
|
it { is_expected.to eq('75008') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for groupe_instructeur table' do
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:label) { 'Groupe instructeur' }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
2024-10-07 15:00:05 +02:00
|
|
|
let!(:dossier) { create(:dossier, procedure:) }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
|
|
|
it { is_expected.to eq('défaut') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for followers_instructeurs table' do
|
2024-10-25 14:09:14 +02:00
|
|
|
let(:label) { 'Instructeurs' }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:dossier) { create(:dossier, procedure:) }
|
2024-02-19 14:35:54 +01:00
|
|
|
let!(:follow1) { create(:follow, dossier: dossier, instructeur: create(:instructeur, email: 'b@host.fr')) }
|
|
|
|
let!(:follow2) { create(:follow, dossier: dossier, instructeur: create(:instructeur, email: 'a@host.fr')) }
|
|
|
|
let!(:follow3) { create(:follow, dossier: dossier, instructeur: create(:instructeur, email: 'c@host.fr')) }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
2024-02-19 14:35:54 +01:00
|
|
|
it { is_expected.to eq "a@host.fr, b@host.fr, c@host.fr" }
|
2021-04-21 12:02:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for type_de_champ table' do
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :text }]) }
|
|
|
|
let(:dossier) { create(:dossier, procedure:) }
|
|
|
|
let(:label) { dossier.procedure.active_revision.types_de_champ_public.first.libelle }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
2024-10-07 15:00:05 +02:00
|
|
|
before do
|
|
|
|
dossier.project_champs_public.first.update(value: 'kale')
|
|
|
|
end
|
2021-04-21 12:02:57 +02:00
|
|
|
|
|
|
|
it { is_expected.to eq('kale') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for type_de_champ_private table' do
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_private: [{ type: :text }]) }
|
|
|
|
let(:dossier) { create(:dossier, procedure:) }
|
|
|
|
let(:label) { dossier.procedure.active_revision.types_de_champ_private.first.libelle }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
2024-09-27 15:37:11 +02:00
|
|
|
before { dossier.project_champs_private.first.update(value: 'quinoa') }
|
2021-04-21 12:02:57 +02:00
|
|
|
|
|
|
|
it { is_expected.to eq('quinoa') }
|
|
|
|
end
|
2021-05-18 11:34:05 +02:00
|
|
|
|
|
|
|
context 'for type_de_champ table and value to.s' do
|
2024-01-23 09:52:20 +01:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :yes_no }]) }
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:dossier) { create(:dossier, procedure:) }
|
|
|
|
let(:label) { dossier.procedure.active_revision.types_de_champ_public.first.libelle }
|
2021-05-18 11:34:05 +02:00
|
|
|
|
2024-09-27 15:37:11 +02:00
|
|
|
before { dossier.project_champs_public.first.update(value: 'true') }
|
2021-05-18 11:34:05 +02:00
|
|
|
|
|
|
|
it { is_expected.to eq('Oui') }
|
|
|
|
end
|
2021-05-19 15:59:18 +02:00
|
|
|
|
|
|
|
context 'for type_de_champ table and value to.s which needs data field' do
|
2024-01-23 09:52:20 +01:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :address }]) }
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:dossier) { create(:dossier, procedure:) }
|
|
|
|
let(:label) { dossier.procedure.active_revision.types_de_champ_public.first.libelle }
|
2021-05-19 15:59:18 +02:00
|
|
|
|
2024-09-27 15:37:11 +02:00
|
|
|
before { dossier.project_champs_public.first.update(value: '18 a la bonne rue', data: { 'label' => '18 a la bonne rue', 'departement' => 'd' }) }
|
2021-05-19 15:59:18 +02:00
|
|
|
|
|
|
|
it { is_expected.to eq('18 a la bonne rue') }
|
|
|
|
end
|
2021-09-07 14:27:07 +02:00
|
|
|
|
|
|
|
context 'for type_de_champ table: type_de_champ pays which needs external_id field' do
|
2022-08-04 11:39:07 +02:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :pays }]) }
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:dossier) { create(:dossier, procedure:) }
|
|
|
|
let(:label) { dossier.procedure.active_revision.types_de_champ_public.first.libelle }
|
2021-09-07 14:27:07 +02:00
|
|
|
|
2023-04-19 11:39:58 +02:00
|
|
|
around do |example|
|
|
|
|
I18n.with_locale(:fr) do
|
|
|
|
example.run
|
|
|
|
end
|
|
|
|
end
|
2021-09-07 14:27:07 +02:00
|
|
|
|
|
|
|
context 'when external id is set' do
|
|
|
|
before do
|
2024-09-27 15:37:11 +02:00
|
|
|
dossier.project_champs_public.first.update(external_id: 'GB')
|
2021-09-07 14:27:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to eq('Royaume-Uni') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no external id is set' do
|
|
|
|
before do
|
2024-09-27 15:37:11 +02:00
|
|
|
dossier.project_champs_public.first.update(value: "qu'il est beau mon pays")
|
2021-09-07 14:27:07 +02:00
|
|
|
end
|
|
|
|
|
2023-01-18 12:52:38 +01:00
|
|
|
it { is_expected.to eq("") }
|
2021-09-07 14:27:07 +02:00
|
|
|
end
|
|
|
|
end
|
2023-03-27 16:23:11 +02:00
|
|
|
|
2023-04-13 19:10:09 +02:00
|
|
|
context 'for dossier corrections table' do
|
2023-04-03 17:05:54 +02:00
|
|
|
let(:table) { 'dossier_corrections' }
|
2023-03-27 16:23:11 +02:00
|
|
|
let(:column) { 'resolved_at' }
|
2024-10-07 15:00:05 +02:00
|
|
|
let(:procedure) { create(:procedure) }
|
|
|
|
let(:columns) { [Column.new(procedure_id: procedure.id, table:, column:)] } # should somehow be present in column concern
|
|
|
|
let(:dossier) { create(:dossier, :en_construction, procedure:) }
|
|
|
|
subject { described_class.project(dossiers_ids, columns)[0] }
|
2023-03-27 16:23:11 +02:00
|
|
|
|
|
|
|
context "when dossier has pending correction" do
|
2023-04-03 17:05:54 +02:00
|
|
|
before { create(:dossier_correction, dossier:) }
|
2023-03-27 16:23:11 +02:00
|
|
|
|
|
|
|
it { expect(subject.pending_correction?).to be(true) }
|
2024-01-24 17:01:59 +01:00
|
|
|
it { expect(subject.resolved_corrections?).to eq(false) }
|
2023-03-27 16:23:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when dossier has a resolved correction" do
|
2023-04-03 17:05:54 +02:00
|
|
|
before { create(:dossier_correction, :resolved, dossier:) }
|
2023-03-27 16:23:11 +02:00
|
|
|
|
|
|
|
it { expect(subject.pending_correction?).to eq(false) }
|
2024-01-24 17:01:59 +01:00
|
|
|
it { expect(subject.resolved_corrections?).to eq(true) }
|
2023-03-27 16:23:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when dossier has no correction at all" do
|
|
|
|
it { expect(subject.pending_correction?).to eq(false) }
|
2024-01-24 17:01:59 +01:00
|
|
|
it { expect(subject.resolved_corrections?).to eq(false) }
|
2023-03-27 16:23:11 +02:00
|
|
|
end
|
|
|
|
end
|
2021-04-21 12:02:57 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|