[#2180] Expose migrated types PJ as legacy types PJ in procedure API

This commit is contained in:
Frederic Merizen 2019-01-10 15:55:04 +01:00
parent 5e37400834
commit 082255b489
3 changed files with 57 additions and 1 deletions

View file

@ -16,7 +16,7 @@ class ProcedureSerializer < ActiveModel::Serializer
has_one :geographic_information, serializer: ModuleApiCartoSerializer has_one :geographic_information, serializer: ModuleApiCartoSerializer
has_many :types_de_champ, serializer: TypeDeChampSerializer has_many :types_de_champ, serializer: TypeDeChampSerializer
has_many :types_de_champ_private, serializer: TypeDeChampSerializer has_many :types_de_champ_private, serializer: TypeDeChampSerializer
has_many :types_de_piece_justificative, serializer: TypeDePieceJustificativeSerializer has_many :types_de_piece_justificative
def archived_at def archived_at
object.archived_at&.in_time_zone('UTC') object.archived_at&.in_time_zone('UTC')
@ -43,4 +43,9 @@ class ProcedureSerializer < ActiveModel::Serializer
ModuleAPICarto.new(procedure: object) ModuleAPICarto.new(procedure: object)
end end
end end
def types_de_piece_justificative
ActiveModelSerializers::SerializableResource.new(object.types_de_piece_justificative).serializable_hash +
PiecesJustificativesService.serialize_types_de_champ_as_type_pj(object)
end
end end

View file

@ -67,4 +67,21 @@ class PiecesJustificativesService
[] []
end end
end end
def self.serialize_types_de_champ_as_type_pj(procedure)
tdcs = procedure.types_de_champ.select { |type_champ| type_champ.old_pj.present? }
tdcs.map.with_index do |type_champ, order_place|
description = type_champ.description
if /^(?<original_description>.*?)(?:[\r\n]+)Récupérer le formulaire vierge pour mon dossier : (?<lien_demarche>http.*)$/m =~ description
description = original_description
end
{
id: type_champ.old_pj[:stable_id],
libelle: type_champ.libelle,
description: description,
order_place: order_place,
lien_demarche: lien_demarche
}
end
end
end end

View file

@ -8,4 +8,38 @@ describe ProcedureSerializer do
is_expected.to include(state: "publiee") is_expected.to include(state: "publiee")
} }
end end
context 'when a type PJ was cloned to a type champ PJ' do
let(:original_procedure) do
p = create(:procedure, :published)
p.types_de_piece_justificative.create(
libelle: "Vidéo de votre demande de subvention",
description: "Pour optimiser vos chances, soignez la chorégraphie et privilégiez le chant polyphonique",
lien_demarche: "https://www.dance-academy.gouv.fr",
order_place: 0
)
p
end
let(:procedure) { original_procedure.clone(original_procedure.administrateur, false) }
let(:type_pj) { original_procedure.types_de_piece_justificative.first }
let(:migrated_type_champ) { procedure.types_de_champ.find_by(libelle: type_pj.libelle) }
subject { ProcedureSerializer.new(procedure).serializable_hash }
it "is exposed as a legacy type PJ" do
is_expected.to include(
types_de_piece_justificative: [
{
"id" => type_pj.id,
"libelle" => type_pj.libelle,
"description" => type_pj.description,
"lien_demarche" => type_pj.lien_demarche,
"order_place" => type_pj.order_place
}
]
)
end
end
end end