Skip to content

Commit

Permalink
log delete bug
Browse files Browse the repository at this point in the history
  • Loading branch information
GraysonAdams committed Mar 31, 2024
1 parent dea5c80 commit 2f03cd9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
22 changes: 12 additions & 10 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -46,9 +51,9 @@ import { match } from "assert";

const pendingTransactions = amexAccount.pendingTransactions
? await convertPendingTransactions(
amexAccount.pendingTransactions,
ynabAccount.id
)
amexAccount.pendingTransactions,
ynabAccount.id
)
: [];

ynabAccount.queuedTransactions = [
Expand All @@ -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();
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -244,7 +246,7 @@ import { match } from "assert";
console.error("Unable to update stale transaction", e);
}
} else {
await deleteTransaction(transaction.id);
await deleteTransaction(transaction);
}
}

Expand All @@ -254,7 +256,7 @@ import { match } from "assert";
transaction
)}`
);
await deleteTransaction(transaction.id);
await deleteTransaction(transaction);
}

console.log(
Expand Down
13 changes: 11 additions & 2 deletions ynab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<YNABAccount, "last_reconciled_at"> {
last_reconciled_at?: Date;
Expand All @@ -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<Account[]> => {
Expand Down

0 comments on commit 2f03cd9

Please sign in to comment.