feat: implement coordinator notifications for signup changes and cancellations

Added functionality to send notifications to active coordinators when a volunteer signs up, cancels, or changes their signup details. This includes updates to the mail queue and templates to handle coordinator notices, ensuring they receive relevant information without including private signup links. Enhanced the database schema to support these notifications and updated tests to verify the new behavior.
This commit is contained in:
blake committed 2026-10-28 16:35:22 -05:00
1 parent 208f08753e
commit e9bc80cd95
12 files changed
+668 -10

No files matched your search

+14 -2
View File
@@ -22,7 +22,7 @@ type ClaimedRow = {
};
/**
* Claims due confirmation, change, cancellation, reminder, and sign-in mail, then sends outside the database transaction.
* Claims due confirmation, change, cancellation, reminder, sign-in, and coordinator notice mail, then sends outside the database transaction.
* An expired sending lease becomes delivery_unknown and is not sent again.
* sent_at means the provider accepted the message.
*/
@@ -72,7 +72,7 @@ function claimNext(db: Database.Database, now: string): ClaimedRow | null {
.prepare(
`SELECT id AS id
FROM outbox
WHERE status = 'pending' AND send_after <= ? AND kind IN ('confirmation', 'cancelled', 'changed', 'reminder', 'magic_link')
WHERE status = 'pending' AND send_after <= ? AND kind IN ('confirmation', 'cancelled', 'changed', 'reminder', 'magic_link', 'notice')
ORDER BY id
LIMIT 1`,
)
@@ -97,6 +97,7 @@ function claimNext(db: Database.Database, now: string): ClaimedRow | null {
function obsoleteReason(db: Database.Database, row: ClaimedRow, now: string): string | null {
if (row.kind === 'magic_link') return magicLinkReason(db, row, now);
if (row.kind === 'notice') return noticeReason(db, row);
if (row.signup_id === null) return 'The signup is no longer available.';
const signup = db
.prepare(
@@ -134,6 +135,17 @@ function obsoleteReason(db: Database.Database, row: ClaimedRow, now: string): st
return null;
}
function noticeReason(db: Database.Database, row: ClaimedRow): string | null {
if (row.signup_id === null) return 'The signup is no longer available.';
const signup = db.prepare('SELECT anonymized_at AS anonymized_at FROM signups WHERE id = ?').get(row.signup_id) as
| { anonymized_at: string | null }
| undefined;
if (!signup || signup.anonymized_at) return 'The signup is no longer available.';
const coordinator = db.prepare('SELECT id AS id FROM coordinators WHERE email = ? AND active = 1').get(row.to_email);
if (!coordinator) return 'That coordinator no longer has access.';
return null;
}
function magicLinkReason(db: Database.Database, row: ClaimedRow, now: string): string | null {
const match = /^magic_link:([a-f0-9]{64})$/.exec(row.dedupe_key);
const hash = match?.[1];
+79 -1
View File
@@ -1,6 +1,16 @@
import type Database from 'better-sqlite3';
import { absoluteUrl } from '../url.js';
import { renderCancellation, renderChanged, renderConfirmation, renderMagicLink, renderMoved, renderSwitched, type MailShift } from './templates.js';
import {
renderCancellation,
renderChanged,
renderConfirmation,
renderCoordinatorNotice,
renderMagicLink,
renderMoved,
renderSwitched,
type CoordinatorNoticeAction,
type MailShift,
} from './templates.js';
import type { MailIdentity } from './types.js';
export type QueuedSignup = {
@@ -54,6 +64,74 @@ export function queueChanged(db: Database.Database, input: QueuedSignup, reason:
});
}
/**
* One roster notice per active coordinator.
* The private signup link is not included. The same signup and change does not add a second row.
*/
export function queueCoordinatorNotices(
db: Database.Database,
input: {
action: CoordinatorNoticeAction;
signupId: number;
shiftId: number;
shiftVersion: number;
volunteerName: string;
volunteerEmail: string;
groupSize?: number;
shift: MailShift;
fromShift?: MailShift;
mail: MailIdentity;
now: string;
},
): void {
const coordinators = db
.prepare(
`SELECT name AS name, email AS email
FROM coordinators
WHERE active = 1
ORDER BY id`,
)
.all() as { name: string; email: string }[];
if (coordinators.length === 0) return;
const phoneRow = db.prepare('SELECT phone AS phone FROM signups WHERE id = ?').get(input.signupId) as
| { phone: string | null }
| undefined;
const rosterUrl = absoluteUrl(input.mail, `/admin/shifts/${input.shiftId}`);
const insert = db.prepare(
`INSERT INTO outbox (
dedupe_key, signup_id, shift_id, kind, to_email, subject, body_text, body_html,
headers_json, send_after, attempts, status, created_at
) VALUES (?, ?, ?, 'notice', ?, ?, ?, ?, ?, ?, 0, 'pending', ?)
ON CONFLICT (dedupe_key) DO NOTHING`,
);
for (const coordinator of coordinators) {
const rendered = renderCoordinatorNotice({
coordinatorName: coordinator.name,
volunteerName: input.volunteerName,
volunteerEmail: input.volunteerEmail,
phone: phoneRow?.phone ?? null,
groupSize: input.groupSize,
action: input.action,
shift: input.shift,
fromShift: input.fromShift,
rosterUrl,
replyTo: input.mail.replyTo,
});
insert.run(
`notice:${input.action}:v${input.shiftVersion}:${input.signupId}:${coordinator.email}`,
input.signupId,
input.shiftId,
coordinator.email,
rendered.subject,
rendered.text,
rendered.html,
JSON.stringify({ From: input.mail.from, 'Reply-To': input.mail.replyTo }),
input.now,
input.now,
);
}
}
export function queueMagicLink(
db: Database.Database,
input: { email: string; token: string; tokenHash: string; mail: MailIdentity; now: string },
+98
View File
@@ -165,6 +165,66 @@ export function renderReminder(input: {
return { subject, text: lines.join('\n'), html };
}
export type CoordinatorNoticeAction = 'signup' | 'cancel' | 'switch' | 'move' | 'change';
/** A roster notice for coordinators. It names the person and links to the shift. It does not include the private signup link. */
export function renderCoordinatorNotice(input: {
coordinatorName: string;
volunteerName: string;
volunteerEmail: string;
phone: string | null;
groupSize?: number;
action: CoordinatorNoticeAction;
shift: MailShift;
fromShift?: MailShift;
rosterUrl: string;
replyTo: string;
}): RenderedMail {
const copy = noticeCopy(input);
const greeting = `Hello ${singleLine(input.coordinatorName)},`;
const phone = input.phone ? singleLine(input.phone) : '';
const contact = [singleLine(input.volunteerName), singleLine(input.volunteerEmail), ...(phone ? [phone] : [])];
const when = input.shift.whenLabel;
const place = input.shift.location.trim();
const lines = [
greeting,
'',
copy.hello,
'',
...contact,
'',
input.shift.eventTitle,
when,
...(place ? [place] : []),
'',
'Open this shift:',
input.rosterUrl,
'',
'That page lists the roster. Sign in if you are not already.',
`Replies go to ${input.replyTo}.`,
'',
'Hope Food Pantry',
];
const htmlPlace = place ? `<br>${escapeHtml(place)}` : '';
const html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>${escapeHtml(copy.subject)}</title>
</head>
<body style="margin:0;padding:24px;background:#f4fafd;color:#222;font-family:sans-serif;line-height:1.5">
<p>${escapeHtml(greeting)}</p>
<p>${escapeHtml(copy.hello)}</p>
<p>${contact.map((line) => escapeHtml(line)).join('<br>')}</p>
<p>${escapeHtml(input.shift.eventTitle)}<br>${escapeHtml(when)}${htmlPlace}</p>
<p><a href="${escapeHtml(input.rosterUrl)}" style="color:#222">Open this shift</a></p>
<p>That page lists the roster. Sign in if you are not already. Replies go to ${escapeHtml(input.replyTo)}.</p>
<p>Hope Food Pantry</p>
</body>
</html>`;
return { subject: copy.subject, text: lines.join('\n'), html };
}
export function renderMagicLink(input: { signInUrl: string; replyTo: string }): RenderedMail {
const subject = 'Sign in to Hope Volunteer';
const hello = 'Use this link to sign in. It expires in 15 minutes and works once.';
@@ -187,6 +247,44 @@ export function renderMagicLink(input: { signInUrl: string; replyTo: string }):
return { subject, text, html };
}
function noticeCopy(input: {
volunteerName: string;
groupSize?: number;
action: CoordinatorNoticeAction;
shift: MailShift;
fromShift?: MailShift;
}): { subject: string; hello: string } {
const name = singleLine(input.volunteerName);
const role = singleLine(input.shift.role);
const party = partyNote(input.groupSize);
const from = input.fromShift
? ` from ${singleLine(input.fromShift.role)} (${singleLine(input.fromShift.whenLabel)})`
: '';
if (input.action === 'signup') {
return { subject: `${name} signed up for ${role}`, hello: `${name} signed up for ${role}.${party}` };
}
if (input.action === 'cancel') {
const spots =
input.groupSize && input.groupSize > 1
? `The ${input.groupSize} spots this signup held are open for someone else.`
: 'The spot is open for someone else.';
return {
subject: `${name}'s signup for ${role} was cancelled`,
hello: `${name}'s signup for ${role} is cancelled. ${spots}`,
};
}
if (input.action === 'switch') {
return { subject: `${name} switched to ${role}`, hello: `${name} switched${from} to ${role}.${party}` };
}
if (input.action === 'move') {
return { subject: `${name} was moved to ${role}`, hello: `${name} was moved${from} to ${role}.${party}` };
}
return {
subject: `${name}'s ${role} shift changed`,
hello: `The time or place for ${name}'s ${role} shift has changed.${party}`,
};
}
function partyNote(groupSize: number | undefined): string {
if (!groupSize || groupSize <= 1) return '';
return ` This signup holds ${groupSize} spots.`;