Fixes
- Added Actual account ids and log level to .env.example. - Fixed timestamp error by downloading the budget after init. - Hardcoded date in test. - Separate logging file for configurations. - Organized test - More logging in main - Fixed wrong paths for some files - Load a .env.test.local file when running tests Signed-off-by: Martin Berg Alstad <git@martials.no>
This commit is contained in:
@ -3,9 +3,11 @@ import {
|
||||
ACTUAL_DATA_DIR,
|
||||
ACTUAL_PASSWORD,
|
||||
ACTUAL_SERVER_URL,
|
||||
ACTUAL_SYNC_ID,
|
||||
} from "../config.ts"
|
||||
import type { TransactionEntity } from "@actual-app/api/@types/loot-core/types/models"
|
||||
import { type UUID } from "node:crypto"
|
||||
import logger from "@/logger.ts"
|
||||
|
||||
export interface Actual {
|
||||
importTransactions: (
|
||||
@ -38,6 +40,9 @@ export class ActualImpl implements Actual {
|
||||
// This is the password you use to log into the server
|
||||
password: ACTUAL_PASSWORD,
|
||||
})
|
||||
logger.info(`Initialized ActualBudget API for ${ACTUAL_SERVER_URL}`)
|
||||
await actual.downloadBudget(ACTUAL_SYNC_ID)
|
||||
logger.info(`Downloaded budget`)
|
||||
return new ActualImpl()
|
||||
}
|
||||
|
||||
|
8
src/logger.ts
Normal file
8
src/logger.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import pino from "pino"
|
||||
|
||||
/**
|
||||
* / Returns a logging instance with the default log-level "info"
|
||||
*/
|
||||
export default pino({
|
||||
level: process.env.LOG_LEVEL as string || "info",
|
||||
})
|
20
src/main.ts
20
src/main.ts
@ -7,7 +7,7 @@ import {
|
||||
} from "@/bank/sparebank1.ts"
|
||||
import { bankTransactionIntoActualTransaction } from "@/mappings.ts"
|
||||
import { ACTUAL_ACCOUNT_IDS, BANK_ACCOUNT_IDS } from "../config.ts"
|
||||
import logger from "pino"
|
||||
import logger from "./logger.ts"
|
||||
import type { UUID } from "node:crypto"
|
||||
|
||||
// TODO Transports api for pino https://github.com/pinojs/pino/blob/HEAD/docs/transports.md
|
||||
@ -16,7 +16,7 @@ import type { UUID } from "node:crypto"
|
||||
export async function daily(actual: Actual, bank: Bank): Promise<void> {
|
||||
// Fetch transactions from the bank
|
||||
const transactions = await fetchTransactionsFromPastDay(bank)
|
||||
logger().info(`Fetched ${transactions.length} transactions`)
|
||||
logger.info(`Fetched ${transactions.length} transactions`)
|
||||
|
||||
// TODO multiple accounts
|
||||
const accountId = ACTUAL_ACCOUNT_IDS[0] as UUID
|
||||
@ -24,12 +24,14 @@ export async function daily(actual: Actual, bank: Bank): Promise<void> {
|
||||
bankTransactionIntoActualTransaction(transaction, accountId),
|
||||
)
|
||||
|
||||
logger.debug(`Mapped ${JSON.stringify(transactions)} to ${JSON.stringify(actualTransactions)} transactions`)
|
||||
|
||||
// TODO Import transactions into Actual
|
||||
// If multiple accounts, loop over them
|
||||
// Get account ID from mapper
|
||||
|
||||
// TODO TypeError: Cannot read properties of undefined (reading 'timestamp')
|
||||
await actual.importTransactions(accountId, actualTransactions)
|
||||
const response = await actual.importTransactions(accountId, actualTransactions)
|
||||
logger.info(`ImportTransactionsResponse=${JSON.stringify(response)}`)
|
||||
}
|
||||
|
||||
async function fetchTransactionsFromPastDay(
|
||||
@ -41,17 +43,17 @@ async function fetchTransactionsFromPastDay(
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
logger().info("Starting application")
|
||||
logger.info("Starting application")
|
||||
const actual = await ActualImpl.init()
|
||||
logger().info("Initialized Actual Budget API")
|
||||
logger.info("Waiting for CRON job to start")
|
||||
|
||||
cronJobDaily(async () => {
|
||||
logger().info("Running daily job")
|
||||
logger.info("Running daily job")
|
||||
await daily(actual, new Sparebank1Impl())
|
||||
logger().info("Finished daily job")
|
||||
logger.info("Finished daily job")
|
||||
})
|
||||
|
||||
// logger().info("Shutting down")
|
||||
// logger.info("Shutting down")
|
||||
// await actual.shutdown()
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import type { Transaction } from "@/sparebank1.ts"
|
||||
import type { Transaction } from "@/bank/sparebank1.ts"
|
||||
import type { TransactionEntity } from "@actual-app/api/@types/loot-core/types/models"
|
||||
import type { UUID } from "node:crypto"
|
||||
|
||||
|
Reference in New Issue
Block a user