generated from betagouv/rails-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor adresse entity subtypes (#1336)
-> the specificities of the adresse mapper were simply ignored -> this refactor also allows to clean up the mess up i left in there
- Loading branch information
Showing
17 changed files
with
270 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# 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 | ||
|
||
# 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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# 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 | ||
|
||
def root_node_name | ||
"adresse" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# 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(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) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# 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] } | ||
|
||
it "uses the address details of the student" do # rubocop:disable RSpec/MultipleExpectations | ||
expect(document.at("codepostalcedex").text).to eq student.address_postal_code | ||
expect(document.at("codeinseepays").text).to eq InseeCodes::FRANCE_INSEE_COUNTRY_CODE | ||
expect(document.at("codetypeadr").text).to eq "PRINCIPALE" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 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) } | ||
|
||
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(: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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
describe ASP::Entities::Adresse::Indu, type: :model do | ||
describe "fragment" do | ||
let(:pfmp) { create(:pfmp, :rectified) } | ||
|
||
before do | ||
pfmp.student.update( | ||
address_line1: "A" * 50, | ||
address_line2: "B" * 50 | ||
) | ||
end | ||
|
||
describe "validation" do | ||
it { is_expected.to validate_presence_of(:pointremise) } | ||
it { is_expected.to validate_presence_of(:cpltdistribution) } | ||
|
||
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(pfmp.latest_payment_request) } | ||
let(:probe) { %w[codetypeadr PRINCIPALE] } | ||
|
||
it "uses the establishment details for the address" do # rubocop:disable RSpec/MultipleExpectations | ||
expect(document.at("pointremise").text).to eq "A" * 38 | ||
expect(document.at("cpltdistribution").text).to eq "B" * 38 | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.