diff --git a/README.md b/README.md index f50f029..4a905ee 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ Saving a weekly template updates shifts from that template that have not ended, **Remove from roster** deletes a cancelled signup, including the name, email, and notes. A confirmed signup has to be cancelled first. **Remove this shift** deletes a cancelled or ended shift after the roster is empty. If that date came from a weekly template that is still active, saving that template can add the date again. **Remove this template** deletes an ended template. Any shifts left are no longer tied to that weekly pattern. Cancelled shifts stay off the upcoming list. **Remove this event** asks for confirmation, then deletes that event and the shifts, weekly templates, and signups on it, including names and emails. An event on the public list comes off that list. It does not send email. Other events stay. -A reminder is queued at each saved time before a confirmed signup, while the shift has not started. The defaults are 48 hours before and 2 hours before. The check runs every five minutes. If the app was off and more than one reminder is already due, only the closest one is sent. A person who signs up after an earlier reminder was due does not get that missed reminder. A reminder is not sent after the shift starts. Changing the shift, moving the signup, or cancelling it drops a reminder that has not been sent yet. Mail that was already accepted cannot be recalled. **Stop reminder emails**, on the private signup page or in the reminder itself, turns reminders off for that signup. The signup stays. Confirmation, change, and cancellation emails still arrive. The reminder asks the person to use the link in the confirmation email to cancel. Mail sent to the reply-to address is read by the pantry. +A reminder is queued at each saved time before a confirmed signup, while the shift has not started. The defaults are 48 hours before and 2 hours before. The check runs every five minutes. If the app was off and more than one reminder is already due, only the closest one is sent. A person who signs up after an earlier reminder was due does not get that missed reminder. The same applies after a shift change or a move: a reminder that was already due is not sent. A reminder is not sent after the shift starts. Changing the shift, moving the signup, or cancelling it drops a reminder that has not been sent yet. Mail that was already accepted cannot be recalled. **Stop reminder emails**, on the private signup page or in the reminder itself, turns reminders off for that signup. The signup stays. Confirmation, change, and cancellation emails still arrive. The reminder asks the person to use the link in the confirmation email to cancel. Mail sent to the reply-to address is read by the pantry. **Download this roster**, on a shift, and **Download this event**, on an event, save a spreadsheet of the signups. **Download this date range**, on the coordinator page, uses each event's local shift date. The file includes cancelled signups. A cell that would be a spreadsheet formula is saved as text. The download is not cached. A copy on a coordinator's computer is outside this app, and the app does not delete it later. diff --git a/migrations/010_shift_version_at.sql b/migrations/010_shift_version_at.sql new file mode 100644 index 0000000..19eb2ce --- /dev/null +++ b/migrations/010_shift_version_at.sql @@ -0,0 +1,14 @@ +-- version_at is when this shift's version last changed. +-- A reminder due before that time, or before the signup, is not sent. +ALTER TABLE shifts ADD COLUMN version_at TEXT NULL CHECK ( + version_at IS NULL + OR version_at GLOB '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9]Z' +); + +UPDATE shifts +SET version_at = ( + SELECT MAX(created_at) + FROM outbox + WHERE outbox.shift_id = shifts.id AND outbox.kind = 'changed' +) +WHERE version > 1; diff --git a/src/admin/store.ts b/src/admin/store.ts index 6b805ae..36e4343 100644 --- a/src/admin/store.ts +++ b/src/admin/store.ts @@ -1626,10 +1626,10 @@ function bumpAndNotify(db: Database.Database, shiftId: number, now: string, mail const bumped = db .prepare( manual - ? 'UPDATE shifts SET version = version + 1, manually_modified = 1 WHERE id = ? AND cancelled = 0' - : 'UPDATE shifts SET version = version + 1 WHERE id = ? AND cancelled = 0 AND manually_modified = 0', + ? 'UPDATE shifts SET version = version + 1, version_at = ?, manually_modified = 1 WHERE id = ? AND cancelled = 0' + : 'UPDATE shifts SET version = version + 1, version_at = ? WHERE id = ? AND cancelled = 0 AND manually_modified = 0', ) - .run(shiftId); + .run(now, shiftId); if (bumped.changes !== 1) return 0; db.prepare( `UPDATE outbox diff --git a/src/reminders.ts b/src/reminders.ts index 8076f25..1e5eb7b 100644 --- a/src/reminders.ts +++ b/src/reminders.ts @@ -14,7 +14,9 @@ type DueSignup = { id: number; name: string; email_norm: string; + created_at: string; version: number; + version_at: string | null; starts_at_utc: string; ends_at_utc: string; role: string; @@ -27,8 +29,9 @@ type DueSignup = { /** * Queues reminders that are due and still before the shift starts. + * A reminder whose due time was before the signup, or before this shift version, is skipped. * When more than one offset is already due, only the closest one is sent. - * Older offsets are recorded as not sent, so a late signup does not get every missed reminder. + * Older offsets are recorded as not sent. * The same signup, offset, and shift version is not queued twice. * Offset hours and the reply-to address come from coordinator settings, or the defaults when none are saved. */ @@ -39,7 +42,8 @@ export function queueDueReminders(db: Database.Database, mail: MailIdentity, sec const rows = db .prepare( `SELECT su.id AS id, su.name AS name, su.email_norm AS email_norm, su.group_size AS group_size, - su.shift_id AS shift_id, s.version AS version, s.starts_at_utc AS starts_at_utc, + su.created_at AS created_at, su.shift_id AS shift_id, s.version AS version, + s.version_at AS version_at, s.starts_at_utc AS starts_at_utc, s.ends_at_utc AS ends_at_utc, s.role AS role, e.title AS title, e.location AS location, e.tz AS tz FROM signups su @@ -59,9 +63,12 @@ export function queueDueReminders(db: Database.Database, mail: MailIdentity, sec const nowInstant = Temporal.Instant.from(now); for (const row of rows) { const start = Temporal.Instant.from(row.starts_at_utc); + const anchor = reminderAnchor(row.created_at, row.version_at); const due = offsets.filter((offset) => { const dueAt = start.subtract({ hours: offset.hours }); - return Temporal.Instant.compare(nowInstant, dueAt) >= 0 && !alreadyQueued(db, row.id, offset.kind, row.version); + return Temporal.Instant.compare(dueAt, anchor) >= 0 + && Temporal.Instant.compare(nowInstant, dueAt) >= 0 + && !alreadyQueued(db, row.id, offset.kind, row.version); }); if (due.length === 0) continue; const nearest = due.reduce((closest, offset) => (offset.hours < closest.hours ? offset : closest)); @@ -136,6 +143,14 @@ export function readReminderOffToken(secret: string, token: string): number | nu return id; } +/** The later of the signup and the current shift version. A move starts a new signup. */ +function reminderAnchor(createdAt: string, versionAt: string | null): Temporal.Instant { + const signedUp = Temporal.Instant.from(createdAt); + if (!versionAt) return signedUp; + const changed = Temporal.Instant.from(versionAt); + return Temporal.Instant.compare(changed, signedUp) > 0 ? changed : signedUp; +} + function alreadyQueued(db: Database.Database, signupId: number, kind: string, version: number): boolean { const row = db .prepare('SELECT signup_id FROM reminders_sent WHERE signup_id = ? AND kind = ? AND shift_version = ?') diff --git a/tests/reminders.test.ts b/tests/reminders.test.ts index b5dca76..7aefd95 100644 --- a/tests/reminders.test.ts +++ b/tests/reminders.test.ts @@ -2,7 +2,7 @@ import { mkdtempSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { afterEach, describe, expect, it } from 'vitest'; -import { updateShift } from '../src/admin/store.js'; +import { moveSignup, updateShift } from '../src/admin/store.js'; import { createApp } from '../src/app.js'; import { bootstrapAdmin } from '../src/auth/bootstrap.js'; import { loadConfig } from '../src/config.js'; @@ -15,6 +15,10 @@ import { reserveSignup, type ReserveInput } from '../src/signup.js'; import { devEnv } from './env.js'; const NOW = '2026-10-14T18:00:00.000Z'; +const EARLY = '2026-10-01T15:00:00.000Z'; +const INSIDE_START = '2026-10-16T00:00:00.000Z'; +const INSIDE_END = '2026-10-16T03:00:00.000Z'; +const INSIDE_NEAR = '2026-10-15T22:00:00.000Z'; const SOON = '2026-10-14T18:30:00.000Z'; const SOON_END = '2026-10-14T20:00:00.000Z'; const LATER_START = '2026-10-15T14:00:00.000Z'; @@ -41,7 +45,7 @@ describe('reminder mail', () => { it('queues the 48 hour reminder, sends it once, and records provider acceptance', async () => { const db = await openDb(); const shiftId = insertShift(db, LATER_START, LATER_END); - reserveSignup(db, reservation(shiftId, { name: 'Pat