From 807d43a9a7b5e3b30ca720e7b19c4b1f6783314c Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 1 Aug 2024 09:49:47 +0200 Subject: [PATCH] feat(graphql): add desarchiver mutation --- app/graphql/api/v2/stored_query.rb | 12 ++++++ app/graphql/mutations/dossier_desarchiver.rb | 25 +++++++++++ app/graphql/schema.graphql | 42 +++++++++++++++++++ app/graphql/types/mutation_type.rb | 1 + .../graphql_controller_stored_queries_spec.rb | 29 +++++++++++++ 5 files changed, 109 insertions(+) create mode 100644 app/graphql/mutations/dossier_desarchiver.rb diff --git a/app/graphql/api/v2/stored_query.rb b/app/graphql/api/v2/stored_query.rb index b246cc22b..b893d6802 100644 --- a/app/graphql/api/v2/stored_query.rb +++ b/app/graphql/api/v2/stored_query.rb @@ -728,6 +728,18 @@ class API::V2::StoredQuery } } + mutation dossierDesarchiver($input: DossierDesarchiverInput!) { + dossierDesarchiver(input: $input) { + dossier { + id + archived + } + errors { + message + } + } + } + mutation dossierPasserEnInstruction($input: DossierPasserEnInstructionInput!) { dossierPasserEnInstruction(input: $input) { dossier { diff --git a/app/graphql/mutations/dossier_desarchiver.rb b/app/graphql/mutations/dossier_desarchiver.rb new file mode 100644 index 000000000..397205358 --- /dev/null +++ b/app/graphql/mutations/dossier_desarchiver.rb @@ -0,0 +1,25 @@ +module Mutations + class DossierDesarchiver < Mutations::BaseMutation + description "Désarchiver le dossier." + + argument :dossier_id, ID, "Dossier ID", required: true, loads: Types::DossierType + argument :instructeur_id, ID, "Instructeur qui prend la décision sur le dossier.", required: true, loads: Types::ProfileType + + field :dossier, Types::DossierType, null: true + field :errors, [Types::ValidationErrorType], null: true + + def resolve(dossier:, instructeur:) + dossier.desarchiver! + + { dossier: } + end + + def authorized?(dossier:, instructeur:) + if !dossier.archived? + return false, { errors: ["Un dossier non archivé ne peut pas être désarchivé"] } + end + + dossier_authorized_for?(dossier, instructeur) + end + end +end diff --git a/app/graphql/schema.graphql b/app/graphql/schema.graphql index 1ae80f784..240bdb86e 100644 --- a/app/graphql/schema.graphql +++ b/app/graphql/schema.graphql @@ -1682,6 +1682,38 @@ enum DossierDeclarativeState { en_instruction } +""" +Autogenerated input type of DossierDesarchiver +""" +input DossierDesarchiverInput { + """ + A unique identifier for the client performing the mutation. + """ + clientMutationId: String + + """ + Dossier ID + """ + dossierId: ID! + + """ + Instructeur qui prend la décision sur le dossier. + """ + instructeurId: ID! +} + +""" +Autogenerated return type of DossierDesarchiver. +""" +type DossierDesarchiverPayload { + """ + A unique identifier for the client performing the mutation. + """ + clientMutationId: String + dossier: Dossier + errors: [ValidationError!] +} + """ An edge in a connection. """ @@ -3271,6 +3303,16 @@ type Mutation { input: DossierClasserSansSuiteInput! ): DossierClasserSansSuitePayload + """ + Désarchiver le dossier. + """ + dossierDesarchiver( + """ + Parameters for DossierDesarchiver + """ + input: DossierDesarchiverInput! + ): DossierDesarchiverPayload + """ Envoyer un message à l'usager du dossier. """ diff --git a/app/graphql/types/mutation_type.rb b/app/graphql/types/mutation_type.rb index 286cbeac3..e3dfed910 100644 --- a/app/graphql/types/mutation_type.rb +++ b/app/graphql/types/mutation_type.rb @@ -11,6 +11,7 @@ module Types field :dossier_repasser_en_instruction, mutation: Mutations::DossierRepasserEnInstruction field :dossier_repasser_en_construction, mutation: Mutations::DossierRepasserEnConstruction field :dossier_archiver, mutation: Mutations::DossierArchiver + field :dossier_desarchiver, mutation: Mutations::DossierDesarchiver field :dossier_changer_groupe_instructeur, mutation: Mutations::DossierChangerGroupeInstructeur field :dossier_modifier_annotation_text, mutation: Mutations::DossierModifierAnnotationText diff --git a/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb b/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb index dfd5ed005..6ebd5fd37 100644 --- a/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb +++ b/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb @@ -889,6 +889,35 @@ describe API::V2::GraphqlController do end end + context 'dossierDesarchiver' do + let(:dossier) { create(:dossier, :refuse, :with_individual, :archived, procedure:) } + let(:variables) { { input: { dossierId: dossier.to_typed_id, instructeurId: instructeur.to_typed_id } } } + let(:operation_name) { 'dossierDesarchiver' } + + it { + expect(gql_errors).to be_nil + expect(gql_data[:dossierDesarchiver][:errors]).to be_nil + expect(gql_data[:dossierDesarchiver][:dossier][:id]).to eq(dossier.to_typed_id) + expect(gql_data[:dossierDesarchiver][:dossier][:archived]).to be_falsey + } + + context 'read only token' do + before { api_token.update(write_access: false) } + + it { + expect(gql_data[:dossierDesarchiver][:errors].first[:message]).to eq('Le jeton utilisé est configuré seulement en lecture') + } + end + + context 'when not processed' do + let(:dossier) { create(:dossier, :refuse, :with_individual, procedure:) } + + it { + expect(gql_data[:dossierDesarchiver][:errors].first[:message]).to eq('Un dossier non archivé ne peut pas être désarchivé') + } + end + end + context 'dossierPasserEnInstruction' do let(:dossier) { create(:dossier, :en_construction, :with_individual, procedure: procedure) } let(:variables) { { input: { dossierId: dossier.to_typed_id, instructeurId: instructeur.to_typed_id, disableNotification: } } }