[GraphQL] expose dossier pdf, geojson and attestation

This commit is contained in:
Paul Chavard 2020-10-01 12:41:57 +02:00
parent e3f2421741
commit 0aa06d0197
8 changed files with 129 additions and 3 deletions

View file

@ -0,0 +1,26 @@
class API::V2::DossiersController < API::V2::BaseController
before_action :ensure_dossier_present
def pdf
@include_infos_administration = true
render(file: 'dossiers/show', formats: [:pdf])
end
def geojson
send_data dossier.to_feature_collection.to_json,
type: 'application/json',
filename: "dossier-#{dossier.id}-features.json"
end
private
def ensure_dossier_present
if dossier.blank?
head :unauthorized
end
end
def dossier
@dossier ||= GlobalID::Locator.locate_signed(params[:id].to_s, for: 'api_v2')
end
end

View file

@ -1,6 +1,5 @@
module Types::Champs
class PieceJustificativeChampType < Types::BaseObject
include Rails.application.routes.url_helpers
implements Types::ChampType
field :file, Types::File, null: true, extensions: [

View file

@ -24,6 +24,10 @@ module Types
{ Extensions::Attachment => { attachment: :justificatif_motivation } }
]
field :pdf, Types::File, "LURL du dossier au format PDF.", null: true
field :geojson, Types::File, "LURL du GeoJSON contenant les données cartographiques du dossier.", null: true
field :attestation, Types::File, "LURL de lattestation au format PDF.", null: true
field :usager, Types::ProfileType, null: false
field :groupe_instructeur, Types::GroupeInstructeurType, null: false
field :revision, Types::RevisionType, null: false
@ -81,6 +85,30 @@ module Types
Loaders::Association.for(object.class, :champs_private).load(object)
end
def pdf
sgid = object.to_sgid(expires_in: 1.hour, for: 'api_v2')
{
filename: "dossier-#{object.id}.pdf",
content_type: 'application/pdf',
url: Rails.application.routes.url_helpers.api_v2_dossier_pdf_url(id: sgid)
}
end
def geojson
sgid = object.to_sgid(expires_in: 1.hour, for: 'api_v2')
{
filename: "dossier-#{object.id}-features.json",
content_type: 'application/json',
url: Rails.application.routes.url_helpers.api_v2_dossier_geojson_url(id: sgid)
}
end
def attestation
if object.termine? && object.procedure.attestation_template&.activated?
Loaders::Association.for(object.class, attestation: { pdf_attachment: :blob }).load(object).then(&:pdf)
end
end
def self.authorized?(object, context)
authorized_demarche?(object.procedure, context)
end

View file

@ -7,7 +7,11 @@ module Types
field :content_type, String, null: false
def url
object.service_url
if object.is_a?(Hash)
object[:url]
else
object.service_url
end
end
end
end

View file

@ -286,7 +286,7 @@ class Dossier < ApplicationRecord
end
scope :for_procedure, -> (procedure) { includes(:user, :groupe_instructeur).where(groupe_instructeurs: { procedure: procedure }) }
scope :for_api_v2, -> { includes(procedure: [:administrateurs], etablissement: [], individual: [], traitements: []) }
scope :for_api_v2, -> { includes(procedure: [:administrateurs, :attestation_template], etablissement: [], individual: [], traitements: []) }
scope :with_notifications, -> do
joins(:follows)

View file

@ -222,6 +222,8 @@ Rails.application.routes.draw do
namespace :v2 do
post :graphql, to: "graphql#execute"
get 'dossiers/pdf/:id', format: :pdf, to: "dossiers#pdf", as: :dossier_pdf
get 'dossiers/geojson/:id', to: "dossiers#geojson", as: :dossier_geojson
end
end

View file

@ -0,0 +1,38 @@
describe API::V2::DossiersController do
let(:dossier) { create(:dossier, :accepte, :with_attestation) }
let(:sgid) { dossier.to_sgid(expires_in: 1.hour, for: 'api_v2') }
describe 'fetch pdf' do
subject { get :pdf, params: { id: sgid } }
it 'should get' do
expect(subject.status).to eq(200)
expect(subject.body).not_to be_nil
end
context 'error' do
let(:sgid) { 'yolo' }
it 'should error' do
expect(subject.status).to eq(401)
end
end
end
describe 'fetch geojson' do
subject { get :geojson, params: { id: sgid } }
it 'should get' do
expect(subject.status).to eq(200)
expect(subject.body).not_to be_nil
end
context 'error' do
let(:sgid) { 'yolo' }
it 'should error' do
expect(subject.status).to eq(401)
end
end
end
end

View file

@ -436,6 +436,35 @@ describe API::V2::GraphqlController do
end
end
context "with links" do
let(:dossier) { create(:dossier, :accepte, :with_attestation, procedure: procedure) }
let(:query) do
"{
dossier(number: #{dossier.id}) {
id
number
pdf {
url
}
geojson {
url
}
attestation {
url
}
}
}"
end
it "urls should be returned" do
expect(gql_errors).to eq(nil)
expect(gql_data[:dossier][:pdf][:url]).not_to be_nil
expect(gql_data[:dossier][:geojson][:url]).not_to be_nil
expect(gql_data[:dossier][:attestation][:url]).not_to be_nil
end
end
context "when there are missing data" do
before do
dossier.etablissement.update!(entreprise_code_effectif_entreprise: nil, entreprise_capital_social: nil,