diff --git a/app/graphql/types/dossier_type.rb b/app/graphql/types/dossier_type.rb index db83acae2..9a5eeeb99 100644 --- a/app/graphql/types/dossier_type.rb +++ b/app/graphql/types/dossier_type.rb @@ -143,8 +143,10 @@ module Types end def attestation - if object.termine? && object.procedure.attestation_template&.activated? - Loaders::Association.for(object.class, attestation: { pdf_attachment: :blob }).load(object).then(&:pdf) + if object.attestation_activated? + Loaders::Association.for(object.class, attestation: { pdf_attachment: :blob }) + .load(object) + .then { |attestation| attestation&.pdf } end end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index d687c4011..99f2a31b5 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -779,6 +779,10 @@ class Dossier < ApplicationRecord end end + def attestation_activated? + termine? && procedure.attestation_template&.activated? + end + def after_passer_en_construction self.conservation_extension = 0.days self.depose_at = self.en_construction_at = self.traitements diff --git a/spec/graphql/dossier_spec.rb b/spec/graphql/dossier_spec.rb new file mode 100644 index 000000000..3497fbcbc --- /dev/null +++ b/spec/graphql/dossier_spec.rb @@ -0,0 +1,45 @@ +RSpec.describe Types::DossierType, type: :graphql do + let(:query) { DOSSIER_QUERY } + let(:context) { { internal_use: true } } + let(:variables) { {} } + + subject { API::V2::Schema.execute(query, variables: variables, context: context) } + + let(:data) { subject['data'].deep_symbolize_keys } + let(:errors) { subject['errors'].deep_symbolize_keys } + + describe 'dossier with attestation' do + let(:dossier) { create(:dossier, :accepte, :with_attestation) } + let(:query) { DOSSIER_WITH_ATTESTATION_QUERY } + let(:variables) { { number: dossier.id } } + + it { expect(data[:dossier][:attestation]).not_to be_nil } + + context 'when attestation is nil' do + before do + dossier.update(attestation: nil) + end + + it { expect(data[:dossier][:attestation]).to be_nil } + end + end + + DOSSIER_QUERY = <<-GRAPHQL + query($number: Int!) { + dossier(number: $number) { + id + number + } + } + GRAPHQL + + DOSSIER_WITH_ATTESTATION_QUERY = <<-GRAPHQL + query($number: Int!) { + dossier(number: $number) { + attestation { + url + } + } + } + GRAPHQL +end