From 226c8cb5bdd83e5663283d3d85e2041007619b80 Mon Sep 17 00:00:00 2001 From: Robert Dyer Date: Tue, 6 May 2025 09:56:07 -0500 Subject: [PATCH] clean up how note tags are used --- apply-interest.js | 6 +++--- rentcast.js | 2 +- sync-bitcoin.js | 6 +++--- utils.js | 3 +++ zestimate.js | 6 +++--- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/apply-interest.js b/apply-interest.js index 1609701..024eb46 100644 --- a/apply-interest.js +++ b/apply-interest.js @@ -1,5 +1,5 @@ const api = require('@actual-app/api'); -const { closeBudget, ensurePayee, getAccountBalance, getAccountNote, getLastTransactionDate, openBudget, showPercent } = require('./utils'); +const { closeBudget, ensurePayee, getAccountBalance, getAccountNote, getLastTransactionDate, getTagValue, openBudget, showPercent } = require('./utils'); require("dotenv").config(); (async () => { @@ -17,8 +17,8 @@ require("dotenv").config(); if (note) { if (note.indexOf('interestRate:') > -1 && note.indexOf('interestDay:') > -1) { - let interestRate = parseFloat(note.split('interestRate:')[1].split(' ')[0]); - const interestDay = parseInt(note.split('interestDay:')[1].split(' ')[0]); + let interestRate = parseFloat(getTagValue(note, 'interestRate')); + const interestDay = parseInt(getTagValue(note, 'interestDay')); const interestTransactionDate = new Date(); if (interestTransactionDate.getDate() < interestDay) { diff --git a/rentcast.js b/rentcast.js index a3be362..a06bd45 100644 --- a/rentcast.js +++ b/rentcast.js @@ -44,7 +44,7 @@ async function getRentCast(URL) { let ownership = 1; if (note.indexOf('ownership:') > -1) { - ownership = parseFloat(note.split('ownership:')[1].split(' ')[0]); + ownership = parseFloat(getTagValue(note, 'ownership')); } console.log('Fetching RentCast for account:', account.name); diff --git a/sync-bitcoin.js b/sync-bitcoin.js index ead09d1..62c9e41 100644 --- a/sync-bitcoin.js +++ b/sync-bitcoin.js @@ -1,4 +1,4 @@ -const { closeBudget, openBudget, getTransactions, getAccountNote, getAccountBalance, ensurePayee } = require('./utils'); +const { closeBudget, openBudget, getTransactions, getAccountNote, getAccountBalance, getTagValue, ensurePayee } = require('./utils'); const api = require('@actual-app/api'); function getValueAtPath(obj, path) { @@ -51,10 +51,10 @@ async function getBitcoinPrice() { const cutoffDate = new Date(); cutoffDate.setDate(cutoffDate.getDate() + 1); const note = await getAccountNote(account, cutoffDate); - if (!note || note.indexOf("BTC:") === -1) { + const btc_amount = parseFloat(getTagValue(note, 'BTC', 0.0)); + if (!btc_amount) { continue; } - const btc_amount = note.split('BTC:')[1].split(' ')[0]; const currentBalance = await getAccountBalance(account); const targetBalance = Math.round(bitcoinPrice * btc_amount * 100); const diff = targetBalance - currentBalance; diff --git a/utils.js b/utils.js index 8db0a48..a4b2b15 100644 --- a/utils.js +++ b/utils.js @@ -148,6 +148,9 @@ const Utils = { }, getTagValue: function (note, tag, defaultValue=undefined) { + if (!note) { + return undefined; + } tag += ':' const tagIndex = note.indexOf(tag); if (tagIndex === -1) { diff --git a/zestimate.js b/zestimate.js index 581c685..4201598 100644 --- a/zestimate.js +++ b/zestimate.js @@ -1,6 +1,6 @@ const { Builder, Browser, By, until } = require('selenium-webdriver') const api = require('@actual-app/api'); -const { closeBudget, ensurePayee, getAccountBalance, getAccountNote, openBudget, showPercent, sleep } = require('./utils'); +const { closeBudget, ensurePayee, getAccountBalance, getAccountNote, getTagValue, openBudget, showPercent, sleep } = require('./utils'); require("dotenv").config(); async function getZestimate(URL) { @@ -43,11 +43,11 @@ async function getZestimate(URL) { const note = await getAccountNote(account); if (note && note.indexOf('zestimate:') > -1) { - const URL = note.split('zestimate:')[1].split(' ')[0]; + const URL = getTagValue(note, 'zestimate'); let ownership = 1; if (note.indexOf('ownership:') > -1) { - ownership = parseFloat(note.split('ownership:')[1].split(' ')[0]); + ownership = parseFloat(getTagValue(note, 'ownership')); } console.log('Fetching zestimate for account:', account.name);