Add BankAccountConnector to implement bank import methods in plugins
This commit is contained in:
parent
d476993321
commit
5d84156bd8
11 changed files with 281 additions and 22 deletions
155
lib/bank_account_connector.rb
Normal file
155
lib/bank_account_connector.rb
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
class BankAccountConnector
|
||||
|
||||
class TextItem
|
||||
def initialize(text)
|
||||
@text = text
|
||||
end
|
||||
|
||||
def name
|
||||
nil
|
||||
end
|
||||
|
||||
def text
|
||||
@text
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class TextField
|
||||
def initialize(name, value, label)
|
||||
@name = name
|
||||
@value = value
|
||||
@label = label
|
||||
end
|
||||
|
||||
def type
|
||||
nil
|
||||
end
|
||||
|
||||
def name
|
||||
@name
|
||||
end
|
||||
|
||||
def value
|
||||
@value
|
||||
end
|
||||
|
||||
def label
|
||||
@label || @name.to_s
|
||||
end
|
||||
end
|
||||
|
||||
class PasswordField < TextField
|
||||
|
||||
def type
|
||||
:password
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class HiddenField < TextField
|
||||
|
||||
def type
|
||||
:hidden
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
@@registered_classes = Set.new
|
||||
|
||||
def self.register(klass)
|
||||
@@registered_classes.add klass
|
||||
end
|
||||
|
||||
def self.find(iban)
|
||||
@@registered_classes.each do |klass|
|
||||
return klass if klass.handles(iban)
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
def initialize(bank_account)
|
||||
@bank_account = bank_account
|
||||
@auto_submit = nil
|
||||
@controls = []
|
||||
@count = 0
|
||||
end
|
||||
|
||||
def iban
|
||||
@bank_account.iban
|
||||
end
|
||||
|
||||
def auto_submit
|
||||
@auto_submit
|
||||
end
|
||||
|
||||
def controls
|
||||
@controls
|
||||
end
|
||||
|
||||
def count
|
||||
@count
|
||||
end
|
||||
|
||||
def text(data)
|
||||
@controls += [TextItem.new(data)]
|
||||
end
|
||||
|
||||
def wait_with_text(auto_submit, data)
|
||||
@auto_submit = auto_submit
|
||||
@controls += [TextItem.new(data)]
|
||||
end
|
||||
|
||||
def wait_for_app(code)
|
||||
hidden_field :twofactor, code
|
||||
wait_with_text 3000, t('.confirm_app', code: code)
|
||||
nil
|
||||
end
|
||||
|
||||
def text_field(name, value=nil)
|
||||
@controls += [TextField.new(name, value, t(name))]
|
||||
end
|
||||
|
||||
def hidden_field(name, value)
|
||||
@controls += [HiddenField.new(name, value, 'HIDDEN')]
|
||||
end
|
||||
|
||||
def password_field(name, value=nil)
|
||||
@controls += [PasswordField.new(name, value, t(name))]
|
||||
end
|
||||
|
||||
|
||||
def set_balance(amount)
|
||||
@bank_account.balance = amount
|
||||
end
|
||||
|
||||
def continuation_point
|
||||
@bank_account.import_continuation_point
|
||||
end
|
||||
|
||||
def set_continuation_point(data)
|
||||
@bank_account.import_continuation_point = data
|
||||
end
|
||||
|
||||
def update_or_create_transaction(external_id, data={})
|
||||
@bank_account.bank_transactions.where(external_id: external_id).first_or_create.update(data)
|
||||
@count += 1
|
||||
end
|
||||
|
||||
def finish
|
||||
@bank_account.last_import = Time.now
|
||||
@bank_account.save!
|
||||
end
|
||||
|
||||
def load(data)
|
||||
end
|
||||
|
||||
def dump
|
||||
end
|
||||
|
||||
def t(key, args={})
|
||||
return t(".fields.#{key}") unless key.is_a? String
|
||||
I18n.t 'bank_account_connector' + key, args
|
||||
end
|
||||
end
|
||||
31
lib/bank_account_connector_external.rb
Normal file
31
lib/bank_account_connector_external.rb
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
class BankAccountConnectorExternal < BankAccountConnector
|
||||
|
||||
def load(data)
|
||||
@connector = create_connector
|
||||
@connector.load data
|
||||
end
|
||||
|
||||
def dump
|
||||
@connector.dump
|
||||
end
|
||||
|
||||
def connector_import
|
||||
set_balance @connector.balance iban
|
||||
cp = @connector.transactions iban, continuation_point do |t|
|
||||
update_or_create_transaction t[:id], map_transaction(t)
|
||||
end
|
||||
set_continuation_point cp if cp
|
||||
end
|
||||
|
||||
def connector_logout
|
||||
@connector.logout
|
||||
end
|
||||
|
||||
def import(data)
|
||||
return false unless connector_login(data)
|
||||
connector_import
|
||||
connector_logout
|
||||
true
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -67,11 +67,14 @@ namespace :foodsoft do
|
|||
desc "Import and assign bank transactions"
|
||||
task :import_and_assign_bank_transactions => :environment do
|
||||
BankAccount.find_each do |ba|
|
||||
import_method = ba.find_import_method
|
||||
next unless import_method
|
||||
import_count = import_method.call(ba)
|
||||
importer = ba.find_connector
|
||||
next unless importer
|
||||
importer.load nil
|
||||
ok = importer.import nil
|
||||
next unless ok
|
||||
importer.finish
|
||||
assign_count = ba.assign_unlinked_transactions
|
||||
rake_say "#{ba.name}: imported #{import_count}, assigned #{assign_count}"
|
||||
rake_say "#{ba.name}: imported #{importer.count}, assigned #{assign_count}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue