feat: added budget from schedule

This commit is contained in:
2024-07-23 11:41:19 +02:00
commit e51e42efa8
9 changed files with 726 additions and 0 deletions

27
lib/actual.js Normal file
View File

@@ -0,0 +1,27 @@
import api from '@actual-app/api';
import { checkEnv } from './env.js';
checkEnv([
'ACTUAL_URL',
'ACTUAL_PASSWORD',
'ACTUAL_ENCRYPTION',
'ACTUAL_ID'
]);
await api.init({
dataDir: './data',
serverURL: process.env.ACTUAL_URL,
password: process.env.ACTUAL_PASSWORD,
});
await api.downloadBudget(process.env.ACTUAL_ID, {
password: process.env.ACTUAL_ENCRYPTION,
});
export async function getSchedules(select = '*') {
return (await api.runQuery(api.q('schedules').select(select).filter({ completed: false }))).data;
}
export * from '@actual-app/api';

26
lib/dates.js Normal file
View File

@@ -0,0 +1,26 @@
import { add, parse, format } from 'date-fns';
const startNow = new Date();
export function addFromFrequency(date, frequency, f = 'yyyy-MM') {
const toAdd = {};
switch (frequency) {
case 'daily':
toAdd.days = 1;
break;
case 'weekly':
toAdd.weeks = 1;
break;
case 'monthly':
toAdd.months = 1;
break;
case 'yearly':
toAdd.years = 1;
break;
default:
break;
}
return format(add(parse(date, f, startNow), toAdd), f);
}

9
lib/env.js Normal file
View File

@@ -0,0 +1,9 @@
import 'dotenv/config';
export function checkEnv(keys) {
for (const key of keys) {
if (!process.env[key]) {
throw new Error(`${key} is not set`);
}
}
}