Compare commits
2 Commits
a2c6c55430
...
080b65854d
Author | SHA1 | Date | |
---|---|---|---|
080b65854d | |||
a0cefcdfa1 |
@ -1,10 +1,10 @@
|
||||
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 dayjs from "dayjs"
|
||||
|
||||
import type { ActualTransaction } from "@common/types.ts"
|
||||
import type { SB1Transaction } from "@sb1/types.ts"
|
||||
import type { UUID } from "node:crypto"
|
||||
|
||||
export function bankTransactionIntoActualTransaction(
|
||||
transaction: SB1Transaction,
|
||||
@ -18,7 +18,6 @@ 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),
|
||||
}
|
||||
}
|
||||
@ -38,9 +37,7 @@ function getActualAccountId(transcation: SB1Transaction): UUID {
|
||||
return ACTUAL_ACCOUNT_IDS[i] as UUID
|
||||
}
|
||||
}
|
||||
const error = new Error(
|
||||
throw new Error(
|
||||
"Failed to find ActualAccountId, length of BANK_ACCOUNT_IDS and ACTUAL_ACCOUNT_IDS must match",
|
||||
)
|
||||
logger.error(error)
|
||||
throw error
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
import * as Transactions from "@sb1/transactions.ts"
|
||||
import * as Oauth from "@sb1/oauth.ts"
|
||||
import {
|
||||
BANK_INITIAL_REFRESH_TOKEN,
|
||||
BANK_OAUTH_CLIENT_ID,
|
||||
@ -5,22 +7,21 @@ 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 type { OAuthTokenResponse } from "@sb1/types.ts"
|
||||
import { createDirIfMissing } from "@/fs.ts"
|
||||
import logger from "@common/logger.ts"
|
||||
import dayjs from "dayjs"
|
||||
|
||||
import type { ActualTransaction, Bank, Interval } from "@common/types.ts"
|
||||
import type { TokenResponse } from "@sb1impl/db/types.ts"
|
||||
import { createDirIfMissing } from "@/fs.ts"
|
||||
import type { OAuthTokenResponse } from "@sb1/types.ts"
|
||||
import type { Database } from "better-sqlite3"
|
||||
|
||||
export class Sparebank1Impl implements Bank {
|
||||
private readonly db: Database
|
||||
@ -31,6 +32,7 @@ export class Sparebank1Impl implements Bank {
|
||||
this.db = createDb(databaseFilePath)
|
||||
}
|
||||
|
||||
// TODO if not cleared rerun later
|
||||
async fetchTransactions(
|
||||
interval: Interval,
|
||||
...accountKeys: ReadonlyArray<string>
|
||||
@ -40,8 +42,15 @@ export class Sparebank1Impl implements Bank {
|
||||
accountKeys,
|
||||
interval,
|
||||
)
|
||||
const sparebankTransactions = response.transactions
|
||||
return sparebankTransactions.map(bankTransactionIntoActualTransaction)
|
||||
|
||||
return response.transactions
|
||||
.map((transaction) => {
|
||||
const actualTransaction =
|
||||
bankTransactionIntoActualTransaction(transaction)
|
||||
|
||||
return actualTransaction.cleared === true ? actualTransaction : null
|
||||
})
|
||||
.filter((transaction) => transaction !== null)
|
||||
}
|
||||
|
||||
shutdown(): void {
|
||||
|
@ -15,7 +15,6 @@ 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")
|
||||
@ -38,6 +37,11 @@ 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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user