LifeMate — Personal, Family & Learning Companion
fix(planner): restore future reminder when completion is undone
Quinn White committed
Oct 18, 2026 at 21:46 UTC
9d349815820cc326315eae8203de99b68f1a9b96
2 files changed
+29
-3
backend/src/plan_mutations.js
+14
-3
@@ -116,7 +116,7 @@ async function grants(client, item, selected) {
116
"insert into sharing_grant(resource_type,resource_id,grantee_user_id,granted_by) values('plan_item',$1,$2,$3)", [item.id, member, item.owner_user_id],
117
);
118
}
119
-async function reminders(client, item, payload) {
119
+async function reminders(client, item, payload, previous = null) {
120
if (payload.reminderMinutesBefore !== undefined) {
121
if (!Array.isArray(payload.reminderMinutesBefore) || payload.reminderMinutesBefore.length > 100 || payload.reminderMinutesBefore.some((x) => !Number.isSafeInteger(x) || x < 0 || x > 10080)) fail(400, 'invalid_reminders');
122
await client.query('delete from reminder where plan_item_id=$1', [item.id]);
@@ -126,7 +126,18 @@ async function reminders(client, item, payload) {
126
);
127
}
128
if (['completed', 'cancelled'].includes(item.status)) await client.query("update reminder set status='cancelled' where plan_item_id=$1 and status in ('scheduled','claimed')", [item.id]);
129
- else await client.query('select recompute_reminder_schedule($1)', [item.id]);
129
+ else {
130
+ // Undo before the next scheduled alert restores only this occurrence's
131
+ // existing choices, never an already-past alert or another day's row.
132
+ if (previous && ['completed','cancelled'].includes(previous.status)
133
+ && payload.reminderMinutesBefore === undefined) {
134
+ await client.query(`update reminder set status='scheduled',claimed_at=null,
135
+ delivered_at=null,failure_reason=null where plan_item_id=$1 and status='cancelled'
136
+ and coalesce($2::timestamptz,$3::timestamptz)-make_interval(mins=>minutes_before)>now()`,
137
+ [item.id,item.starts_at,item.due_at]);
138
+ }
139
+ await client.query('select recompute_reminder_schedule($1)', [item.id]);
140
+ }
141
}
142
export async function createPlanItem(client, userId, payload, itemId = crypto.randomUUID(), occurrence = null) {
143
if (!validUuid(itemId)) fail(400, 'invalid_id');
@@ -157,7 +168,7 @@ export async function updatePlanItem(client, userId, itemId, payload, expectedVe
168
const update = [...Object.keys(fields).map((key, i) => `${key}=$${i + 2}`), 'updated_at=now()'];
169
const item = (await client.query(`update plan_item set ${update.join(',')} where id=$1 returning *`, values)).rows[0];
170
await grants(client, item, selected);
160
- await reminders(client, item, payload);
171
+ await reminders(client, item, payload, current);
172
return item;
173
}
174
export async function planTransaction(pool, userId, execute) {
backend/tests/planner_series.test.js
+15
@@ -58,6 +58,21 @@ test('same-id retry and simultaneous materialization do not duplicate occurrence
58
for (const r of await Promise.all([1,2].map(() => request(actor,'POST',`/planner-series/${body.id}/materialize`,{ from:day(2),to:day(5) })))) assert.equal(r.status,200);
59
assert.equal((await rows(body.id)).length,4);
60
});
61
+test('undoing completion restores future reminder once for only that occurrence', async () => {
62
+ const actor=await user();
63
+ const start=new Date(Date.now()+3600000).toISOString(),end=new Date(Date.now()+5400000).toISOString();
64
+ const made=await request(actor,'POST','/plan-items',{title:'Synthetic undo',kind:'routine',startsAt:start,endsAt:end,reminderMinutesBefore:[5]});
65
+ assert.equal(made.status,201);const id=made.body.id;
66
+ const row=async()=> (await pool.query('select status from reminder where plan_item_id=$1',[id])).rows;
67
+ assert.deepEqual(await row(),[{status:'scheduled'}]);
68
+ assert.equal((await request(actor,'PATCH',`/plan-items/${id}`,{expectedVersion:1,status:'completed'})).status,200);
69
+ assert.deepEqual(await row(),[{status:'cancelled'}]);
70
+ assert.equal((await request(actor,'PATCH',`/plan-items/${id}`,{expectedVersion:2,status:'planned'})).status,200);
71
+ assert.deepEqual(await row(),[{status:'scheduled'}]);
72
+ const window=`?from=${encodeURIComponent(new Date().toISOString())}&to=${encodeURIComponent(new Date(Date.now()+86400000).toISOString())}`;
73
+ const list=await request(actor,'GET','/planner-reminders'+window);
74
+ assert.equal(list.status,200);assert.equal(list.body.reminders.filter(r=>r.plan_item_id===id).length,1);
75
+});
76
test('alternate days keep original start anchor when initial materialization begins today', async () => {
77
const actor = await user(), body = config({ startsOn:day(-7),untilOn:day(5),intervalDays:2 });
78
assert.equal((await request(actor,'POST','/planner-series',body)).status,201);