Skip to content

Commit

Permalink
Goals: Check template action (#1108)
Browse files Browse the repository at this point in the history
This adds an option to the month drop down to check all the template
lines. If there are errors the offending line is shown with its
category.

I also modified the wording on the regular template return to be more
accurate. Fixes #1100
  • Loading branch information
youngcw authored Jun 11, 2023
1 parent c1af40f commit ded6ee8
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,10 @@ export function BudgetSummary({
name: 'set-3-avg',
text: 'Set budgets to 3 month avg',
},
isGoalTemplatesEnabled && {
name: 'check-templates',
text: 'Check templates',
},
isGoalTemplatesEnabled && {
name: 'apply-goal-template',
text: 'Apply budget template',
Expand Down
3 changes: 3 additions & 0 deletions packages/loot-core/src/client/actions/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export function applyBudgetAction(month, type, args) {
case 'set-3-avg':
await send('budget/set-3month-avg', { month });
break;
case 'check-templates':
dispatch(addNotification(await send('budget/check-templates')));
break;
case 'apply-goal-template':
dispatch(
addNotification(await send('budget/apply-goal-template', { month })),
Expand Down
4 changes: 4 additions & 0 deletions packages/loot-core/src/server/budget/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ app.method(
);
app.method('budget/set-zero', mutator(undoable(actions.setZero)));
app.method('budget/set-3month-avg', mutator(undoable(actions.set3MonthAvg)));
app.method(
'budget/check-templates',
mutator(undoable(goalActions.runCheckTemplates)),
);
app.method(
'budget/apply-goal-template',
mutator(undoable(goalActions.applyTemplate)),
Expand Down
43 changes: 40 additions & 3 deletions packages/loot-core/src/server/budget/goaltemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export function overwriteTemplate({ month }) {
return processTemplate(month, true);
}

export function runCheckTemplates() {
return checkTemplates();
}

function checkScheduleTemplates(template) {
let lowPriority = template[0].priority;
let errorNotice = false;
Expand Down Expand Up @@ -211,9 +215,7 @@ async function processTemplate(month, force) {
return { type: 'message', message: 'All categories were up to date.' };
}
} else {
let applied = `Successfully applied templates to ${num_applied} ${
num_applied === 1 ? 'category' : 'categories'
}.`;
let applied = `Successfully applied ${num_applied} templates.`;
if (errors.length) {
return {
sticky: true,
Expand Down Expand Up @@ -617,3 +619,38 @@ async function applyCategoryTemplate(
return { amount: to_budget, errors };
}
}

async function checkTemplates() {
let category_templates = await getCategoryTemplates();
let errors = [];

let categories = await db.all(
'SELECT * FROM v_categories WHERE tombstone = 0',
);

// run through each line and see if its an error
for (let c = 0; c < categories.length; c++) {
let category = categories[c];
let template = category_templates[category.id];
if (template) {
for (let l = 0; l < template.length; l++) {
if (template[l].type === 'error') {
//return { type: 'message', message: "found a bad one",};
errors.push(category.name + ': ' + template[l].line);
}
}
}
}
if (errors.length) {
return {
sticky: true,
message: `There were errors interpreting some templates:`,
pre: errors.join('\n\n'),
};
} else {
return {
type: 'message',
message: 'All templates passed! 🎉',
};
}
}
2 changes: 2 additions & 0 deletions packages/loot-core/src/server/budget/types/handlers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export interface BudgetHandlers {

'budget/set-3month-avg': (...args: unknown[]) => Promise<unknown>;

'budget/check-templates': (...args: unknown[]) => Promise<unknown>;

'budget/apply-goal-template': (...args: unknown[]) => Promise<unknown>;

'budget/overwrite-goal-template': (...args: unknown[]) => Promise<unknown>;
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/1108.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [youncw]
---

Add action in month drop down to check template lines for proper formatting

0 comments on commit ded6ee8

Please sign in to comment.