fix creating categories for track_investments

This commit is contained in:
Robert Dyer
2024-08-10 18:46:21 -05:00
parent b7c5000883
commit 78b61ec76e
3 changed files with 65 additions and 25 deletions
+5 -2
View File
@@ -41,9 +41,9 @@ INTEREST_PAYEE_NAME="Loan Interest"
# optional, name of the payee for added interest transactions # optional, name of the payee for added interest transactions
INVESTMENT_PAYEE_NAME="Investment" INVESTMENT_PAYEE_NAME="Investment"
# optional, name of the cateogry group for added investment tracking transactions # optional, name of the cateogry group for added investment tracking transactions
INTEREST_CATEGORY_GROUP_NAME="Income" INVESTMENT_CATEGORY_GROUP_NAME="Income"
# optional, name of the category for added investment tracking transactions # optional, name of the category for added investment tracking transactions
INTEREST_CATEGORY_NAME="Investment" INVESTMENT_CATEGORY_NAME="Investment"
``` ```
## Installation ## Installation
@@ -209,6 +209,9 @@ need to update this to add additional notes to look for.
You can optionally change the payee used for the transactions by setting You can optionally change the payee used for the transactions by setting
`INVESTMENT_PAYEE_NAME` in the `.env` file. `INVESTMENT_PAYEE_NAME` in the `.env` file.
You can optionally change the category group used for the transactions by setting
`INVESTMENT_CATEGORY_GROUP_NAME` in the `.env` file.
You can optionally change the category used for the transactions by setting You can optionally change the category used for the transactions by setting
`INVESTMENT_CATEGORY_NAME` in the `.env` file. `INVESTMENT_CATEGORY_NAME` in the `.env` file.
+3 -2
View File
@@ -1,7 +1,7 @@
const api = require('@actual-app/api'); const api = require('@actual-app/api');
const fs = require('fs'); const fs = require('fs');
const readline = require('readline-sync'); const readline = require('readline-sync');
const { closeBudget, ensureCategory, ensurePayee, getAccountBalance, getAccountNote, getSimpleFinID, getTransactions, openBudget } = require('./utils'); const { closeBudget, ensureCategory, ensureCategoryGroup, ensurePayee, getAccountBalance, getAccountNote, getSimpleFinID, getTransactions, openBudget } = require('./utils');
require("dotenv").config(); require("dotenv").config();
@@ -72,7 +72,8 @@ const zeroTransaction = async (payment) => {
await openBudget(); await openBudget();
const payeeId = await ensurePayee(process.env.INVESTMENT_PAYEE_NAME || 'Investment'); const payeeId = await ensurePayee(process.env.INVESTMENT_PAYEE_NAME || 'Investment');
const categoryId = await ensureCategory(process.env.INVESTMENT_CATEGORY_NAME || 'Investment'); const categoryGroupId = await ensureCategoryGroup(process.env.INVESTMENT_CATEGORY_GROUP_NAME || 'Income');
const categoryId = await ensureCategory(process.env.INVESTMENT_CATEGORY_NAME || 'Investment', categoryGroupId, true);
const simplefinBalances = await getSimplefinBalances(); const simplefinBalances = await getSimplefinBalances();
if (simplefinBalances) { if (simplefinBalances) {
+57 -21
View File
@@ -4,8 +4,9 @@ require("dotenv").config();
const Utils = { const Utils = {
openBudget: async function () { openBudget: async function () {
process.on('unhandledRejection', (reason, p) => { process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason); console.error('Unhandled Rejection at: Promise', p, 'reason:', reason);
console.log(reason.stack); console.error(reason.stack);
process.exit(1);
}); });
const url = process.env.ACTUAL_SERVER_URL || ''; const url = process.env.ACTUAL_SERVER_URL || '';
@@ -32,7 +33,12 @@ const Utils = {
closeBudget: async function () { closeBudget: async function () {
console.log("done"); console.log("done");
await api.shutdown(); try {
await api.shutdown();
} catch (e) {
console.error(e);
process.exit(1);
}
}, },
getAccountBalance: async function (account, cutoffDate=new Date()) { getAccountBalance: async function (account, cutoffDate=new Date()) {
@@ -83,29 +89,59 @@ const Utils = {
}, },
ensurePayee: async function (payeeName) { ensurePayee: async function (payeeName) {
const payees = await api.getPayees(); try {
let payeeId = payees.find(p => p.name === payeeName)?.id; const payees = await api.getPayees();
if (!payeeId) { let payeeId = payees.find(p => p.name === payeeName)?.id;
payeeId = await api.createPayee({ name: payeeName }); if (!payeeId) {
payeeId = await api.createPayee({ name: payeeName });
}
if (payeeId) {
return payeeId;
}
} catch (e) {
console.error(e);
} }
if (!payeeId) { console.error('Failed to create payee:', payeeName);
console.error('Failed to create payee:', payeeName); process.exit(1);
process.exit(1);
}
return payeeId;
}, },
ensureCategory: async function (categoryName) { ensureCategoryGroup: async function (categoryGroupName) {
const categories = await api.getCategories(); try {
let categoryId = categories.find(c => c.name === categoryName)?.id; const groups = await api.getCategoryGroups();
if (!categoryId) { let groupId = groups.find(g => g.name === categoryGroupName)?.id;
categoryId = await api.createCategory({ name: categoryName }); if (!groupId) {
groupId = await api.createCategoryGroup({ name: categoryGroupName });
}
if (groupId) {
return groupId;
}
} catch (e) {
console.error(e);
} }
if (!categoryId) { console.error('Failed to create category group:', categoryGroupName);
console.error('Failed to create category:', categoryName); process.exit(1);
process.exit(1); },
ensureCategory: async function (categoryName, groupId, is_income=false) {
try {
const categories = await api.getCategories();
let categoryId = categories.find(c => c.name === categoryName)?.id;
if (!categoryId) {
categoryId = await api.createCategory({
name: categoryName,
group_id: groupId,
is_income: is_income,
hidden: false,
});
}
if (categoryId) {
return categoryId;
}
} catch (e) {
console.error(e);
} }
return categoryId; console.error('Failed to create category:', categoryName);
process.exit(1);
}, },
getTagValue: function (note, tag, defaultValue=undefined) { getTagValue: function (note, tag, defaultValue=undefined) {