Skip to content

Commit

Permalink
feat(graphql): returns dossier & message correction status
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Chavard <github@paul.chavard.net>
  • Loading branch information
colinux and tchak committed Jul 13, 2023
1 parent 1f08846 commit 1c9b34f
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 4 deletions.
23 changes: 23 additions & 0 deletions app/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,23 @@ GeoJSON coordinates
"""
scalar Coordinates

type Correction {
dateResolution: ISO8601DateTime
reason: CorrectionReason!
}

enum CorrectionReason {
"""
Le dossier est incomplet et nécessite d’être complété
"""
incomplete

"""
Le dossier n’est pas valide et nécessite une correction
"""
incorrect
}

"""
Autogenerated input type of CreateDirectUpload
"""
Expand Down Expand Up @@ -1301,6 +1318,11 @@ type Dossier {
"""
dateDepot: ISO8601DateTime!

"""
Date de la dernière demande de correction qui n’a pas encore été traitée par l’usager.
"""
dateDerniereCorrectionEnAttente: ISO8601DateTime

"""
Date de la dernière modification.
"""
Expand Down Expand Up @@ -2826,6 +2848,7 @@ type Message {
attachment: File @deprecated(reason: "Utilisez le champ `attachments` à la place.")
attachments: [File!]!
body: String!
correction: Correction
createdAt: ISO8601DateTime!
email: String!
id: ID!
Expand Down
20 changes: 20 additions & 0 deletions app/graphql/types/correction_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Types
class CorrectionType < Types::BaseObject
class CorrectionReason < Types::BaseEnum
DossierCorrection.reasons.each do |symbol_name, string_name|
value(string_name, I18n.t(symbol_name, scope: [:reasons]), value: symbol_name)
end
end

field :reason, CorrectionReason, null: false
field :date_resolution, GraphQL::Types::ISO8601DateTime, null: true

def date_resolution
object.resolved_at
end

def message
object.commentaire
end
end
end
6 changes: 6 additions & 0 deletions app/graphql/types/dossier_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class ConnectionUsager < Types::BaseEnum
field :date_suppression_par_administration, GraphQL::Types::ISO8601DateTime, "Date de la suppression par l’administration.", null: true, method: :hidden_by_administration_at
field :date_expiration, GraphQL::Types::ISO8601DateTime, "Date d’expiration.", null: true

field :date_derniere_correction_en_attente, GraphQL::Types::ISO8601DateTime, "Date de la dernière demande de correction qui n’a pas encore été traitée par l’usager.", null: true

field :archived, Boolean, null: false

field :connection_usager, ConnectionUsager, null: false
Expand Down Expand Up @@ -75,6 +77,10 @@ def date_expiration
end
end

def date_derniere_correction_en_attente
Loaders::Association.for(object.class, :pending_correction).load(object).then { _1&.created_at }
end

def connection_usager
if object.user_deleted?
:deleted
Expand Down
5 changes: 5 additions & 0 deletions app/graphql/types/message_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ class MessageType < Types::BaseObject
field :attachments, [Types::File], null: false, extensions: [
{ Extensions::Attachment => { attachment: :piece_jointe, as: :multiple } }
]
field :correction, CorrectionType, null: true

def body
object.body.nil? ? "" : object.body
end

def correction
Loaders::Association.for(object.class, :dossier_correction).load(object)
end
end
end
5 changes: 1 addition & 4 deletions app/models/concerns/dossier_correctable_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module DossierCorrectableConcern
A_CORRIGER = 'a_corriger'
has_many :corrections, class_name: 'DossierCorrection', dependent: :destroy
has_many :pending_corrections, -> { DossierCorrection.pending }, class_name: 'DossierCorrection', inverse_of: :dossier
has_one :pending_correction, -> { DossierCorrection.pending }, class_name: 'DossierCorrection', inverse_of: :dossier

scope :with_pending_corrections, -> { joins(:corrections).where(corrections: { resolved_at: nil }) }

Expand Down Expand Up @@ -37,10 +38,6 @@ def pending_correction?
pending_corrections.exists?
end

def pending_correction
pending_corrections.first
end

def resolve_pending_correction!
pending_corrections.update!(resolved_at: Time.current)
pending_corrections.reset
Expand Down
4 changes: 4 additions & 0 deletions config/locales/models/dossier_correction/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
reasons:
incorrect: "The file is invalid and needs to be corrected"
incomplete: "The file is incomplete and needs to be completed"
4 changes: 4 additions & 0 deletions config/locales/models/dossier_correction/fr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fr:
reasons:
incorrect: "Le dossier n’est pas valide et nécessite une correction"
incomplete: "Le dossier est incomplet et nécessite d’être complété"
28 changes: 28 additions & 0 deletions spec/graphql/dossier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,19 @@

it {
expect(data[:dossier][:messages]).not_to be_nil
expect(data[:dossier][:messages][0][:correction]).to be_nil
}
end

describe 'dossier with pending correction' do
let(:dossier) { create(:dossier, :en_construction) }
let!(:correction) { create(:dossier_correction, dossier:) }
let(:query) { DOSSIER_WITH_CORRECTION_QUERY }
let(:variables) { { number: dossier.id } }

it {
expect(data[:dossier][:messages][0][:correction]).to eq({ reason: "incorrect", dateResolution: nil })
expect(data[:dossier][:dateDerniereCorrectionEnAttente]).not_to be_nil
}
end

Expand Down Expand Up @@ -412,6 +425,21 @@
}
GRAPHQL

DOSSIER_WITH_CORRECTION_QUERY = <<-GRAPHQL
query($number: Int!) {
dossier(number: $number) {
dateDerniereCorrectionEnAttente
messages {
body
correction {
reason
dateResolution
}
}
}
}
GRAPHQL

DOSSIER_WITH_SELECTED_CHAMP_QUERY = <<-GRAPHQL
query($number: Int!, $id: ID!) {
dossier(number: $number) {
Expand Down

0 comments on commit 1c9b34f

Please sign in to comment.