Martin Berg Alstad 4367c24fb0
🌟 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
2025-02-13 21:07:30 +01:00

67 lines
1.9 KiB
TypeScript

import * as actual from "@actual-app/api"
import {
ACTUAL_DATA_DIR,
ACTUAL_PASSWORD,
ACTUAL_SERVER_URL,
ACTUAL_SYNC_ID,
} from "@/config.ts"
import logger from "@common/logger.ts"
import type { UUID } from "node:crypto"
import type {
Actual,
ActualTransaction,
ImportTransactionsResponse,
} from "@common/types.ts"
import { createDirIfMissing } from "@/fs.ts"
export class ActualImpl implements Actual {
private constructor() {}
static async init(): Promise<Actual> {
createDirIfMissing(ACTUAL_DATA_DIR)
await actual.init({
// Budget data will be cached locally here, in subdirectories for each file.
dataDir: ACTUAL_DATA_DIR,
// This is the URL of your running server
serverURL: ACTUAL_SERVER_URL,
// This is the password you use to log into the server
password: ACTUAL_PASSWORD,
})
logger.info(`Initialized ActualBudget API for ${ACTUAL_SERVER_URL}`)
await this.downloadBudget()
return new ActualImpl()
}
/**
* Attempts to connect then immediatly shutsdown the connection
* @exception error If connection fails
*/
static async testConnection(): Promise<void> {
let actual: Actual | undefined
logger.info("Testing ActualBudget connection")
try {
actual = await ActualImpl.init()
} finally {
await actual?.shutdown()
logger.info("Finished testing ActualBudget connection")
}
}
async importTransactions(
accountId: UUID,
transactions: Iterable<ActualTransaction>,
): Promise<ImportTransactionsResponse> {
return actual.importTransactions(accountId, transactions)
}
async shutdown(): Promise<void> {
logger.info(`Shutting down ActualBudget API for ${ACTUAL_SERVER_URL}`)
return actual.shutdown()
}
private static async downloadBudget(): Promise<void> {
await actual.downloadBudget(ACTUAL_SYNC_ID)
logger.info(`Downloaded budget`)
}
}