Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: gestion des labels #11201

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions app/graphql/mutations/dossier_ajouter_label.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module Mutations
class DossierAjouterLabel < Mutations::BaseMutation
description "Ajouter un label à un dossier"

argument :dossier_id, ID, required: true, loads: Types::DossierType
argument :label_id, ID, "ID du label", required: true, loads: Types::LabelType

field :dossier, Types::DossierType, null: true
field :label, Types::LabelType, null: true
field :errors, [Types::ValidationErrorType], null: true

def resolve(dossier:, label:)
dossier_label = dossier.dossier_labels.create(label: label)

Check warning on line 15 in app/graphql/mutations/dossier_ajouter_label.rb

View check run for this annotation

Codecov / codecov/patch

app/graphql/mutations/dossier_ajouter_label.rb#L15

Added line #L15 was not covered by tests

if dossier_label.persisted?

Check warning on line 17 in app/graphql/mutations/dossier_ajouter_label.rb

View check run for this annotation

Codecov / codecov/patch

app/graphql/mutations/dossier_ajouter_label.rb#L17

Added line #L17 was not covered by tests
{ dossier: }
else
{ errors: dossier_label.errors.full_messages }
end
end

def authorized?(dossier:, label:, **_args)
if dossier.labels.include?(label)

Check warning on line 25 in app/graphql/mutations/dossier_ajouter_label.rb

View check run for this annotation

Codecov / codecov/patch

app/graphql/mutations/dossier_ajouter_label.rb#L25

Added line #L25 was not covered by tests
[false, { errors: ["Ce label est déjà associé au dossier"] }]
elsif dossier.procedure != label.procedure
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avec authorized? sur le label, ça ne devrait pas être utile

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

j'ai rajouté le authorized sur les types, mais c'est insuffisant car si l'admin a plusieurs démarches on veut éviter qu'il associe le label d'une autre démarche

[false, { errors: ["Ce label n’appartient pas à la même démarche que le dossier"] }]
else
true
end
end
end
end
4 changes: 2 additions & 2 deletions app/graphql/mutations/dossier_changer_groupe_instructeur.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Mutations
class DossierChangerGroupeInstructeur < Mutations::BaseMutation
include DossierHelper

description "Changer le grope instructeur du dossier."
description "Changer le groupe instructeur du dossier."

argument :dossier_id, ID, "Dossier ID", required: true, loads: Types::DossierType
argument :groupe_instructeur_id, ID, "Group instructeur a affecter", required: true, loads: Types::GroupeInstructeurType
Expand All @@ -20,7 +20,7 @@ def resolve(dossier:, groupe_instructeur:)

def authorized?(dossier:, groupe_instructeur:)
if dossier.groupe_instructeur == groupe_instructeur
return false, { errors: ["Le dossier est déjà avec le grope instructeur: '#{groupe_instructeur.label}'"] }
return false, { errors: ["Le dossier est déjà avec le groupe instructeur: '#{groupe_instructeur.label}'"] }
elsif dossier.procedure != groupe_instructeur.procedure
return false, { errors: ["Le groupe instructeur '#{groupe_instructeur.label}' n’appartient pas à la même démarche que le dossier"] }
else
Expand Down
32 changes: 32 additions & 0 deletions app/graphql/mutations/dossier_supprimer_label.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module Mutations
class DossierSupprimerLabel < Mutations::BaseMutation
description "Supprimer un label d'un dossier"

argument :dossier_id, ID, required: true, loads: Types::DossierType
argument :label_id, ID, required: true, loads: Types::LabelType

field :dossier, Types::DossierType, null: true
field :label, Types::LabelType, null: true
field :errors, [Types::ValidationErrorType], null: true

def resolve(dossier:, label:)
dossier_label = dossier.dossier_labels.find_by(label: label)

Check warning on line 15 in app/graphql/mutations/dossier_supprimer_label.rb

View check run for this annotation

Codecov / codecov/patch

app/graphql/mutations/dossier_supprimer_label.rb#L15

Added line #L15 was not covered by tests

if dossier_label.destroy

Check warning on line 17 in app/graphql/mutations/dossier_supprimer_label.rb

View check run for this annotation

Codecov / codecov/patch

app/graphql/mutations/dossier_supprimer_label.rb#L17

Added line #L17 was not covered by tests
{ dossier: }
else
{ errors: ['Impossible de supprimer le label'] }
end
end

