2024-11-15 22:55:53 +01:00
|
|
|
import * as actual from "@actual-app/api"
|
|
|
|
import {
|
|
|
|
ACTUAL_DATA_DIR,
|
|
|
|
ACTUAL_PASSWORD,
|
|
|
|
ACTUAL_SERVER_URL,
|
2024-12-23 16:47:07 +01:00
|
|
|
ACTUAL_SYNC_ID,
|
2025-02-09 13:01:34 +01:00
|
|
|
} from "@/config.ts"
|
2024-11-17 22:27:29 +01:00
|
|
|
import type { TransactionEntity } from "@actual-app/api/@types/loot-core/types/models"
|
|
|
|
import { type UUID } from "node:crypto"
|
2025-02-09 12:35:08 +01:00
|
|
|
import logger from "@common/logger.ts"
|
2024-11-17 22:27:29 +01:00
|
|
|
|
|
|
|
export interface Actual {
|
|
|
|
importTransactions: (
|
|
|
|
accountId: UUID,
|
2025-02-02 12:37:43 +01:00
|
|
|
transactions: Iterable<ActualTransaction>,
|
2024-11-17 22:27:29 +01:00
|
|
|
) => Promise<ImportTransactionsResponse>
|
|
|
|
|
|
|
|
shutdown: () => Promise<void>
|
|
|
|
}
|
|
|
|
|
2025-01-25 18:01:47 +01:00
|
|
|
export interface ActualTransaction extends TransactionEntity {
|
2025-02-02 12:37:43 +01:00
|
|
|
account: UUID
|
2025-01-25 18:01:47 +01:00
|
|
|
payee_name?: string
|
|
|
|
}
|
|
|
|
|
2024-11-17 22:27:29 +01:00
|
|
|
export interface Message {
|
|
|
|
message: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ImportTransactionsResponse {
|
|
|
|
errors?: Message[]
|
|
|
|
added: number
|
|
|
|
updated: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ActualImpl implements Actual {
|
|
|
|
private constructor() {}
|
|
|
|
|
|
|
|
static async init(): Promise<Actual> {
|
|
|
|
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,
|
|
|
|
})
|
2024-12-23 16:47:07 +01:00
|
|
|
logger.info(`Initialized ActualBudget API for ${ACTUAL_SERVER_URL}`)
|
2025-01-25 18:01:47 +01:00
|
|
|
await this.downloadBudget()
|
2024-11-17 22:27:29 +01:00
|
|
|
return new ActualImpl()
|
|
|
|
}
|
|
|
|
|
|
|
|
async importTransactions(
|
|
|
|
accountId: UUID,
|
2025-02-02 12:37:43 +01:00
|
|
|
transactions: Iterable<ActualTransaction>,
|
2024-11-17 22:27:29 +01:00
|
|
|
): Promise<ImportTransactionsResponse> {
|
2025-01-25 18:01:47 +01:00
|
|
|
return actual.importTransactions(accountId, transactions)
|
2024-11-17 22:27:29 +01:00
|
|
|
}
|
|
|
|
|
2025-01-25 18:01:47 +01:00
|
|
|
async shutdown(): Promise<void> {
|
|
|
|
return actual.shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
private static async downloadBudget(): Promise<void> {
|
|
|
|
await actual.downloadBudget(ACTUAL_SYNC_ID)
|
|
|
|
logger.info(`Downloaded budget`)
|
2024-11-17 22:27:29 +01:00
|
|
|
}
|
|
|
|
}
|