Add revision dossiers filters

This commit is contained in:
Paul Chavard 2021-01-21 19:04:35 +01:00
parent d014201521
commit 866ae256a3
3 changed files with 98 additions and 3 deletions

View file

@ -395,7 +395,7 @@ type Demarche {
after: String
"""
Si présent, permet de filtrer les dossiers archivés ou non
Seulement les dossiers archivés.
"""
archived: Boolean
@ -419,11 +419,26 @@ type Demarche {
"""
last: Int
"""
Seulement les dossiers pour les révisons avant la révision donnée.
"""
maxRevision: ID
"""
Seulement les dossiers pour les révisons après la révision donnée.
"""
minRevision: ID
"""
Lordre des dossiers.
"""
order: Order = ASC
"""
Seulement les dossiers pour la révision donnée.
"""
revision: ID
"""
Dossiers avec statut.
"""

View file

@ -37,7 +37,10 @@ module Types
argument :created_since, GraphQL::Types::ISO8601DateTime, required: false, description: "Dossiers déposés depuis la date."
argument :updated_since, GraphQL::Types::ISO8601DateTime, required: false, description: "Dossiers mis à jour depuis la date."
argument :state, Types::DossierType::DossierState, required: false, description: "Dossiers avec statut."
argument :archived, Boolean, required: false, description: "Si présent, permet de filtrer les dossiers archivés ou non"
argument :archived, Boolean, required: false, description: "Seulement les dossiers archivés."
argument :revision, ID, required: false, description: "Seulement les dossiers pour la révision donnée."
argument :max_revision, ID, required: false, description: "Seulement les dossiers pour les révisons avant la révision donnée."
argument :min_revision, ID, required: false, description: "Seulement les dossiers pour les révisons après la révision donnée."
end
field :champ_descriptors, [Types::ChampDescriptorType], null: false, method: :types_de_champ
@ -63,7 +66,7 @@ module Types
Loaders::Association.for(object.class, :revisions).load(object)
end
def dossiers(updated_since: nil, created_since: nil, state: nil, archived: nil, order:)
def dossiers(updated_since: nil, created_since: nil, state: nil, archived: nil, revision: nil, max_revision: nil, min_revision: nil, order:)
dossiers = object.dossiers.state_not_brouillon.for_api_v2
if state.present?
@ -74,6 +77,18 @@ module Types
dossiers = dossiers.where(archived: archived)
end
if !revision.nil?
dossiers = dossiers.where(revision: find_revision(revision))
else
if !min_revision.nil?
dossiers = dossiers.joins(:revision).where('procedure_revisions.created_at >= ?', find_revision(min_revision).created_at)
end
if !max_revision.nil?
dossiers = dossiers.joins(:revision).where('procedure_revisions.created_at <= ?', find_revision(max_revision).created_at)
end
end
if updated_since.present?
dossiers = dossiers.updated_since(updated_since).order_by_updated_at(order)
else
@ -90,5 +105,12 @@ module Types
def self.authorized?(object, context)
context.authorized_demarche?(object)
end
private
def find_revision(revision)
revision_id = GraphQL::Schema::UniqueWithinType.decode(revision).second
object.revisions.find(revision_id)
end
end
end

View file

@ -234,6 +234,64 @@ describe API::V2::GraphqlController do
end
end
end
context "filter by minRevision" do
let(:query) do
"{
demarche(number: #{procedure.id}) {
id
number
dossiers(minRevision: \"#{procedure.revisions.first.to_typed_id}\") {
nodes {
id
}
}
}
}"
end
it "should be returned" do
expect(gql_errors).to eq(nil)
expect(gql_data).to eq(demarche: {
id: procedure.to_typed_id,
number: procedure.id,
dossiers: {
nodes: procedure.dossiers.order(:created_at).map do |dossier|
{ id: dossier.to_typed_id }
end
}
})
end
end
context "filter by maxRevision" do
let(:query) do
"{
demarche(number: #{procedure.id}) {
id
number
dossiers(maxRevision: \"#{procedure.revisions.last.to_typed_id}\") {
nodes {
id
}
}
}
}"
end
it "should be returned" do
expect(gql_errors).to eq(nil)
expect(gql_data).to eq(demarche: {
id: procedure.to_typed_id,
number: procedure.id,
dossiers: {
nodes: procedure.dossiers.order(:created_at).map do |dossier|
{ id: dossier.to_typed_id }
end
}
})
end
end
end
context "dossier" do