Skip to content

Commit

Permalink
Merge pull request #86 from fintech-to-ynab/feature/provider-import-ids
Browse files Browse the repository at this point in the history
Using provider ids for import_id
  • Loading branch information
scottrobertson authored Jul 21, 2018
2 parents 6ac87a4 + d99c902 commit 0635fe4
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 29 deletions.
9 changes: 7 additions & 2 deletions app/services/import/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ class Import::Csv
def initialize(path, ynab_account_id)
@path = path
@ynab_account_id = ynab_account_id
@import_id_creator = YNAB::ImportIdCreator.new
end

def import
transactions_to_create = []

::CSV.foreach(@path, headers: true) do |transaction|
transaction = transaction.to_h.symbolize_keys
amount = (transaction[:amount].to_f * 1000).to_i
date = Date.parse(transaction[:date])

transactions_to_create << {
amount: (transaction[:amount].to_f * 1000).to_i,
id: @import_id_creator.import_id(amount, date),
amount: amount,
payee_name: transaction[:description],
date: Date.parse(transaction[:date])
date: date
}
end

Expand Down
10 changes: 7 additions & 3 deletions app/services/import/csv/starling_bank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ class Import::Csv::StarlingBank
def initialize(path, ynab_account_id = ENV['YNAB_STARLING_ACCOUNT_ID'])
@path = path
@ynab_account_id = ynab_account_id
@import_id_creator = YNAB::ImportIdCreator.new
end

def import
transactions_to_create = []

::CSV.foreach(@path, headers: true) do |transaction|

# First row can be blank
next unless transaction['Date'].present?

amount = (transaction['Amount (GBP)'].to_f * 1000).to_i
date = Date.parse(transaction['Date'])

transactions_to_create << {
amount: (transaction['Amount (GBP)'].to_f * 1000).to_i,
id: @import_id_creator.import_id(amount, date),
amount: amount,
payee_name: transaction['Counter Party'],
date: Date.parse(transaction['Date']),
date: date,
description: transaction['Reference']
}
end
Expand Down
1 change: 1 addition & 0 deletions app/services/import/monzo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def import
end

transactions_to_create << {
id: "M#{transaction[:id]}",
amount: transaction[:amount] * 10,
payee_name: payee_name,
date: Time.parse(transaction[:created]).to_date,
Expand Down
1 change: 1 addition & 0 deletions app/services/import/starling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def import
transactions_to_create = []
@starling.transactions.list(params: { from: from, to: Date.today }).each do |transaction|
transactions_to_create << {
id: "S:#{transaction.id}",
amount: (transaction.amount * 1000).to_i,
payee_name: transaction.narrative.strip,
date: transaction.created,
Expand Down
1 change: 1 addition & 0 deletions app/services/import/teller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def import
transactions_to_create = []
transactions.select{|t| Date.parse(t[:date]) <= Date.today }.each do |transaction|
transactions_to_create << {
id: "T#{transaction[:id]}",
amount: (transaction[:amount].to_f * 1000).to_i,
payee_name: transaction[:counterparty],
date: Date.parse(transaction[:date]),
Expand Down
10 changes: 2 additions & 8 deletions app/services/ynab/bulk_transaction_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class YNAB::BulkTransactionCreator
def initialize(transactions, budget_id: nil, account_id: nil)
@transactions = transactions
@client = YNAB::Client.new(ENV['YNAB_ACCESS_TOKEN'], budget_id, account_id)
@import_id_creator = YNAB::ImportIdCreator.new
end

def create
Expand All @@ -24,9 +23,8 @@ def create

transactions_to_create = []
transactions.each do |transaction|

transactions_to_create << {
import_id: @import_id_creator.import_id(transaction[:amount], transaction[:date].to_date),
import_id: transaction[:id].to_s.truncate(36),
account_id: @client.selected_account_id,
payee_name: transaction[:payee_name],
amount: transaction[:amount],
Expand All @@ -38,11 +36,7 @@ def create
end

if transactions_to_create.any?
begin
Rails.logger.info(@client.create_transactions(transactions_to_create))
rescue => e
Rails.logger.error(e.response.body)
end
Rails.logger.info(@client.create_transactions(transactions_to_create))
else
Rails.logger.info(:no_transactions_to_create)
end
Expand Down
4 changes: 0 additions & 4 deletions app/services/ynab/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,10 @@ def create_transaction(id: nil, payee_id: nil, payee_name: nil, amount: nil, cle
import_id: id
}
}).data.transaction
rescue YnabApi::ApiError => e
JSON.parse(e.response_body)
end

def create_transactions(transactions)
client.transactions.bulk_create_transactions(selected_budget_id, { transactions: transactions }).data.bulk
rescue YnabApi::ApiError => e
JSON.parse(e.response_body)
end

def selected_budget_id
Expand Down
6 changes: 0 additions & 6 deletions app/services/ynab/import_id_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ def initialize
@occurence = Hash.new
end

def prefill!(ynab_client, start_date = nil)
ynab_client.transactions(since_date: start_date).each do |transaction|
import_id(transaction.amount.to_i, transaction.date)
end
end

def import_id(amount, date)
key = ['YNAB', amount, date].join(':')
@occurence[key] ||= 0
Expand Down
7 changes: 1 addition & 6 deletions app/services/ynab/transaction_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ def initialize(id: nil, date: nil, amount: nil, payee_name: nil, description: tr
@description = description
@cleared = cleared
@flag = flag

@client = YNAB::Client.new(ENV['YNAB_ACCESS_TOKEN'], budget_id, account_id)
@import_id_creator = YNAB::ImportIdCreator.new
end

def create
Rails.logger.info("Prefilling import ids")
@import_id_creator.prefill!(@client, @date.to_date)

create = @client.create_transaction(
id: @import_id_creator.import_id(@amount, @date.to_date),
id: @id,
payee_name: @payee_name,
amount: @amount,
cleared: @cleared,
Expand Down

0 comments on commit 0635fe4

Please sign in to comment.