diff --git a/app/services/asp/mappers/adresse/etranger_mapper.rb b/app/services/asp/mappers/adresse/etranger_mapper.rb new file mode 100644 index 000000000..e070edf9c --- /dev/null +++ b/app/services/asp/mappers/adresse/etranger_mapper.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module ASP + module Mappers + module Adresse + class EtrangerMapper + attr_reader :establishment + + def initialize(payment_request) + @establishment = payment_request.student.establishment + end + + def codecominsee + establishment.commune_code + end + + def codetypeadr + FranceMapper::PRINCIPAL_ADDRESS_TYPE + end + + def codeinseepays + InseeCodes::FRANCE_INSEE_COUNTRY_CODE + end + end + end + end +end diff --git a/app/services/asp/mappers/adresse/base_mapper.rb b/app/services/asp/mappers/adresse/france_mapper.rb similarity index 73% rename from app/services/asp/mappers/adresse/base_mapper.rb rename to app/services/asp/mappers/adresse/france_mapper.rb index fe8f3c7b1..20a708742 100644 --- a/app/services/asp/mappers/adresse/base_mapper.rb +++ b/app/services/asp/mappers/adresse/france_mapper.rb @@ -3,7 +3,7 @@ module ASP module Mappers module Adresse - class BaseMapper + class FranceMapper PRINCIPAL_ADDRESS_TYPE = "PRINCIPALE" MAPPING = { @@ -28,16 +28,6 @@ def codetypeadr def codeinseepays InseeCountryCodeMapper.call(student.address_country_code) end - - # Max 38 characters - def pointremise - student.address_line1.slice(0, 38) - end - - # Max 38 characters - def cpltdistribution - student.address_line2&.slice(0, 38) - end end end end diff --git a/app/services/asp/mappers/adresse/indu_mapper.rb b/app/services/asp/mappers/adresse/indu_mapper.rb new file mode 100644 index 000000000..e409a515f --- /dev/null +++ b/app/services/asp/mappers/adresse/indu_mapper.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module ASP + module Mappers + module Adresse + class InduMapper < FranceMapper + # Max 38 characters + def pointremise + student.address_line1.slice(0, 38) + end + + # Max 38 characters + def cpltdistribution + student.address_line2&.slice(0, 38) + end + end + end + end +end diff --git a/lib/asp/entities/adresse/etranger.rb b/lib/asp/entities/adresse/etranger.rb index 3f7187ebd..98a0c399f 100644 --- a/lib/asp/entities/adresse/etranger.rb +++ b/lib/asp/entities/adresse/etranger.rb @@ -3,18 +3,28 @@ 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 + class Etranger < Entity + attribute :codetypeadr, :string + attribute :codecominsee, :string + attribute :codeinseepays, :string + attribute :codepostalcedex, :string + + validates_presence_of %i[ + codetypeadr + codeinseepays + codepostalcedex + codecominsee + ] - raise ASP::Errors::MissingEstablishmentCommuneCodeError if establishment.commune_code.blank? - raise ASP::Errors::MissingEstablishmentPostalCodeError if establishment.postal_code.blank? + def self.payment_mapper_class + Mappers::Adresse::EtrangerMapper + end - 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) + def fragment(xml) + xml.codetypeadr(codetypeadr) + xml.codecominsee(codecominsee) + xml.codepostalcedex(codepostalcedex) + xml.codeinseepays(codeinseepays) end end end diff --git a/lib/asp/entities/adresse/base.rb b/lib/asp/entities/adresse/france.rb similarity index 83% rename from lib/asp/entities/adresse/base.rb rename to lib/asp/entities/adresse/france.rb index 7ac220f35..37126be69 100644 --- a/lib/asp/entities/adresse/base.rb +++ b/lib/asp/entities/adresse/france.rb @@ -3,7 +3,7 @@ module ASP module Entities module Adresse - class Base < Entity + class France < Entity attribute :codetypeadr, :string attribute :codecominsee, :string attribute :codeinseepays, :string @@ -16,20 +16,16 @@ class Base < Entity codecominsee ] + def self.payment_mapper_class + Mappers::Adresse::FranceMapper + end + 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 - - def root_node_name - "adresse" - end end end end diff --git a/lib/asp/entities/adresse/indu.rb b/lib/asp/entities/adresse/indu.rb index 7cb2bed01..be5124c6e 100644 --- a/lib/asp/entities/adresse/indu.rb +++ b/lib/asp/entities/adresse/indu.rb @@ -3,23 +3,34 @@ module ASP module Entities module Adresse - class Indu < Base + class Indu < Entity attribute :pointremise, :string attribute :cpltdistribution, :string + attribute :codetypeadr, :string + attribute :codecominsee, :string + attribute :codeinseepays, :string + attribute :codepostalcedex, :string validates_presence_of %i[ pointremise cpltdistribution + codetypeadr + codeinseepays + codepostalcedex + codecominsee ] + def self.payment_mapper_class + Mappers::Adresse::InduMapper + end + def fragment(xml) xml.pointremise(pointremise) xml.cpltdistribution(cpltdistribution) - - xml.codetypeadr(ASP::Mappers::Adresse::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) + xml.codetypeadr(codetypeadr) + xml.codeinseepays(codeinseepays) + xml.codepostalcedex(codepostalcedex) + xml.codecominsee(codecominsee) end end end diff --git a/lib/asp/entities/entity.rb b/lib/asp/entities/entity.rb index 794454c3f..9d2dde408 100644 --- a/lib/asp/entities/entity.rb +++ b/lib/asp/entities/entity.rb @@ -65,7 +65,7 @@ def adresse_entity_class if payment_request.pfmp.rectified? Adresse::Indu else - payment_request.student.lives_in_france? ? Adresse::Base : Adresse::Etranger + payment_request.student.lives_in_france? ? Adresse::France : Adresse::Etranger end end end diff --git a/spec/lib/asp/entities/enregistrement_spec.rb b/spec/lib/asp/entities/enregistrement_spec.rb index c53c3bb87..8ec1f0962 100644 --- a/spec/lib/asp/entities/enregistrement_spec.rb +++ b/spec/lib/asp/entities/enregistrement_spec.rb @@ -6,7 +6,7 @@ let(:payment_request) { create(:asp_payment_request, :ready) } before do - %w[PersPhysique Adresse::Base CoordPaie Dossier].each { |name| mock_entity(name) } + %w[PersPhysique Adresse::France CoordPaie Dossier].each { |name| mock_entity(name) } end it_behaves_like "an ASP payment mapping entity" diff --git a/spec/services/asp/mappers/adresse/base_mapper_spec.rb b/spec/services/asp/mappers/adresse/france_mapper_spec.rb similarity index 93% rename from spec/services/asp/mappers/adresse/base_mapper_spec.rb rename to spec/services/asp/mappers/adresse/france_mapper_spec.rb index bfcec6aa3..0b1dd1ce4 100644 --- a/spec/services/asp/mappers/adresse/base_mapper_spec.rb +++ b/spec/services/asp/mappers/adresse/france_mapper_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" -describe ASP::Mappers::Adresse::BaseMapper do +describe ASP::Mappers::Adresse::FranceMapper do subject(:mapper) { described_class.new(payment_request) } let(:payment_request) { create(:asp_payment_request) }