Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Commit

Permalink
Set future/pending transactions to uncleared
Browse files Browse the repository at this point in the history
This adds support for sending 'uncleared' in the cleared field that goes
to YNAB if the dumper passes a falsy value in that field.

For the default dumper, this checks if the transaction date is in the
future.

For the N26 dumper, this reuses the pending transaction checks, so if
skip_pending_transactions is disabled those pending transactions are
sent to YNAB, to be hopefully picked up by the transaction matching.
  • Loading branch information
dequis committed Jun 11, 2019
1 parent 8569abd commit 04ed64a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def to_ynab_transaction(transaction)
memo: memo(transaction),
amount: amount(transaction),
is_withdrawal: withdrawal?(transaction),
import_id: import_id(transaction)
import_id: import_id(transaction),
is_cleared: cleared?(transaction),
)
end
# rubocop:enable Metrics/MethodLength
Expand All @@ -43,4 +44,8 @@ def category_id(_transaction)
def normalize_iban(iban)
iban.delete(' ')
end

def cleared?(_transaction)
date(_transaction) < Time.now
end
end
4 changes: 4 additions & 0 deletions lib/dumper/n26.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ def withdrawal?(transaction)
WITHDRAWAL_CATEGORIES.include?(transaction['category'])
end

def cleared?(transaction)
return already_processed?(transaction)
end

def import_id(transaction)
data = [transaction['visibleTS'],
transaction['transactionNature'],
Expand Down
8 changes: 6 additions & 2 deletions lib/transaction_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class TransactionCreator
attr_accessor :account_id, :date, :amount, :payee_name, :payee_id,
:category_name, :category_id, :memo,
:import_id, :is_withdrawal
:import_id, :is_withdrawal, :is_cleared

class <<self
require 'ynab/models/save_transaction'
Expand All @@ -22,7 +22,7 @@ def call(options = {})
memo: memo(options),
import_id: options.fetch(:import_id),
flag_color: flag_color(options),
cleared: 'cleared' # TODO: shouldn't be cleared if date is in the future
cleared: cleared(options),
)
end
# rubocop:enable Metrics/MethodLength
Expand Down Expand Up @@ -76,6 +76,10 @@ def withdrawal?(options)
options.fetch(:is_withdrawal, nil)
end

def cleared(options)
options.fetch(:is_cleared, true) ? "cleared" : "uncleared"
end

def account_payee_id(options)
result = Settings.all['accounts'].find do |account|
payee_iban = payee_iban(options)
Expand Down
19 changes: 18 additions & 1 deletion spec/dumper/n26_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@

describe '.accept?' do
subject(:accept?) { object.accept?(transaction) }
subject(:cleared?) { object.send(:cleared?, transaction) }

context 'when skip_pending_transactions feature is disabled' do
context 'when the transaction is pending' do
Expand All @@ -173,6 +174,10 @@
it 'returns true' do
expect(accept?).to be_truthy
end

it 'the transaction is not cleared' do
expect(cleared?).to be_falsy
end
end

context 'when the transaction is processed' do
Expand All @@ -181,10 +186,14 @@
it 'returns true' do
expect(accept?).to be_truthy
end

it 'the transaction is cleared' do
expect(cleared?).to be_truthy
end
end
end

context 'when skip_pending_transactions feature is disabled' do
context 'when skip_pending_transactions feature is enabled' do
let(:skip_pending_transactions) { true }

context 'when the transaction is pending' do
Expand All @@ -193,6 +202,10 @@
it 'returns false' do
expect(accept?).to be_falsy
end

it 'the transaction is not cleared' do
expect(cleared?).to be_falsy
end
end

context 'when the transaction is processed' do
Expand All @@ -201,6 +214,10 @@
it 'returns true' do
expect(accept?).to be_truthy
end

it 'the transaction is cleared' do
expect(cleared?).to be_truthy
end
end
end
end
Expand Down

0 comments on commit 04ed64a

Please sign in to comment.