[#2180] Expose migrated PJs as legacy PJs in dossier API

This commit is contained in:
Frederic Merizen 2019-01-10 18:46:19 +01:00
parent 5f74ae1545
commit bf23abdfe3
3 changed files with 58 additions and 0 deletions

View file

@ -47,6 +47,11 @@ class DossierSerializer < ActiveModel::Serializer
[]
end
def pieces_justificatives
ActiveModelSerializers::SerializableResource.new(object.pieces_justificatives).serializable_hash +
PiecesJustificativesService.serialize_champs_as_pjs(object)
end
def email
object.user&.email
end

View file

@ -84,4 +84,15 @@ class PiecesJustificativesService
}
end
end
def self.serialize_champs_as_pjs(dossier)
dossier.champs.select { |champ| champ.type_de_champ.old_pj }.map do |champ|
{
created_at: champ.created_at&.in_time_zone('UTC'),
type_de_piece_justificative_id: champ.type_de_champ.old_pj[:stable_id],
content_url: champ.for_api,
user: champ.dossier.user
}
end
end
end

View file

@ -45,4 +45,46 @@ describe DossierSerializer do
}
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) do
p = original_procedure.clone(original_procedure.administrateur, false)
p.save
p
end
let(:type_pj) { original_procedure.types_de_piece_justificative.first }
let(:dossier) { create(:dossier, procedure: procedure) }
let(:champ_pj) { dossier.champs.last }
before do
champ_pj.piece_justificative_file.attach(io: StringIO.new("toto"), filename: "toto.txt", content_type: "text/plain")
end
subject { DossierSerializer.new(dossier).serializable_hash }
it "exposes the PJ in the legacy format" do
is_expected.to include(
pieces_justificatives: [
{
"content_url" => champ_pj.for_api,
"created_at" => champ_pj.created_at.in_time_zone('UTC').iso8601(3),
"type_de_piece_justificative_id" => type_pj.id,
"user" => a_hash_including("id" => dossier.user.id)
}
]
)
end
end
end