From 8e6c5d1567b4370c4f83b939b0c36a4e1eea79b7 Mon Sep 17 00:00:00 2001 From: mfo Date: Wed, 24 Jul 2024 14:43:50 +0200 Subject: [PATCH 1/2] api(mutation.dossier_modifier_annotation_drop_down_list): add mutation to change drop_down_list annotations value --- app/graphql/api/v2/stored_query.rb | 14 ++++++ .../mutations/dossier_modifier_annotation.rb | 2 + ...sier_modifier_annotation_drop_down_list.rb | 17 +++++++ app/graphql/schema.graphql | 49 ++++++++++++++++++ app/graphql/types/mutation_type.rb | 1 + .../api/v2/graphql_controller_spec.rb | 50 ++++++++++++++++++- 6 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 app/graphql/mutations/dossier_modifier_annotation_drop_down_list.rb diff --git a/app/graphql/api/v2/stored_query.rb b/app/graphql/api/v2/stored_query.rb index 1c48a5bd1..b246cc22b 100644 --- a/app/graphql/api/v2/stored_query.rb +++ b/app/graphql/api/v2/stored_query.rb @@ -894,6 +894,20 @@ class API::V2::StoredQuery } } + mutation dossierModifierAnnotationDropDownList( + $input: DossierModifierAnnotationDropDownListInput! + ) { + dossierModifierAnnotationDropDownList(input: $input) { + annotation { + id + value: stringValue + } + errors { + message + } + } + } + mutation dossierModifierAnnotationIntegerNumber( $input: DossierModifierAnnotationIntegerNumberInput! ) { diff --git a/app/graphql/mutations/dossier_modifier_annotation.rb b/app/graphql/mutations/dossier_modifier_annotation.rb index de467fa00..aca7fb396 100644 --- a/app/graphql/mutations/dossier_modifier_annotation.rb +++ b/app/graphql/mutations/dossier_modifier_annotation.rb @@ -65,6 +65,8 @@ module Mutations TypeDeChamp.type_champs.fetch(:datetime) when :integer_number TypeDeChamp.type_champs.fetch(:integer_number) + when :drop_down_list + TypeDeChamp.type_champs.fetch(:drop_down_list) end end end diff --git a/app/graphql/mutations/dossier_modifier_annotation_drop_down_list.rb b/app/graphql/mutations/dossier_modifier_annotation_drop_down_list.rb new file mode 100644 index 000000000..5136242c7 --- /dev/null +++ b/app/graphql/mutations/dossier_modifier_annotation_drop_down_list.rb @@ -0,0 +1,17 @@ +module Mutations + class DossierModifierAnnotationDropDownList < Mutations::DossierModifierAnnotation + description "Modifier l’annotation d'un champs de type dropdown list." + + argument :value, GraphQL::Types::String, required: true + + def resolve(dossier:, annotation_id:, instructeur:, value:) + resolve_with_type(dossier:, annotation_id:, instructeur:, value:) + end + + private + + def input_type + :drop_down_list + end + end +end diff --git a/app/graphql/schema.graphql b/app/graphql/schema.graphql index c422408fe..1ae80f784 100644 --- a/app/graphql/schema.graphql +++ b/app/graphql/schema.graphql @@ -1937,6 +1937,45 @@ type DossierModifierAnnotationDatetimePayload { errors: [ValidationError!] } +""" +Autogenerated input type of DossierModifierAnnotationDropDownList +""" +input DossierModifierAnnotationDropDownListInput { + """ + Annotation ID + """ + annotationId: ID! + + """ + A unique identifier for the client performing the mutation. + """ + clientMutationId: String + + """ + Dossier ID + """ + dossierId: ID! + + """ + Instructeur qui demande la modification. + """ + instructeurId: ID! + value: String! +} + +""" +Autogenerated return type of DossierModifierAnnotationDropDownList. +""" +type DossierModifierAnnotationDropDownListPayload { + annotation: Champ + + """ + A unique identifier for the client performing the mutation. + """ + clientMutationId: String + errors: [ValidationError!] +} + """ Autogenerated input type of DossierModifierAnnotationIntegerNumber """ @@ -3278,6 +3317,16 @@ type Mutation { input: DossierModifierAnnotationDatetimeInput! ): DossierModifierAnnotationDatetimePayload + """ + Modifier l’annotation d'un champs de type dropdown list. + """ + dossierModifierAnnotationDropDownList( + """ + Parameters for DossierModifierAnnotationDropDownList + """ + input: DossierModifierAnnotationDropDownListInput! + ): DossierModifierAnnotationDropDownListPayload + """ Modifier l’annotation au format nombre entier. """ diff --git a/app/graphql/types/mutation_type.rb b/app/graphql/types/mutation_type.rb index b9ad5a92c..286cbeac3 100644 --- a/app/graphql/types/mutation_type.rb +++ b/app/graphql/types/mutation_type.rb @@ -19,6 +19,7 @@ module Types field :dossier_modifier_annotation_datetime, mutation: Mutations::DossierModifierAnnotationDatetime field :dossier_modifier_annotation_integer_number, mutation: Mutations::DossierModifierAnnotationIntegerNumber field :dossier_modifier_annotation_ajouter_ligne, mutation: Mutations::DossierModifierAnnotationAjouterLigne + field :dossier_modifier_annotation_drop_down_list, mutation: Mutations::DossierModifierAnnotationDropDownList field :groupe_instructeur_modifier, mutation: Mutations::GroupeInstructeurModifier field :groupe_instructeur_creer, mutation: Mutations::GroupeInstructeurCreer diff --git a/spec/controllers/api/v2/graphql_controller_spec.rb b/spec/controllers/api/v2/graphql_controller_spec.rb index f54c63190..4382eb52c 100644 --- a/spec/controllers/api/v2/graphql_controller_spec.rb +++ b/spec/controllers/api/v2/graphql_controller_spec.rb @@ -1230,9 +1230,13 @@ describe API::V2::GraphqlController do end describe 'dossierModifierAnnotation' do - let(:procedure) { create(:procedure, :published, :for_individual, :with_service, :with_all_annotations, administrateurs: [admin]) } + let(:procedure) do + create(:procedure, :published, :for_individual, :with_service, administrateurs: [admin], types_de_champ_private:) + end describe 'text' do + let(:types_de_champ_private) { [{ type: :text }] } + let(:query) do "mutation { dossierModifierAnnotationText(input: { @@ -1266,6 +1270,8 @@ describe API::V2::GraphqlController do end describe 'checkbox' do + let(:types_de_champ_private) { [{ type: :checkbox }] } + let(:value) { 'true' } let(:query) do @@ -1316,6 +1322,7 @@ describe API::V2::GraphqlController do end describe 'yes_no' do + let(:types_de_champ_private) { [{ type: :yes_no }] } let(:value) { 'true' } let(:query) do @@ -1366,6 +1373,8 @@ describe API::V2::GraphqlController do end describe 'date' do + let(:types_de_champ_private) { [{ type: :date }] } + let(:query) do "mutation { dossierModifierAnnotationDate(input: { @@ -1399,6 +1408,8 @@ describe API::V2::GraphqlController do end describe 'datetime' do + let(:types_de_champ_private) { [{ type: :datetime }] } + let(:query) do "mutation { dossierModifierAnnotationDatetime(input: { @@ -1431,7 +1442,44 @@ describe API::V2::GraphqlController do end end + describe 'drop_down_list' do + let(:drop_down_list_options) { ['bijour'] } + let(:types_de_champ_private) { [{ type: :drop_down_list, options: drop_down_list_options }] } + let(:query) do + "mutation { + dossierModifierAnnotationDropDownList(input: { + dossierId: \"#{dossier.to_typed_id}\", + annotationId: \"#{dossier.champs_private.find { |c| c.type_champ == 'drop_down_list' }.to_typed_id}\", + instructeurId: \"#{instructeur.to_typed_id}\", + value: \"#{value}\" + }) { + annotation { + stringValue + } + errors { + message + } + } + }" + end + + context "success" do + let(:value) { drop_down_list_options.first } + it 'should be a success' do + expect(gql_errors).to eq(nil) + + expect(gql_data).to eq(dossierModifierAnnotationDropDownList: { + annotation: { + stringValue: dossier.reload.champs_private.find { |c| c.type_champ == 'drop_down_list' }.to_s + }, + errors: nil + }) + end + end + end + describe 'integer_number' do + let(:types_de_champ_private) { [{ type: :integer_number }] } let(:query) do "mutation { dossierModifierAnnotationIntegerNumber(input: { From e63b777bd902fbddaf98c72e4fe239f105b08937 Mon Sep 17 00:00:00 2001 From: mfo Date: Wed, 24 Jul 2024 17:48:00 +0200 Subject: [PATCH 2/2] feat(api.dossier.modifier_annotation): stop validation only with graphql schema, also validate with our validations methods --- app/graphql/mutations/dossier_modifier_annotation.rb | 2 +- spec/controllers/api/v2/graphql_controller_spec.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/graphql/mutations/dossier_modifier_annotation.rb b/app/graphql/mutations/dossier_modifier_annotation.rb index aca7fb396..3d3c140ba 100644 --- a/app/graphql/mutations/dossier_modifier_annotation.rb +++ b/app/graphql/mutations/dossier_modifier_annotation.rb @@ -20,7 +20,7 @@ module Mutations annotation.value = value end - if annotation.save + if annotation.validate(:champs_private_value) && annotation.save { annotation: } else { errors: annotation.errors.full_messages } diff --git a/spec/controllers/api/v2/graphql_controller_spec.rb b/spec/controllers/api/v2/graphql_controller_spec.rb index 4382eb52c..ec5275d8e 100644 --- a/spec/controllers/api/v2/graphql_controller_spec.rb +++ b/spec/controllers/api/v2/graphql_controller_spec.rb @@ -1476,6 +1476,17 @@ describe API::V2::GraphqlController do }) end end + context "failure" do + let(:value) { drop_down_list_options.first.reverse } + it 'should be a success' do + expect(gql_errors).to eq(nil) + + expect(gql_data).to eq(dossierModifierAnnotationDropDownList: { + annotation: nil, + errors: [{ message: "doit être dans les options proposées" }] + }) + end + end end describe 'integer_number' do