Skip to content

A local-first personal finance app

[AI] Fix schedules skipping a weekend date moved to the next weekday (#9191)

Andreadd224f6dfd74
diff --git a/packages/loot-core/src/server/schedules/app.test.ts b/packages/loot-core/src/server/schedules/app.test.ts
index 84d9707166..ff85d25b9f 100644
--- a/packages/loot-core/src/server/schedules/app.test.ts
+++ b/packages/loot-core/src/server/schedules/app.test.ts
@@ -638,6 +638,127 @@ describe('schedule app', () => {
expect(row.next_date).toBe('2020-12-07');
});
+ it('createSchedule keeps an occurrence moved `after` the weekend onto a later day', async () => {
+ // Tests run on Sunday 1 January 2017. Saturday 31 December moves to
+ // Monday 2 January, which is still ahead, so it is the next date.
+ for (const frequency of ['weekly', 'monthly'] as const) {
+ const id = await createSchedule({
+ schedule: { name: frequency },
+ conditions: [
+ {
+ op: 'is',
+ field: 'date',
+ value: {
+ start: '2016-12-31',
+ frequency,
+ patterns: [],
+ skipWeekend: true,
+ weekendSolveMode: 'after',
+ },
+ },
+ ],
+ });
+
+ const {
+ data: [row],
+ } = await aqlQuery(q('schedules').filter({ id }).select(['next_date']));
+
+ expect(row.next_date).toBe('2017-01-02');
+ }
+ });
+
+ it('updateSchedule keeps an occurrence moved `after` the weekend onto a later day', async () => {
+ const id = await createSchedule({
+ conditions: [{ op: 'is', field: 'date', value: '2020-12-20' }],
+ });
+
+ // Edited on Sunday 23 May: Saturday 22 May moves to Monday 24 May,
+ // which is still ahead, so it is the next date.
+ MockDate.set(new Date(2021, 4, 23, 12));
+
+ try {
+ await updateSchedule({
+ schedule: { id },
+ conditions: [
+ {
+ op: 'is',
+ field: 'date',
+ value: {
+ start: '2021-05-22',
+ frequency: 'weekly',
+ patterns: [],
+ skipWeekend: true,
+ weekendSolveMode: 'after',
+ },
+ },
+ ],
+ });
+
+ const {
+ data: [row],
+ } = await aqlQuery(q('schedules').filter({ id }).select(['next_date']));
+
+ expect(row.next_date).toBe('2021-05-24');
+ } finally {
+ MockDate.reset();
+ }
+ });
+
+ it('createSchedule uses the local day west of UTC', async () => {
+ // `new Date('2017-01-01')` is UTC midnight, which is still Saturday
+ // 31 December in New York, so that past Saturday became the next date.
+ const tz = process.env.TZ;
+ process.env.TZ = 'America/New_York';
+
+ try {
+ const id = await createSchedule({
+ conditions: [
+ {
+ op: 'is',
+ field: 'date',
+ value: {
+ start: '2016-12-24',
+ frequency: 'weekly',
+ patterns: [],
+ },
+ },
+ ],
+ });
+
+ const {
+ data: [row],
+ } = await aqlQuery(q('schedules').filter({ id }).select(['next_date']));
+
+ expect(row.next_date).toBe('2017-01-07');
+ } finally {
+ process.env.TZ = tz;
+ }
+ });
+
+ it('createSchedule keeps the last date of a schedule that has ended', async () => {
+ const id = await createSchedule({
+ conditions: [
+ {
+ op: 'is',
+ field: 'date',
+ value: {
+ start: '2016-11-07',
+ frequency: 'weekly',
+ patterns: [],
+ endMode: 'after_n_occurrences',
+ endOccurrences: 2,
+ },
+ },
+ ],
+ });
+
+ const {
+ data: [row],
+ } = await aqlQuery(q('schedules').filter({ id }).select(['next_date']));
+
+ expect(row.next_date).toBe('2016-11-14');
+ });
+
it('auto-posts every missed recurring occurrence while catching up', async () => {
MockDate.set(new Date(2016, 11, 31, 12));
schedulesApp.startServices();
diff --git a/packages/loot-core/src/server/schedules/app.ts b/packages/loot-core/src/server/schedules/app.ts
index de582a9cf0..52d07d05b0 100644
--- a/packages/loot-core/src/server/schedules/app.ts
+++ b/packages/loot-core/src/server/schedules/app.ts
@@ -22,7 +22,7 @@ import {
} from '#server/transactions/transaction-rules';
import { undoable } from '#server/undo';
import { RSchedule } from '#server/util/rschedule';
-import { currentDay, dayFromDate } from '#shared/months';
+import { currentDay, dayFromDate, parseDate, subDays } from '#shared/months';
import { q } from '#shared/query';
import {
DEFAULT_UPCOMING_SCHEDULE_DAYS,
@@ -209,6 +209,20 @@ async function fixRuleForSchedule(id) {
return getRules().find(rule => rule.id === newId);
}
+// `getNextDate` only looks at occurrences from `today` on, before moving them
+// off the weekend, so it misses an earlier occurrence moved `after` the
+// weekend onto `today` or later. `getNextDateAfter` sees that one, and it is
+// only used when it comes first, so every other case keeps its next date.
+function getNextDateFrom(dateCond, today: Date) {
+ const nextDate = getNextDate(dateCond, today);
+ const movedOntoToday = getNextDateAfter(dateCond, subDays(today, 1));
+
+ return movedOntoToday != null &&
+ (nextDate == null || movedOntoToday < nextDate)
+ ? movedOntoToday
+ : nextDate;
+}
+
export async function setNextDate({
id,
conditions,
@@ -238,7 +252,7 @@ export async function setNextDate({
if (dateCond) {
const newNextDate = advance
? getNextDateAfter(dateCond, nextDate)
- : getNextDate(dateCond, new Date());
+ : getNextDateFrom(dateCond, new Date());
if (newNextDate != null && newNextDate !== nextDate) {
// Our `update` functon requires the id of the item and we don't
@@ -335,7 +349,7 @@ export async function createSchedule({
throw new Error('Date is required');
}
- const nextDate = getNextDate(dateCond);
+ const nextDate = getNextDateFrom(dateCond, parseDate(currentDay()));
const nextDateRepr = nextDate ? toDateRepr(nextDate) : null;
const scheduleFields = schedule && {
...schedule,
diff --git a/upcoming-release-notes/fix-schedule-next-date-after-weekend.md b/upcoming-release-notes/fix-schedule-next-date-after-weekend.md
new file mode 100644
index 0000000000..c4dcbe2377
--- /dev/null
+++ b/upcoming-release-notes/fix-schedule-next-date-after-weekend.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [agigante80]
+---
+
+Fix new and edited schedules skipping a weekend date that moves to the next weekday