Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asp: clarify the filename attribute and fix file_saved? for payments #681

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions app/services/asp/file_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ module ASP
class FileHandler
include Errors

attr_reader :filepath, :filename
attr_reader :filepath, :filename_noext
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A quoi sert ce renommage, j'ai pas compris 🤔

ça veut dire quoi "noext" ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aaah, "no extension" ok, j'ai.

C'est pas ouf les abréviations dans les noms de variable. Je préfèrerait un truc plus explicite comme filename_without_extension.


FILE_TYPES = %i[rejects integrations payments].freeze

def initialize(filepath)
@filepath = filepath
@filename = File.basename(filepath, ".*")
@filename_noext = File.basename(filepath, ".*")
end

def parse!
Expand All @@ -21,13 +21,13 @@ def parse!
begin
reader.process!
rescue StandardError => e
raise ResponseFileParsingError, "couldn't parse #{filename}: #{e}"
raise ResponseFileParsingError, "couldn't parse #{filename_noext}: #{e}"
end
end

def file_saved?
if payments_file?
ASP::PaymentReturn.exists?(filename: filename)
ASP::PaymentReturn.exists?(filename: "#{filename_noext}.xml")
else
target_attachment.attached?
end
Expand All @@ -42,7 +42,7 @@ def file_saved?
end

def kind
case filename
case filename_noext
when /^rejets_integ_idp/
:rejects
when /^identifiants_generes/
Expand All @@ -56,9 +56,9 @@ def original_filename
return if payments_file?

name = if rejects_file?
filename.split("integ_idp_").last
filename_noext.split("integ_idp_").last
elsif integrations_file?
filename.split("generes_").last
filename_noext.split("generes_").last
end

"#{name}.xml"
Expand Down Expand Up @@ -89,7 +89,7 @@ def persist_file!
end

def persist_payment_file!
ASP::PaymentReturn.create_with_file!(io: File.read(filepath), filename: "#{filename}.xml")
ASP::PaymentReturn.create_with_file!(io: File.read(filepath), filename: "#{filename_noext}.xml")
end

def attach_to_request!
Expand Down
11 changes: 9 additions & 2 deletions spec/services/asp/file_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@

expect(request.send("#{type}_file")).to be_attached
end

it "can tell the file was attached correctly" do
expect { reader.parse! }.to change(reader, :file_saved?).from(false).to(true)
end
end
# rubocop:enable Rspec/MultipleMemoizedHelpers

%i[rejects integrations].each do |type|
%i[integrations rejects].each do |type|
context "when the file is #{type} file" do
it_behaves_like "a reader for the ASP integration process", type
end
Expand All @@ -60,6 +64,9 @@

expect(ASP::PaymentReturn.last.filename).to eq basename
end

it "knows whether the file was saved" do
expect { reader.parse! }.to change(reader, :file_saved?).from(false).to(true)
end
end
end
# frozen_string_literal: true