2019-11-12 15:51:51 +01:00
|
|
|
module Mutations
|
|
|
|
class DossierEnvoyerMessage < Mutations::BaseMutation
|
|
|
|
description "Envoyer un message à l'usager du dossier."
|
|
|
|
|
|
|
|
argument :dossier_id, ID, required: true, loads: Types::DossierType
|
|
|
|
argument :instructeur_id, ID, required: true, loads: Types::ProfileType
|
|
|
|
argument :body, String, required: true
|
|
|
|
argument :attachment, ID, required: false
|
|
|
|
|
|
|
|
field :message, Types::MessageType, null: true
|
|
|
|
field :errors, [Types::ValidationErrorType], null: true
|
|
|
|
|
|
|
|
def resolve(dossier:, instructeur:, body:, attachment: nil)
|
|
|
|
message = CommentaireService.build(instructeur, dossier, body: body, piece_jointe: attachment)
|
|
|
|
|
|
|
|
if message.save
|
|
|
|
{ message: message }
|
|
|
|
else
|
|
|
|
{ errors: message.errors.full_messages }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-11 13:14:18 +01:00
|
|
|
def authorized?(dossier:, instructeur:, body:, attachment: nil)
|
2019-11-12 15:51:51 +01:00
|
|
|
instructeur.is_a?(Instructeur) && instructeur.dossiers.exists?(id: dossier.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|