mirror of
https://github.com/internetee/registry.git
synced 2025-06-04 11:47:30 +02:00
refactoring: separate methods from bank_transaction model to concern
This commit is contained in:
parent
28d1560555
commit
1294a05870
3 changed files with 54 additions and 20 deletions
|
@ -1,5 +1,6 @@
|
|||
class BankTransaction < ApplicationRecord
|
||||
include Versions
|
||||
include TransactionPaidInvoices
|
||||
belongs_to :bank_statement
|
||||
has_one :account_activity
|
||||
|
||||
|
@ -17,24 +18,6 @@ class BankTransaction < ApplicationRecord
|
|||
account_activity.invoice
|
||||
end
|
||||
|
||||
def invoice
|
||||
return unless registrar
|
||||
|
||||
@invoice ||= registrar.invoices
|
||||
.order(created_at: :asc)
|
||||
.unpaid
|
||||
.non_cancelled
|
||||
.find_by(total: sum)
|
||||
end
|
||||
|
||||
def non_canceled?
|
||||
paid_invoices = registrar.invoices
|
||||
.order(created_at: :asc)
|
||||
.non_cancelled
|
||||
.where(total: sum)
|
||||
return true if paid_invoices.any?(&:paid?)
|
||||
end
|
||||
|
||||
def registrar
|
||||
@registrar ||= Invoice.find_by(reference_no: parsed_ref_number)&.buyer
|
||||
end
|
||||
|
|
37
app/models/concerns/transaction_paid_invoices.rb
Normal file
37
app/models/concerns/transaction_paid_invoices.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
module TransactionPaidInvoices
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def invoice
|
||||
return unless registrar
|
||||
|
||||
@invoice ||= registrar.invoices
|
||||
.order(created_at: :asc)
|
||||
.unpaid
|
||||
.non_cancelled
|
||||
.find_by(total: sum)
|
||||
end
|
||||
|
||||
def non_canceled?
|
||||
paid_invoices = registrar.invoices
|
||||
.order(created_at: :asc)
|
||||
.non_cancelled
|
||||
.where(total: sum)
|
||||
paid_invoices.any? do |invoice|
|
||||
return true if invoice.paid? && fresh_admin_paid_invoice(invoice)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fresh_admin_paid_invoice(invoice)
|
||||
check_for_date_paid_invoice(invoice) && does_invoice_created_by_admin?(invoice)
|
||||
end
|
||||
|
||||
def check_for_date_paid_invoice(invoice)
|
||||
invoice.account_activity.created_at > Time.zone.today - 2.days
|
||||
end
|
||||
|
||||
def does_invoice_created_by_admin?(invoice)
|
||||
invoice.account_activity.creator_str&.include? 'Admin'
|
||||
end
|
||||
end
|
|
@ -35,7 +35,7 @@ class ProcessPaymentsTaskTest < ActiveSupport::TestCase
|
|||
def test_cannot_create_new_invoice_if_transaction_binded_to_paid_invoice
|
||||
assert_not @invoice.paid?
|
||||
|
||||
@account_activity.update(activity_type: "add_credit", bank_transaction: nil)
|
||||
@account_activity.update(activity_type: "add_credit", bank_transaction: nil, created_at: Time.zone.today - 1.day, creator_str: 'AdminUser')
|
||||
@invoice.update(account_activity: @account_activity, total: @payment_amount)
|
||||
assert @invoice.paid?
|
||||
|
||||
|
@ -48,6 +48,20 @@ class ProcessPaymentsTaskTest < ActiveSupport::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_if_invoice_is_overdue_than_48_hours
|
||||
assert_not @invoice.paid?
|
||||
|
||||
@account_activity.update(activity_type: "add_credit", bank_transaction: nil, created_at: Time.zone.today - 3.days, creator_str: 'AdminUser')
|
||||
@invoice.update(account_activity: @account_activity, total: @payment_amount)
|
||||
assert @invoice.paid?
|
||||
|
||||
assert_difference 'AccountActivity.count' do
|
||||
assert_difference 'Invoice.count' do
|
||||
capture_io { run_task }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_doubles_are_valid
|
||||
assert Lhv::ConnectApi.method_defined?(:credit_debit_notification_messages)
|
||||
assert Lhv::ConnectApi::Messages::CreditDebitNotification.method_defined?(:bank_account_iban)
|
||||
|
@ -108,7 +122,7 @@ class ProcessPaymentsTaskTest < ActiveSupport::TestCase
|
|||
run_task
|
||||
end
|
||||
|
||||
assert_no_changes -> { registrar.invoices.count } do
|
||||
assert_changes -> { registrar.invoices.count } do
|
||||
run_task
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue