From 2f03cd973dd3bff0fdcf48f33470c83d9a970113 Mon Sep 17 00:00:00 2001 From: graysonvcadams Date: Sun, 31 Mar 2024 09:22:12 +0000 Subject: [PATCH] log delete bug --- index.ts | 22 ++++++++++++---------- ynab.ts | 13 +++++++++++-- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/index.ts b/index.ts index 6b68cfb..4b8f4ac 100644 --- a/index.ts +++ b/index.ts @@ -16,6 +16,11 @@ import { SaveTransaction, TransactionDetail } from "ynab"; import stringSimilarity from "string-similarity"; import { match } from "assert"; + +export const formatTransaction = (t: TransactionDetail | SaveTransaction) => + `${t.account_id}: $${t.amount! / 1000} at ${t.payee_name} on ${t.date}`; + + (async () => { try { const ynabAccounts = await fetchAccounts(); @@ -46,9 +51,9 @@ import { match } from "assert"; const pendingTransactions = amexAccount.pendingTransactions ? await convertPendingTransactions( - amexAccount.pendingTransactions, - ynabAccount.id - ) + amexAccount.pendingTransactions, + ynabAccount.id + ) : []; ynabAccount.queuedTransactions = [ @@ -65,9 +70,6 @@ import { match } from "assert"; console.log(`${ynabAccount.name} may have some transactions imported`); }); - const formatTransaction = (t: TransactionDetail | SaveTransaction) => - `${t.account_id}: $${t.amount! / 1000} at ${t.payee_name} on ${t.date}`; - const unfilteredImportTransactions = readyAccounts .map((ynabAccount) => ynabAccount.queuedTransactions) .flat(); @@ -112,7 +114,7 @@ import { match } from "assert"; const dateMatch = Math.abs( new Date(t.date as string).getTime() - - new Date(existingPendingTransaction.date as string).getTime() + new Date(existingPendingTransaction.date as string).getTime() ) <= 86400 * 3 * 1000; @@ -165,7 +167,7 @@ import { match } from "assert"; if ( existingPendingTransaction.date !== matchedImportTransaction.date || existingPendingTransaction.import_id !== - matchedImportTransaction.import_id + matchedImportTransaction.import_id ) { console.log( `Pending transaction ${formatTransaction( @@ -244,7 +246,7 @@ import { match } from "assert"; console.error("Unable to update stale transaction", e); } } else { - await deleteTransaction(transaction.id); + await deleteTransaction(transaction); } } @@ -254,7 +256,7 @@ import { match } from "assert"; transaction )}` ); - await deleteTransaction(transaction.id); + await deleteTransaction(transaction); } console.log( diff --git a/ynab.ts b/ynab.ts index 61e6e8d..95d22e9 100644 --- a/ynab.ts +++ b/ynab.ts @@ -10,6 +10,7 @@ import ynab, { import titleize from "titleize"; import dateFormat from "dateformat"; import "dotenv/config"; +import { formatTransaction } from "./index.js"; export interface Account extends Omit { last_reconciled_at?: Date; @@ -27,8 +28,16 @@ export const ynabAPI = new ynab.API(apiToken); const ynabAmount = (amount: string) => Math.round(-parseFloat(amount) * 1000); const ynabDateFormat = (date: Date) => dateFormat(date, "yyyy-mm-dd"); -export const deleteTransaction = async (transactionId: string) => { - await ynabAPI.transactions.deleteTransaction(budgetId, transactionId); +export const deleteTransaction = async (transaction: TransactionDetail) => { + try { + await ynabAPI.transactions.deleteTransaction(budgetId, transaction.id); + } catch (e) { + console.error( + "Failed to delete transaction", + formatTransaction(transaction), + e + ); + } }; export const fetchAccounts = async (): Promise => {