Fix YNAB created transactions count

This commit is contained in:
Gabriel Augendre 2020-02-11 23:58:08 +01:00
parent 6bc72468de
commit 01e0d481b6
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4

View file

@ -39,15 +39,23 @@ def push_transactions(transactions, account):
url = f"{BASE_URL}/budgets/{budget_id}/transactions" url = f"{BASE_URL}/budgets/{budget_id}/transactions"
for transaction in transactions: for transaction in transactions:
transaction["account_id"] = section["account"] transaction["account_id"] = section["account"]
data = {"transactions": transactions} data = {"transactions": transactions}
token = section["token"] token = section["token"]
headers = {"Authorization": f"Bearer {token}"} headers = {"Authorization": f"Bearer {token}"}
res = requests.post(url, json=data, headers=headers) res = requests.post(url, json=data, headers=headers)
res.raise_for_status() res.raise_for_status()
data = res.json()["data"] data = res.json()["data"]
created = data["transactions"]
duplicates = data["duplicate_import_ids"] created = set()
for transaction in data["transactions"]:
matched_id = transaction["matched_transaction_id"]
if not matched_id or matched_id not in created:
created.add(transaction["id"])
click.secho(f"{len(created)} transactions created in YNAB.", fg="green", bold=True) click.secho(f"{len(created)} transactions created in YNAB.", fg="green", bold=True)
duplicates = data["duplicate_import_ids"]
if duplicates: if duplicates:
click.secho( click.secho(
f"{len(duplicates)} transactions ignored (duplicates).", fg="yellow" f"{len(duplicates)} transactions ignored (duplicates).", fg="yellow"