Skip to content

Commit

Permalink
[BANKING] Extract codes to a dedicated detail within operation
Browse files Browse the repository at this point in the history
  • Loading branch information
lauragilgz committed Oct 30, 2024
1 parent d7dc0b7 commit 8d9242c
Show file tree
Hide file tree
Showing 21 changed files with 192 additions and 129 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ CFONB::OperationDetails.register('FEE', self)
| LC2 | `unstructured_label_2` | Not structured label line 2 (last 70 characters) |
| LCS | `structured_label` | Structured label |
| LIB | `free_label` | Free label |
| MMO | `original_currency`, `original_amount`, `scale`, `exchange_rate`, `exchange_rate_scale` | Amount and currency if it has been converted from a foreign currency |
| MMO | `original_currency`, `original_amount`, `scale`, `exchange_rate`, `exchange_rate_scale` | Amount and currency if it has been converted from a foreign currency. The `original_amount` is unsigned, meaning it is always non-negative. |
| NBE | `creditor` | Name of the creditor or beneficiary |
| NPY | `debtor` | Name of the debtor or payer |
| RCN | `reference`, `purpose` | Client reference and Payment nature/purpose |
Expand All @@ -59,9 +59,6 @@ CFONB::OperationDetails.register('FEE', self)
TODO:
| Detail Code | Attributes | Description |
| --- | --- | --- |
| IPY | `debtor_identifier` | Identifier of the debtor or payer |
| NPO | `ultimate_debtor` | Name of the ultimate debtor or beneficiary |
| NBU | `ultimate_creditor` | Name of the ultimate creditor or payer |
| RET | `unifi_code`, `sit_code`, `payback_label` | Payback informations |
| CBE | `creditor_account` | Account of the creditor or beneficiary |
| BDB | `creditor_bank` | Bank of the creditor or beneficiary |
Expand Down
9 changes: 9 additions & 0 deletions lib/cfonb/details.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module CFONB
class Details
def initialize(reference)
@reference = reference
end
end
end
6 changes: 4 additions & 2 deletions lib/cfonb/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Operation
rejection_code
unavailability_code
value_date
details
].freeze

attr_accessor(*BASE_ATTRIBUTES)
Expand All @@ -33,12 +34,13 @@ def initialize(line)
self.value_date = line.value_date
self.label = line.label.strip
self.number = line.number
self.reference = line.reference
self.details = Details.new(line.reference)
end

def merge_detail(line)
self.raw += "\n#{line.body}"
OperationDetails.for(line)&.apply(self, line)

OperationDetails.for(line)&.apply(details, line)
end

def type_code
Expand Down
4 changes: 3 additions & 1 deletion lib/cfonb/operation_details.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# frozen_string_literal: true

require_relative 'details'

module CFONB
module OperationDetails
@details = {}

