2020-07-28 18:16:03 +02:00
|
|
|
|
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
|
|
|
|
|
|
2022-07-21 14:54:14 +02:00
|
|
|
|
def resolve_with_type(dossier:, annotation_id:, instructeur:, value:)
|
|
|
|
|
annotation = find_annotation(dossier, annotation_id)
|
|
|
|
|
|
|
|
|
|
if annotation.nil?
|
|
|
|
|
return { errors: ["L’annotation \"#{annotation_id}\" n’existe pas"] }
|
|
|
|
|
end
|
2020-07-28 18:16:03 +02:00
|
|
|
|
|
|
|
|
|
if block_given?
|
|
|
|
|
annotation.value = yield annotation.type_champ, value
|
|
|
|
|
else
|
|
|
|
|
annotation.value = value
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if annotation.save
|
2022-12-07 13:21:55 +01:00
|
|
|
|
{ annotation: }
|
2020-07-28 18:16:03 +02:00
|
|
|
|
else
|
|
|
|
|
{ errors: annotation.errors.full_messages }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def authorized?(dossier:, instructeur:, **args)
|
|
|
|
|
dossier_authorized_for?(dossier, instructeur)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2022-07-21 14:54:14 +02:00
|
|
|
|
def input_type
|
|
|
|
|
:text
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def find_annotation(dossier, annotation_id)
|
2022-12-16 15:07:26 +01:00
|
|
|
|
stable_id, row_id = Champ.decode_typed_id(annotation_id)
|
2024-03-27 11:35:42 +01:00
|
|
|
|
type_de_champ = dossier.revision.types_de_champ
|
|
|
|
|
.private_only
|
|
|
|
|
.find_by(type_champ: annotation_type_champ, stable_id:)
|
2022-07-21 14:54:14 +02:00
|
|
|
|
|
2024-03-27 11:35:42 +01:00
|
|
|
|
return nil if type_de_champ.nil?
|
|
|
|
|
dossier.champ_for_update(type_de_champ, row_id)
|
2020-07-28 18:16:03 +02:00
|
|
|
|
end
|
|
|
|
|
|
2022-07-21 14:54:14 +02:00
|
|
|
|
def annotation_type_champ
|
|
|
|
|
case input_type
|
2020-07-28 18:16:03 +02:00
|
|
|
|
when :text
|
|
|
|
|
[
|
|
|
|
|
TypeDeChamp.type_champs.fetch(:text),
|
|
|
|
|
TypeDeChamp.type_champs.fetch(:textarea)
|
|
|
|
|
]
|
|
|
|
|
when :checkbox
|
|
|
|
|
[
|
|
|
|
|
TypeDeChamp.type_champs.fetch(:checkbox),
|
2022-07-25 10:24:17 +02:00
|
|
|
|
TypeDeChamp.type_champs.fetch(:yes_no)
|
2020-07-28 18:16:03 +02:00
|
|
|
|
]
|
|
|
|
|
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
|