add exportables_pieces_jointes_for_all_versions and outdated to pj_list_concern

This commit is contained in:
simon lehericey 2024-07-26 10:06:38 +02:00
parent 106698a242
commit 24109a0128
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
2 changed files with 79 additions and 39 deletions

View file

@ -11,14 +11,26 @@ module PiecesJointesListConcern
pieces_jointes(exclude_titre_identite: true) pieces_jointes(exclude_titre_identite: true)
end end
def exportables_pieces_jointes_for_all_versions
pieces_jointes(
exclude_titre_identite: true,
revision: revisions
).sort_by { - _1.id }.uniq(&:stable_id)
end
def outdated_exportables_pieces_jointes
exportables_pieces_jointes_for_all_versions - exportables_pieces_jointes
end
private private
def pieces_jointes( def pieces_jointes(
exclude_titre_identite: false, exclude_titre_identite: false,
public_only: false, public_only: false,
wrap_with_parent: false wrap_with_parent: false,
revision: active_revision
) )
coordinates = active_revision.revision_types_de_champ coordinates = ProcedureRevisionTypeDeChamp.where(revision:)
.includes(:type_de_champ, revision_types_de_champ: :type_de_champ) .includes(:type_de_champ, revision_types_de_champ: :type_de_champ)
coordinates = coordinates.public_only if public_only coordinates = coordinates.public_only if public_only

View file

@ -1,50 +1,78 @@
describe PiecesJointesListConcern do describe PiecesJointesListConcern do
describe '#pieces_jointes_list' do describe '#pieces_jointes_list' do
include Logic include Logic
let(:procedure) { create(:procedure, types_de_champ_public:, types_de_champ_private:) }
let(:types_de_champ_public) do
[
{ type: :integer_number, stable_id: 900 },
{ type: :piece_justificative, libelle: "pj1", stable_id: 910 },
{ type: :piece_justificative, libelle: "pj-cond", stable_id: 911, condition: ds_eq(champ_value(900), constant(1)) },
{ type: :repetition, libelle: "Répétition", stable_id: 920, children: [{ type: :piece_justificative, libelle: "pj2", stable_id: 921 }] },
{ type: :titre_identite, libelle: "pj3", stable_id: 930 }
]
end
let(:types_de_champ_private) do describe 'public_wrapped_partionned_pjs and exportables_pieces_jointes' do
[ let(:procedure) { create(:procedure, types_de_champ_public:, types_de_champ_private:) }
{ type: :integer_number, stable_id: 950 }, let(:types_de_champ_public) do
{ type: :piece_justificative, libelle: "pj5", stable_id: 960 }, [
{ type: :piece_justificative, libelle: "pj-cond2", stable_id: 961, condition: ds_eq(champ_value(900), constant(1)) }, { type: :integer_number, stable_id: 900 },
{ type: :repetition, libelle: "Répétition2", stable_id: 970, children: [{ type: :piece_justificative, libelle: "pj6", stable_id: 971 }] } { type: :piece_justificative, libelle: "pj1", stable_id: 910 },
] { type: :piece_justificative, libelle: "pj-cond", stable_id: 911, condition: ds_eq(champ_value(900), constant(1)) },
end { type: :repetition, libelle: "Répétition", stable_id: 920, children: [{ type: :piece_justificative, libelle: "pj2", stable_id: 921 }] },
{ type: :titre_identite, libelle: "pj3", stable_id: 930 }
]
end
let(:types_de_champ) { procedure.active_revision.types_de_champ } let(:types_de_champ_private) do
def find_by_stable_id(stable_id) = types_de_champ.find { _1.stable_id == stable_id } [
{ type: :integer_number, stable_id: 950 },
{ type: :piece_justificative, libelle: "pj5", stable_id: 960 },
{ type: :piece_justificative, libelle: "pj-cond2", stable_id: 961, condition: ds_eq(champ_value(900), constant(1)) },
{ type: :repetition, libelle: "Répétition2", stable_id: 970, children: [{ type: :piece_justificative, libelle: "pj6", stable_id: 971 }] }
]
end
let(:pj1) { find_by_stable_id(910) } let(:types_de_champ) { procedure.active_revision.types_de_champ }
let(:pjcond) { find_by_stable_id(911) } def find_by_stable_id(stable_id) = types_de_champ.find { _1.stable_id == stable_id }
let(:repetition) { find_by_stable_id(920) }
let(:pj2) { find_by_stable_id(921) }
let(:pj3) { find_by_stable_id(930) }
let(:pj5) { find_by_stable_id(960) } let(:pj1) { find_by_stable_id(910) }
let(:pjcond2) { find_by_stable_id(961) } let(:pjcond) { find_by_stable_id(911) }
let(:repetition2) { find_by_stable_id(970) } let(:repetition) { find_by_stable_id(920) }
let(:pj6) { find_by_stable_id(971) } let(:pj2) { find_by_stable_id(921) }
let(:pj3) { find_by_stable_id(930) }
it "returns the list of pieces jointes without conditional" do let(:pj5) { find_by_stable_id(960) }
expect(procedure.public_wrapped_partionned_pjs.first).to match_array([[pj1], [pj2, repetition], [pj3]]) let(:pjcond2) { find_by_stable_id(961) }
end let(:repetition2) { find_by_stable_id(970) }
let(:pj6) { find_by_stable_id(971) }
it "returns the list of pieces jointes having conditional" do it "returns the list of pieces jointes without conditional" do
expect(procedure.public_wrapped_partionned_pjs.second).to match_array([[pjcond]]) expect(procedure.public_wrapped_partionned_pjs.first).to match_array([[pj1], [pj2, repetition], [pj3]])
end end
it "returns the list of pieces jointes with private, without parent repetition, without titre identite" do it "returns the list of pieces jointes having conditional" do
expect(procedure.exportables_pieces_jointes.map(&:libelle)).to match_array([pj1, pj2, pjcond, pj5, pjcond2, pj6].map(&:libelle)) expect(procedure.public_wrapped_partionned_pjs.second).to match_array([[pjcond]])
end
it "returns the list of pieces jointes with private, without parent repetition, without titre identite" do
expect(procedure.exportables_pieces_jointes.map(&:libelle)).to match_array([pj1, pj2, pjcond, pj5, pjcond2, pj6].map(&:libelle))
end
it "returns the same list but for all versions" do
expect(procedure.exportables_pieces_jointes.map(&:libelle)).to match_array([pj1, pj2, pjcond, pj5, pjcond2, pj6].map(&:libelle))
end
end end
end end
describe '#outdated_exportables_pieces_jointes' do
let(:types_de_champ_public) do
[
{ type: :piece_justificative, libelle: "outdated", stable_id: 1 },
{ type: :piece_justificative, libelle: "kept", stable_id: 2 }
]
end
let(:procedure) { create(:procedure, :published, types_de_champ_public:) }
before do
procedure.draft_revision.remove_type_de_champ(1)
procedure.draft_revision.add_type_de_champ(type_champ: :piece_justificative, libelle: 'new', mandatory: false)
procedure.publish_revision!
end
it { expect(procedure.exportables_pieces_jointes_for_all_versions.map(&:libelle)).to eq(["new", "kept", "outdated"]) }
it { expect(procedure.exportables_pieces_jointes.map(&:libelle)).to match_array(["kept", "new"]) }
it { expect(procedure.outdated_exportables_pieces_jointes.map(&:libelle)).to match_array(["outdated"]) }
end
end end