def self.register(code, klass)
if klass.const_defined?(:ATTRIBUTES)
Operation.class_eval do
CFONB::Details.class_eval do
attr_accessor(*klass::ATTRIBUTES)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/cfonb/operation_details/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class Base
def self.inherited(base)
base.singleton_class.prepend(
Module.new do
def apply(operation, line)
operation.instance_variable_set(:"@#{line.detail_code}", line.detail)
def apply(details, line)
details.instance_variable_set(:"@#{line.detail_code}", line.detail)

super
end
Expand Down
6 changes: 3 additions & 3 deletions lib/cfonb/operation_details/fee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ module OperationDetails
class FEE < Base
ATTRIBUTES = %i[fee fee_currency].freeze

def self.apply(operation, line)
operation.fee_currency = line.detail[0..2]
def self.apply(details, line)
details.fee_currency = line.detail[0..2]
scale = line.detail[3].to_i

operation.fee = BigDecimal(line.detail[4..17]) / (10**scale)
details.fee = BigDecimal(line.detail[4..17]) / (10**scale)
end

CFONB::OperationDetails.register('FEE', self)
Expand Down
6 changes: 3 additions & 3 deletions lib/cfonb/operation_details/ibe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module OperationDetails
class IBE < Base
ATTRIBUTES = %i[creditor_identifier creditor_identifier_type].freeze

def self.apply(operation, line)
operation.creditor_identifier = line.detail[0..34].strip
operation.creditor_identifier_type = line.detail[35..-1].strip
def self.apply(details, line)
details.creditor_identifier = line.detail[0..34].strip
details.creditor_identifier_type = line.detail[35..-1].strip
end

CFONB::OperationDetails.register('IBE', self)
Expand Down
6 changes: 3 additions & 3 deletions lib/cfonb/operation_details/ipy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module OperationDetails
class IPY < Base
ATTRIBUTES = %i[debtor_identifier debtor_identifier_type].freeze

def self.apply(operation, line)
operation.debtor_identifier = line.detail[0..34].strip
operation.debtor_identifier_type = line.detail[35..-1].strip
def self.apply(details, line)
details.debtor_identifier = line.detail[0..34].strip
details.debtor_identifier_type = line.detail[35..-1].strip
end

CFONB::OperationDetails.register('IPY', self)
Expand Down
10 changes: 4 additions & 6 deletions lib/cfonb/operation_details/lc2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
module CFONB
module OperationDetails
class LC2 < Base
def self.apply(operation, line)
operation.unstructured_label_2 = if operation.unstructured_label_2.nil?
line.detail.strip.to_s
else
"#{operation.unstructured_label_2}\n#{line.detail.strip}"
end
ATTRIBUTES = %i[unstructured_label_2].freeze

def self.apply(details, line)
details.unstructured_label_2 = line.detail.strip
end

CFONB::OperationDetails.register('LC2', self)
Expand Down
10 changes: 4 additions & 6 deletions lib/cfonb/operation_details/lcc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
module CFONB
module OperationDetails
class LCC < Base
def self.apply(operation, line)
operation.unstructured_label = if operation.unstructured_label.nil?
line.detail.strip.to_s
else
"#{operation.unstructured_label}\n#{line.detail.strip}"
end
ATTRIBUTES = %i[unstructured_label].freeze

def self.apply(details, line)
details.unstructured_label = line.detail.strip.to_s
end

CFONB::OperationDetails.register('LCC', self)
Expand Down
10 changes: 3 additions & 7 deletions lib/cfonb/operation_details/lcs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
module CFONB
module OperationDetails
class LCS < Base
def self.apply(operation, line)
formatted_label = line.detail[0..35].strip
ATTRIBUTES = %i[structured_label].freeze

operation.structured_label = if operation.structured_label.nil?
formatted_label.to_s
else
"#{operation.structured_label}\n#{formatted_label}"
end
def self.apply(details, line)
details.structured_label = line.detail[0..35].strip
end

CFONB::OperationDetails.register('LCS', self)
Expand Down
10 changes: 4 additions & 6 deletions lib/cfonb/operation_details/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
module CFONB
module OperationDetails
class LIB < Base
def self.apply(operation, line)
operation.free_label = if operation.free_label.nil?
line.detail.strip.to_s
else
"#{operation.free_label}\n#{line.detail.strip}"
end
ATTRIBUTES = %i[free_label].freeze

def self.apply(details, line)
details.free_label = [details.free_label, line.detail.strip].compact.join("\n")
end

CFONB::OperationDetails.register('LIB', self)
Expand Down
10 changes: 4 additions & 6 deletions lib/cfonb/operation_details/mmo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@ module OperationDetails
class MMO < Base
ATTRIBUTES = %i[original_currency original_amount exchange_rate].freeze

def self.apply(operation, line)
operation.original_currency = line.detail[0..2]

def self.apply(details, line)
details.original_currency = line.detail[0..2]
scale = line.detail[3].to_i
sign = operation.amount <=> 0 # the detail amount is unsigned

operation.original_amount = sign * BigDecimal(line.detail[4..17]) / (10**scale)
details.original_amount = BigDecimal(line.detail[4..17]) / (10**scale)
exchange_rate_value = line.detail[26..29]

return if exchange_rate_value.nil? || exchange_rate_value.strip.empty?

exchange_rate_scale = line.detail[18]
operation.exchange_rate = BigDecimal(exchange_rate_value) / (10**BigDecimal(exchange_rate_scale))
details.exchange_rate = BigDecimal(exchange_rate_value) / (10**BigDecimal(exchange_rate_scale))
end

CFONB::OperationDetails.register('MMO', self)
Expand Down
4 changes: 2 additions & 2 deletions lib/cfonb/operation_details/nbe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module OperationDetails
class NBE < Base
ATTRIBUTES = %i[creditor].freeze

def self.apply(operation, line)
operation.creditor = line.detail.strip
def self.apply(details, line)
details.creditor = line.detail.strip
end

CFONB::OperationDetails.register('NBE', self)
Expand Down
4 changes: 2 additions & 2 deletions lib/cfonb/operation_details/nbu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module OperationDetails
class NBU < Base
ATTRIBUTES = %i[ultimate_creditor].freeze

def self.apply(operation, line)
operation.ultimate_creditor = line.detail.strip
def self.apply(details, line)
details.ultimate_creditor = line.detail.strip
end

CFONB::OperationDetails.register('NBU', self)
Expand Down
4 changes: 2 additions & 2 deletions lib/cfonb/operation_details/npo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module OperationDetails
class NPO < Base
ATTRIBUTES = %i[ultimate_debtor].freeze

def self.apply(operation, line)
operation.ultimate_debtor = line.detail.strip
def self.apply(details, line)
details.ultimate_debtor = line.detail.strip
end

CFONB::OperationDetails.register('NPO', self)
Expand Down
4 changes: 2 additions & 2 deletions lib/cfonb/operation_details/npy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ module OperationDetails
class NPY < Base
ATTRIBUTES = %i[debtor].freeze

def self.apply(operation, line)
operation.debtor = line.detail.strip
def self.apply(details, line)
details.debtor = line.detail.strip
end

CFONB::OperationDetails.register('NPY', self)
Expand Down
8 changes: 4 additions & 4 deletions lib/cfonb/operation_details/rcn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class RCN < Base

ATTRIBUTES = %i[reference purpose].freeze

def self.apply(operation, line)
operation.reference = [
operation.reference,
def self.apply(details, line)
details.reference = [
details.reference,
line.detail[0..34].strip,
].filter_map(&:presence).join(' - ')

operation.purpose = line.detail[35..-1]&.strip
details.purpose = line.detail[35..-1]&.strip
end

CFONB::OperationDetails.register('RCN', self)
Expand Down
6 changes: 3 additions & 3 deletions lib/cfonb/operation_details/ref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class REF < Base

ATTRIBUTES = %i[reference].freeze

def self.apply(operation, line)
operation.reference = [
operation.reference,
def self.apply(details, line)
details.reference = [
details.reference,
line.detail.strip,
].filter_map(&:presence).join(' - ')
end
Expand Down
Loading

0 comments on commit 8d9242c

Please sign in to comment.