-
Notifications
You must be signed in to change notification settings - Fork 91
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
base: main
Are you sure you want to change the base?
API: gestion des labels #11201
Changes from all commits
4c9223b
1d939c2
fea736e
31f495a
e1c7672
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
|
||
if dossier_label.persisted? | ||
{ dossier: } | ||
else | ||
{ errors: dossier_label.errors.full_messages } | ||
end | ||
end | ||
|
||
def authorized?(dossier:, label:, **_args) | ||
if dossier.labels.include?(label) | ||
[false, { errors: ["Ce label est déjà associé au dossier"] }] | ||
elsif dossier.procedure != label.procedure | ||
[false, { errors: ["Ce label n’appartient pas à la même démarche que le dossier"] }] | ||
else | ||
true | ||
end | ||
end | ||
end | ||
end |
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) | ||
|
||
if dossier_label.destroy | ||
{ dossier: } | ||
else | ||
{ errors: ['Impossible de supprimer le label'] } | ||
end | ||
end | ||
|
||
def authorized?(dossier:, label:, **_args) | ||
if dossier.labels.exclude?(label) | ||
[false, { errors: ["Ce label n‘est pas associé au dossier"] }] | ||
else | ||
true | ||
end | ||
end | ||
end | ||
end |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. J'ajouterai bien un authorized? :
Ça ne coûte rien et ça garantit que l'objet ne sera jamais visible s'il n'est pas autorisé There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pourquoi expose-t-on ici There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
def state | ||||||
object.state | ||||||
|
@@ -210,6 +211,10 @@ def attestation | |||||
end | ||||||
end | ||||||
|
||||||
def labels | ||||||
Loaders::Association.for(object.class, dossier_labels: [:label]).load(object) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
end | ||||||
|
||||||
def self.authorized?(object, context) | ||||||
context.authorized_demarche?(object.revision.procedure) | ||||||
end | ||||||
|
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) | ||
end | ||
end |
There was a problem hiding this comment.
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 utileThere was a problem hiding this comment.
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