Skip to content

A local-first personal finance app

[AI] Fix auto-post schedule catch-up (#7997)

JinHyuk Sungcf6b2065f988
diff --git a/packages/loot-core/src/server/schedules/app.test.ts b/packages/loot-core/src/server/schedules/app.test.ts
index aca8080139..5d73fd2984 100644
--- a/packages/loot-core/src/server/schedules/app.test.ts
+++ b/packages/loot-core/src/server/schedules/app.test.ts
@@ -2,15 +2,18 @@
import MockDate from 'mockdate';
import { aqlQuery } from '#server/aql';
+import * as db from '#server/db';
import { loadMappings } from '#server/db/mappings';
import { loadRules, updateRule } from '#server/transactions/transaction-rules';
import { q } from '#shared/query';
import { getNextDate } from '#shared/schedules';
import {
+ advanceSchedulesService,
areConditionValuesEqual,
createSchedule,
deleteSchedule,
+ app as schedulesApp,
setNextDate,
skipNextDate,
updateConditions,
@@ -547,5 +550,265 @@ describe('schedule app', () => {
expect(row.next_date).toBe('2020-12-11');
});
+
+ it('auto-posts every missed recurring occurrence while catching up', async () => {
+ 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-05',
+ frequency: 'weekly',
+ interval: 2,
+ patterns: [],
+ },
+ },
+ ],
+ });
+ 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: 20161205,
+ local_next_date_ts: Date.now(),
+ base_next_date: 20161205,
+ base_next_date_ts: Date.now(),
+ });
+
+ 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-05', amount: -10000 },
+ { date: '2016-12-19', amount: -10000 },
+ ]);
+
+ const {
+ data: [schedule],
+ } = await aqlQuery(q('schedules').filter({ id }).select(['next_date']));
+
+ expect(schedule.next_date).toBe('2017-01-02');
+ } finally {
+ MockDate.reset();
+ await schedulesApp.stopServices();
+ }
+ });
+
+ it('continues auto-post catch-up after an already paid occurrence', async () => {
+ 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-05',
+ frequency: 'weekly',
+ interval: 2,
+ patterns: [],
+ },
+ },
+ ],
+ });
+ 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: 20161205,
+ local_next_date_ts: Date.now(),
+ base_next_date: 20161205,
+ base_next_date_ts: Date.now(),
+ });
+ await db.insertTransaction({
+ account: accountId,
+ amount: -10000,
+ date: '2016-12-05',
+ 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-05', amount: -10000 },
+ { date: '2016-12-19', amount: -10000 },
+ ]);
+
+ const {
+ data: [schedule],
+ } = await aqlQuery(q('schedules').filter({ id }).select(['next_date']));
+
+ expect(schedule.next_date).toBe('2017-01-02');
+ } 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();
+
+ 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: '2016-12-05',
+ },
+ ],
+ });
+ await db.insertTransaction({
+ account: accountId,
+ amount: -10000,
+ date: '2016-12-05',
+ schedule: id,
+ });
+
+ await advanceSchedulesService(true);
+
+ const {
+ data: [schedule],
+ } = await aqlQuery(q('schedules').filter({ id }).select(['completed']));
+
+ expect(schedule.completed).toBe(true);
+
+ const { data: transactions } = await aqlQuery(
+ q('transactions').filter({ schedule: id }).select(['date']),
+ );
+
+ expect(transactions).toHaveLength(1);
+ } finally {
+ MockDate.reset();
+ await schedulesApp.stopServices();
+ }
+ });
+
+ it('auto-posts one-time schedules that are due', async () => {
+ 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: '2016-12-31',
+ },
+ ],
+ });
+
+ await advanceSchedulesService(true);
+
+ const { data: transactions } = await aqlQuery(
+ q('transactions').filter({ schedule: id }).select(['date', 'amount']),
+ );
+
+ expect(
+ transactions.map(({ date, amount }) => ({ date, amount })),
+ ).toEqual([{ date: '2016-12-31', amount: -10000 }]);
+ } finally {
+ MockDate.reset();
+ await schedulesApp.stopServices();
+ }
+ });
});
});
diff --git a/packages/loot-core/src/server/schedules/app.ts b/packages/loot-core/src/server/schedules/app.ts
index 27b2d4bd88..d77c8f4fc3 100644
--- a/packages/loot-core/src/server/schedules/app.ts
+++ b/packages/loot-core/src/server/schedules/app.ts
@@ -526,9 +526,64 @@ async function postTransactionForSchedule({
}
}
+async function getSchedule(id: string): Promise<ScheduleEntity | null> {
+ const {
+ data: [schedule],
+ } = await aqlQuery(q('schedules').filter({ id }).select('*'));
+
+ return schedule ?? null;
+}
+
+async function hasTransactionForSchedule(
+ schedule: ScheduleEntity,
+): Promise<boolean> {
+ const { data } = await aqlQuery(getHasTransactionsQuery([schedule]));
+
+ return data.filter(Boolean).some(row => row.schedule === schedule.id);
+}
+
+function isRecurringSchedule(schedule: ScheduleEntity): boolean {
+ return (
+ schedule._date != null &&
+ typeof schedule._date === 'object' &&
+ 'frequency' in schedule._date
+ );
+}
+
+async function advanceRecurringScheduleFromNextDate(
+ schedule: ScheduleEntity,
+): Promise<ScheduleEntity | null> {
+ if (!isRecurringSchedule(schedule)) {
+ return null;
+ }
+
+ const previousNextDate = schedule.next_date;
+
+ try {
+ await setNextDate({
+ id: schedule.id,
+ start: nextDate => d.addDays(parseDate(nextDate), 1),
+ });
+ } catch {
+ // This might error if the rule is corrupted and it can't find the rule.
+ return null;
+ }
+
+ const updatedSchedule = await getSchedule(schedule.id);
+
+ if (
+ updatedSchedule == null ||
+ updatedSchedule.next_date === previousNextDate
+ ) {
+ return null;
+ }
+
+ return updatedSchedule;
+}
+
// TODO: make this sequential
-async function advanceSchedulesService(syncSuccess) {
+export async function advanceSchedulesService(syncSuccess) {
// Move all paid schedules
const { data: schedules } = await aqlQuery(
q('schedules')
@@ -560,10 +615,77 @@ async function advanceSchedulesService(syncSuccess) {
schedule.custom_upcoming_length ?? upcomingLength[0]?.value ?? '7',
);
- if (status === 'paid') {
+ if (
+ schedule.posts_transaction &&
+ schedule._account &&
+ (status !== 'paid' || isRecurringSchedule(schedule)) &&
+ (status === 'paid' || status === 'due' || status === 'missed')
+ ) {
+ let currentSchedule = schedule;
+ let currentStatus = status;
+
+ while (
+ currentSchedule.posts_transaction &&
+ currentSchedule._account &&
+ (currentStatus === 'paid' ||
+ currentStatus === 'due' ||
+ currentStatus === 'missed')
+ ) {
+ if (currentStatus === 'paid') {
+ const updatedSchedule =
+ await advanceRecurringScheduleFromNextDate(currentSchedule);
+
+ if (updatedSchedule == null) {
+ break;
+ }
+
+ currentSchedule = updatedSchedule;
+ currentStatus = getStatus(
+ currentSchedule.next_date,
+ currentSchedule.completed,
+ await hasTransactionForSchedule(currentSchedule),
+ currentSchedule.custom_upcoming_length ??
+ upcomingLength[0]?.value ??
+ '7',
+ );
+ continue;
+ }
+
+ // Automatically create a transaction for due schedules.
+ if (syncSuccess) {
+ await postTransactionForSchedule({ id: currentSchedule.id });
+
+ didPost = true;
+ } else {
+ failedToPost.push(currentSchedule._payee);
+ break;
+ }
+
+ if (!isRecurringSchedule(currentSchedule)) {
+ break;
+ }
+
+ const updatedSchedule =
+ await advanceRecurringScheduleFromNextDate(currentSchedule);
+
+ if (updatedSchedule == null) {
+ break;
+ }
+
+ currentSchedule = updatedSchedule;
+ currentStatus = getStatus(
+ currentSchedule.next_date,
+ currentSchedule.completed,
+ await hasTransactionForSchedule(currentSchedule),
+ currentSchedule.custom_upcoming_length ??
+ upcomingLength[0]?.value ??
+ '7',
+ );
+ }
+ } else if (status === 'paid') {
if (schedule._date) {
// Move forward recurring schedules
- if (schedule._date.frequency) {
+ if (isRecurringSchedule(schedule)) {
try {
await setNextDate({ id: schedule.id });
} catch {
@@ -579,19 +701,6 @@ async function advanceSchedulesService(syncSuccess) {
}
}
}
- } else if (
- (status === 'due' || status === 'missed') &&
- schedule.posts_transaction &&
- schedule._account
- ) {
- // Automatically create a transaction for due schedules
- if (syncSuccess) {
- await postTransactionForSchedule({ id: schedule.id });
-
- didPost = true;
- } else {
- failedToPost.push(schedule._payee);
- }
}
}
diff --git a/upcoming-release-notes/7997.md b/upcoming-release-notes/7997.md
new file mode 100644
index 0000000000..66af7ff8cf
--- /dev/null
+++ b/upcoming-release-notes/7997.md
@@ -0,0 +1,6 @@
+---
+category: Bugfixes
+authors: [sjh9714]
+---
+
+Post every missed auto-post scheduled transaction when catching up.