Skip to content

Commit

Permalink
Define mappers for each entity
Browse files Browse the repository at this point in the history
  • Loading branch information
pskl committed Jan 22, 2025
1 parent dfbc93c commit a5e70b2
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 44 deletions.
31 changes: 31 additions & 0 deletions app/services/asp/mappers/adresse/etranger_mapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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 codepostalcedex
establishment.postal_code
end

def codetypeadr
FranceMapper::PRINCIPAL_ADDRESS_TYPE
end

def codeinseepays
InseeCodes::FRANCE_INSEE_COUNTRY_CODE
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module ASP
module Mappers
module Adresse
class BaseMapper
class FranceMapper
PRINCIPAL_ADDRESS_TYPE = "PRINCIPALE"

MAPPING = {
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions app/services/asp/mappers/adresse/indu_mapper.rb
Original file line number Diff line number Diff line change
@@ -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
30 changes: 20 additions & 10 deletions lib/asp/entities/adresse/etranger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
23 changes: 17 additions & 6 deletions lib/asp/entities/adresse/indu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/asp/entities/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/asp/entities/adresse/etranger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
let(:establishment) { request.pfmp.establishment }

before do
establishment.update(commune_code: "12345", postal_code: "54321")
establishment.update!(commune_code: "12345", postal_code: "54321")
end

it_behaves_like "an XML-fragment producer" do
Expand All @@ -37,7 +37,7 @@

it "raises MissingEstablishmentCommuneCodeError" do
expect { described_class.from_payment_request(request).to_xml(Nokogiri::XML::Builder.new) }
.to raise_error(ASP::Errors::MissingEstablishmentCommuneCodeError)
.to raise_error(ActiveModel::ValidationError)
end
end

Expand All @@ -46,7 +46,7 @@

it "raises MissingEstablishmentPostalCodeError" do
expect { described_class.from_payment_request(request).to_xml(Nokogiri::XML::Builder.new) }
.to raise_error(ASP::Errors::MissingEstablishmentPostalCodeError)
.to raise_error(ActiveModel::ValidationError)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "rails_helper"

describe ASP::Entities::Adresse::Base, type: :model do
describe ASP::Entities::Adresse::France, type: :model do
subject(:model) { described_class.from_payment_request(request) }

let(:request) { create(:asp_payment_request, :ready) }
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/asp/entities/enregistrement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/asp/entities/prestadoss_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let(:schooling) { payment_request.schooling }

before do
mock_entity("Adresse::Base")
mock_entity("Adresse::France")
mock_entity("CoordPaie")
mock_entity("ElementPaiement")
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down

0 comments on commit a5e70b2

Please sign in to comment.