From f9f4f9889a8ebb484d16538473cd2f1faf48a435 Mon Sep 17 00:00:00 2001 From: pskl Date: Thu, 16 Jan 2025 15:43:03 +0100 Subject: [PATCH] Refactor adresse entity subtypes --- .../asp/mappers/adresse/base_mapper.rb | 34 ++++++++ app/services/asp/mappers/adresse_mapper.rb | 33 ------- lib/asp/entities/adresse.rb | 58 ------------- lib/asp/entities/adresse/base.rb | 32 +++++++ lib/asp/entities/adresse/etranger.rb | 22 +++++ lib/asp/entities/adresse/indu.rb | 26 ++++++ lib/asp/entities/enregistrement.rb | 2 +- lib/asp/entities/entity.rb | 8 ++ lib/asp/entities/prestadoss.rb | 2 +- spec/lib/asp/entities/adresse/base_spec.rb | 27 ++++++ .../lib/asp/entities/adresse/etranger_spec.rb | 55 ++++++++++++ spec/lib/asp/entities/adresse/indu_spec.rb | 28 ++++++ spec/lib/asp/entities/adresse_spec.rb | 86 ------------------- spec/lib/asp/entities/pers_physique_spec.rb | 2 +- spec/lib/asp/entities/prestadoss_spec.rb | 2 +- .../base_mapper_spec.rb} | 2 +- 16 files changed, 237 insertions(+), 182 deletions(-) create mode 100644 app/services/asp/mappers/adresse/base_mapper.rb delete mode 100644 app/services/asp/mappers/adresse_mapper.rb delete mode 100644 lib/asp/entities/adresse.rb create mode 100644 lib/asp/entities/adresse/base.rb create mode 100644 lib/asp/entities/adresse/etranger.rb create mode 100644 lib/asp/entities/adresse/indu.rb create mode 100644 spec/lib/asp/entities/adresse/base_spec.rb create mode 100644 spec/lib/asp/entities/adresse/etranger_spec.rb create mode 100644 spec/lib/asp/entities/adresse/indu_spec.rb delete mode 100644 spec/lib/asp/entities/adresse_spec.rb rename spec/services/asp/mappers/{adresse_mapper_spec.rb => adresse/base_mapper_spec.rb} (93%) diff --git a/app/services/asp/mappers/adresse/base_mapper.rb b/app/services/asp/mappers/adresse/base_mapper.rb new file mode 100644 index 000000000..b5761d256 --- /dev/null +++ b/app/services/asp/mappers/adresse/base_mapper.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +module ASP + module Mappers + module Adresse + class BaseMapper + PRINCIPAL_ADDRESS_TYPE = "PRINCIPALE" + + MAPPING = { + codecominsee: :address_city_insee_code, + codepostalcedex: :address_postal_code + }.freeze + + attr_reader :student + + def initialize(payment_request) + @student = payment_request.student + end + + MAPPING.each do |name, attr| + define_method(name) { student[attr] } + end + + def codetypeadr + PRINCIPAL_ADDRESS_TYPE + end + + def codeinseepays + InseeCountryCodeMapper.call(student.address_country_code) + end + end + end + end +end diff --git a/app/services/asp/mappers/adresse_mapper.rb b/app/services/asp/mappers/adresse_mapper.rb deleted file mode 100644 index d96fae1e5..000000000 --- a/app/services/asp/mappers/adresse_mapper.rb +++ /dev/null @@ -1,33 +0,0 @@ -# frozen_string_literal: true - -module ASP - module Mappers - class AdresseMapper - PRINCIPAL_ADDRESS_TYPE = "PRINCIPALE" - ABROAD_ADDRESS_TYPE = "ADMIN" # From ASP Doc (Apparently not supported by ASP!) - - MAPPING = { - codecominsee: :address_city_insee_code, - codepostalcedex: :address_postal_code - }.freeze - - attr_reader :student - - def initialize(payment_request) - @student = payment_request.student - end - - MAPPING.each do |name, attr| - define_method(name) { student[attr] } - end - - def codetypeadr - PRINCIPAL_ADDRESS_TYPE - end - - def codeinseepays - InseeCountryCodeMapper.call(student.address_country_code) - end - end - end -end diff --git a/lib/asp/entities/adresse.rb b/lib/asp/entities/adresse.rb deleted file mode 100644 index 533747adb..000000000 --- a/lib/asp/entities/adresse.rb +++ /dev/null @@ -1,58 +0,0 @@ -# frozen_string_literal: true - -module ASP - module Entities - class Adresse < Entity - attribute :codetypeadr, :string - attribute :codecominsee, :string - attribute :codeinseepays, :string - attribute :codepostalcedex, :string - - attribute :pointremise, :string - attribute :cpltdistribution, :string - - validates_presence_of %i[ - codetypeadr - codeinseepays - codepostalcedex - codecominsee - ] - - def fragment(xml) - xml.codetypeadr(codetypeadr) - xml.codeinseepays(codeinseepays) - xml.codepostalcedex(codepostalcedex) - xml.codecominsee(codecominsee) - end - - def self.from_payment_request(payment_request) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength - if payment_request.pfmp.rectified? - return new( - pointremise: payment_request.student.address_line1.slice(0, 38), # Max 38 characters - cpltdistribution: payment_request.student.address_line2&.slice(0, 38), # Max 38 characters - codetypeadr: ASP::Mappers::AdresseMapper::PRINCIPAL_ADDRESS_TYPE, - codeinseepays: InseeCountryCodeMapper.call(payment_request.student.address_country_code), - codepostalcedex: payment_request.student.address_postal_code, - codecominsee: payment_request.student.address_city_insee_code - ) - end - - if payment_request.student.lives_in_france? - super - else - establishment = payment_request.pfmp.establishment - - raise ASP::Errors::MissingEstablishmentCommuneCodeError if establishment.commune_code.blank? - raise ASP::Errors::MissingEstablishmentPostalCodeError if establishment.postal_code.blank? - - new( - codetypeadr: Mappers::AdresseMapper::PRINCIPAL_ADDRESS_TYPE, - codecominsee: establishment.commune_code, - codepostalcedex: establishment.postal_code, - codeinseepays: InseeCodes::FRANCE_INSEE_COUNTRY_CODE - ) - end - end - end - end -end diff --git a/lib/asp/entities/adresse/base.rb b/lib/asp/entities/adresse/base.rb new file mode 100644 index 000000000..2a9e9d4ae --- /dev/null +++ b/lib/asp/entities/adresse/base.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module ASP + module Entities + module Adresse + class Base < Entity + attribute :codetypeadr, :string + attribute :codecominsee, :string + attribute :codeinseepays, :string + attribute :codepostalcedex, :string + + validates_presence_of %i[ + codetypeadr + codeinseepays + codepostalcedex + codecominsee + ] + + def fragment(xml) + xml.codetypeadr(codetypeadr) + xml.codeinseepays(codeinseepays) + xml.codepostalcedex(codepostalcedex) + xml.codecominsee(codecominsee) + end + + def self.payment_mapper_class + ASP::Mappers::Adresse::BaseMapper + end + end + end + end +end diff --git a/lib/asp/entities/adresse/etranger.rb b/lib/asp/entities/adresse/etranger.rb new file mode 100644 index 000000000..3f7187ebd --- /dev/null +++ b/lib/asp/entities/adresse/etranger.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module ASP + module Entities + module Adresse + class Etranger < Base + # NOTE: for students living outside of France we take address attributes from the establishment + def fragment(xml) + establishment = payment_request.pfmp.establishment + + raise ASP::Errors::MissingEstablishmentCommuneCodeError if establishment.commune_code.blank? + raise ASP::Errors::MissingEstablishmentPostalCodeError if establishment.postal_code.blank? + + xml.codetypeadr(Mappers::Adresse::BaseMapper::PRINCIPAL_ADDRESS_TYPE) + xml.codecominsee(establishment.commune_code) + xml.codepostalcedex(establishment.postal_code) + xml.codeinseepays(InseeCodes::FRANCE_INSEE_COUNTRY_CODE) + end + end + end + end +end diff --git a/lib/asp/entities/adresse/indu.rb b/lib/asp/entities/adresse/indu.rb new file mode 100644 index 000000000..927a8b8e7 --- /dev/null +++ b/lib/asp/entities/adresse/indu.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module ASP + module Entities + module Adresse + class Indu < Base + attribute :pointremise, :string + attribute :cpltdistribution, :string + + validates_presence_of %i[ + pointremise + cpltdistribution + ] + + def fragment(xml) + xml.pointremise(payment_request.student.address_line1.slice(0, 38)) # Max 38 characters + xml.cpltdistribution(payment_request.student.address_line2&.slice(0, 38)) # Max 38 characters + xml.codetypeadr(ASP::Mappers::Addresse::BaseMapper::PRINCIPAL_ADDRESS_TYPE) + xml.codeinseepays(InseeCountryCodeMapper.call(payment_request.student.address_country_code)) + xml.codepostalcedex(payment_request.student.address_postal_code) + xml.codecominsee(payment_request.student.address_city_insee_code) + end + end + end + end +end diff --git a/lib/asp/entities/enregistrement.rb b/lib/asp/entities/enregistrement.rb index 1dd077363..eaa74e39f 100644 --- a/lib/asp/entities/enregistrement.rb +++ b/lib/asp/entities/enregistrement.rb @@ -25,7 +25,7 @@ def fragment(xml) def individu(xml) xml.natureindividu("P") PersPhysique.from_payment_request(payment_request).to_xml(xml) - xml.adressesindividu { Adresse.from_payment_request(payment_request).to_xml(xml) } + xml.adressesindividu { adresse_entity_class.from_payment_request(payment_request).to_xml(xml) } xml.listedossier { Dossier.from_payment_request(payment_request).to_xml(xml) } end diff --git a/lib/asp/entities/entity.rb b/lib/asp/entities/entity.rb index 28b109fe1..45d7d89c4 100644 --- a/lib/asp/entities/entity.rb +++ b/lib/asp/entities/entity.rb @@ -57,6 +57,14 @@ def to_xml(builder) end end end + + def adresse_entity_class + if payment_request.pfmp.rectified? + Adresse::Indu + else + payment_request.student.lives_in_france? ? Adresse::Base : Adresse::Etranger + end + end end end end diff --git a/lib/asp/entities/prestadoss.rb b/lib/asp/entities/prestadoss.rb index 9fac9f0f6..23deee790 100644 --- a/lib/asp/entities/prestadoss.rb +++ b/lib/asp/entities/prestadoss.rb @@ -31,7 +31,7 @@ def xml_root_args def fragment(xml) prestadoss_xml(xml) - xml.adressesprestadoss { Adresse.from_payment_request(payment_request).to_xml(xml) } + xml.adressesprestadoss { adresse_entity_class.from_payment_request(payment_request).to_xml(xml) } xml.coordpaiesprestadoss { CoordPaie.from_payment_request(payment_request).to_xml(xml) } xml.listeelementpaiement { ElementPaiement.from_payment_request(payment_request).to_xml(xml) } end diff --git a/spec/lib/asp/entities/adresse/base_spec.rb b/spec/lib/asp/entities/adresse/base_spec.rb new file mode 100644 index 000000000..2b90bc538 --- /dev/null +++ b/spec/lib/asp/entities/adresse/base_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe ASP::Entities::Adresse::Base, type: :model do + subject(:model) { described_class.from_payment_request(request) } + + let(:request) { create(:asp_payment_request, :ready) } + let(:student) { create(:student, :with_all_asp_info, :with_french_address) } + + before do + request.pfmp.update!(student: student) + request.reload + end + + describe "validation" do + it { is_expected.to validate_presence_of(:codepostalcedex) } + it { is_expected.to validate_presence_of(:codecominsee) } + it { is_expected.to validate_presence_of(:codeinseepays) } + it { is_expected.to validate_presence_of(:codetypeadr) } + end + + it_behaves_like "an XML-fragment producer" do + let(:entity) { described_class.from_payment_request(request) } + let(:probe) { ["codecominsee", student.address_city_insee_code] } + end +end diff --git a/spec/lib/asp/entities/adresse/etranger_spec.rb b/spec/lib/asp/entities/adresse/etranger_spec.rb new file mode 100644 index 000000000..6a2aa0657 --- /dev/null +++ b/spec/lib/asp/entities/adresse/etranger_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe ASP::Entities::Adresse::Etranger, type: :model do + subject(:model) { described_class.from_payment_request(request) } + + let(:request) { create(:asp_payment_request, :ready) } + let(:student) { create(:student, :with_all_asp_info, :with_french_address) } + + describe "validation" do + it { is_expected.to validate_presence_of(:codepostalcedex) } + it { is_expected.to validate_presence_of(:codecominsee) } + it { is_expected.to validate_presence_of(:codeinseepays) } + it { is_expected.to validate_presence_of(:codetypeadr) } + end + + describe "fragment" do + let(:student) { create(:student, :with_all_asp_info, :with_foreign_address) } + let(:establishment) { request.pfmp.establishment } + + before do + establishment.update(commune_code: "12345", postal_code: "54321") + end + + it_behaves_like "an XML-fragment producer" do + let(:entity) { described_class.from_payment_request(request) } + let(:probe) { %w[codetypeadr PRINCIPALE] } + + it "uses the establishment details for the address" do # rubocop:disable RSpec/MultipleExpectations + expect(document.at("codecominsee").text).to eq establishment.commune_code + expect(document.at("codepostalcedex").text).to eq establishment.postal_code + expect(document.at("codeinseepays").text).to eq InseeCodes::FRANCE_INSEE_COUNTRY_CODE + end + end + + context "when establishment commune_code is missing" do + before { establishment.update(commune_code: nil) } + + it "raises MissingEstablishmentCommuneCodeError" do + expect { described_class.from_payment_request(request).to_xml(Nokogiri::XML::Builder.new) } + .to raise_error(ASP::Errors::MissingEstablishmentCommuneCodeError) + end + end + + context "when establishment postal_code is missing" do + before { establishment.update(postal_code: nil) } + + it "raises MissingEstablishmentPostalCodeError" do + expect { described_class.from_payment_request(request).to_xml(Nokogiri::XML::Builder.new) } + .to raise_error(ASP::Errors::MissingEstablishmentPostalCodeError) + end + end + end +end diff --git a/spec/lib/asp/entities/adresse/indu_spec.rb b/spec/lib/asp/entities/adresse/indu_spec.rb new file mode 100644 index 000000000..6662e38bd --- /dev/null +++ b/spec/lib/asp/entities/adresse/indu_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe ASP::Entities::Adresse::Indu, type: :model do + describe "fragment" do + let(:request) { create(:asp_payment_request, :ready) } + let(:student) { create(:student, :with_all_asp_info, :with_french_address) } + + let(:pfmp) { create(:pfmp, :rectified) } + + before do + pfmp.student.update( + address_line1: "A" * 50, + address_line2: "B" * 50 + ) + end + + it "creates an Adresse instance with truncated attrs" do # rubocop:disable RSpec/ExampleLength + adresse = described_class.from_payment_request(pfmp.latest_payment_request).to_xml(Nokogiri::XML::Builder.new) + expect(adresse).to have_attributes( + codetypeadr: ASP::Mappers::Adresse::BaseMapper::PRINCIPAL_ADDRESS_TYPE, + pointremise: "A" * 38, + cpltdistribution: "B" * 38 + ) + end + end +end diff --git a/spec/lib/asp/entities/adresse_spec.rb b/spec/lib/asp/entities/adresse_spec.rb deleted file mode 100644 index 75092017d..000000000 --- a/spec/lib/asp/entities/adresse_spec.rb +++ /dev/null @@ -1,86 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -describe ASP::Entities::Adresse, type: :model do - subject(:model) { described_class.from_payment_request(request) } - - let(:request) { create(:asp_payment_request, :ready) } - let(:student) { create(:student, :with_all_asp_info, :with_french_address) } - - before do - request.pfmp.update!(student: student) - request.reload - end - - describe "validation" do - it { is_expected.to validate_presence_of(:codepostalcedex) } - it { is_expected.to validate_presence_of(:codecominsee) } - it { is_expected.to validate_presence_of(:codeinseepays) } - it { is_expected.to validate_presence_of(:codetypeadr) } - end - - it_behaves_like "an XML-fragment producer" do - let(:entity) { described_class.from_payment_request(request) } - let(:probe) { ["adresse/codecominsee", student.address_city_insee_code] } - end - - describe ".from_payment_request" do - context "when the student lives abroad" do - let(:student) { create(:student, :with_all_asp_info, :with_foreign_address) } - let(:establishment) { request.pfmp.establishment } - - before do - establishment.update(commune_code: "12345", postal_code: "54321") - end - - it "creates an Adresse instance with establishment details" do # rubocop:disable RSpec/ExampleLength - adresse = described_class.from_payment_request(request) - expect(adresse).to have_attributes( - codetypeadr: ASP::Mappers::AdresseMapper::PRINCIPAL_ADDRESS_TYPE, - codecominsee: establishment.commune_code, - codepostalcedex: establishment.postal_code, - codeinseepays: InseeCodes::FRANCE_INSEE_COUNTRY_CODE - ) - end - - context "when establishment commune_code is missing" do - before { establishment.update(commune_code: nil) } - - it "raises MissingEstablishmentCommuneCodeError" do - expect { described_class.from_payment_request(request) } - .to raise_error(ASP::Errors::MissingEstablishmentCommuneCodeError) - end - end - - context "when establishment postal_code is missing" do - before { establishment.update(postal_code: nil) } - - it "raises MissingEstablishmentPostalCodeError" do - expect { described_class.from_payment_request(request) } - .to raise_error(ASP::Errors::MissingEstablishmentPostalCodeError) - end - end - - context "when the PFMP is rectified" do - let(:pfmp) { create(:pfmp, :rectified) } - - before do - pfmp.student.update( - address_line1: "A" * 50, - address_line2: "B" * 50 - ) - end - - it "creates an Adresse instance with truncated attrs" do # rubocop:disable RSpec/ExampleLength - adresse = described_class.from_payment_request(pfmp.latest_payment_request) - expect(adresse).to have_attributes( - codetypeadr: ASP::Mappers::AdresseMapper::PRINCIPAL_ADDRESS_TYPE, - pointremise: "A" * 38, - cpltdistribution: "B" * 38 - ) - end - end - end - end -end diff --git a/spec/lib/asp/entities/pers_physique_spec.rb b/spec/lib/asp/entities/pers_physique_spec.rb index 1dc604f27..2689fdd10 100644 --- a/spec/lib/asp/entities/pers_physique_spec.rb +++ b/spec/lib/asp/entities/pers_physique_spec.rb @@ -39,7 +39,7 @@ let(:entity) { described_class.from_payment_request(payment_request) } let(:probe) { ["persphysique/prenom", "Marie"] } - context "when the student is born abrod" do + context "when the student is born abroad" do let(:student) { create(:student, :with_all_asp_info, :born_abroad) } it "does not include the tag" do diff --git a/spec/lib/asp/entities/prestadoss_spec.rb b/spec/lib/asp/entities/prestadoss_spec.rb index 39c5f7d19..16f12e6f4 100644 --- a/spec/lib/asp/entities/prestadoss_spec.rb +++ b/spec/lib/asp/entities/prestadoss_spec.rb @@ -7,7 +7,7 @@ let(:schooling) { payment_request.schooling } before do - mock_entity("Adresse") + mock_entity("Adresse::Base") mock_entity("CoordPaie") mock_entity("ElementPaiement") end diff --git a/spec/services/asp/mappers/adresse_mapper_spec.rb b/spec/services/asp/mappers/adresse/base_mapper_spec.rb similarity index 93% rename from spec/services/asp/mappers/adresse_mapper_spec.rb rename to spec/services/asp/mappers/adresse/base_mapper_spec.rb index 8aea1f6d9..bfcec6aa3 100644 --- a/spec/services/asp/mappers/adresse_mapper_spec.rb +++ b/spec/services/asp/mappers/adresse/base_mapper_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" -describe ASP::Mappers::AdresseMapper do +describe ASP::Mappers::Adresse::BaseMapper do subject(:mapper) { described_class.new(payment_request) } let(:payment_request) { create(:asp_payment_request) }