[GraphQL] modifier annotation

This commit is contained in:
Paul Chavard 2020-07-28 18:16:03 +02:00
parent e0f7f1f20c
commit 9ce07be1ee
11 changed files with 625 additions and 3 deletions

View file

@ -0,0 +1,66 @@
module Mutations
class DossierModifierAnnotation < Mutations::BaseMutation
argument :dossier_id, ID, "Dossier ID", required: true, loads: Types::DossierType
argument :instructeur_id, ID, "Instructeur qui demande la modification.", required: true, loads: Types::ProfileType
argument :annotation_id, ID, "Annotation ID", required: true
field :annotation, Types::ChampType, null: true
field :errors, [Types::ValidationErrorType], null: true
def resolve_with_type(type, dossier, annotation_id, instructeur, value)
annotation = find_annotation(dossier, type, annotation_id)
if block_given?
annotation.value = yield annotation.type_champ, value
else
annotation.value = value
end
if annotation.save
dossier.log_modifier_annotation!(annotation, instructeur)
{ annotation: annotation }
else
{ errors: annotation.errors.full_messages }
end
end
def authorized?(dossier:, instructeur:, **args)
dossier_authorized_for?(dossier, instructeur)
end
private
def find_annotation(dossier, type, annotation_id)
_, stable_id = GraphQL::Schema::UniqueWithinType.decode(annotation_id)
dossier.champs_private
.joins(:type_de_champ)
.find_by!(types_de_champ: {
type_champ: annotation_type_champ(type),
stable_id: stable_id
})
end
def annotation_type_champ(type)
case type
when :text
[
TypeDeChamp.type_champs.fetch(:text),
TypeDeChamp.type_champs.fetch(:textarea)
]
when :checkbox
[
TypeDeChamp.type_champs.fetch(:checkbox),
TypeDeChamp.type_champs.fetch(:yes_no),
TypeDeChamp.type_champs.fetch(:engagement)
]
when :date
TypeDeChamp.type_champs.fetch(:date)
when :datetime
TypeDeChamp.type_champs.fetch(:datetime)
when :integer_number
TypeDeChamp.type_champs.fetch(:integer_number)
end
end
end
end

View file

@ -0,0 +1,23 @@
module Mutations
class DossierModifierAnnotationCheckbox < Mutations::DossierModifierAnnotation
description "Modifier lannotation au format oui/non."
argument :value, Boolean, required: true
def resolve(dossier:, annotation_id:, instructeur:, value:)
resolve_with_type(
:checkbox,
dossier,
annotation_id,
instructeur,
value
) do |type_champ, value|
if type_champ == TypeDeChamp.type_champs.fetch(:yes_no)
value ? 'true' : 'false'
else
value ? 'on' : 'off'
end
end
end
end
end

View file

@ -0,0 +1,17 @@
module Mutations
class DossierModifierAnnotationDate < Mutations::DossierModifierAnnotation
description "Modifier lannotation au format date."
argument :value, GraphQL::Types::ISO8601Date, required: true
def resolve(dossier:, annotation_id:, instructeur:, value:)
resolve_with_type(
:date,
dossier,
annotation_id,
instructeur,
value
)
end
end
end

View file

@ -0,0 +1,17 @@
module Mutations
class DossierModifierAnnotationDatetime < Mutations::DossierModifierAnnotation
description "Modifier lannotation au format date et heure."
argument :value, GraphQL::Types::ISO8601DateTime, required: true
def resolve(dossier:, annotation_id:, instructeur:, value:)
resolve_with_type(
:datetime,
dossier,
annotation_id,
instructeur,
value
)
end
end
end

View file

@ -0,0 +1,17 @@
module Mutations
class DossierModifierAnnotationIntegerNumber < Mutations::DossierModifierAnnotation
description "Modifier lannotation au format nombre entier."
argument :value, Int, required: true
def resolve(dossier:, annotation_id:, instructeur:, value:)
resolve_with_type(
:integer_number,
dossier,
annotation_id,
instructeur,
value
)
end
end
end

View file

@ -0,0 +1,17 @@
module Mutations
class DossierModifierAnnotationText < Mutations::DossierModifierAnnotation
description "Modifier lannotation au format text."
argument :value, String, required: true
def resolve(dossier:, annotation_id:, instructeur:, value:)
resolve_with_type(
:text,
dossier,
annotation_id,
instructeur,
value
)
end
end
end