A local-first personal finance app
[AI] fix(schedules): allow auto-posting to run again the same day (#8951)
Bar Mdf4dc0d6cccb
diff --git a/packages/loot-core/src/server/schedules/app.test.ts b/packages/loot-core/src/server/schedules/app.test.tsindex e83524ba8c..84d9707166 100644--- a/packages/loot-core/src/server/schedules/app.test.ts+++ b/packages/loot-core/src/server/schedules/app.test.ts@@ -4,6 +4,7 @@ import MockDate from 'mockdate';import { aqlQuery } from '#server/aql';import * as db from '#server/db';import { loadMappings } from '#server/db/mappings';+import * as prefs from '#server/prefs';import { loadRules, updateRule } from '#server/transactions/transaction-rules';import { q } from '#shared/query';import { getNextDate } from '#shared/schedules';@@ -1140,4 +1141,68 @@ describe('schedule app', () => {}});});++ describe('auto-post service throttling', () => {+ const dateCondition = {+ op: 'is',+ field: 'date',+ value: '2020-12-01',+ } as const;++ beforeEach(async () => {+ await prefs.loadPrefs();+ await prefs.savePrefs({ lastScheduleRun: '2020-12-01' });+ });++ afterEach(() => {+ prefs.unloadPrefs();+ });++ it('clears the marker when a schedule that posts transactions is created', async () => {+ await createSchedule({+ schedule: { posts_transaction: true },+ conditions: [dateCondition],+ });++ expect(prefs.getPrefs().lastScheduleRun).toBeUndefined();+ });++ it('keeps the marker when the created schedule does not post transactions', async () => {+ await createSchedule({+ schedule: { posts_transaction: false },+ conditions: [dateCondition],+ });++ expect(prefs.getPrefs().lastScheduleRun).toBe('2020-12-01');+ });++ it('clears the marker when a schedule starts posting transactions', async () => {+ const id = await createSchedule({ conditions: [dateCondition] });++ await updateSchedule({ schedule: { id, posts_transaction: true } });++ expect(prefs.getPrefs().lastScheduleRun).toBeUndefined();+ });++ it('clears the marker when updating a schedule that already posts transactions', async () => {+ const id = await createSchedule({+ schedule: { posts_transaction: true },+ conditions: [dateCondition],+ });++ await prefs.savePrefs({ lastScheduleRun: '2020-12-01' });++ await updateSchedule({ schedule: { id, name: 'Renamed schedule' } });++ expect(prefs.getPrefs().lastScheduleRun).toBeUndefined();+ });++ it('keeps the marker when updating a schedule that does not post transactions', async () => {+ const id = await createSchedule({ conditions: [dateCondition] });++ await updateSchedule({ schedule: { id, name: 'Renamed schedule' } });++ expect(prefs.getPrefs().lastScheduleRun).toBe('2020-12-01');+ });+ });});diff --git a/packages/loot-core/src/server/schedules/app.ts b/packages/loot-core/src/server/schedules/app.tsindex 4b31378182..de582a9cf0 100644--- a/packages/loot-core/src/server/schedules/app.ts+++ b/packages/loot-core/src/server/schedules/app.ts@@ -301,6 +301,23 @@ async function moveSchedule({return {};}+// The auto-posting service is throttled to one run per day through the+// `lastScheduleRun` pref (see the `sync` event listener at the bottom of this+// file). A schedule that posts transactions can be created or edited after+// that run has already happened and still be due today, in which case it+// would not be posted until the next day. Clearing the marker lets the+// service run again for the current day; it only posts occurrences that do+// not have a transaction yet, so an extra run is harmless.+async function allowAutoPostingToRunAgainToday(postsTransaction: boolean) {+ if (!postsTransaction) {+ return;+ }++ if (prefs.getPrefs()?.lastScheduleRun) {+ await prefs.savePrefs({ lastScheduleRun: undefined });+ }+}+export async function createSchedule({schedule = null,conditions = [],@@ -355,6 +372,8 @@ export async function createSchedule({rule: ruleId,});+ await allowAutoPostingToRunAgainToday(scheduleFields?.posts_transaction);+return scheduleId;}@@ -449,6 +468,12 @@ export async function updateSchedule({await db.updateWithSchema('schedules', scheduleFields);});+ const postsTransaction =+ scheduleFields.posts_transaction ??+ (await getSchedule(scheduleFields.id))?.posts_transaction;++ await allowAutoPostingToRunAgainToday(postsTransaction);+return scheduleFields.id;}diff --git a/upcoming-release-notes/fix-schedule-auto-post-same-day.md b/upcoming-release-notes/fix-schedule-auto-post-same-day.mdnew file mode 100644index 0000000000..6bb41093cd--- /dev/null+++ b/upcoming-release-notes/fix-schedule-auto-post-same-day.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [galshir]+---++Post scheduled transactions that automatically add a transaction on their due date, even when the schedule was created or edited later the same day