diff --git a/app/serializers/procedure_serializer.rb b/app/serializers/procedure_serializer.rb index 674d8ab91..d219ee4b6 100644 --- a/app/serializers/procedure_serializer.rb +++ b/app/serializers/procedure_serializer.rb @@ -16,7 +16,7 @@ class ProcedureSerializer < ActiveModel::Serializer has_one :geographic_information, serializer: ModuleApiCartoSerializer has_many :types_de_champ, 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 object.archived_at&.in_time_zone('UTC') @@ -43,4 +43,9 @@ class ProcedureSerializer < ActiveModel::Serializer ModuleAPICarto.new(procedure: object) 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 diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 8a199a011..d0bc69815 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -67,4 +67,21 @@ class PiecesJustificativesService [] 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 /^(?.*?)(?:[\r\n]+)Récupérer le formulaire vierge pour mon dossier : (?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 diff --git a/spec/serializers/procedure_serializer_spec.rb b/spec/serializers/procedure_serializer_spec.rb index 487866665..fa5bd006b 100644 --- a/spec/serializers/procedure_serializer_spec.rb +++ b/spec/serializers/procedure_serializer_spec.rb @@ -8,4 +8,38 @@ describe ProcedureSerializer do is_expected.to include(state: "publiee") } 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