🌟 Refactor
- Moved mappings into Sb1 impl - Moved Actual types to @common - Moved createDirIfMissing to respective functions - Refactored main into multiple functions - Moved create db into Sb1impl and close - Log requests on info
This commit is contained in:
95
packages/sparebank1/sparebank1.ts
Normal file
95
packages/sparebank1/sparebank1.ts
Normal file
@ -0,0 +1,95 @@
|
||||
import {
|
||||
BANK_INITIAL_REFRESH_TOKEN,
|
||||
BANK_OAUTH_CLIENT_ID,
|
||||
BANK_OAUTH_CLIENT_SECRET,
|
||||
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 type { ActualTransaction, Bank, Interval } from "@common/types.ts"
|
||||
import type { TokenResponse } from "@sb1impl/db/types.ts"
|
||||
import { createDirIfMissing } from "@/fs.ts"
|
||||
|
||||
export class Sparebank1Impl implements Bank {
|
||||
private readonly db: Database
|
||||
|
||||
constructor() {
|
||||
createDirIfMissing(DB_DIRECTORY)
|
||||
const databaseFilePath = `${DB_DIRECTORY}/${DB_FILENAME}.sqlite`
|
||||
this.db = createDb(databaseFilePath)
|
||||
}
|
||||
|
||||
async fetchTransactions(
|
||||
interval: Interval,
|
||||
...accountKeys: ReadonlyArray<string>
|
||||
): Promise<ReadonlyArray<ActualTransaction>> {
|
||||
const response = await Transactions.list(
|
||||
await this.getAccessToken(),
|
||||
accountKeys,
|
||||
interval,
|
||||
)
|
||||
const sparebankTransactions = response.transactions
|
||||
return sparebankTransactions.map(bankTransactionIntoActualTransaction)
|
||||
}
|
||||
|
||||
shutdown(): void {
|
||||
this.db.close()
|
||||
}
|
||||
|
||||
private async getAccessToken(): Promise<string> {
|
||||
const accessToken = fetchToken(this.db, "access-token")
|
||||
|
||||
if (accessToken && this.isValidToken(accessToken)) {
|
||||
return accessToken.token
|
||||
}
|
||||
const response = await this.fetchNewTokens()
|
||||
return response.access_token
|
||||
}
|
||||
|
||||
private isValidToken(tokenResponse: TokenResponse): boolean {
|
||||
// TODO make sure the same timezone is used. Db uses UTC
|
||||
return dayjs().isBefore(tokenResponse.expires_at)
|
||||
}
|
||||
|
||||
private async getRefreshToken(): Promise<string> {
|
||||
const tokenResponse = fetchToken(this.db, "refresh-token")
|
||||
|
||||
if (!tokenResponse) {
|
||||
return BANK_INITIAL_REFRESH_TOKEN
|
||||
} else if (this.isValidToken(tokenResponse)) {
|
||||
return tokenResponse.token
|
||||
}
|
||||
logger.warn("Refresh token expired, deleting tokens from database")
|
||||
clearTokens(this.db)
|
||||
throw new Error("Refresh token is expired. Create a new one")
|
||||
}
|
||||
|
||||
private async fetchNewTokens(): Promise<OAuthTokenResponse> {
|
||||
const refreshToken = await this.getRefreshToken()
|
||||
const result = await Oauth.refreshToken(
|
||||
BANK_OAUTH_CLIENT_ID,
|
||||
BANK_OAUTH_CLIENT_SECRET,
|
||||
refreshToken,
|
||||
)
|
||||
|
||||
if (result.status === "failure") {
|
||||
throw new Error(`Failed to fetch refresh token: '${result.data}'`)
|
||||
}
|
||||
const oAuthToken = result.data
|
||||
|
||||
insertTokens(this.db, oAuthToken)
|
||||
return oAuthToken
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user