A local-first personal finance app
Improve error reporting for goal templates (#895)
shall0passadb20868cc29
diff --git a/packages/loot-core/src/server/budget/goaltemplates.js b/packages/loot-core/src/server/budget/goaltemplates.jsindex 90ce80aa12..8d490e8779 100644--- a/packages/loot-core/src/server/budget/goaltemplates.js+++ b/packages/loot-core/src/server/budget/goaltemplates.js@@ -54,16 +54,17 @@ async function processTemplate(month, force) {].join('\n'),),);- let to_budget = await applyCategoryTemplate(- category,- template,- month,- force,- );+ let { amount: to_budget, errors: applyErrors } =+ await applyCategoryTemplate(category, template, month, force);if (to_budget != null) {num_applied++;await setBudget({ category: category.id, month, amount: to_budget });}+ if (applyErrors != null) {+ errors = errors.concat(+ applyErrors.map(error => `${category.name}: ${error}`),+ );+ }}}}@@ -129,6 +130,7 @@ async function getCategoryTemplates() {async function applyCategoryTemplate(category, template_lines, month, force) {let current_month = new Date(`${month}-01`);+ let errors = [];// remove lines for past dates, calculate repeating datestemplate_lines = template_lines.filter(template => {@@ -156,12 +158,8 @@ async function applyCategoryTemplate(category, template_lines, month, force) {num_months = differenceInCalendarMonths(target_month, current_month);}if (num_months < 0) {- console.log(- `${category.name}: ${`${template.month} is in the past:`} ${- template.line- }`,- );- return null;+ errors.push(`${template.month} is in the past.`);+ return false;}template.month = format(target_month, 'yyyy-MM');if (spend_from) {@@ -170,7 +168,7 @@ async function applyCategoryTemplate(category, template_lines, month, force) {break;default:}- return template;+ return true;});if (template_lines.length > 1) {@@ -200,10 +198,8 @@ async function applyCategoryTemplate(category, template_lines, month, force) {// simple has 'monthly' and/or 'limit' paramsif (template.limit != null) {if (limit != null) {- console.log(- `${category.name}: More than one “up to” limit found. ${template.line}`,- );- return null;+ errors.push(`More than one “up to” limit found.`);+ return { errors };} else {limit = amountToInteger(template.limit);}@@ -244,10 +240,8 @@ async function applyCategoryTemplate(category, template_lines, month, force) {let weeks = template.weeks != null ? Math.round(template.weeks) : 1;if (template.limit != null) {if (limit != null) {- console.log(- `${category.name}: More than one “up to” limit found. ${template.line}`,- );- return null;+ errors.push(`More than one “up to” limit found.`);+ return { errors };} else {limit = amountToInteger(template.limit);}@@ -299,12 +293,8 @@ async function applyCategoryTemplate(category, template_lines, month, force) {let num_months = differenceInCalendarMonths(to_month, current_month);let target = amountToInteger(template.amount);if (num_months < 0) {- console.log(- `${category.name}: ${`${template.to} is in the past:`} ${- template.line- }`,- );- return null;+ errors.push(`${template.month} is in the past.`);+ return { errors };} else if (num_months === 0) {to_budget = target - already_budgeted;} else {@@ -331,7 +321,7 @@ async function applyCategoryTemplate(category, template_lines, month, force) {break;}case 'error':- return null;+ return { errors };default:}}@@ -347,7 +337,7 @@ async function applyCategoryTemplate(category, template_lines, month, force) {to_budget === 0) &&!force) {- return null;+ return { errors };} else if (category.budgeted === to_budget && force) {return null;} else {@@ -359,6 +349,6 @@ async function applyCategoryTemplate(category, template_lines, month, force) {integerToAmount(last_month_balance + to_budget);str += ' ' + template_lines.map(x => x.line).join('\n');console.log(str);- return to_budget;+ return { amount: to_budget };}}diff --git a/upcoming-release-notes/895.md b/upcoming-release-notes/895.mdnew file mode 100644index 0000000000..c807b4c03b--- /dev/null+++ b/upcoming-release-notes/895.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [shall0pass]+---++Improve error reporting for goal templates