Skip to content

Commit

Permalink
Refactor adresse entity subtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
pskl committed Jan 16, 2025
1 parent 6b74cd8 commit 27181db
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 60 deletions.
58 changes: 0 additions & 58 deletions lib/asp/entities/adresse.rb

This file was deleted.

28 changes: 28 additions & 0 deletions lib/asp/entities/adresse/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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
end
end
end
end
22 changes: 22 additions & 0 deletions lib/asp/entities/adresse/etranger.rb
Original file line number Diff line number Diff line change
@@ -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::AdresseMapper::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
26 changes: 26 additions & 0 deletions lib/asp/entities/adresse/indu.rb
Original file line number Diff line number Diff line change
@@ -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::AdresseMapper::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
2 changes: 1 addition & 1 deletion lib/asp/entities/enregistrement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/asp/entities/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/asp/entities/prestadoss.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 27181db

Please sign in to comment.