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 16d36d9
Show file tree
Hide file tree
Showing 15 changed files with 241 additions and 182 deletions.
34 changes: 34 additions & 0 deletions app/services/asp/mappers/adresse/base_mapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module ASP
module Mappers
module Adresse
class BaseMapper
PRINCIPAL_ADDRESS_TYPE = "PRINCIPALE"

MAPPING = {
codecominsee: :address_city_insee_code,
codepostalcedex: :address_postal_code
}.freeze

attr_reader :student

def initialize(payment_request)
@student = payment_request.student
end

MAPPING.each do |name, attr|
define_method(name) { student[attr] }
end

def codetypeadr
PRINCIPAL_ADDRESS_TYPE
end

def codeinseepays
InseeCountryCodeMapper.call(student.address_country_code)
end
end
end
end
end
33 changes: 0 additions & 33 deletions app/services/asp/mappers/adresse_mapper.rb

This file was deleted.

58 changes: 0 additions & 58 deletions lib/asp/entities/adresse.rb

This file was deleted.

32 changes: 32 additions & 0 deletions lib/asp/entities/adresse/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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

def self.payment_mapper_class
ASP::Mappers::Adresse::BaseMapper
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::Adresse::BaseMapper::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::Addresse::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)
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
14 changes: 13 additions & 1 deletion lib/asp/entities/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ class Entity
ASP_NO_MODIFICATION = { modification: "N" }.freeze

class << self
def mapper_name
name
end

def payment_mapper_class
klass = name.demodulize
klass = mapper_name.demodulize

"ASP::Mappers::#{klass}Mapper".constantize
end
Expand Down Expand Up @@ -57,6 +61,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
27 changes: 27 additions & 0 deletions spec/lib/asp/entities/adresse/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require "rails_helper"

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

let(:request) { create(:asp_payment_request, :ready) }
let(:student) { create(:student, :with_all_asp_info, :with_french_address) }

before do
request.pfmp.update!(student: student)
request.reload
end

describe "validation" do
it { is_expected.to validate_presence_of(:codepostalcedex) }
it { is_expected.to validate_presence_of(:codecominsee) }
it { is_expected.to validate_presence_of(:codeinseepays) }
it { is_expected.to validate_presence_of(:codetypeadr) }
end

it_behaves_like "an XML-fragment producer" do
let(:entity) { described_class.from_payment_request(request) }
let(:probe) { ["codecominsee", student.address_city_insee_code] }
end
end
55 changes: 55 additions & 0 deletions spec/lib/asp/entities/adresse/etranger_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require "rails_helper"

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

let(:request) { create(:asp_payment_request, :ready) }
let(:student) { create(:student, :with_all_asp_info, :with_french_address) }

describe "validation" do
it { is_expected.to validate_presence_of(:codepostalcedex) }
it { is_expected.to validate_presence_of(:codecominsee) }
it { is_expected.to validate_presence_of(:codeinseepays) }
it { is_expected.to validate_presence_of(:codetypeadr) }
end

describe "fragment" do
let(:student) { create(:student, :with_all_asp_info, :with_foreign_address) }
let(:establishment) { request.pfmp.establishment }

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

it_behaves_like "an XML-fragment producer" do
let(:entity) { described_class.from_payment_request(request) }
let(:probe) { %w[codetypeadr PRINCIPALE] }

it "uses the establishment details for the address" do # rubocop:disable RSpec/MultipleExpectations
expect(document.at("codecominsee").text).to eq establishment.commune_code
expect(document.at("codepostalcedex").text).to eq establishment.postal_code
expect(document.at("codeinseepays").text).to eq InseeCodes::FRANCE_INSEE_COUNTRY_CODE
end
end

context "when establishment commune_code is missing" do
before { establishment.update(commune_code: nil) }

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

context "when establishment postal_code is missing" do
before { establishment.update(postal_code: nil) }

it "raises MissingEstablishmentPostalCodeError" do
expect { described_class.from_payment_request(request).to_xml(Nokogiri::XML::Builder.new) }
.to raise_error(ASP::Errors::MissingEstablishmentPostalCodeError)
end
end
end
end
28 changes: 28 additions & 0 deletions spec/lib/asp/entities/adresse/indu_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require "rails_helper"

describe ASP::Entities::Adresse::Indu, type: :model do
describe "fragment" do
let(:request) { create(:asp_payment_request, :ready) }
let(:student) { create(:student, :with_all_asp_info, :with_french_address) }

let(:pfmp) { create(:pfmp, :rectified) }

before do
pfmp.student.update(
address_line1: "A" * 50,
address_line2: "B" * 50
)
end

it "creates an Adresse instance with truncated attrs" do # rubocop:disable RSpec/ExampleLength
adresse = described_class.from_payment_request(pfmp.latest_payment_request).to_xml(Nokogiri::XML::Builder.new)
expect(adresse).to have_attributes(
codetypeadr: ASP::Mappers::Adresse::BaseMapper::PRINCIPAL_ADDRESS_TYPE,
pointremise: "A" * 38,
cpltdistribution: "B" * 38
)
end
end
end
Loading

0 comments on commit 16d36d9

Please sign in to comment.