fix(apiv1): piece_justificative continue to returns a single attachment

This commit is contained in:
Colin Darie 2022-12-06 14:41:03 +01:00
parent 342a7b8ee4
commit f4d403a81c
4 changed files with 16 additions and 16 deletions

View file

@ -49,10 +49,12 @@ class Champs::PieceJustificativeChamp < Champ
def for_api def for_api
return nil unless piece_justificative_file.attached? return nil unless piece_justificative_file.attached?
piece_justificative_file.filter_map do |attachment| # API v1 don't support multiple PJ
attachment = piece_justificative_file.first
return nil if attachment.nil?
if attachment.virus_scanner.safe? || attachment.virus_scanner.pending? if attachment.virus_scanner.safe? || attachment.virus_scanner.pending?
attachment.service_url attachment.service_url
end end
end end
end
end end

View file

@ -46,14 +46,12 @@ class PiecesJustificativesService
def self.serialize_champs_as_pjs(dossier) def self.serialize_champs_as_pjs(dossier)
dossier.champs_public.filter { |champ| champ.type_de_champ.old_pj }.map do |champ| dossier.champs_public.filter { |champ| champ.type_de_champ.old_pj }.map do |champ|
champ.for_api&.map do |content_url|
{ {
created_at: champ.created_at&.in_time_zone('UTC'), created_at: champ.created_at&.in_time_zone('UTC'),
type_de_piece_justificative_id: champ.type_de_champ.old_pj[:stable_id], type_de_piece_justificative_id: champ.type_de_champ.old_pj[:stable_id],
content_url:, content_url: champ.for_api,
user: champ.dossier.user user: champ.dossier.user
} }
end
end.flatten end.flatten
end end

View file

@ -61,17 +61,17 @@ describe Champs::PieceJustificativeChamp do
context 'when file is safe' do context 'when file is safe' do
let(:status) { ActiveStorage::VirusScanner::SAFE } let(:status) { ActiveStorage::VirusScanner::SAFE }
it { is_expected.to match_array([include("/rails/active_storage/disk/")]) } it { is_expected.to include("/rails/active_storage/disk/") }
end end
context 'when file is not scanned' do context 'when file is not scanned' do
let(:status) { ActiveStorage::VirusScanner::PENDING } let(:status) { ActiveStorage::VirusScanner::PENDING }
it { is_expected.to match_array([include("/rails/active_storage/disk/")]) } it { is_expected.to include("/rails/active_storage/disk/") }
end end
context 'when file is infected' do context 'when file is infected' do
let(:status) { ActiveStorage::VirusScanner::INFECTED } let(:status) { ActiveStorage::VirusScanner::INFECTED }
it { is_expected.to eq([]) } it { is_expected.to be_nil }
end end
end end
end end

View file

@ -7,7 +7,7 @@ describe ChampSerializer do
let(:champ) { create(:champ_piece_justificative) } let(:champ) { create(:champ_piece_justificative) }
it { it {
expect(subject[:value]).to match_array([a_string_matching('/rails/active_storage/disk/')]) expect(subject[:value]).to a_string_matching('/rails/active_storage/disk/')
} }
end end