From 6231c66df4453cf4563a3058aded4d3cbdf3561c Mon Sep 17 00:00:00 2001 From: Jan Zaydowicz Date: Fri, 26 Jul 2024 14:58:37 +0200 Subject: [PATCH] fix: correct imports With ruby 3 the import logic was adjusted which broke the current import hierarchy. --- box/apis/v2/credit_transfers.rb | 6 +- box/apis/v2/direct_debits.rb | 3 +- box/business_processes/credit.rb | 92 +++++++-------- box/business_processes/direct_debit.rb | 90 +++++++-------- box/business_processes/foreign_credit.rb | 138 ++++++++++++----------- spec/adapters/fake_spec.rb | 8 +- spec/apis/v2/credit_transfers_spec.rb | 14 +-- spec/apis/v2/direct_debits_spec.rb | 6 +- 8 files changed, 183 insertions(+), 174 deletions(-) diff --git a/box/apis/v2/credit_transfers.rb b/box/apis/v2/credit_transfers.rb index 3374673c..8ebd1cd4 100644 --- a/box/apis/v2/credit_transfers.rb +++ b/box/apis/v2/credit_transfers.rb @@ -4,6 +4,8 @@ require_relative "api_endpoint" require_relative "../../entities/v2/credit_transfer" +require_relative "../../business_processes/credit" +require_relative "../../business_processes/foreign_credit" require_relative "../../validations/unique_transaction_eref" require_relative "../../validations/length" require_relative "../../errors/business_process_failure" @@ -98,10 +100,10 @@ class CreditTransfers < Grape::API if sanitized_params[:currency] == "EUR" sanitized_params.reject! { |k, _v| k.in? %w[big country_code fee_handling] } # still related to workaround - Credit.v2_create!(current_user, account, sanitized_params) + BusinessProcesses::Credit.v2_create!(current_user, account, sanitized_params) else sanitized_params.reject! { |k, _v| k.in? %w[urgent] } # still related to workaround - ForeignCredit.v2_create!(current_user, account, sanitized_params) + BusinessProcesses::ForeignCredit.v2_create!(current_user, account, sanitized_params) end {message: "Credit transfer has been initiated successfully!"} diff --git a/box/apis/v2/direct_debits.rb b/box/apis/v2/direct_debits.rb index 93955fe5..7dbd31a5 100644 --- a/box/apis/v2/direct_debits.rb +++ b/box/apis/v2/direct_debits.rb @@ -4,6 +4,7 @@ require_relative "api_endpoint" require_relative "../../entities/v2/direct_debit" +require_relative "../../business_processes/direct_debit" require_relative "../../validations/unique_transaction_eref" require_relative "../../validations/length" require_relative "../../errors/business_process_failure" @@ -90,7 +91,7 @@ class DirectDebits < Grape::API post do account = current_organization.find_account!(params[:account]) - DirectDebit.v2_create!(current_user, account, declared(params)) + BusinessProcesses::DirectDebit.v2_create!(current_user, account, declared(params)) {message: "Direct debit has been initiated successfully!"} end diff --git a/box/business_processes/credit.rb b/box/business_processes/credit.rb index 5181d269..cc14824b 100644 --- a/box/business_processes/credit.rb +++ b/box/business_processes/credit.rb @@ -8,57 +8,59 @@ require_relative "../queue" module Box - class Credit - def self.create!(account, params, user) - sct = SEPA::CreditTransfer.new(account.credit_pain_attributes_hash).tap do |credit| - credit.message_identification = "EBICS-BOX/#{SecureRandom.hex(11).upcase}" - credit.add_transaction( - name: params[:name], - bic: params[:bic], - iban: params[:iban], - amount: params[:amount] / 100.0, - reference: params[:eref], - remittance_information: params[:remittance_information], - requested_date: Time.at(params[:requested_date]).to_date, - batch_booking: false, - service_level: params[:service_level] - ) - end + module BusinessProcesses + class Credit + def self.create!(account, params, user) + sct = SEPA::CreditTransfer.new(account.credit_pain_attributes_hash).tap do |credit| + credit.message_identification = "EBICS-BOX/#{SecureRandom.hex(11).upcase}" + credit.add_transaction( + name: params[:name], + bic: params[:bic], + iban: params[:iban], + amount: params[:amount] / 100.0, + reference: params[:eref], + remittance_information: params[:remittance_information], + requested_date: Time.at(params[:requested_date]).to_date, + batch_booking: false, + service_level: params[:service_level] + ) + end - if sct.valid? - Queue.execute_credit( - account_id: account.id, - user_id: user.id, - payload: Base64.strict_encode64(sct.to_xml("pain.001.001.03")), - eref: params[:eref], - currency: "EUR", - amount: params[:amount], - metadata: { - **params.slice(:name, :iban, :bic, :reference), - execution_date: params[:execution_date]&.iso8601 - } - ) - else - raise Box::BusinessProcessFailure, sct.errors + if sct.valid? + Queue.execute_credit( + account_id: account.id, + user_id: user.id, + payload: Base64.strict_encode64(sct.to_xml("pain.001.001.03")), + eref: params[:eref], + currency: "EUR", + amount: params[:amount], + metadata: { + **params.slice(:name, :iban, :bic, :reference), + execution_date: params[:execution_date]&.iso8601 + } + ) + else + raise Box::BusinessProcessFailure, sct.errors + end + rescue ArgumentError => e + # TODO: Will be fixed upstream in the sepa_king gem by us + raise Box::BusinessProcessFailure.new({base: e.message}, "Invalid data") end - rescue ArgumentError => e - # TODO: Will be fixed upstream in the sepa_king gem by us - raise Box::BusinessProcessFailure.new({base: e.message}, "Invalid data") - end - def self.v2_create!(user, account, params) - # EBICS requires a unix timestamp - params[:requested_date] = params[:execution_date].to_time.to_i + def self.v2_create!(user, account, params) + # EBICS requires a unix timestamp + params[:requested_date] = params[:execution_date].to_time.to_i - # Transform a few params - params[:amount] = params[:amount_in_cents] - params[:eref] = params[:end_to_end_reference] - params[:remittance_information] = params[:reference] + # Transform a few params + params[:amount] = params[:amount_in_cents] + params[:eref] = params[:end_to_end_reference] + params[:remittance_information] = params[:reference] - # Set urgent flag or fall back to SEPA - params[:service_level] = params[:urgent] ? "URGP" : "SEPA" + # Set urgent flag or fall back to SEPA + params[:service_level] = params[:urgent] ? "URGP" : "SEPA" - create!(account, params, user) + create!(account, params, user) + end end end end diff --git a/box/business_processes/direct_debit.rb b/box/business_processes/direct_debit.rb index 5701eeca..83a58b25 100644 --- a/box/business_processes/direct_debit.rb +++ b/box/business_processes/direct_debit.rb @@ -8,55 +8,57 @@ require_relative "../queue" module Box - class DirectDebit - def self.create!(account, params, user) - sdd = SEPA::DirectDebit.new(account.pain_attributes_hash).tap do |debit| - debit.message_identification = "EBICS-BOX/#{SecureRandom.hex(11).upcase}" - debit.add_transaction( - name: params[:name], - bic: params[:bic], - iban: params[:iban], - amount: params[:amount] / 100.0, - instruction: params[:instruction], - mandate_id: params[:mandate_id], - mandate_date_of_signature: Time.at(params[:mandate_signature_date]).to_date, - local_instrument: params[:instrument], - sequence_type: params[:sequence_type], - reference: params[:eref], - remittance_information: params[:remittance_information], - requested_date: Time.at(params[:requested_date]).to_date, - batch_booking: true - ) - end + module BusinessProcesses + class DirectDebit + def self.create!(account, params, user) + sdd = SEPA::DirectDebit.new(account.pain_attributes_hash).tap do |debit| + debit.message_identification = "EBICS-BOX/#{SecureRandom.hex(11).upcase}" + debit.add_transaction( + name: params[:name], + bic: params[:bic], + iban: params[:iban], + amount: params[:amount] / 100.0, + instruction: params[:instruction], + mandate_id: params[:mandate_id], + mandate_date_of_signature: Time.at(params[:mandate_signature_date]).to_date, + local_instrument: params[:instrument], + sequence_type: params[:sequence_type], + reference: params[:eref], + remittance_information: params[:remittance_information], + requested_date: Time.at(params[:requested_date]).to_date, + batch_booking: true + ) + end - if sdd.valid? - Queue.execute_debit( - account_id: account.id, - user_id: user.id, - payload: Base64.strict_encode64(sdd.to_xml("pain.008.001.02")), - amount: params[:amount], - eref: params[:eref], - instrument: params[:instrument] - ) - else - raise Box::BusinessProcessFailure, sdd.errors + if sdd.valid? + Queue.execute_debit( + account_id: account.id, + user_id: user.id, + payload: Base64.strict_encode64(sdd.to_xml("pain.008.001.02")), + amount: params[:amount], + eref: params[:eref], + instrument: params[:instrument] + ) + else + raise Box::BusinessProcessFailure, sdd.errors + end + rescue ArgumentError => e + # TODO: Will be fixed upstream in the sepa_king gem by us + raise Box::BusinessProcessFailure.new({base: e.message}, "Invalid data") end - rescue ArgumentError => e - # TODO: Will be fixed upstream in the sepa_king gem by us - raise Box::BusinessProcessFailure.new({base: e.message}, "Invalid data") - end - def self.v2_create!(user, account, params) - # EBICS requires a unix timestamp - params[:requested_date] = params[:execution_date].to_time.to_i + def self.v2_create!(user, account, params) + # EBICS requires a unix timestamp + params[:requested_date] = params[:execution_date].to_time.to_i - # Transform a few params - params[:amount] = params[:amount_in_cents] - params[:eref] = params[:end_to_end_reference] - params[:remittance_information] = params[:reference] + # Transform a few params + params[:amount] = params[:amount_in_cents] + params[:eref] = params[:end_to_end_reference] + params[:remittance_information] = params[:reference] - # Execute v1 method - create!(account, params, user) + # Execute v1 method + create!(account, params, user) + end end end end diff --git a/box/business_processes/foreign_credit.rb b/box/business_processes/foreign_credit.rb index efcd1a53..2c00c9f6 100644 --- a/box/business_processes/foreign_credit.rb +++ b/box/business_processes/foreign_credit.rb @@ -5,88 +5,90 @@ require_relative "../errors/business_process_failure" module Box - class ForeignCredit - class Payload < OpenStruct - def sender - KingDta::Account.new( - owner_name: account.name, - bank_number: account.bank_number, - owner_country_code: account.bank_country_code, - bank_account_number: account.bank_account_number - ) - end + module BusinessProcesses + class ForeignCredit + class Payload < OpenStruct + def sender + KingDta::Account.new( + owner_name: account.name, + bank_number: account.bank_number, + owner_country_code: account.bank_country_code, + bank_account_number: account.bank_account_number + ) + end - def receiver - KingDta::Account.new( - bank_bic: params[:bic], - owner_name: params[:name], - owner_country_code: params[:country_code], - ** account_number - ) - end + def receiver + KingDta::Account.new( + bank_bic: params[:bic], + owner_name: params[:name], + owner_country_code: params[:country_code], + ** account_number + ) + end - def account_number - if /[A-Z]{2}/.match?(params[:iban]) - {bank_iban: params[:iban]} - else - {bank_account_number: params[:iban]} + def account_number + if /[A-Z]{2}/.match?(params[:iban]) + {bank_iban: params[:iban]} + else + {bank_account_number: params[:iban]} + end end - end - def amount - params[:amount] / 100.0 - end + def amount + params[:amount] / 100.0 + end - def fee_handling - { - split: "00", - sender: "01", - receiver: "02" - }[params[:fee_handling]] - end + def fee_handling + { + split: "00", + sender: "01", + receiver: "02" + }[params[:fee_handling]] + end - def booking - KingDta::Booking.new(receiver, amount, params[:eref], nil, params[:currency]).tap do |booking| - booking.payment_type = "00" - booking.charge_bearer_code = fee_handling + def booking + KingDta::Booking.new(receiver, amount, params[:eref], nil, params[:currency]).tap do |booking| + booking.payment_type = "00" + booking.charge_bearer_code = fee_handling + end end - end - def create - azv = KingDta::Dtazv.new(params[:execution_date]) - azv.account = sender - azv.add(booking) + def create + azv = KingDta::Dtazv.new(params[:execution_date]) + azv.account = sender + azv.add(booking) - azv.create + azv.create + end end - end - def self.v2_create!(user, account, params) - params[:requested_date] = params[:execution_date].to_time.to_i + def self.v2_create!(user, account, params) + params[:requested_date] = params[:execution_date].to_time.to_i - # Transform a few params - params[:amount] = params[:amount_in_cents] - params[:eref] = params[:end_to_end_reference] - params[:remittance_information] = params[:reference] + # Transform a few params + params[:amount] = params[:amount_in_cents] + params[:eref] = params[:end_to_end_reference] + params[:remittance_information] = params[:reference] - payload = Payload.new(account: account, params: params) + payload = Payload.new(account: account, params: params) - Queue.execute_credit( - account_id: account.id, - user_id: user.id, - payload: Base64.strict_encode64(payload.create), - eref: params[:eref], - currency: params[:currency], - amount: params[:amount], - metadata: { - **params.slice(:name, :iban, :bic, :reference, :country_code), - execution_date: params[:execution_date]&.iso8601, - fee_handling: params[:fee_handling]&.to_s - } - ) - rescue ArgumentError => e - # TODO: Will be fixed upstream in the sepa_king gem by us - raise Box::BusinessProcessFailure.new({base: e.message}, "Invalid data") + Queue.execute_credit( + account_id: account.id, + user_id: user.id, + payload: Base64.strict_encode64(payload.create), + eref: params[:eref], + currency: params[:currency], + amount: params[:amount], + metadata: { + **params.slice(:name, :iban, :bic, :reference, :country_code), + execution_date: params[:execution_date]&.iso8601, + fee_handling: params[:fee_handling]&.to_s + } + ) + rescue ArgumentError => e + # TODO: Will be fixed upstream in the sepa_king gem by us + raise Box::BusinessProcessFailure.new({base: e.message}, "Invalid data") + end end end end diff --git a/spec/adapters/fake_spec.rb b/spec/adapters/fake_spec.rb index cfc0b517..426b0cf0 100644 --- a/spec/adapters/fake_spec.rb +++ b/spec/adapters/fake_spec.rb @@ -53,7 +53,7 @@ module Adapters end context "auto accept any credits and create statements" do - let(:run_job) { Credit.create!(account, valid_payload.merge(amount: 100_00), user) } + let(:run_job) { BusinessProcesses::Credit.create!(account, valid_payload.merge(amount: 100_00), user) } before do Sidekiq::Testing.inline! end @@ -78,7 +78,7 @@ module Adapters end context "amounts" do - let(:run_job) { Credit.create!(account, valid_payload.merge(amount: 251_995), user) } + let(:run_job) { BusinessProcesses::Credit.create!(account, valid_payload.merge(amount: 251_995), user) } it "sets correct statement account" do run_job @@ -108,7 +108,7 @@ module Adapters end context "auto accept any direct debits and create statements" do - let(:run_job) { DirectDebit.create!(account, valid_payload.merge(amount: 100_00), user) } + let(:run_job) { BusinessProcesses::DirectDebit.create!(account, valid_payload.merge(amount: 100_00), user) } before do Sidekiq::Testing.inline! end @@ -138,7 +138,7 @@ module Adapters end context "amounts" do - let(:run_job) { DirectDebit.create!(account, valid_payload.merge(amount: 251_995), user) } + let(:run_job) { BusinessProcesses::DirectDebit.create!(account, valid_payload.merge(amount: 251_995), user) } it "sets correct statement account" do run_job diff --git a/spec/apis/v2/credit_transfers_spec.rb b/spec/apis/v2/credit_transfers_spec.rb index dc288c91..e26aaa39 100644 --- a/spec/apis/v2/credit_transfers_spec.rb +++ b/spec/apis/v2/credit_transfers_spec.rb @@ -263,12 +263,12 @@ module Box end it "triggers a credit transfer" do - expect(Credit).to receive(:create!) + expect(BusinessProcesses::Credit).to receive(:create!) post "/credit_transfers", valid_attributes, TestHelpers::VALID_HEADERS end it "triggers a credit transfer without bic" do - expect(Credit).to receive(:create!) + expect(BusinessProcesses::Credit).to receive(:create!) post "/credit_transfers", valid_attributes.except(:bic), TestHelpers::VALID_HEADERS end @@ -278,7 +278,7 @@ module Box end it "transforms parameters so they are understood by credit business process" do - expect(Credit).to receive(:create!).with(account, anything, user) + expect(BusinessProcesses::Credit).to receive(:create!).with(account, anything, user) post "/credit_transfers", valid_attributes, TestHelpers::VALID_HEADERS end @@ -290,14 +290,14 @@ module Box end it "ignores fee_handling & country_code flag" do - allow(Credit).to receive(:v2_create!).and_return(true) + allow(BusinessProcesses::Credit).to receive(:v2_create!).and_return(true) post "/credit_transfers", valid_attributes.merge(fee_handling: :split, country_code: "FooBar"), TestHelpers::VALID_HEADERS expected_attributes = valid_attributes .merge(reference: nil, execution_date: Date.today, urgent: false, currency: "EUR") .stringify_keys - expect(Credit).to have_received(:v2_create!).with(anything, anything, expected_attributes) + expect(BusinessProcesses::Credit).to have_received(:v2_create!).with(anything, anything, expected_attributes) end context "foreign currency" do @@ -312,14 +312,14 @@ module Box end it "ignores urgent flag" do - allow(ForeignCredit).to receive(:v2_create!).and_return(true) + allow(BusinessProcesses::ForeignCredit).to receive(:v2_create!).and_return(true) post "/credit_transfers", valid_attributes_foreign.merge(urgent: true), TestHelpers::VALID_HEADERS expected_attributes = valid_attributes_foreign .merge(reference: nil, execution_date: Date.today, fee_handling: :split) .stringify_keys - expect(ForeignCredit).to have_received(:v2_create!).with(anything, anything, expected_attributes) + expect(BusinessProcesses::ForeignCredit).to have_received(:v2_create!).with(anything, anything, expected_attributes) end end end diff --git a/spec/apis/v2/direct_debits_spec.rb b/spec/apis/v2/direct_debits_spec.rb index 35c57ba7..9056a1a8 100644 --- a/spec/apis/v2/direct_debits_spec.rb +++ b/spec/apis/v2/direct_debits_spec.rb @@ -248,17 +248,17 @@ module Box end it "triggers a debit transfer" do - expect(DirectDebit).to receive(:create!) + expect(BusinessProcesses::DirectDebit).to receive(:create!) post "/direct_debits", valid_attributes, valid_debit_headers end it "triggers a debit transfer without bic" do - expect(DirectDebit).to receive(:create!) + expect(BusinessProcesses::DirectDebit).to receive(:create!) post "/direct_debits", valid_attributes.except(:bic), valid_debit_headers end it "transforms parameters so they are understood by debit business process" do - expect(DirectDebit).to receive(:create!).with(account, anything, user) + expect(BusinessProcesses::DirectDebit).to receive(:create!).with(account, anything, user) post "/direct_debits", valid_attributes, valid_debit_headers end