def authorized?(dossier:, label:, **_args)
if dossier.labels.exclude?(label)

Check warning on line 25 in app/graphql/mutations/dossier_supprimer_label.rb

View check run for this annotation

Codecov / codecov/patch

app/graphql/mutations/dossier_supprimer_label.rb#L25

Added line #L25 was not covered by tests
[false, { errors: ["Ce label n‘est pas associé au dossier"] }]
else
true
end
end
end
end
125 changes: 124 additions & 1 deletion app/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,11 @@ type Demarche {
groupeInstructeurs(closed: Boolean): [GroupeInstructeur!]!
id: ID!

"""
Liste des labels associables aux dossiers
"""
labels: [Label!]!

"""
Numero de la démarche.
"""
Expand Down Expand Up @@ -1478,6 +1483,11 @@ type Dossier {
groupeInstructeur: GroupeInstructeur!
id: ID!
instructeurs: [Profile!]!

"""
Labels associés au dossier
"""
labels: [DossierLabel!]!
messages(id: ID): [Message!]!
motivation: String
motivationAttachment: File
Expand Down Expand Up @@ -1547,6 +1557,35 @@ type DossierAccepterPayload {
errors: [ValidationError!]
}

"""
Autogenerated input type of DossierAjouterLabel
"""
input DossierAjouterLabelInput {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
dossierId: ID!

"""
ID du label
"""
labelId: ID!
}

"""
Autogenerated return type of DossierAjouterLabel.
"""
type DossierAjouterLabelPayload {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
dossier: Dossier
errors: [ValidationError!]
label: Label
}

"""
Autogenerated input type of DossierArchiver
"""
Expand Down Expand Up @@ -1761,6 +1800,20 @@ type DossierEnvoyerMessagePayload {
message: Message
}

type DossierLabel {
"""
Couleur du label
"""
color: LabelColorEnum!

"""
Date de dernière modification de ce label sur le dossier
"""
dateModification: ISO8601DateTime!
id: ID!
name: String!
}

type DossierLinkChamp implements Champ {
"""
L'identifiant du champDescriptor de ce champ
Expand Down Expand Up @@ -2263,6 +2316,31 @@ enum DossierState {
sans_suite
}

"""
Autogenerated input type of DossierSupprimerLabel
"""
input DossierSupprimerLabelInput {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
dossierId: ID!
labelId: ID!
}

"""
Autogenerated return type of DossierSupprimerLabel.
"""
type DossierSupprimerLabelPayload {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
dossier: Dossier
errors: [ValidationError!]
label: Label
}

"""
Autogenerated input type of DossierSupprimerMessage
"""
Expand Down Expand Up @@ -3086,6 +3164,31 @@ type IntegerNumberChampDescriptor implements ChampDescriptor {
type: TypeDeChamp! @deprecated(reason: "Utilisez le champ `__typename` à la place.")
}

type Label {
"""
Couleur du label
"""
color: LabelColorEnum!
id: ID!
name: String!
}

"""
Couleurs disponibles pour les labels
"""
enum LabelColorEnum {
beige_gris_galet
blue_ecume
brown_cafe_creme
green_bourgeon
green_emeraude
green_menthe
green_tilleul_verveine
pink_macaron
purple_glycine
yellow_tournesol
}

type LinkedDropDownListChamp implements Champ {
"""
L'identifiant du champDescriptor de ce champ
Expand Down Expand Up @@ -3273,6 +3376,16 @@ type Mutation {
input: DossierAccepterInput!
): DossierAccepterPayload

"""
Ajouter un label à un dossier
"""
dossierAjouterLabel(
"""
Parameters for DossierAjouterLabel
"""
input: DossierAjouterLabelInput!
): DossierAjouterLabelPayload

"""
Archiver le dossier.
"""
Expand All @@ -3284,7 +3397,7 @@ type Mutation {
): DossierArchiverPayload

"""
Changer le grope instructeur du dossier.
Changer le groupe instructeur du dossier.
"""
dossierChangerGroupeInstructeur(
"""
Expand Down Expand Up @@ -3429,6 +3542,16 @@ type Mutation {
input: DossierRepasserEnInstructionInput!
): DossierRepasserEnInstructionPayload

"""
Supprimer un label d'un dossier
"""
dossierSupprimerLabel(
"""
Parameters for DossierSupprimerLabel
"""
input: DossierSupprimerLabelInput!
): DossierSupprimerLabelPayload

"""
Supprimer un message.
"""
Expand Down
1 change: 1 addition & 0 deletions app/graphql/types/demarche_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class DossierDeclarativeState < Types::BaseEnum
field :published_revision, Types::RevisionType, null: true
field :revisions, [Types::RevisionType], null: false
field :chorus_configuration, Types::ChorusConfigurationType, null: true, description: "Cadre budgétaire Chorus"
field :labels, [Types::LabelType], null: false, description: 'Liste des labels associables aux dossiers'

def state
object.aasm.current_state
Expand Down
22 changes: 22 additions & 0 deletions app/graphql/types/dossier_label_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

class Types::DossierLabelType < Types::LabelType
field :date_modification, GraphQL::Types::ISO8601DateTime,
"Date de dernière modification de ce label sur le dossier", null: false, method: :updated_at

def id
object.label.to_typed_id
end

def name
object.label.name
end

def color
object.label.color
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'ajouterai bien un authorized? :

def self.authorized?(object, context)
    context.authorized_demarche?(object.procedure)
end

Ça ne coûte rien et ça garantit que l'objet ne sera jamais visible s'il n'est pas autorisé

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pourquoi expose-t-on ici DossierLabel et pas Label ? Je n'ai pas l'impression que DossierLabel contient des informations utiles. Je sais que tu m'as posé la question et je crois que j'ai mal compris. On peut en parler à l'occasion. Ce n'est pas bloquant pour merger non plus – on peut toujours changer plus tard. Le nom du type GraphQL est bon, je pense – DossierLabel.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah mais non, tu as exposé les deux sur l'API... Je voudrais qu'on en parle


def self.authorized?(object, context)
context.authorized_demarche?(object.label.procedure)
end
end
5 changes: 5 additions & 0 deletions app/graphql/types/dossier_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class ConnectionUsager < Types::BaseEnum
argument :id, ID, required: false
end
field :traitements, [Types::TraitementType], null: false
field :labels, [Types::DossierLabelType], "Labels associés au dossier", null: false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
field :labels, [Types::DossierLabelType], "Labels associés au dossier", null: false
field :labels, [Types::LabelType], "Labels associés au dossier", null: false


def state
object.state
Expand Down Expand Up @@ -210,6 +211,10 @@ def attestation
end
end

def labels
Loaders::Association.for(object.class, dossier_labels: [:label]).load(object)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Loaders::Association.for(object.class, dossier_labels: [:label]).load(object)
Loaders::Association.for(object.class, dossier_labels: [:label]).load(object).then { _1.map(:label) }

end

def self.authorized?(object, context)
context.authorized_demarche?(object.revision.procedure)
end
Expand Down
20 changes: 20 additions & 0 deletions app/graphql/types/label_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

class Types::LabelType < Types::BaseObject
global_id_field :id

class Types::LabelColorEnum < Types::BaseEnum
Label.colors.each_key do |color|
value color, value: color
end

description "Couleurs disponibles pour les labels"
end

field :name, String, null: false
field :color, Types::LabelColorEnum, null: false, description: "Couleur du label"

def self.authorized?(object, context)
context.authorized_demarche?(object.procedure)

Check warning on line 18 in app/graphql/types/label_type.rb

View check run for this annotation

Codecov / codecov/patch

app/graphql/types/label_type.rb#L18

Added line #L18 was not covered by tests
end
end
3 changes: 3 additions & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class MutationType < Types::BaseObject
field :dossier_modifier_annotation_ajouter_ligne, mutation: Mutations::DossierModifierAnnotationAjouterLigne
field :dossier_modifier_annotation_drop_down_list, mutation: Mutations::DossierModifierAnnotationDropDownList

field :dossier_ajouter_label, mutation: Mutations::DossierAjouterLabel
field :dossier_supprimer_label, mutation: Mutations::DossierSupprimerLabel

field :groupe_instructeur_modifier, mutation: Mutations::GroupeInstructeurModifier
field :groupe_instructeur_creer, mutation: Mutations::GroupeInstructeurCreer
field :groupe_instructeur_ajouter_instructeurs, mutation: Mutations::GroupeInstructeurAjouterInstructeurs
Expand Down
Loading
Loading