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 470780c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 36 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
17 changes: 17 additions & 0 deletions app/services/asp/mappers/adresse/indu_mapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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
24 changes: 15 additions & 9 deletions lib/asp/entities/adresse/etranger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ 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
attribute :codetypeadr, :string
attribute :codecominsee, :string
attribute :codeinseepays, :string
attribute :codepostalcedex, :string

raise ASP::Errors::MissingEstablishmentCommuneCodeError if establishment.commune_code.blank?
raise ASP::Errors::MissingEstablishmentPostalCodeError if establishment.postal_code.blank?
validates_presence_of %i[
codetypeadr
codeinseepays
codepostalcedex
codecominsee
]

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 @@ -22,14 +22,6 @@ def fragment(xml)
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
19 changes: 13 additions & 6 deletions lib/asp/entities/adresse/indu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,30 @@
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 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
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 470780c

Please sign in to comment.