ynab / ynab-sdk-ruby

YNAB API Client for Ruby
https://api.ynab.com
Apache License 2.0
65 stars 12 forks source link

Update Transactions does not take id/import_id as an argument #38

Closed ntdef closed 5 years ago

ntdef commented 5 years ago

The docs for update transactions say that the PATCH will update multiple transactions with changes by transaction id or import id.

However, the API doesn't accept id as a field in the transaction change. I get a 400 back when I try to, say, change the flag color on a group of transactions.

For example, the following doesn't work.

require 'date'
require 'ynab'

def update_transactions
  access_token = ENV['YNAB_ACCESS_TOKEN']
  budget_id = ENV['YNAB_BUDGET_ID']
  account_id = ENV['YNAB_ACCOUNT_ID']

  ynab = YNAB::API.new(access_token)

  since_date = Date.parse("2018-11-01")
  target_color = 'purple'

  transactions_response = ynab.transactions.get_transactions_by_account(
    budget_id, account_id, since_date: since_date)

  my_tx = transactions_response.data.transactions

  updated_tx = my_tx.select{ |x| x.flag_color == target_color }.map do |t|
    { id: t.id, flag_color: "red" }
  end
  puts updated_tx
  ynab.transactions.update_transactions(budget_id, { transactions: updated_tx })
  puts "all done"
end

update_transactions
bradymholt commented 5 years ago

Hello @ntdef 👋!

Thanks for this issue - I am almost certain you are receiving a 400 response because you are not providing complete transaction resources in the transactions array (only sending up { id: t.id, flag_color: "red" }). The PATCH is on the collection, not the resources themselves. So, you are PATCHing, or modifying the collection of transactions but the data for the individual transaction resources must be complete (i.e. have all necessary data).

In https://github.com/ynab/ynab-sdk-ruby/pull/40, I've added an example for update_transactions usage to hopefully make this clearer: https://github.com/ynab/ynab-sdk-ruby/blob/master/examples/update-multiple-transactions.rb

ntdef commented 5 years ago

Thanks Brady this is just what I needed.

On Jan 21, 2019, at 7:12 PM, Brady Holt notifications@github.com wrote:

Hello @ntdef 👋!

Thanks for this issue - I am almost certain you are receiving a 400 response because you are not providing complete transaction resources in the transactions array (only sending up { id: t.id, flag_color: "red" }). The PATCH is on the collection, not the resources themselves. So, you are PATCHing, or modifying the collection of transactions but the data for the individual transaction resources must be complete (i.e. have all necessary data).

In #40, I've added an example for update_transactions usage to hopefully make this clearer: https://github.com/ynab/ynab-sdk-ruby/blob/master/examples/update-multiple-transactions.rb

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.