fix(graphql): do not load brouillon or deleted linked dossier
This commit is contained in:
parent
96147330ba
commit
ac75afb77c
3 changed files with 64 additions and 1 deletions
18
app/graphql/loaders/dossier.rb
Normal file
18
app/graphql/loaders/dossier.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
module Loaders
|
||||
class Dossier < GraphQL::Batch::Loader
|
||||
def load(key)
|
||||
super(key.to_i)
|
||||
end
|
||||
|
||||
def perform(keys)
|
||||
query(keys).each { |record| fulfill(record.id, record) }
|
||||
keys.each { |key| fulfill(key, nil) unless fulfilled?(key) }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def query(keys)
|
||||
::Dossier.visible_by_administration.where(id: keys)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -6,7 +6,7 @@ module Types::Champs
|
|||
|
||||
def dossier
|
||||
if object.value.present?
|
||||
Loaders::Record.for(Dossier).load(object.value)
|
||||
Loaders::Dossier.for.load(object.value)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -108,6 +108,35 @@ RSpec.describe Types::DossierType, type: :graphql do
|
|||
}
|
||||
end
|
||||
|
||||
describe 'dossier with linked dossier' do
|
||||
let(:procedure) { create(:procedure, :published, types_de_champ_public: [{ type: :dossier_link }]) }
|
||||
let(:dossier) { create(:dossier, :en_construction, :with_populated_champs, procedure: procedure) }
|
||||
let(:linked_dossier) { create(:dossier, :en_construction) }
|
||||
let(:query) { DOSSIER_WITH_LINKED_DOSIER_QUERY }
|
||||
let(:variables) { { number: dossier.id } }
|
||||
|
||||
before do
|
||||
dossier.champs.first.update(value: linked_dossier.id)
|
||||
end
|
||||
|
||||
context 'en_construction' do
|
||||
it {
|
||||
expect(data[:dossier][:champs].first).not_to be_nil
|
||||
expect(data[:dossier][:champs].first[:dossier][:id]).to eq(linked_dossier.to_typed_id)
|
||||
expect(data[:dossier][:champs].first[:dossier][:state]).to eq('en_construction')
|
||||
}
|
||||
end
|
||||
|
||||
context 'brouillon' do
|
||||
let(:linked_dossier) { create(:dossier, :brouillon) }
|
||||
|
||||
it {
|
||||
expect(data[:dossier][:champs].first).not_to be_nil
|
||||
expect(data[:dossier][:champs].first[:dossier]).to be_nil
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
DOSSIER_QUERY = <<-GRAPHQL
|
||||
query($number: Int!) {
|
||||
dossier(number: $number) {
|
||||
|
@ -179,4 +208,20 @@ RSpec.describe Types::DossierType, type: :graphql do
|
|||
streetNumber
|
||||
}
|
||||
GRAPHQL
|
||||
|
||||
DOSSIER_WITH_LINKED_DOSIER_QUERY = <<-GRAPHQL
|
||||
query($number: Int!) {
|
||||
dossier(number: $number) {
|
||||
champs {
|
||||
id
|
||||
... on DossierLinkChamp {
|
||||
dossier {
|
||||
id
|
||||
state
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
GRAPHQL
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue