Skip to content

A local-first personal finance app

fix schedules moved before weekend occasionally becoming stuck (#8854)

Matt Fiddaman35e04473aa32
diff --git a/packages/loot-core/src/server/schedules/app.test.ts b/packages/loot-core/src/server/schedules/app.test.ts
index 01bb3c0adc..e83524ba8c 100644
--- a/packages/loot-core/src/server/schedules/app.test.ts
+++ b/packages/loot-core/src/server/schedules/app.test.ts
@@ -597,6 +597,46 @@ describe('schedule app', () => {
expect(row.next_date).toBe('2020-12-11');
});
+ it('skipNextDate keeps the next occurrence when moving `before` weekend', async () => {
+ /* Dec 2020 calendar for reference:
+ | Su | Mo | Tu | We | Th | Fr | Sa |
+ | | | 01 | 02 | 03 | 04 | 05 |
+ | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
+ | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
+ | 20 | 21 | 22 | 23 | 24 | 25 | 26 |
+ | 27 | 28 | 29 | 30 | 31 |
+ */
+ const id = await createSchedule({
+ conditions: [
+ {
+ op: 'is',
+ field: 'date',
+ value: {
+ start: '2020-12-04',
+ frequency: 'daily',
+ patterns: [],
+ skipWeekend: true,
+ weekendSolveMode: 'before',
+ },
+ },
+ ],
+ });
+
+ let res = await aqlQuery(
+ q('schedules').filter({ id }).select(['next_date']),
+ );
+ let row = res.data[0];
+
+ expect(row.next_date).toBe('2020-12-04');
+
+ await skipNextDate({ id });
+
+ res = await aqlQuery(q('schedules').filter({ id }).select(['next_date']));
+ row = res.data[0];
+
+ expect(row.next_date).toBe('2020-12-07');
+ });
+
it('auto-posts every missed recurring occurrence while catching up', async () => {
MockDate.set(new Date(2016, 11, 31, 12));
schedulesApp.startServices();
@@ -753,6 +793,95 @@ describe('schedule app', () => {
}
});
+ it('catches up an auto-post schedule moved `before` a weekend occurrence', async () => {
+ /* Dec 2016 / Jan 2017 calendar for reference:
+ | Su | Mo | Tu | We | Th | Fr | Sa |
+ | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
+ | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
+ | 01 | 02 | 03 | 04 | 05 | 06 | 07 |
+ */
+ MockDate.set(new Date(2016, 11, 31, 12));
+ schedulesApp.startServices();
+
+ try {
+ const accountId = await db.insertAccount({
+ name: 'Checking',
+ offbudget: 0,
+ closed: 0,
+ });
+
+ const id = await createSchedule({
+ schedule: { posts_transaction: true },
+ conditions: [
+ {
+ op: 'is',
+ field: 'account',
+ value: accountId,
+ },
+ {
+ op: 'is',
+ field: 'amount',
+ value: -10000,
+ },
+ {
+ op: 'is',
+ field: 'date',
+ value: {
+ start: '2016-12-24',
+ frequency: 'weekly',
+ interval: 1,
+ patterns: [],
+ skipWeekend: true,
+ weekendSolveMode: 'before',
+ },
+ },
+ ],
+ });
+ const nextDateRow = await db.first<{
+ id: string;
+ }>('SELECT id FROM schedules_next_date WHERE schedule_id = ?', [id]);
+
+ await db.update('schedules_next_date', {
+ id: nextDateRow.id,
+ local_next_date: 20161223,
+ local_next_date_ts: Date.now(),
+ base_next_date: 20161223,
+ base_next_date_ts: Date.now(),
+ });
+ await db.insertTransaction({
+ account: accountId,
+ amount: -10000,
+ date: '2016-12-23',
+ schedule: id,
+ });
+
+ await advanceSchedulesService(true);
+
+ const { data: transactions } = await aqlQuery(
+ q('transactions')
+ .filter({ schedule: id })
+ .select(['date', 'amount'])
+ .orderBy({ date: 'asc' }),
+ );
+
+ expect(
+ transactions.map(({ date, amount }) => ({ date, amount })),
+ ).toEqual([
+ { date: '2016-12-23', amount: -10000 },
+ { date: '2016-12-30', amount: -10000 },
+ ]);
+
+ const {
+ data: [schedule],
+ } = await aqlQuery(q('schedules').filter({ id }).select(['next_date']));
+
+ expect(schedule.next_date).toBe('2017-01-06');
+ } finally {
+ MockDate.reset();
+ await schedulesApp.stopServices();
+ }
+ });
+
it('completes one-time auto-post schedules that are already paid', 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 e27ecd19e6..4b31378182 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, parseDate } from '#shared/months';
+import { currentDay, dayFromDate } from '#shared/months';
import { q } from '#shared/query';
import {
DEFAULT_UPCOMING_SCHEDULE_DAYS,
@@ -30,6 +30,7 @@ import {
getDateWithSkippedWeekend,
getHasTransactionsQuery,
getNextDate,
+ getNextDateAfter,
getScheduledAmount,
getStatus,
recurConfigToRSchedule,
@@ -210,16 +211,14 @@ async function fixRuleForSchedule(id) {
export async function setNextDate({
id,
- start,
conditions,
reset,
- skipRequested,
+ advance,
}: {
id: string;
- start?;
conditions?;
reset?: boolean;
- skipRequested?: boolean;
+ advance?: boolean;
}) {
if (conditions == null) {
const rule = await getRuleForSchedule(id);
@@ -231,32 +230,15 @@ export async function setNextDate({
const { date: dateCond } = extractScheduleConds(conditions);
- let { data: nextDate } = await aqlQuery(
+ const { data: nextDate } = await aqlQuery(
q('schedules').filter({ id }).calculate('next_date'),
);
- if (skipRequested === true) {
- const skipWeekend: boolean = dateCond.value?.skipWeekend;
- const weekendSolveMode: string = dateCond.value?.weekendSolveMode;
-
- if (weekendSolveMode === 'before' && skipWeekend === true) {
- const parsedNextDate = parseDate(nextDate);
- if (d.isFriday(parsedNextDate) || d.isWeekend(parsedNextDate)) {
- // nextDate is on weekend or friday, moving to monday
- // so getNextDate and getDateWithSkippedWeekend
- // don't push the date back to Friday, thus causing
- // `(newNextDate !== nextDate) ` to be false and not updating the next date
- nextDate = dayFromDate(d.nextMonday(parsedNextDate));
- }
- }
- }
-
// Only do this if a date condition exists
if (dateCond) {
- const newNextDate = getNextDate(
- dateCond,
- start ? start(nextDate) : new Date(),
- );
+ const newNextDate = advance
+ ? getNextDateAfter(dateCond, nextDate)
+ : getNextDate(dateCond, new Date());
if (newNextDate != null && newNextDate !== nextDate) {
// Our `update` functon requires the id of the item and we don't
@@ -482,13 +464,7 @@ export async function deleteSchedule({ id }) {
}
export async function skipNextDate({ id }) {
- return setNextDate({
- id,
- start: nextDate => {
- return d.addDays(parseDate(nextDate), 1);
- },
- skipRequested: true,
- });
+ return setNextDate({ id, advance: true });
}
function discoverSchedules() {
@@ -642,10 +618,7 @@ async function advanceRecurringScheduleFromNextDate(
const previousNextDate = schedule.next_date;
try {
- await setNextDate({
- id: schedule.id,
- start: nextDate => d.addDays(parseDate(nextDate), 1),
- });
+ await setNextDate({ id: schedule.id, advance: true });
} catch {
// This might error if the rule is corrupted and it can't find the rule.
return null;
diff --git a/packages/loot-core/src/shared/schedules.test.ts b/packages/loot-core/src/shared/schedules.test.ts
index 2e4c13f468..5bd000a193 100644
--- a/packages/loot-core/src/shared/schedules.test.ts
+++ b/packages/loot-core/src/shared/schedules.test.ts
@@ -7,6 +7,7 @@ import {
computeSchedulePreviewTransactions,
getHasTransactionsQuery,
getNextDate,
+ getNextDateAfter,
getScheduleOccurrenceMatchStartDate,
getStatus,
getUpcomingDays,
@@ -498,6 +499,121 @@ describe('schedules', () => {
});
});
+ describe('getNextDateAfter', () => {
+ /* Dec 2020 calendar for reference:
+ | Su | Mo | Tu | We | Th | Fr | Sa |
+ | | | 01 | 02 | 03 | 04 | 05 |
+ | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
+ | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
+ | 20 | 21 | 22 | 23 | 24 | 25 | 26 |
+ | 27 | 28 | 29 | 30 | 31 |
+ */
+ function weeklyOnSaturday(extra = {}) {
+ return {
+ op: 'isapprox',
+ value: {
+ start: '2020-12-05',
+ frequency: 'weekly',
+ patterns: [],
+ ...extra,
+ },
+ };
+ }
+
+ it('returns the next occurrence after the given date', () => {
+ expect(getNextDateAfter(weeklyOnSaturday(), '2020-12-05')).toBe(
+ '2020-12-12',
+ );
+ });
+
+ it('returns the next occurrence when moving `after` the weekend', () => {
+ const dateCond = weeklyOnSaturday({
+ skipWeekend: true,
+ weekendSolveMode: 'after',
+ });
+
+ expect(getNextDateAfter(dateCond, '2020-12-07')).toBe('2020-12-14');
+ });
+
+ it('includes a weekend occurrence moved `after` the given date', () => {
+ const dateCond = weeklyOnSaturday({
+ skipWeekend: true,
+ weekendSolveMode: 'after',
+ });
+
+ expect(getNextDateAfter(dateCond, '2020-12-13')).toBe('2020-12-14');
+ });
+
+ it('does not return the same occurrence when moving `after` the weekend', () => {
+ const dateCond = {
+ op: 'isapprox',
+ value: {
+ start: '2020-12-06',
+ frequency: 'weekly',
+ patterns: [],
+ skipWeekend: true,
+ weekendSolveMode: 'after',
+ },
+ };
+
+ expect(getNextDateAfter(dateCond, '2020-12-07')).toBe('2020-12-14');
+ });
+
+ it('does not return the same occurrence when moving `before` the weekend', () => {
+ const dateCond = weeklyOnSaturday({
+ skipWeekend: true,
+ weekendSolveMode: 'before',
+ });
+
+ expect(getNextDateAfter(dateCond, '2020-12-04')).toBe('2020-12-11');
+ });
+
+ it('keeps a Monday occurrence that follows a `before` weekend adjustment', () => {
+ const dateCond = {
+ op: 'isapprox',
+ value: {
+ start: '2020-12-04',
+ frequency: 'daily',
+ patterns: [],
+ skipWeekend: true,
+ weekendSolveMode: 'before',
+ },
+ };
+
+ expect(getNextDateAfter(dateCond, '2020-12-04')).toBe('2020-12-07');
+ });
+
+ it('walks past every occurrence that resolves on or before the given date', () => {
+ /* Aug 2026 calendar for reference:
+ | Su | Mo | Tu | We | Th | Fr | Sa |
+ | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
+ | 30 | 31 |
+ */
+ const dateCond = {
+ op: 'isapprox',
+ value: {
+ start: '2026-08-24',
+ frequency: 'daily',
+ patterns: [],
+ skipWeekend: true,
+ weekendSolveMode: 'before',
+ },
+ };
+
+ expect(getNextDateAfter(dateCond, '2026-08-28')).toBe('2026-08-31');
+ });
+
+ it('returns null when the schedule has no further occurrences', () => {
+ const dateCond = weeklyOnSaturday({
+ endMode: 'after_n_occurrences',
+ endOccurrences: 2,
+ });
+
+ expect(getNextDateAfter(dateCond, '2020-12-05')).toBe('2020-12-12');
+ expect(getNextDateAfter(dateCond, '2020-12-12')).toBeNull();
+ });
+ });
+
describe('shared presets', () => {
it('preset values and options align', () => {
const valuesFromOptions = UPCOMING_LENGTH_PRESET_OPTIONS.map(
diff --git a/packages/loot-core/src/shared/schedules.ts b/packages/loot-core/src/shared/schedules.ts
index 9f9a2d4adb..b83cb4b956 100644
--- a/packages/loot-core/src/shared/schedules.ts
+++ b/packages/loot-core/src/shared/schedules.ts
@@ -315,6 +315,28 @@ export function getNextDate(
return null;
}
+const MAX_ADVANCE_ATTEMPTS = 7;
+
+export function getNextDateAfter(dateCond, afterDate: string): string | null {
+ let start = d.subDays(monthUtils.parseDate(afterDate), 1);
+
+ for (let attempt = 0; attempt < MAX_ADVANCE_ATTEMPTS; attempt++) {
+ const occurrence = getNextDate(dateCond, start, true);
+ if (occurrence == null || occurrence < monthUtils.dayFromDate(start)) {
+ return null;
+ }
+
+ const nextDate = getNextDate(dateCond, start);
+ if (nextDate != null && nextDate > afterDate) {
+ return nextDate;
+ }
+
+ start = d.addDays(monthUtils.parseDate(occurrence), 1);
+ }
+
+ return null;
+}
+
export function getDateWithSkippedWeekend(
date: Date,
solveMode: 'after' | 'before',
diff --git a/upcoming-release-notes/fix-schedules-stuck-before-weekend.md b/upcoming-release-notes/fix-schedules-stuck-before-weekend.md
new file mode 100644
index 0000000000..6aa755ae82
--- /dev/null
+++ b/upcoming-release-notes/fix-schedules-stuck-before-weekend.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [matt-fidd]
+---
+
+Fix schedules set to run before the weekend getting stuck on the same date and no longer posting