From 30be20eac69e99c9fd54c89715f674c12c7bb30d Mon Sep 17 00:00:00 2001 From: Russ Kollmansberger Date: Sun, 24 May 2026 09:11:52 -0500 Subject: [PATCH] Add update-investments.js --- update-investments.js | 61 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 update-investments.js diff --git a/update-investments.js b/update-investments.js new file mode 100644 index 0000000..ace3c84 --- /dev/null +++ b/update-investments.js @@ -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); + } + } +} \ No newline at end of file