Add attestation url to dossier on api

This commit is contained in:
Paul Chavard 2019-07-31 16:09:28 +02:00
parent fd977a4a84
commit fba195c0b5
4 changed files with 31 additions and 8 deletions

View file

@ -2,4 +2,13 @@ class Attestation < ApplicationRecord
belongs_to :dossier belongs_to :dossier
mount_uploader :pdf, AttestationUploader mount_uploader :pdf, AttestationUploader
def pdf_url
if Rails.application.secrets.fog[:enabled]
RemoteDownloader.new(pdf.path).url
elsif pdf&.url
# FIXME: this is horrible but used only in dev and will be removed after migration
File.join(LOCAL_DOWNLOAD_URL, pdf.url)
end
end
end end

View file

@ -144,7 +144,9 @@ class Dossier < ApplicationRecord
:etablissement, :etablissement,
piece_justificative_file_attachment: :blob piece_justificative_file_attachment: :blob
], ],
avis: [], justificatif_motivation_attachment: :blob,
attestation: [],
avis: { piece_justificative_file_attachment: :blob },
etablissement: [], etablissement: [],
individual: [], individual: [],
user: []) user: [])

View file

@ -14,6 +14,9 @@ class DossierSerializer < ActiveModel::Serializer
:motivation, :motivation,
:instructeurs :instructeurs
attribute :attestation, if: :include_attestation?
attribute :justificatif_motivation, if: :include_justificatif_motivation?
has_one :individual has_one :individual
has_one :entreprise has_one :entreprise
has_one :etablissement has_one :etablissement
@ -22,7 +25,6 @@ class DossierSerializer < ActiveModel::Serializer
has_many :champs_private has_many :champs_private
has_many :pieces_justificatives has_many :pieces_justificatives
has_many :types_de_piece_justificative has_many :types_de_piece_justificative
has_one :justificatif_motivation
has_many :avis has_many :avis
has_many :champs, serializer: ChampSerializer has_many :champs, serializer: ChampSerializer
@ -53,10 +55,12 @@ class DossierSerializer < ActiveModel::Serializer
PiecesJustificativesService.serialize_champs_as_pjs(object) PiecesJustificativesService.serialize_champs_as_pjs(object)
end end
def justificatif_motivation def attestation
if object.justificatif_motivation.attached? object.attestation.pdf_url
Rails.application.routes.url_helpers.url_for(object.justificatif_motivation)
end end
def justificatif_motivation
Rails.application.routes.url_helpers.url_for(object.justificatif_motivation)
end end
def types_de_piece_justificative def types_de_piece_justificative
@ -102,4 +106,12 @@ class DossierSerializer < ActiveModel::Serializer
def processed_at def processed_at
object.processed_at&.in_time_zone('UTC') object.processed_at&.in_time_zone('UTC')
end end
def include_attestation?
object.accepte?
end
def include_justificatif_motivation?
object.justificatif_motivation.attached?
end
end end

View file

@ -154,10 +154,10 @@ describe API::V1::DossiersController do
context 'when dossier exists and belongs to procedure' do context 'when dossier exists and belongs to procedure' do
let(:procedure_id) { procedure.id } let(:procedure_id) { procedure.id }
let(:date_creation) { Time.zone.local(2008, 9, 1, 10, 5, 0) } let(:date_creation) { Time.zone.local(2008, 9, 1, 10, 5, 0) }
let!(:dossier) { Timecop.freeze(date_creation) { create(:dossier, :with_entreprise, :en_construction, procedure: procedure, motivation: "Motivation") } } let!(:dossier) { Timecop.freeze(date_creation) { create(:dossier, :with_entreprise, :with_attestation, :accepte, procedure: procedure, motivation: "Motivation") } }
let(:dossier_id) { dossier.id } let(:dossier_id) { dossier.id }
let(:body) { JSON.parse(retour.body, symbolize_names: true) } let(:body) { JSON.parse(retour.body, symbolize_names: true) }
let(:field_list) { [:id, :created_at, :updated_at, :archived, :individual, :entreprise, :etablissement, :cerfa, :types_de_piece_justificative, :pieces_justificatives, :champs, :champs_private, :commentaires, :state, :simplified_state, :initiated_at, :processed_at, :received_at, :motivation, :email, :instructeurs, :justificatif_motivation, :avis] } let(:field_list) { [:id, :created_at, :updated_at, :archived, :individual, :entreprise, :etablissement, :cerfa, :types_de_piece_justificative, :pieces_justificatives, :champs, :champs_private, :commentaires, :state, :simplified_state, :initiated_at, :processed_at, :received_at, :motivation, :email, :instructeurs, :attestation, :avis] }
subject { body[:dossier] } subject { body[:dossier] }
it 'return REST code 200', :show_in_doc do it 'return REST code 200', :show_in_doc do
@ -165,7 +165,7 @@ describe API::V1::DossiersController do
end end
it { expect(subject[:id]).to eq(dossier.id) } it { expect(subject[:id]).to eq(dossier.id) }
it { expect(subject[:state]).to eq('initiated') } it { expect(subject[:state]).to eq('closed') }
it { expect(subject[:created_at]).to eq('2008-09-01T08:05:00.000Z') } it { expect(subject[:created_at]).to eq('2008-09-01T08:05:00.000Z') }
it { expect(subject[:updated_at]).to eq('2008-09-01T08:05:00.000Z') } it { expect(subject[:updated_at]).to eq('2008-09-01T08:05:00.000Z') }
it { expect(subject[:archived]).to eq(dossier.archived) } it { expect(subject[:archived]).to eq(dossier.archived) }