From dee0952b2a74c07ca46b98c30ecb239fc906e887 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 4 Apr 2023 16:58:47 +0200 Subject: [PATCH] fix(graphql): demandeur personne morale can be in degraded mode --- app/graphql/api/v2/schema.rb | 1 + app/graphql/api/v2/stored_query.rb | 2 + app/graphql/schema.graphql | 5 +++ app/graphql/types/demandeur_type.rb | 6 ++- .../types/personne_morale_incomplete_type.rb | 7 +++ .../graphql_controller_stored_queries_spec.rb | 45 +++++++++++++++++-- 6 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 app/graphql/types/personne_morale_incomplete_type.rb diff --git a/app/graphql/api/v2/schema.rb b/app/graphql/api/v2/schema.rb index 831cdc906..8c4c083c8 100644 --- a/app/graphql/api/v2/schema.rb +++ b/app/graphql/api/v2/schema.rb @@ -78,6 +78,7 @@ class API::V2::Schema < GraphQL::Schema Types::GeoAreas::ParcelleCadastraleType, Types::GeoAreas::SelectionUtilisateurType, Types::PersonneMoraleType, + Types::PersonneMoraleIncompleteType, Types::PersonnePhysiqueType, Types::Champs::Descriptor::AddressChampDescriptorType, Types::Champs::Descriptor::AnnuaireEducationChampDescriptorType, diff --git a/app/graphql/api/v2/stored_query.rb b/app/graphql/api/v2/stored_query.rb index b3fa110a5..ea6e93ee8 100644 --- a/app/graphql/api/v2/stored_query.rb +++ b/app/graphql/api/v2/stored_query.rb @@ -253,12 +253,14 @@ class API::V2::StoredQuery ...GroupeInstructeurFragment } demandeur { + __typename ... on PersonnePhysique { civilite nom prenom dateDeNaissance } + ... on PersonneMoraleIncomplete { siret } ...PersonneMoraleFragment } demarche { diff --git a/app/graphql/schema.graphql b/app/graphql/schema.graphql index d11659d5d..7395afb98 100644 --- a/app/graphql/schema.graphql +++ b/app/graphql/schema.graphql @@ -3085,6 +3085,11 @@ type PersonneMorale implements Demandeur { typeVoie: String @deprecated(reason: "Utilisez le champ `address.street_address` à la place.") } +type PersonneMoraleIncomplete implements Demandeur { + id: ID! + siret: String! +} + type PersonnePhysique implements Demandeur { civilite: Civilite dateDeNaissance: ISO8601Date diff --git a/app/graphql/types/demandeur_type.rb b/app/graphql/types/demandeur_type.rb index 9f5d01b3d..4f4c677af 100644 --- a/app/graphql/types/demandeur_type.rb +++ b/app/graphql/types/demandeur_type.rb @@ -10,7 +10,11 @@ module Types when Individual Types::PersonnePhysiqueType when Etablissement - Types::PersonneMoraleType + if object.as_degraded_mode? && context.has_fragment?(:PersonneMoraleIncomplete) + Types::PersonneMoraleIncompleteType + else + Types::PersonneMoraleType + end end end end diff --git a/app/graphql/types/personne_morale_incomplete_type.rb b/app/graphql/types/personne_morale_incomplete_type.rb new file mode 100644 index 000000000..37f4c1641 --- /dev/null +++ b/app/graphql/types/personne_morale_incomplete_type.rb @@ -0,0 +1,7 @@ +module Types + class PersonneMoraleIncompleteType < Types::BaseObject + implements Types::DemandeurType + + field :siret, String, null: false + end +end 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 2668b9496..712eb1c8a 100644 --- a/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb +++ b/spec/controllers/api/v2/graphql_controller_stored_queries_spec.rb @@ -71,7 +71,35 @@ describe API::V2::GraphqlController do it { expect(gql_errors).to be_nil expect(gql_data[:dossier][:id]).to eq(dossier.to_typed_id) + expect(gql_data[:dossier][:demandeur][:__typename]).to eq('PersonnePhysique') + expect(gql_data[:dossier][:demandeur][:nom]).to eq(dossier.individual.nom) + expect(gql_data[:dossier][:demandeur][:prenom]).to eq(dossier.individual.prenom) } + + context 'with entreprise' do + let(:procedure) { create(:procedure, :published, :with_service, administrateurs: [admin], types_de_champ_public:) } + let(:dossier) { create(:dossier, :en_construction, :with_entreprise, procedure: procedure) } + + it { + expect(gql_errors).to be_nil + expect(gql_data[:dossier][:id]).to eq(dossier.to_typed_id) + expect(gql_data[:dossier][:demandeur][:__typename]).to eq('PersonneMorale') + expect(gql_data[:dossier][:demandeur][:siret]).to eq(dossier.etablissement.siret) + expect(gql_data[:dossier][:demandeur][:libelleNaf]).to eq(dossier.etablissement.libelle_naf) + } + + context 'when in degraded mode' do + before { dossier.etablissement.update(adresse: nil) } + + it { + expect(gql_errors).to be_nil + expect(gql_data[:dossier][:id]).to eq(dossier.to_typed_id) + expect(gql_data[:dossier][:demandeur][:__typename]).to eq('PersonneMoraleIncomplete') + expect(gql_data[:dossier][:demandeur][:siret]).to eq(dossier.etablissement.siret) + expect(gql_data[:dossier][:demandeur][:libelleNaf]).to be_nil + } + end + end end context 'getDemarche' do @@ -324,15 +352,24 @@ describe API::V2::GraphqlController do } end - context 'when in degraded mode' do + context 'with entreprise' do let(:procedure) { create(:procedure, :published, :with_service, administrateurs: [admin]) } let(:dossier) { create(:dossier, :en_instruction, :with_entreprise, procedure:) } - before { dossier.etablissement.update(adresse: nil) } - it { - expect(gql_data[:dossierAccepter][:errors].first[:message]).to eq('Les informations du SIRET du dossier ne sont pas complètes. Veuillez réessayer plus tard.') + expect(gql_errors).to be_nil + expect(gql_data[:dossierAccepter][:errors]).to be_nil + expect(gql_data[:dossierAccepter][:dossier][:id]).to eq(dossier.to_typed_id) + expect(gql_data[:dossierAccepter][:dossier][:state]).to eq('accepte') } + + context 'when in degraded mode' do + before { dossier.etablissement.update(adresse: nil) } + + it { + expect(gql_data[:dossierAccepter][:errors].first[:message]).to eq('Les informations du SIRET du dossier ne sont pas complètes. Veuillez réessayer plus tard.') + } + end end end