mirror of
https://github.com/internetee/registry.git
synced 2025-06-06 20:55:44 +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
|
class BankTransaction < ApplicationRecord
|
||||||
include Versions
|
include Versions
|
||||||
|
include TransactionPaidInvoices
|
||||||
belongs_to :bank_statement
|
belongs_to :bank_statement
|
||||||
has_one :account_activity
|
has_one :account_activity
|
||||||
|
|
||||||
|
@ -17,24 +18,6 @@ class BankTransaction < ApplicationRecord
|
||||||
account_activity.invoice
|
account_activity.invoice
|
||||||
end
|
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
|
def registrar
|
||||||
@registrar ||= Invoice.find_by(reference_no: parsed_ref_number)&.buyer
|
@registrar ||= Invoice.find_by(reference_no: parsed_ref_number)&.buyer
|
||||||
end
|
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
|
def test_cannot_create_new_invoice_if_transaction_binded_to_paid_invoice
|
||||||
assert_not @invoice.paid?
|
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)
|
@invoice.update(account_activity: @account_activity, total: @payment_amount)
|
||||||
assert @invoice.paid?
|
assert @invoice.paid?
|
||||||
|
|
||||||
|
@ -48,6 +48,20 @@ class ProcessPaymentsTaskTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
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
|
def test_doubles_are_valid
|
||||||
assert Lhv::ConnectApi.method_defined?(:credit_debit_notification_messages)
|
assert Lhv::ConnectApi.method_defined?(:credit_debit_notification_messages)
|
||||||
assert Lhv::ConnectApi::Messages::CreditDebitNotification.method_defined?(:bank_account_iban)
|
assert Lhv::ConnectApi::Messages::CreditDebitNotification.method_defined?(:bank_account_iban)
|
||||||
|
@ -108,7 +122,7 @@ class ProcessPaymentsTaskTest < ActiveSupport::TestCase
|
||||||
run_task
|
run_task
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_no_changes -> { registrar.invoices.count } do
|
assert_changes -> { registrar.invoices.count } do
|
||||||
run_task
|
run_task
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue