Compare commits

..

No commits in common. "080b65854d115e3ad0dc9149699eb6b22c55e068" and "a2c6c55430c6182464ad3963c1e0d56fe9355418" have entirely different histories.

3 changed files with 20 additions and 30 deletions

View File

@ -1,10 +1,10 @@
import { ACTUAL_ACCOUNT_IDS, BANK_ACCOUNT_IDS } from "@/config.ts"
import { toISODateString } from "@common/date.ts"
import dayjs from "dayjs"
import type { ActualTransaction } from "@common/types.ts"
import type { SB1Transaction } from "@sb1/types.ts"
import type { UUID } from "node:crypto"
import dayjs from "dayjs"
import { ACTUAL_ACCOUNT_IDS, BANK_ACCOUNT_IDS } from "@/config.ts"
import logger from "@common/logger.ts"
import { toISODateString } from "@common/date.ts"
import type { SB1Transaction } from "@sb1/types.ts"
import type { ActualTransaction } from "@common/types.ts"
export function bankTransactionIntoActualTransaction(
transaction: SB1Transaction,
@ -18,6 +18,7 @@ export function bankTransactionIntoActualTransaction(
amount: Math.floor(transaction.amount * 100),
date: toISODateString(dayjs(transaction.date)),
payee_name: transaction.cleanedDescription,
// TODO if not cleared or nonUniqueId is 0, rerun later
cleared: isCleared(transaction),
}
}
@ -37,7 +38,9 @@ function getActualAccountId(transcation: SB1Transaction): UUID {
return ACTUAL_ACCOUNT_IDS[i] as UUID
}
}
throw new Error(
const error = new Error(
"Failed to find ActualAccountId, length of BANK_ACCOUNT_IDS and ACTUAL_ACCOUNT_IDS must match",
)
logger.error(error)
throw error
}

View File

@ -1,5 +1,3 @@
import * as Transactions from "@sb1/transactions.ts"
import * as Oauth from "@sb1/oauth.ts"
import {
BANK_INITIAL_REFRESH_TOKEN,
BANK_OAUTH_CLIENT_ID,
@ -7,21 +5,22 @@ import {
DB_DIRECTORY,
DB_FILENAME,
} from "@/config.ts"
import logger from "@common/logger.ts"
import dayjs from "dayjs"
import type { Database } from "better-sqlite3"
import {
clearTokens,
createDb,
fetchToken,
insertTokens,
} from "@sb1impl/db/queries.ts"
import * as Oauth from "@sb1/oauth.ts"
import * as Transactions from "@sb1/transactions.ts"
import { bankTransactionIntoActualTransaction } from "./mappings.ts"
import { createDirIfMissing } from "@/fs.ts"
import logger from "@common/logger.ts"
import dayjs from "dayjs"
import type { OAuthTokenResponse } from "@sb1/types.ts"
import type { ActualTransaction, Bank, Interval } from "@common/types.ts"
import type { TokenResponse } from "@sb1impl/db/types.ts"
import type { OAuthTokenResponse } from "@sb1/types.ts"
import type { Database } from "better-sqlite3"
import { createDirIfMissing } from "@/fs.ts"
export class Sparebank1Impl implements Bank {
private readonly db: Database
@ -32,7 +31,6 @@ export class Sparebank1Impl implements Bank {
this.db = createDb(databaseFilePath)
}
// TODO if not cleared rerun later
async fetchTransactions(
interval: Interval,
...accountKeys: ReadonlyArray<string>
@ -42,15 +40,8 @@ export class Sparebank1Impl implements Bank {
accountKeys,
interval,
)
return response.transactions
.map((transaction) => {
const actualTransaction =
bankTransactionIntoActualTransaction(transaction)
return actualTransaction.cleared === true ? actualTransaction : null
})
.filter((transaction) => transaction !== null)
const sparebankTransactions = response.transactions
return sparebankTransactions.map(bankTransactionIntoActualTransaction)
}
shutdown(): void {

View File

@ -15,6 +15,7 @@ import { Sparebank1Impl } from "@sb1impl/sparebank1.ts"
// TODO move tsx to devDependency. Requires ts support for Node with support for @ alias
// TODO verbatimSyntax in tsconfig, conflicts with jest
// TODO store last fetched date in db, and refetch from that date, if app has been offline for some time
// TODO do not fetch if saturday or sunday
async function main(): Promise<void> {
logger.info("Starting application")
@ -37,11 +38,6 @@ export async function moveTransactions(
)
logger.info(`Fetched ${actualTransactions.length} transactions`)
if (actualTransactions.length === 0) {
logger.debug("No transactions to import, skipping")
return
}
const transactionsByAccount = Object.groupBy(
actualTransactions,
(transaction) => transaction.account,