Skip to content

A local-first personal finance app

[AI] Fix crash viewing account ledger with expired recurring schedules (#7381)

Matiss Janis Aboltins4fe4421ab760
diff --git a/packages/loot-core/src/server/schedules/app.ts b/packages/loot-core/src/server/schedules/app.ts
index eee97f8897..e33e48fc15 100644
--- a/packages/loot-core/src/server/schedules/app.ts
+++ b/packages/loot-core/src/server/schedules/app.ts
@@ -206,7 +206,7 @@ export async function setNextDate({
start ? start(nextDate) : new Date(),
);
- if (newNextDate !== nextDate) {
+ if (newNextDate != null && newNextDate !== nextDate) {
// Our `update` functon requires the id of the item and we don't
// have it, so we need to query it
const nd = await db.first<
diff --git a/packages/loot-core/src/shared/schedules.test.ts b/packages/loot-core/src/shared/schedules.test.ts
index abcb64834e..6e92f2d0b5 100644
--- a/packages/loot-core/src/shared/schedules.test.ts
+++ b/packages/loot-core/src/shared/schedules.test.ts
@@ -7,6 +7,7 @@ import type { ScheduleEntity } from '../types/models';
import * as monthUtils from './months';
import {
computeSchedulePreviewTransactions,
+ getNextDate,
getRecurringDescription,
getStatus,
getUpcomingDays,
@@ -566,5 +567,90 @@ describe('schedules', () => {
).toBe(true);
});
});
+
+ it('does not crash when a recurring schedule has an end date in the past', () => {
+ function makeSchedule(
+ overrides: Partial<ScheduleEntity> &
+ Pick<ScheduleEntity, 'id' | 'next_date' | '_conditions'>,
+ ): ScheduleEntity {
+ return {
+ rule: 'rule-1',
+ completed: false,
+ posts_transaction: false,
+ tombstone: false,
+ _payee: 'payee-1',
+ _account: 'acct-1',
+ _amount: -10000,
+ _amountOp: 'is',
+ _date: overrides.next_date,
+ _actions: [],
+ ...overrides,
+ };
+ }
+
+ // Schedule that recurs monthly but ended in the past (2016-08-25)
+ // while current date is 2017-01-01
+ const schedule = makeSchedule({
+ id: 'sched-expired',
+ next_date: '2016-08-25',
+ _conditions: [
+ {
+ field: 'date',
+ op: 'isapprox',
+ value: {
+ start: '2016-01-25',
+ frequency: 'monthly',
+ endMode: 'on_date',
+ endDate: '2016-08-25',
+ },
+ },
+ ],
+ });
+
+ const statuses: ScheduleStatuses = new Map([['sched-expired', 'missed']]);
+ const result = computeSchedulePreviewTransactions(
+ [schedule],
+ statuses,
+ '7',
+ );
+
+ // Should not crash; schedule with past end date produces its next_date entry only
+ expect(result).toBeDefined();
+ });
+ });
+
+ describe('getNextDate', () => {
+ it('returns last occurrence for a recurring schedule with an end date in the past', () => {
+ const dateCond = {
+ op: 'isapprox',
+ value: {
+ start: '2016-01-25',
+ frequency: 'monthly',
+ endMode: 'on_date',
+ endDate: '2016-08-25',
+ },
+ };
+
+ // Current date is 2017-01-01 via MockDate
+ const result = getNextDate(dateCond);
+ expect(result).not.toBeNull();
+ // The last occurrence should be returned (reverse lookup)
+ expect(result).toBe('2016-08-25');
+ });
+
+ it('returns null when the end date is before the start date', () => {
+ const dateCond = {
+ op: 'isapprox',
+ value: {
+ start: '2016-03-25',
+ frequency: 'monthly',
+ endMode: 'on_date',
+ endDate: '2016-01-25',
+ },
+ };
+
+ const result = getNextDate(dateCond, new Date(2017, 0, 1));
+ expect(result).toBeNull();
+ });
});
});
diff --git a/packages/loot-core/src/shared/schedules.ts b/packages/loot-core/src/shared/schedules.ts
index 54848c27de..caa36ed4f3 100644
--- a/packages/loot-core/src/shared/schedules.ts
+++ b/packages/loot-core/src/shared/schedules.ts
@@ -345,7 +345,7 @@ export function getNextDate(
dateCond,
start = new Date(monthUtils.currentDay()),
noSkipWeekend = false,
-) {
+): string | null {
start = d.startOfDay(start);
const cond = new Condition(dateCond.op, 'date', dateCond.value, null);
@@ -516,6 +516,10 @@ export function computeSchedulePreviewTransactions(
while (day <= upcomingPeriodEnd) {
const nextDate = getNextDate(dateConditions, day);
+ if (nextDate === null) {
+ break;
+ }
+
if (
d.startOfDay(monthUtils.parseDate(nextDate)) > upcomingPeriodEnd
) {
diff --git a/upcoming-release-notes/7381.md b/upcoming-release-notes/7381.md
new file mode 100644
index 0000000000..b344b7dba6
--- /dev/null
+++ b/upcoming-release-notes/7381.md
@@ -0,0 +1,6 @@
+---
+category: Bugfixes
+authors: [MatissJanis]
+---
+
+Fix crash when viewing account ledger with expired recurring schedules.