Add update-investments.js
Create and publish Docker image on GHCR / publish-docker-image (push) Has been cancelled

This commit is contained in:
2026-05-24 09:11:52 -05:00
parent 6ed45c4785
commit 30be20eac6
+61
View File
@@ -0,0 +1,61 @@
const api = require('@actual-app/api');
const fs = require('fs');
const readline = require('readline');
const { closeBudget, ensurePayee, getAccountBalance, getAccountNote, getTagValue, openBudget, showPercent, sleep,
getBudgetMonth, getCategoryGroups, getCategories } = require('./utils');
require("dotenv").config();
(async function () {
await openBudget();
const fileStream = fs.createReadStream("/var/www/html/data/investment-data.txt");
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
const payeeId = await ensurePayee('Change in Market Value');
for await (const line of rl) {
const parts = line.split('=');
const accountBalance = await api.getAccountBalance(parts[0]);
const adjustment = (accountBalance - parts[1].replace(/\./g, '')) * -1;
if (adjustment == 0) {
continue;
}
console.log(parts[0] + ' --> ' + parts[1] + ' --> $' + adjustment);
await api.importTransactions(parts[0], [{
date: new Date(),
payee: payeeId,
amount: adjustment,
cleared: true,
reconciled: true,
notes: `Change in Market Value`,
}]);
}
deleteFileIfExists('/var/www/html/data/investment-data.txt');
await closeBudget();
})();
function fixedStringLength(str, length) {
if (str === null) {
str = '';
}
return str.padEnd(length).substring(0, length);
}
async function deleteFileIfExists(filePath) {
try {
await fs.promises.unlink(filePath);
console.log(`File ${filePath} deleted successfully.`);
} catch (err) {
if (err.code === 'ENOENT') {
console.log(`File ${filePath} does not exist, so it was not deleted.`);
} else {
console.error('Error deleting file:', err.message);
}
}
}