Skip to content

A local-first personal finance app

[AI] Preserve skipped schedule dates when editing schedules (#9173)

Som Samantray8e6eb272a792
diff --git a/packages/loot-core/src/server/schedules/app.test.ts b/packages/loot-core/src/server/schedules/app.test.ts
index ff85d25b9f..45c2fd6036 100644
--- a/packages/loot-core/src/server/schedules/app.test.ts
+++ b/packages/loot-core/src/server/schedules/app.test.ts
@@ -352,6 +352,56 @@ describe('schedule app', () => {
expect(row.next_date).toBe('2020-12-30');
});
+ it('preserves skipped dates on rename but resets them when the account changes', async () => {
+ MockDate.set(new Date(2021, 4, 17));
+
+ const accountId = await db.insertAccount({ name: 'Checking' });
+ const otherAccountId = await db.insertAccount({ name: 'Savings' });
+ const id = await createSchedule({
+ conditions: [
+ { op: 'is', field: 'account', value: accountId },
+ {
+ op: 'is',
+ field: 'date',
+ value: {
+ start: '2021-05-17',
+ frequency: 'monthly',
+ patterns: [{ type: 'day', value: 17 }],
+ },
+ },
+ ],
+ });
+
+ await skipNextDate({ id });
+ const {
+ data: [schedule],
+ } = await aqlQuery(q('schedules').filter({ id }).select(['_conditions']));
+ // Advance the clock so a reset stamps a newer `base_next_date_ts` than the skip
+ MockDate.set(new Date(2021, 4, 17, 0, 1));
+
+ await updateSchedule({
+ schedule: { id, name: 'Broadband' },
+ conditions: schedule._conditions,
+ });
+ let result = await aqlQuery(
+ q('schedules').filter({ id }).calculate('next_date'),
+ );
+ expect(result.data).toBe('2021-06-17');
+
+ await updateSchedule({
+ schedule: { id },
+ conditions: schedule._conditions.map(condition =>
+ condition.value === accountId
+ ? { ...condition, value: otherAccountId }
+ : condition,
+ ),
+ });
+ result = await aqlQuery(
+ q('schedules').filter({ id }).calculate('next_date'),
+ );
+ expect(result.data).toBe('2021-05-17');
+ });
+
it('updateSchedule ignores the condition `type` field when date value is unchanged', async () => {
const id = await createSchedule({
conditions: [
diff --git a/packages/loot-core/src/server/schedules/app.ts b/packages/loot-core/src/server/schedules/app.ts
index 52d07d05b0..d83760866e 100644
--- a/packages/loot-core/src/server/schedules/app.ts
+++ b/packages/loot-core/src/server/schedules/app.ts
@@ -98,8 +98,10 @@ function areScheduleConditionsEqual(
return left === right;
}
- const { type: _leftType, ...leftCondition } = left;
- const { type: _rightType, ...rightCondition } = right;
+ // `type` is not always present, and `field` may use the public or internal
+ // name (`account` vs `acct`), so compare the remaining properties
+ const { type: _leftType, field: _leftField, ...leftCondition } = left;
+ const { type: _rightType, field: _rightField, ...rightCondition } = right;
return areConditionValuesEqual(leftCondition, rightCondition);
}
@@ -458,12 +460,11 @@ export async function updateSchedule({
// or date changed. We check account because we don't update
// schedules automatically for closed account, and the user
// might switch accounts from a closed one
+ const { account: oldAccountCond } = extractScheduleConds(oldConditions);
+ const { account: newAccountCond } = extractScheduleConds(newConditions);
if (
resetNextDate ||
- !areScheduleConditionsEqual(
- oldConditions.find(c => c.field === 'account'),
- newConditions.find(c => c.field === 'account'),
- ) ||
+ !areScheduleConditionsEqual(oldAccountCond, newAccountCond) ||
!areConditionValuesEqual(
stripType(oldConditions.find(c => c.field === 'date') || {}),
stripType(newConditions.find(c => c.field === 'date') || {}),
diff --git a/upcoming-release-notes/preserve-skipped-schedule-date.md b/upcoming-release-notes/preserve-skipped-schedule-date.md
new file mode 100644
index 0000000000..c68f328b0c
--- /dev/null
+++ b/upcoming-release-notes/preserve-skipped-schedule-date.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [SomSamantray]
+---
+
+Keep skipped schedule dates when editing a schedule