Skip to content

Commit

Permalink
Add deposit account limits API resource
Browse files Browse the repository at this point in the history
  • Loading branch information
trevornelson committed Oct 4, 2024
1 parent d89cdfb commit 8007226
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/unit-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
require 'unit-ruby/types/full_name'
require 'unit-ruby/types/hash'
require 'unit-ruby/types/integer'
require 'unit-ruby/types/limit'
require 'unit-ruby/types/phone'
require 'unit-ruby/types/string'

require 'unit-ruby/account_limits'
require 'unit-ruby/ach_payment'
require 'unit-ruby/application_form'
require 'unit-ruby/atm_location'
Expand Down
108 changes: 108 additions & 0 deletions lib/unit-ruby/account_limits.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
module Unit
class AccountLimits < APIResource
path '/accounts'

attribute :ach_debit_daily_limit, Types::Limit, readonly: true
attribute :ach_debit_monthly_limit, Types::Limit, readonly: true

attribute :ach_credit_daily_limit, Types::Limit, readonly: true
attribute :ach_credit_monthly_limit, Types::Limit, readonly: true

attribute :card_daily_withdrawal_limit, Types::Limit, readonly: true
attribute :card_daily_deposit_limit, Types::Limit, readonly: true
attribute :card_daily_purchase_limit, Types::Limit, readonly: true
attribute :card_daily_card_transaction_limit, Types::Limit, readonly: true

attribute :check_deposits_daily_limit, Types::Limit, readonly: true
attribute :check_deposits_monthly_limit, Types::Limit, readonly: true

def self.resource_path(id)
"#{super(id)}/limits"
end

# rubocop:disable Metrics/AbcSize
def update_resource_from_json_api(data)
extract_base_resource_details(data)

clear_attributes!

extract_ach_debit_limits(data[:attributes][:ach])
extract_ach_credit_limits(data[:attributes][:ach])
extract_card_atm_limits(data[:attributes][:card])
extract_card_transaction_limits(data[:attributes][:card])
extract_check_deposit_limits(data[:attributes][:check_deposit])

mark_as_clean!
end
# rubocop:enable Metrics/AbcSize

def extract_base_resource_details(data)
self.id = data[:id]
self.type = data[:type]
self.raw_data = data
self.relationships = data[:relationships]
end

def extract_ach_debit_limits(ach_data)
self.ach_debit_daily_limit = Types::Limit.new(
total: ach_data[:limits][:daily_debit],
used: ach_data[:totals_daily][:debit]
)

self.ach_debit_monthly_limit = Types::Limit.new(
total: ach_data[:limits][:monthly_debit],
used: ach_data[:totals_monthly][:debit]
)
end

def extract_ach_credit_limits(ach_data)
self.ach_credit_daily_limit = Types::Limit.new(
total: ach_data[:limits][:daily_credit],
used: ach_data[:totals_daily][:credit]
)

self.ach_credit_monthly_limit = Types::Limit.new(
total: ach_data[:limits][:monthly_credit],
used: ach_data[:totals_monthly][:credit]
)
end

def extract_card_atm_limits(card_data)
self.card_daily_withdrawal_limit = Types::Limit.new(
total: card_data[:limits][:daily_withdrawal],
used: card_data[:totals_daily][:withdrawal]
)

self.card_daily_deposit_limit = Types::Limit.new(
total: card_data[:limits][:daily_deposit],
used: card_data[:totals_daily][:deposits]
)
end

def extract_card_transaction_limits(card_data)
self.card_daily_purchase_limit = Types::Limit.new(
total: card_data[:limits][:daily_purchase],
used: card_data[:totals_daily][:purchases]
)

self.card_daily_card_transaction_limit = Types::Limit.new(
total: card_data[:limits][:daily_card_transaction],
used: card_data[:totals_daily][:card_transactions]
)
end

def extract_check_deposit_limits(check_deposit_data)
self.check_deposits_daily_limit = Types::Limit.new(
total: check_deposit_data[:limits][:daily],
used: check_deposit_data[:totals_daily]
)

self.check_deposits_monthly_limit = Types::Limit.new(
total: check_deposit_data[:limits][:monthly],
used: check_deposit_data[:totals_monthly]
)
end

include ResourceOperations::Find
end
end
28 changes: 28 additions & 0 deletions lib/unit-ruby/types/limit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Unit
module Types
class Limit
attr_reader :total, :used

def initialize(total:, used:)
@total = total
@used = used || 0
end

def remaining
return nil if total.nil?

total - used
end

def self.cast(val)
return val if val.is_a? self
return nil if val.nil?

new(
total: val[:total],
used: val[:used]
)
end
end
end
end
13 changes: 13 additions & 0 deletions spec/features/deposit_account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@
expect(account.close_reason).to eq nil

expect(account.customers.first.id).to eq customer.id

# These limits do not match production limits
account_limits = Unit::AccountLimits.find(account.id)
expect(account_limits.ach_debit_daily_limit.total).to eq 500_00
expect(account_limits.ach_debit_monthly_limit.total).to eq 20_000_00
expect(account_limits.ach_credit_daily_limit.total).to eq 50_000_00
expect(account_limits.ach_credit_monthly_limit.total).to eq 50_000_00
expect(account_limits.card_daily_withdrawal_limit.total).to eq 5_000_00
expect(account_limits.card_daily_deposit_limit.total).to eq 5_000_00
expect(account_limits.card_daily_purchase_limit.total).to eq 5_000_00
expect(account_limits.card_daily_card_transaction_limit.total).to eq 5_000_00
expect(account_limits.check_deposits_daily_limit.total).to eq 1_000_00
expect(account_limits.check_deposits_monthly_limit.total).to eq 20_000_00
end
end
end
39 changes: 39 additions & 0 deletions spec/types/limit_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'spec_helper'

RSpec.describe Unit::Types::Limit do
let(:total) { 20_000 }
let(:used) { 5_000 }

subject do
described_class.new(
total: total,
used: used
)
end

it 'instantiates an instance of the type' do
expect(subject.total).to eq 20_000
expect(subject.used).to eq 5_000
expect(subject.remaining).to eq 15_000
end

context 'when remaining is nil' do
let(:used) { nil }

it 'returns 0 for used' do
expect(subject.total).to eq 20_000
expect(subject.used).to eq 0
expect(subject.remaining).to eq 20_000
end
end

context 'when total is nil' do
let(:total) { nil }

it 'returns nil' do
expect(subject.total).to eq nil
expect(subject.used).to eq 5_000
expect(subject.remaining).to eq nil
end
end
end

0 comments on commit 8007226

Please sign in to comment.