From 77a9a0caff425f2e8030728fce9635d3771a443b Mon Sep 17 00:00:00 2001 From: blake Date: Mon, 2 Nov 2026 14:17:10 -0600 Subject: [PATCH] feat: email coordinators only for volunteer actions A location change now sends one email per person and asks how many people that reaches before it is saved, without replacing their signup links. --- README.md | 4 +- migrations/011_location_mail.sql | 66 +++++++++++++ src/admin-http.tsx | 68 +++++++++++-- src/admin-pages.tsx | 29 +++++- src/admin/store.ts | 165 +++++++++++++++++++++++-------- src/mail/deliver.ts | 33 ++++++- src/mail/queue.ts | 102 +++++++++++++++++++ src/mail/templates.ts | 46 +++++++++ src/reminders.ts | 56 +++++++++++ src/signup.ts | 26 ++--- tests/admin.test.ts | 72 ++++++++++++++ tests/coordinator-notice.test.ts | 142 +++++++++++++++++++++++--- 12 files changed, 725 insertions(+), 84 deletions(-) create mode 100644 migrations/011_location_mail.sql diff --git a/README.md b/README.md index 4a905ee..aeb1200 100644 --- a/README.md +++ b/README.md @@ -76,11 +76,11 @@ An open shift has a signup form. It asks for one person. Another person can use With `MAIL_DRIVER=console`, the development server tries to send queued mail every 30 seconds and prints each accepted message in its terminal. That is not delivery to a real inbox. `sent` means the driver accepted the message. A lost result is marked uncertain and is not sent again. SMTP2GO is the mail provider. Set `MAIL_DRIVER=smtp2go` and `SMTP2GO_API_KEY` to send through it. Postmark and SES remain available. -Each active coordinator gets an email when someone signs up, cancels, or changes a signup. Signing up includes a walk-in. A change is a switch, a move, or a new time, role, or place. Cancelling a shift sends one message for each person who was signed up. The message names the person, the email address, the phone number, and the shift, and it links to that shift. It does not include the private signup link. The same signup does not send a second copy of the same notice. A coordinator who has been removed, or whose sign-in address was replaced, does not receive a notice that is still waiting. Mail already accepted cannot be recalled. +Each active coordinator gets an email when a volunteer signs up, cancels, switches shifts, or changes how many people are coming. A walk-in, a roster move, a cancellation a coordinator records, and a change to the time, role, or place do not email coordinators. The message names the person, the email address, the phone number, and the shift, and it links to that shift. It does not include the private signup link. The same signup does not send a second copy of the same notice. A coordinator who has been removed, or whose sign-in address was replaced, does not receive a notice that is still waiting. Mail already accepted cannot be recalled. Coordinator sign-in is at `/login`. The first time the app starts with an empty coordinators table, it creates one administrator from `BOOTSTRAP_ADMIN_EMAIL`. The name on that account is Administrator. It does not create another account on later starts, and it does not change an account that is already there. More administrators are added from **Administrators** after someone is signed in. Sign-in emails a link that expires in 15 minutes and works once. Opening the link does not sign you in. Use the button on that page. The link is printed in the development server log with the rest of the mail. There is no password. -After sign-in, `/admin` lists events, including events that are off the public list. A coordinator can add and edit an event, show or hide it, add and edit a one-off shift, read that shift's roster, cancel one signup, or cancel the shift. Cancelling a shift marks each confirmed signup cancelled, queues a cancellation email, and takes that shift off the upcoming list. The shift stays stored, so a template does not add that date again. **Cancel selected**, on the event page, does this for each open shift that is checked. A shift that has already ended is left out of that batch. A time, role, or location change emails the people who are signed up and replaces their private links. The old link stops working when that email is queued. +After sign-in, `/admin` lists events, including events that are off the public list. A coordinator can add and edit an event, show or hide it, add and edit a one-off shift, read that shift's roster, cancel one signup, or cancel the shift. Cancelling a shift marks each confirmed signup cancelled, queues a cancellation email, and takes that shift off the upcoming list. The shift stays stored, so a template does not add that date again. **Cancel selected**, on the event page, does this for each open shift that is checked. A shift that has already ended is left out of that batch. A time or role change emails the people who are signed up and replaces their private links. The old link stops working when that email is queued. A location change emails each person once, covering every shift of theirs that has not ended, and leaves those signup links in place. Saving shows how many people that email will reach before it is sent. On an open shift, **Add a walk-in** records one person who is here. It takes one spot, uses the same person rule as the public form, and queues a confirmation email. The name is listed on the public sign-up page when that shift is posted there. Email and notes stay off that page. A full, cancelled, or ended shift does not take a walk-in. diff --git a/migrations/011_location_mail.sql b/migrations/011_location_mail.sql new file mode 100644 index 0000000..909ae91 --- /dev/null +++ b/migrations/011_location_mail.sql @@ -0,0 +1,66 @@ +-- foreign-keys: off +-- A location change uses outbox kind location: one letter per person, with no new private link. +-- Rebuilding outbox changes its CHECK list. reminders_sent references it, so foreign keys are off for this file. + +CREATE TABLE outbox_next ( + id INTEGER PRIMARY KEY, + dedupe_key TEXT NOT NULL UNIQUE, + signup_id INTEGER NULL REFERENCES signups (id) ON DELETE NO ACTION, + shift_id INTEGER NULL REFERENCES shifts (id) ON DELETE NO ACTION, + -- confirmation, changed, cancelled, reminder, magic_link, manage_recovery, notice, location + kind TEXT NOT NULL CHECK ( + kind IN ('confirmation', 'changed', 'cancelled', 'reminder', 'magic_link', 'manage_recovery', 'notice', 'location') + ), + -- Pending mail can contain raw manage or login links. Treat the row as sensitive. + to_email TEXT NOT NULL, + subject TEXT NOT NULL, + body_text TEXT NOT NULL, + body_html TEXT NOT NULL, + headers_json TEXT NOT NULL DEFAULT '{}' CHECK (json_valid(headers_json)), + send_after TEXT NOT NULL CHECK ( + send_after 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' + ), + attempts INTEGER NOT NULL DEFAULT 0 CHECK (attempts >= 0), + status TEXT NOT NULL CHECK ( + status IN ('pending', 'sending', 'sent', 'failed', 'cancelled', 'delivery_unknown') + ), + provider_id TEXT NULL, + last_error TEXT NULL, + created_at TEXT NOT NULL CHECK ( + created_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' + ), + claimed_at TEXT NULL CHECK ( + claimed_at IS NULL + OR claimed_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' + ), + lease_expires_at TEXT NULL CHECK ( + lease_expires_at IS NULL + OR lease_expires_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' + ), + -- sent_at means the provider accepted the message, not that the person received it. + sent_at TEXT NULL CHECK ( + sent_at IS NULL + OR sent_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' + ), + delivery_unknown_at TEXT NULL CHECK ( + delivery_unknown_at IS NULL + OR delivery_unknown_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' + ) +); + +INSERT INTO outbox_next ( + id, dedupe_key, signup_id, shift_id, kind, to_email, subject, body_text, body_html, + headers_json, send_after, attempts, status, provider_id, last_error, created_at, + claimed_at, lease_expires_at, sent_at, delivery_unknown_at +) +SELECT + id, dedupe_key, signup_id, shift_id, kind, to_email, subject, body_text, body_html, + headers_json, send_after, attempts, status, provider_id, last_error, created_at, + claimed_at, lease_expires_at, sent_at, delivery_unknown_at +FROM outbox; + +DROP TABLE outbox; + +ALTER TABLE outbox_next RENAME TO outbox; + +CREATE INDEX outbox_by_status_send_after ON outbox (status, send_after); diff --git a/src/admin-http.tsx b/src/admin-http.tsx index eb69921..085005d 100644 --- a/src/admin-http.tsx +++ b/src/admin-http.tsx @@ -19,6 +19,7 @@ import { cancelShift, cancelShifts, cancelSignupAsCoordinator, + countLocationRecipients, createEvent, createShift, createTemplate, @@ -49,6 +50,7 @@ import { AdminHome, EventEditor, eventRemovalPrompt, + locationChangePrompt, LoginConfirmPage, LoginPage, MessagePage, @@ -762,6 +764,7 @@ function showEvent( eventId = idFrom(c), scheduleNote?: string, notice?: string, + values?: EventValues, ) { const session = requireSession(c, services); if (session instanceof Response) return session; @@ -776,14 +779,16 @@ function showEvent( heading={event.title} action={appPath(services.config, `/admin/events/${event.id}`)} csrf={session.csrf} - values={{ - title: event.title, - slug: event.slug, - description: event.description, - location: event.location, - tz: event.tz, - published: event.published, - }} + values={ + values ?? { + title: event.title, + slug: event.slug, + description: event.description, + location: event.location, + tz: event.tz, + published: event.published, + } + } event={event} error={error} notice={notice ?? knownNotice(c.req.query('notice'))} @@ -803,10 +808,39 @@ async function submitEvent(c: Context, services: AdminServices): Promise 0 ? 'saved-location' : 'saved'; + return c.redirect(appPath(services.config, `/admin/events/${id}?notice=${notice}`), 303); } catch (err) { return saveFailed(c, services, session, err); } @@ -1606,6 +1640,19 @@ function localToday(tz: string, now: string): string { return Temporal.Instant.from(now).toZonedDateTimeISO(tz).toPlainDate().toString(); } +function locationConfirmFields(values: EventValues): Record { + const fields: Record = { + title: values.title, + slug: values.slug, + description: values.description, + location: values.location, + tz: values.tz, + confirm_location: 'yes', + }; + if (values.published) fields.published = 'yes'; + return fields; +} + function blankEvent(tz: string): EventValues { return { title: '', slug: '', description: '', location: '', tz, published: false }; } @@ -1614,6 +1661,7 @@ function knownNotice(value: string | undefined): string | undefined { if (!value) return undefined; if ( value === 'saved' || + value === 'saved-location' || value === 'signup-cancelled' || value === 'already-cancelled' || value === 'shift-cancelled' || diff --git a/src/admin-pages.tsx b/src/admin-pages.tsx index 03c2234..d90d18e 100644 --- a/src/admin-pages.tsx +++ b/src/admin-pages.tsx @@ -38,6 +38,7 @@ export type ConfirmPrompt = { note?: string; submit?: string; error?: string; + fields?: Record; }; const SHIFT_DELETE_BODY = 'This deletes the shift. It does not send email.'; @@ -103,6 +104,24 @@ export function signupCancelPrompt(signup: { name: string | null; status: string }; } +export function locationChangePrompt(people: number, location: string): Omit { + const place = location.trim(); + const count = people === 1 ? '1 person' : `${people} people`; + const body = + people === 0 + ? 'No one is signed up for a shift that has not ended. This does not send email.' + : people === 1 + ? 'This emails 1 person about the new location. Their signup link stays the same.' + : `This emails ${people} people about the new location. Each person gets one email. Their signup links stay the same.`; + return { + title: 'Change the location', + detail: count, + note: place ? `New location: ${place}` : 'The address will be removed.', + body, + submit: people === 0 ? 'Save location' : 'Save and email', + }; +} + export function shiftCancelPrompt(shift: { role: string; whenLabel?: string; cancelled: boolean; confirmed: number }): Omit { if (shift.cancelled) { return { title: `Cancel ${shift.role}`, detail: whenDetail(shift.whenLabel), body: SHIFT_ALREADY }; @@ -897,7 +916,7 @@ export function EventEditor(props: { Event details {event.location.trim() || 'No address yet'}

-
+
Edit details