A local-first personal finance app
add limit template type (#5775)
youngcw08b5b7fdc7f7
diff --git a/packages/desktop-client/src/components/budget/goals/reducer.ts b/packages/desktop-client/src/components/budget/goals/reducer.tsindex 14791e0001..2d647d2274 100644--- a/packages/desktop-client/src/components/budget/goals/reducer.ts+++ b/packages/desktop-client/src/components/budget/goals/reducer.ts@@ -33,6 +33,8 @@ export const getInitialState = (template: Template | null): ReducerState => {throw new Error('Goal is not yet supported');case 'remainder':throw new Error('Remainder is not yet supported');+ case 'limit':+ throw new Error('Limit is not yet supported');case 'average':case 'copy':return {diff --git a/packages/loot-core/src/server/budget/category-template-context.test.ts b/packages/loot-core/src/server/budget/category-template-context.test.tsindex 4a022d91ea..41f24f3e0e 100644--- a/packages/loot-core/src/server/budget/category-template-context.test.ts+++ b/packages/loot-core/src/server/budget/category-template-context.test.ts@@ -54,7 +54,7 @@ describe('CategoryTemplateContext', () => {it('should return limit when monthly is not provided', () => {const template: Template = {type: 'simple',- limit: { amount: 500, hold: false },+ limit: { amount: 500, hold: false, period: 'monthly' },directive: 'template',priority: 1,};diff --git a/packages/loot-core/src/server/budget/category-template-context.ts b/packages/loot-core/src/server/budget/category-template-context.tsindex 3050e91680..5ffecbd172 100644--- a/packages/loot-core/src/server/budget/category-template-context.ts+++ b/packages/loot-core/src/server/budget/category-template-context.ts@@ -318,7 +318,11 @@ export class CategoryTemplateContext {// sort the template lines into regular template, goals, and remainder templatesif (templates) {templates.forEach(t => {- if (t.directive === 'template' && t.type !== 'remainder') {+ if (+ t.directive === 'template' &&+ t.type !== 'remainder' &&+ t.type !== 'limit'+ ) {this.templates.push(t);if (t.priority !== null) this.priorities.add(t.priority);} else if (t.directive === 'template' && t.type === 'remainder') {@@ -423,41 +427,53 @@ export class CategoryTemplateContext {}private checkLimit(templates: Template[]) {- for (const template of templates- .filter(- t =>- t.type === 'simple' ||- t.type === 'periodic' ||- t.type === 'remainder',- )- .filter(t => t.limit)) {+ for (const template of templates.filter(+ t =>+ t.type === 'simple' ||+ t.type === 'periodic' ||+ t.type === 'limit' ||+ t.type === 'remainder',+ )) {if (this.limitCheck) {throw new Error('Only one `up to` allowed per category');}- if (template.limit.period === 'daily') {++ let limitDef;+ if (template.type === 'limit') {+ limitDef = template;+ } else {+ if (template.limit) {+ limitDef = template.limit;+ } else {+ continue; // may not have a limit defined in the template+ }+ }++ if (limitDef.period === 'daily') {const numDays = monthUtils.differenceInCalendarDays(monthUtils.addMonths(this.month, 1),this.month,);- this.limitAmount += amountToInteger(template.limit.amount) * numDays;- } else if (template.limit.period === 'weekly') {+ this.limitAmount += amountToInteger(limitDef.amount) * numDays;+ } else if (limitDef.period === 'weekly') {const nextMonth = monthUtils.nextMonth(this.month);- let week = template.limit.start;- const baseLimit = amountToInteger(template.limit.amount);+ let week = limitDef.start;+ const baseLimit = amountToInteger(limitDef.amount);while (week < nextMonth) {if (week >= this.month) {this.limitAmount += baseLimit;}week = monthUtils.addWeeks(week, 1);}- } else if (template.limit.period === 'monthly') {- this.limitAmount = amountToInteger(template.limit.amount);+ } else if (limitDef.period === 'monthly') {+ this.limitAmount = amountToInteger(limitDef.amount);} else {throw new Error('Invalid limit period. Check template syntax');}+//amount is good save the restthis.limitCheck = true;- this.limitHold = template.limit.hold ? true : false;+ this.limitHold = limitDef.hold ? true : false;// check if the limit is already met and save the excessif (this.fromLastMonth >= this.limitAmount) {this.limitMet = true;diff --git a/packages/loot-core/src/types/models/templates.ts b/packages/loot-core/src/types/models/templates.tsindex 3662de4a2f..666fec4470 100644--- a/packages/loot-core/src/types/models/templates.ts+++ b/packages/loot-core/src/types/models/templates.ts@@ -25,7 +25,7 @@ export interface PeriodicTemplate extends BaseTemplateWithPriority {limit?: {amount: number;hold: boolean;- period?: 'daily' | 'weekly' | 'monthly';+ period: 'daily' | 'weekly' | 'monthly';start?: string;};}@@ -54,7 +54,7 @@ export interface SimpleTemplate extends BaseTemplateWithPriority {limit?: {amount: number;hold: boolean;- period?: 'daily' | 'weekly' | 'monthly';+ period: 'daily' | 'weekly' | 'monthly';start?: string;};}@@ -82,7 +82,7 @@ export interface RemainderTemplate extends BaseTemplate {limit?: {amount: number;hold: boolean;- period?: 'daily' | 'weekly' | 'monthly';+ period: 'daily' | 'weekly' | 'monthly';start?: string;};directive: 'template';@@ -95,6 +95,16 @@ export interface GoalTemplate extends BaseTemplate {directive: 'goal';}+export interface LimitTemplate extends BaseTemplate {+ type: 'limit';+ amount: number;+ hold: boolean;+ period: 'daily' | 'weekly' | 'monthly';+ start?: string;+ directive: 'template';+ priority: null;+}+interface ErrorTemplate extends BaseTemplate {type: 'error';line: string;@@ -113,4 +123,5 @@ export type Template =| AverageTemplate| GoalTemplate| CopyTemplate+ | LimitTemplate| ErrorTemplate;diff --git a/upcoming-release-notes/5775.md b/upcoming-release-notes/5775.mdnew file mode 100644index 0000000000..7c9be12c17--- /dev/null+++ b/upcoming-release-notes/5775.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [youngcw]+---++Add limit type template for use in future template GUI