diff --git a/src/mail/queue.ts b/src/mail/queue.ts index 79dc554..9c0ca68 100644 --- a/src/mail/queue.ts +++ b/src/mail/queue.ts @@ -66,7 +66,8 @@ 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. + * The private signup link is not included. + * The same signup, action, shift version, and party size does not add a second row. */ export function queueCoordinatorNotices( db: Database.Database, @@ -78,6 +79,7 @@ export function queueCoordinatorNotices( volunteerName: string; volunteerEmail: string; groupSize?: number; + previousGroupSize?: number; shift: MailShift; fromShift?: MailShift; mail: MailIdentity; @@ -111,6 +113,7 @@ export function queueCoordinatorNotices( volunteerEmail: input.volunteerEmail, phone: phoneRow?.phone ?? null, groupSize: input.groupSize, + previousGroupSize: input.previousGroupSize, action: input.action, shift: input.shift, fromShift: input.fromShift, @@ -118,7 +121,7 @@ export function queueCoordinatorNotices( replyTo: input.mail.replyTo, }); insert.run( - `notice:${input.action}:v${input.shiftVersion}:${input.signupId}:${coordinator.email}`, + `notice:${input.action}:v${input.shiftVersion}:${input.groupSize ?? 1}:${input.signupId}:${coordinator.email}`, input.signupId, input.shiftId, coordinator.email, diff --git a/src/mail/templates.ts b/src/mail/templates.ts index 8f5812a..260804d 100644 --- a/src/mail/templates.ts +++ b/src/mail/templates.ts @@ -165,7 +165,7 @@ export function renderReminder(input: { return { subject, text: lines.join('\n'), html }; } -export type CoordinatorNoticeAction = 'signup' | 'cancel' | 'switch' | 'move' | 'change'; +export type CoordinatorNoticeAction = 'signup' | 'cancel' | 'switch' | 'move' | 'change' | 'party'; /** 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: { @@ -174,6 +174,7 @@ export function renderCoordinatorNotice(input: { volunteerEmail: string; phone: string | null; groupSize?: number; + previousGroupSize?: number; action: CoordinatorNoticeAction; shift: MailShift; fromShift?: MailShift; @@ -250,6 +251,7 @@ export function renderMagicLink(input: { signInUrl: string; replyTo: string }): function noticeCopy(input: { volunteerName: string; groupSize?: number; + previousGroupSize?: number; action: CoordinatorNoticeAction; shift: MailShift; fromShift?: MailShift; @@ -260,6 +262,7 @@ function noticeCopy(input: { const from = input.fromShift ? ` from ${singleLine(input.fromShift.role)} (${singleLine(input.fromShift.whenLabel)})` : ''; + if (input.action === 'party') return partyChangeCopy(name, role, input.previousGroupSize, input.groupSize ?? 1); if (input.action === 'signup') { return { subject: `${name} signed up for ${role}`, hello: `${name} signed up for ${role}.${party}` }; } @@ -285,6 +288,29 @@ function noticeCopy(input: { }; } +function partyChangeCopy( + name: string, + role: string, + previous: number | undefined, + next: number, +): { subject: string; hello: string } { + const nextPeople = next === 1 ? '1 person' : `${next} people`; + const subject = `${name} changed to ${nextPeople} for ${role}`; + if (!previous || previous === next) { + const spots = next === 1 ? '1 spot' : `${next} spots`; + return { subject, hello: `${name} changed this signup for ${role} to ${nextPeople}. This signup holds ${spots}.` }; + } + const previousPeople = previous === 1 ? '1 person' : `${previous} people`; + const difference = next - previous; + const delta = difference > 0 + ? `That takes ${difference} more ${difference === 1 ? 'spot' : 'spots'}.` + : `That opens ${-difference} ${difference === -1 ? 'spot' : 'spots'}.`; + return { + subject, + hello: `${name} changed this signup for ${role} from ${previousPeople} to ${nextPeople}. ${delta}`, + }; +} + function partyNote(groupSize: number | undefined): string { if (!groupSize || groupSize <= 1) return ''; return ` This signup holds ${groupSize} spots.`; diff --git a/src/signup-http.tsx b/src/signup-http.tsx index decd24b..7ce6589 100644 --- a/src/signup-http.tsx +++ b/src/signup-http.tsx @@ -463,7 +463,7 @@ async function submitParty(c: Context, services: SignupServices): Promise writePartySize(db, sha256(token), groupSize, now)).immediate(); + return db.transaction(() => writePartySize(db, sha256(token), groupSize, now, mail)).immediate(); } /** How many people this signup may offer, including the spots it already holds. */ @@ -317,11 +324,17 @@ export function confirmedPersonShiftIds(db: Database.Database, emailNorm: string return rows.map((row) => row.shift_id); } -function writePartySize(db: Database.Database, hash: string, groupSize: number, now: string): PartySizeResult { +function writePartySize( + db: Database.Database, + hash: string, + groupSize: number, + now: string, + mail: MailIdentity, +): PartySizeResult { const row = db .prepare( `SELECT id AS id, status AS status, token_expires_at AS token_expires_at, anonymized_at AS anonymized_at, - shift_id AS shift_id, group_size AS group_size, name AS name + shift_id AS shift_id, group_size AS group_size, name AS name, email_norm AS email_norm FROM signups WHERE manage_token_hash = ?`, ) @@ -334,6 +347,7 @@ function writePartySize(db: Database.Database, hash: string, groupSize: number, shift_id: number; group_size: number; name: string | null; + email_norm: string | null; } | undefined; if (!row || row.anonymized_at || !row.token_expires_at || row.token_expires_at <= now || !row.name) { @@ -357,6 +371,22 @@ function writePartySize(db: Database.Database, hash: string, groupSize: number, ) .run(groupSize, now, row.id, now, current); if (updated.changes !== 1) return { ok: false, reason: 'invalid' }; + const shift = row.email_norm ? loadShiftFacts(db, row.shift_id) : null; + if (shift && row.email_norm) { + queueCoordinatorNotices(db, { + action: 'party', + signupId: row.id, + shiftId: row.shift_id, + shiftVersion: shift.version, + volunteerName: row.name, + volunteerEmail: row.email_norm, + groupSize, + previousGroupSize: current, + shift: shift.facts, + mail, + now, + }); + } return { ok: true, groupSize, changed: true }; } diff --git a/tests/coordinator-notice.test.ts b/tests/coordinator-notice.test.ts index cbbcc6f..3aae7b4 100644 --- a/tests/coordinator-notice.test.ts +++ b/tests/coordinator-notice.test.ts @@ -9,7 +9,7 @@ import { loadConfig } from '../src/config.js'; import { openAppDatabase } from '../src/db.js'; import { deliverDueMail } from '../src/mail/deliver.js'; import type { Mailer } from '../src/mail/types.js'; -import { cancelSignup, reserveSignup, type ReserveInput } from '../src/signup.js'; +import { cancelSignup, changePartySize, reserveSignup, type ReserveInput } from '../src/signup.js'; import { devEnv } from './env.js'; const NOW = '2026-10-14T18:00:00.000Z'; @@ -188,6 +188,36 @@ describe('coordinator roster notices', () => { }); }); + it('tells coordinators when a signup changes from one person to eight, once for that new size', async () => { + const { db, shiftId } = await buildDb(); + db.prepare('UPDATE shifts SET capacity = 10 WHERE id = ?').run(shiftId); + insertCoordinator(db, 'ada@example.invalid', 'Ada Admin', 1); + const saved = reserveSignup(db, reservation(shiftId, { groupSize: 1, operationId: 'party-source' })); + if (!saved.ok || !saved.manageToken) throw new Error('Signup was not saved.'); + + expect(changePartySize(db, saved.manageToken, 8, NOW, MAIL)).toEqual({ ok: true, groupSize: 8, changed: true }); + const party = db + .prepare(`SELECT subject, body_text, dedupe_key FROM outbox WHERE kind = 'notice' AND subject LIKE 'Pat Review changed to %'`) + .get() as { subject: string; body_text: string; dedupe_key: string }; + expect(party.subject).toBe('Pat Review changed to 8 people for Sorting'); + expect(party.body_text).toContain('from 1 person to 8 people'); + expect(party.dedupe_key).toBe(`notice:party:v1:8:${saved.signupId}:ada@example.invalid`); + expect(party.body_text).toContain('That takes 7 more spots.'); + expect(party.body_text).toContain(`/admin/shifts/${shiftId}`); + expect(party.body_text).not.toContain('/m/'); + expect(db.prepare(`SELECT kind FROM outbox WHERE kind != 'notice'`).all()).toEqual([{ kind: 'confirmation' }]); + + expect(changePartySize(db, saved.manageToken, 8, NOW, MAIL)).toEqual({ ok: true, groupSize: 8, changed: false }); + expect(changePartySize(db, saved.manageToken, 5, NOW, MAIL)).toEqual({ ok: true, groupSize: 5, changed: true }); + expect(db.prepare(`SELECT subject FROM outbox WHERE kind = 'notice' AND subject LIKE 'Pat Review changed to %' ORDER BY subject`).all()).toEqual([ + { subject: 'Pat Review changed to 5 people for Sorting' }, + { subject: 'Pat Review changed to 8 people for Sorting' }, + ]); + expect(db.prepare(`SELECT status FROM outbox WHERE kind = 'notice' AND subject = 'Pat Review signed up for Sorting'`).get()).toEqual({ + status: 'pending', + }); + }); + it('stops a waiting notice when an administrator is removed or the sign-in address is replaced', async () => { const { db, shiftId } = await buildDb(); const ada = insertCoordinator(db, 'ada@example.invalid', 'Ada Admin', 1);