A local-first personal finance app
[Goals] upgrade week template to be a periodic template (#5295)
youngcw682d439c3423
diff --git a/packages/desktop-client/src/components/budget/goals/constants.ts b/packages/desktop-client/src/components/budget/goals/constants.tsindex b26e032ab5..313bb0db75 100644--- a/packages/desktop-client/src/components/budget/goals/constants.ts+++ b/packages/desktop-client/src/components/budget/goals/constants.ts@@ -4,7 +4,7 @@ import {type PercentageTemplate,type ScheduleTemplate,type SimpleTemplate,- type WeekTemplate,+ type PeriodicTemplate,} from 'loot-core/types/models/templates';export const displayTemplateTypes = [@@ -23,7 +23,7 @@ export type ReducerState =displayType: 'simple';}| {- template: WeekTemplate;+ template: PeriodicTemplate;displayType: 'week';}| {diff --git a/packages/desktop-client/src/components/budget/goals/editor/WeekAutomation.tsx b/packages/desktop-client/src/components/budget/goals/editor/WeekAutomation.tsxindex 64297f0a43..5f7c6d6764 100644--- a/packages/desktop-client/src/components/budget/goals/editor/WeekAutomation.tsx+++ b/packages/desktop-client/src/components/budget/goals/editor/WeekAutomation.tsx@@ -1,6 +1,6 @@import { useTranslation } from 'react-i18next';-import type { WeekTemplate } from 'loot-core/types/models/templates';+import type { PeriodicTemplate } from 'loot-core/types/models/templates';import {type Action,@@ -10,7 +10,7 @@ import { FormField, FormLabel } from '@desktop-client/components/forms';import { AmountInput } from '@desktop-client/components/util/AmountInput';type WeekAutomationProps = {- template: WeekTemplate;+ template: PeriodicTemplate;dispatch: (action: Action) => void;};@@ -26,7 +26,7 @@ export const WeekAutomation = ({ template, dispatch }: WeekAutomationProps) => {value={template.amount ?? 0}zeroSign="+"onUpdate={(value: number) =>- dispatch(updateTemplate({ type: 'week', amount: value }))+ dispatch(updateTemplate({ type: 'periodic', amount: value }))}/></FormField>diff --git a/packages/desktop-client/src/components/budget/goals/editor/WeekAutomationReadOnly.tsx b/packages/desktop-client/src/components/budget/goals/editor/WeekAutomationReadOnly.tsxindex 2d3c8cbf85..b39c544527 100644--- a/packages/desktop-client/src/components/budget/goals/editor/WeekAutomationReadOnly.tsx+++ b/packages/desktop-client/src/components/budget/goals/editor/WeekAutomationReadOnly.tsx@@ -1,10 +1,10 @@import { Trans } from 'react-i18next';import { integerToCurrency } from 'loot-core/shared/util';-import type { WeekTemplate } from 'loot-core/types/models/templates';+import type { PeriodicTemplate } from 'loot-core/types/models/templates';type WeekAutomationReadOnlyProps = {- template: WeekTemplate;+ template: PeriodicTemplate;};export const WeekAutomationReadOnly = ({diff --git a/packages/desktop-client/src/components/budget/goals/reducer.ts b/packages/desktop-client/src/components/budget/goals/reducer.tsindex 308601d6ae..91e18a46bb 100644--- a/packages/desktop-client/src/components/budget/goals/reducer.ts+++ b/packages/desktop-client/src/components/budget/goals/reducer.ts@@ -21,7 +21,7 @@ export const getInitialState = (template: Template | null): ReducerState => {template,displayType: 'schedule',};- case 'week':+ case 'periodic':return {template,displayType: 'week',@@ -90,16 +90,19 @@ const changeType = (},};case 'week':- if (prevState.template.type === 'week') {+ if (prevState.template.type === 'periodic') {return prevState;}return {displayType: visualType,template: {directive: '',- type: 'week',+ type: 'periodic',amount: 500,- weeks: null,+ period: {+ period: 'week',+ amount: 1,+ },starting: '',},};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 d474d791bd..8701e991f8 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@@ -174,7 +174,7 @@ describe('CategoryTemplateContext', () => {});});- describe('runWeek', () => {+ describe('runPeriodic', () => {let instance: TestCategoryTemplateContext;beforeEach(() => {@@ -190,43 +190,103 @@ describe('CategoryTemplateContext', () => {//5 mondays in January 2024it('should calculate weekly amount for single week', () => {const template: Template = {- type: 'week',+ type: 'periodic',amount: 100,- weeks: 1,+ period: {+ period: 'week',+ amount: 1,+ },starting: '2024-01-01',directive: 'template',priority: 1,};- const result = CategoryTemplateContext.runWeek(template, instance);+ const result = CategoryTemplateContext.runPeriodic(template, instance);expect(result).toBe(amountToInteger(500));});it('should calculate weekly amount for multiple weeks', () => {const template: Template = {- type: 'week',+ type: 'periodic',amount: 100,- weeks: 2,+ period: {+ period: 'week',+ amount: 2,+ },starting: '2024-01-01',directive: 'template',priority: 1,};- const result = CategoryTemplateContext.runWeek(template, instance);+ const result = CategoryTemplateContext.runPeriodic(template, instance);expect(result).toBe(amountToInteger(300));});it('should handle weeks spanning multiple months', () => {const template: Template = {- type: 'week',+ type: 'periodic',amount: 100,- weeks: 7,+ period: {+ period: 'week',+ amount: 7,+ },starting: '2023-12-04',directive: 'template',priority: 1,};- const result = CategoryTemplateContext.runWeek(template, instance);+ const result = CategoryTemplateContext.runPeriodic(template, instance);+ expect(result).toBe(amountToInteger(100));+ });++ it('should handle periodic days', () => {+ const template: Template = {+ type: 'periodic',+ amount: 100,+ period: {+ period: 'day',+ amount: 10,+ },+ starting: '2024-01-01',+ directive: 'template',+ priority: 1,+ };++ const result = CategoryTemplateContext.runPeriodic(template, instance);+ expect(result).toBe(amountToInteger(400)); // for the 1st, 11th, 21st, 31st+ });++ it('should handle periodic years', () => {+ const template: Template = {+ type: 'periodic',+ amount: 100,+ period: {+ period: 'year',+ amount: 1,+ },+ starting: '2023-01-01',+ directive: 'template',+ priority: 1,+ };++ const result = CategoryTemplateContext.runPeriodic(template, instance);+ expect(result).toBe(amountToInteger(100));+ });++ it('should handle periodic months', () => {+ const template: Template = {+ type: 'periodic',+ amount: 100,+ period: {+ period: 'month',+ amount: 2,+ },+ starting: '2023-11-01',+ directive: 'template',+ priority: 1,+ };++ const result = CategoryTemplateContext.runPeriodic(template, instance);expect(result).toBe(amountToInteger(100));});});diff --git a/packages/loot-core/src/server/budget/category-template-context.ts b/packages/loot-core/src/server/budget/category-template-context.tsindex 7ae2664d3b..75b7e8aa0c 100644--- a/packages/loot-core/src/server/budget/category-template-context.ts+++ b/packages/loot-core/src/server/budget/category-template-context.ts@@ -14,7 +14,7 @@ import {SimpleTemplate,SpendTemplate,Template,- WeekTemplate,+ PeriodicTemplate,} from '../../types/models/templates';import { aqlQuery } from '../aql';import * as db from '../db';@@ -151,8 +151,8 @@ export class CategoryTemplateContext {newBudget = await CategoryTemplateContext.runCopy(template, this);break;}- case 'week': {- newBudget = CategoryTemplateContext.runWeek(template, this);+ case 'periodic': {+ newBudget = CategoryTemplateContext.runPeriodic(template, this);break;}case 'spend': {@@ -432,7 +432,10 @@ export class CategoryTemplateContext {private checkLimit() {for (const template of this.templates.filter(- t => t.type === 'simple' || t.type === 'week' || t.type === 'remainder',+ t =>+ t.type === 'simple' ||+ t.type === 'periodic' ||+ t.type === 'remainder',).filter(t => t.limit)) {if (this.limitCheck) {@@ -519,22 +522,51 @@ export class CategoryTemplateContext {);}- static runWeek(- template: WeekTemplate,+ static runPeriodic(+ template: PeriodicTemplate,templateContext: CategoryTemplateContext,): number {let toBudget = 0;const amount = amountToInteger(template.amount);- const weeks = template.weeks != null ? Math.round(template.weeks) : 1;- let w = template.starting;- const nextMonth = monthUtils.addMonths(templateContext.month, 1);+ const period = template.period.period;+ const numPeriods = template.period.amount;+ let date = template.starting;++ let dateShiftFunction;+ switch (period) {+ case 'day':+ dateShiftFunction = monthUtils.addDays;+ break;+ case 'week':+ dateShiftFunction = monthUtils.addWeeks;+ break;+ case 'month':+ dateShiftFunction = monthUtils.addMonths;+ break;+ case 'year':+ // the addYears function doesn't return the month number, so use addMonths+ dateShiftFunction = (date, numPeriods) =>+ monthUtils.addMonths(date, numPeriods * 12);+ break;+ }- while (w < nextMonth) {- if (w >= templateContext.month) {- toBudget += amount;- }- w = monthUtils.addWeeks(w, weeks);+ //shift the starting date until its in our month or in the future+ while (templateContext.month > date) {+ date = dateShiftFunction(date, numPeriods);}++ if (+ monthUtils.differenceInCalendarMonths(templateContext.month, date) < 0+ ) {+ return 0;+ } // nothing needed this month++ const nextMonth = monthUtils.addMonths(templateContext.month, 1);+ while (date < nextMonth) {+ toBudget += amount;+ date = dateShiftFunction(date, numPeriods);+ }+return toBudget;}diff --git a/packages/loot-core/src/server/budget/goal-template.pegjs b/packages/loot-core/src/server/budget/goal-template.pegjsindex 41019ee380..264b4e6c64 100644--- a/packages/loot-core/src/server/budget/goal-template.pegjs+++ b/packages/loot-core/src/server/budget/goal-template.pegjs@@ -3,8 +3,8 @@expr= template: template _ percentOf:percentOf category: name{ return { type: 'percentage', percent: +percentOf.percent, previous: percentOf.prev, category, priority: template.priority, directive: template.directive }}- / template: template _ amount: amount _ repeatEvery _ weeks: weekCount _ starting _ starting: date limit: limit?- { return { type: 'week', amount, weeks, starting, limit, priority: template.priority, directive: template.directive }}+ / template: template _ amount: amount _ repeatEvery _ period: periodCount _ starting _ starting: date limit: limit?+ { return { type: 'periodic', amount, period, starting, limit, priority: template.priority, directive: template.directive }}/ template: template _ amount: amount _ by _ month: month from: spendFrom? repeat: (_ repeatEvery _ repeat)?{ return {type: from ? 'spend' : 'by',@@ -52,9 +52,13 @@ limit = _? upTo _ amount: amount _ 'per week starting'i _ start:date _? hold:hopercentOf = percent:percent _ of _ 'previous'i _ { return { percent: percent, prev: true}}/ percent:percent _ of _ { return { percent: percent, prev: false}}-weekCount- = week { return null }- / n: number _ weeks { return +n }+periodCount = 'day'i { return {period: 'day', amount: 1 }}+ / n: number _ 'days'i _ { return { period: 'day', amount: +n }}+ / week _ { return {period: 'week', amount: 1 }}+ / n: number _ weeks { return {period: 'week', amount: +n }}+ / n: number _ 'months'i _ {return {period: 'month', amount: +n }}+ / 'year'i _ { return {period: 'year', amount: 1 }}+ / n: number _ 'years'i _ { return { period: 'year', amount: +n }}spendFrom = _ 'spend'i _ 'from'i _ month: month { return month }diff --git a/packages/loot-core/src/types/models/templates.ts b/packages/loot-core/src/types/models/templates.tsindex 647eed86c2..76a98582f8 100644--- a/packages/loot-core/src/types/models/templates.ts+++ b/packages/loot-core/src/types/models/templates.ts@@ -11,10 +11,13 @@ export interface PercentageTemplate extends BaseTemplate {category: string;}-export interface WeekTemplate extends BaseTemplate {- type: 'week';+export interface PeriodicTemplate extends BaseTemplate {+ type: 'periodic';amount: number;- weeks: number | null;+ period: {+ period: 'day' | 'week' | 'month' | 'year';+ amount: number;+ };starting: string;limit?: {amount: number;@@ -94,7 +97,7 @@ interface ErrorTemplate extends BaseTemplate {export type Template =| PercentageTemplate- | WeekTemplate+ | PeriodicTemplate| ByTemplate| SpendTemplate| SimpleTemplatediff --git a/upcoming-release-notes/5295.md b/upcoming-release-notes/5295.mdnew file mode 100644index 0000000000..a160cb76bd--- /dev/null+++ b/upcoming-release-notes/5295.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [youngcw]+---++Extend the week template to be a periodic template supporting days, weeks, months, years