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 015841f
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 39 deletions.
27 changes: 27 additions & 0 deletions app/services/asp/mappers/adresse/etranger_mapper.rb
Original file line number Diff line number Diff line change
@@ -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
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
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
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 015841f

Please sign in to comment.