Compare commits

..

No commits in common. "main" and "v0.0.4" have entirely different histories.
main ... v0.0.4

6 changed files with 31 additions and 4385 deletions

9
.envrc
View file

@ -1,9 +0,0 @@
#!/usr/bin/env bash
# the shebang is ignored, but nice for editors
if type -P lorri &>/dev/null; then
eval "$(lorri direnv)"
else
echo 'while direnv evaluated .envrc, could not find the command "lorri" [https://github.com/nix-community/lorri]'
use nix
fi

View file

@ -1,39 +0,0 @@
function parse(data) {
if (typeof data !== 'object' || data === null) {
throw new Error(`Bad Data, expected an object, got: ${data === null ? null : typeof data}`);
}
if ((typeof data.title) !== 'string') {
throw new Error(`Bad Title: ${data.title}`);
}
if (!['string', 'number'].includes(typeof data.amount)) {
throw new Error(`Bad amount: ${data.amount}`);
}
if (typeof data.amount === 'string') {
const value = Number(data.amount.replaceAll(/[^\d]/g, ''));
data.amount = Number.isNaN(value) ? 0 : value;
}
return data;
}
function makeTransaction(data, account) {
return {
account,
date: new Date().toLocaleDateString('sv-SE'), // "YYYY-MM-DD"
payee_name: data.title,
// Actual considers this an integer representing a decimal value so the last two digits of the integer
// are placed after the decimal point (unclear why but okay...)
// It has to be negative or actual will think it's a deposit
amount: data.amount * -100,
cleared: true,
};
}
module.exports = {
makeTransaction,
parse
};

View file

@ -1,49 +0,0 @@
const { describe, expect, test } = require('@jest/globals');
const { parse } = require('./helpers');
describe(parse.name, () => {
const TEST_DATA = {
title: 'test',
amount: 1000,
};
[
['number title', { ...TEST_DATA, title: 11 }],
['boolean title', { ...TEST_DATA, title: false }],
['object title', { ...TEST_DATA, title: {} }],
['null title', { ...TEST_DATA, title: null }],
['boolean amount', { ...TEST_DATA, amount: true }],
['object amount', { ...TEST_DATA, amount: {} }],
].forEach(([tag, badData]) => {
test(`it rejects ${tag} as bad data`, () => {
expect(() => parse(badData)).toThrow();
});
});
test('it accepts a regular value', () => {
let result;
expect(() => {
result = parse(TEST_DATA);
}).not.toThrow();
expect(result).toEqual(TEST_DATA);
});
test('it parses yen', () => {
let result;
expect(() => {
result = parse({ title: 'test', amount: '¥3,746'});
}).not.toThrow();
expect(result).toEqual({
amount: 3746,
title: 'test',
});
});
test('it strips out non-number values from strings', () => {
let result;
expect(() => {
result = parse({ title: 'test', amount: '¥3,7~4j6.1t1'});
}).not.toThrow();
expect(result).toEqual({
amount: 374611,
title: 'test',
});
});
});

4259
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,17 +1,10 @@
{
"type": "module",
"name": "reporter.js",
"version": "0.0.1",
"scripts": {
"test": "jest"
},
"dependencies": {
"@actual-app/api": "^26.3.0",
"async-lock": "^1.4.1",
"cors": "^2.8.6",
"express": "^5.2.1"
},
"devDependencies": {
"jest": "^30.3.0"
}
}

View file

@ -1,9 +1,6 @@
// Expects a JSON config file to be passed in the env variable CONFIG_FILE.
// The file should contain:
// "dataDir": path
// "serverURL": where to point the requests
// "password": The password for logging in
// "budgetId": The ID of the budget containing the account to write transactions into.
// "accountId": UUID for the account to add transactions to.
import express from 'express'
@ -11,22 +8,31 @@ import AsyncLock from 'async-lock';
import cors from 'cors'
import api from '@actual-app/api';
import fs from 'fs';
import { makeTransaction, parse } from './helpers.cjs';
async function addTransaction(config, transaction) {
try {
await api.init({
dataDir: config.dataDir,
serverURL: config.serverURL,
password: config.password,
});
await api.downloadBudget(config.budgetId);
await api.sync();
await api.addTransactions(config.accountId, [transaction]);
} finally {
await api.shutdown();
function parse(data) {
if (typeof data !== 'object' || data === null) {
throw new Error('Bad Data');
}
if (typeof data.title !== 'string') {
throw new Error('Bad Title');
}
if (typeof data.amount !== 'number') {
throw new Error('Bad amount');
}
return data;
}
function makeTransaction(data, account) {
return {
account,
date: new Date().toLocaleDateString('sv-SE'), // "YYYY-MM-DD"
payee_name: data.title,
amount: data.amount,
cleared: true,
};
}
async function init() {
@ -42,16 +48,19 @@ async function init() {
await lock.acquire('transaction', async () => {
console.debug('Executing Transaction');
try {
await api.init({
dataDir: config.dataDir,
});
const transaction = makeTransaction(parse(req.body), config.accountId);
console.log(transaction);
await addTransaction(config, transaction)
await api.addTransactions(config.accountId, [transaction]);
console.log(`Successfully logged "${transaction.payee_name} - ¥${transaction.amount}"`);
return res.json({ result: 'success' });
} catch (e) {
} catch {
console.log('Transaction failed...');
console.log(e);
console.log(e.message);
return res.json({ result: 'failure', error: e.message });
return res.json({ result: 'failure' });
} finally {
await api.shutdown();
}
});
});