Updated the changePartySize function to include mail identity and notify coordinators when a signup's party size changes. Enhanced the writePartySize function to queue notices for coordinators, detailing changes in group size. Added new tests to verify the notification logic for party size adjustments, ensuring accurate communication of changes to coordinators.
295 lines
15 KiB
TypeScript
295 lines
15 KiB
TypeScript
import { mkdtempSync, rmSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import path from 'node:path';
|
|
import { afterEach, describe, expect, it } from 'vitest';
|
|
import { moveSignup, switchSignup, updateShift } from '../src/admin/store.js';
|
|
import { deactivateAdmin } from '../src/admins.js';
|
|
import { recoverCoordinator } from '../src/auth/recover.js';
|
|
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, changePartySize, reserveSignup, type ReserveInput } from '../src/signup.js';
|
|
import { devEnv } from './env.js';
|
|
|
|
const NOW = '2026-10-14T18:00:00.000Z';
|
|
const MAIL = {
|
|
baseUrl: 'http://127.0.0.1:3000',
|
|
basePath: '',
|
|
from: 'Hope Food Pantry <volunteer@example.invalid>',
|
|
replyTo: 'volunteer@example.invalid',
|
|
};
|
|
const dirs: string[] = [];
|
|
const databases: { close: () => void }[] = [];
|
|
|
|
afterEach(() => {
|
|
for (const database of databases.splice(0)) database.close();
|
|
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
|
|
});
|
|
|
|
describe('coordinator roster notices', () => {
|
|
it('tells each active coordinator when someone signs up, and does not repeat that notice', async () => {
|
|
const { db, shiftId } = await buildDb();
|
|
insertCoordinator(db, 'ada@example.invalid', 'Ada Admin', 1, 'admin');
|
|
insertCoordinator(db, 'bea@example.invalid', 'Bea Coordinator', 1, 'coordinator');
|
|
insertCoordinator(db, 'cam@example.invalid', 'Cam Inactive', 0, 'admin');
|
|
const saved = reserveSignup(db, reservation(shiftId, {
|
|
name: 'Pat <script>\nReview',
|
|
email: 'pat@example.invalid',
|
|
phone: '712-555-0199',
|
|
groupSize: 3,
|
|
}));
|
|
expect(saved.ok).toBe(true);
|
|
|
|
const notices = db
|
|
.prepare(`SELECT to_email, subject, body_text, body_html, status FROM outbox WHERE kind = 'notice' ORDER BY to_email`)
|
|
.all() as Array<{ to_email: string; subject: string; body_text: string; body_html: string; status: string }>;
|
|
expect(notices.map((row) => row.to_email)).toEqual(['ada@example.invalid', 'bea@example.invalid']);
|
|
expect(notices[0]).toEqual(expect.objectContaining({
|
|
status: 'pending',
|
|
subject: 'Pat <script> Review signed up for Sorting',
|
|
}));
|
|
expect(notices[0]?.subject).not.toContain('\n');
|
|
const letter = notices[0];
|
|
if (!letter) throw new Error('Ada did not get a notice.');
|
|
expect(letter.body_html).toContain('Hello Ada Admin,');
|
|
expect(letter.body_html).toContain('Pat <script> Review');
|
|
expect(letter.body_html).not.toContain('<script>');
|
|
expect(letter.body_text).toContain('pat@example.invalid');
|
|
expect(letter.body_text).toContain('712-555-0199');
|
|
expect(letter.body_text).toContain('This signup holds 3 spots.');
|
|
expect(letter.body_text).toContain(`http://127.0.0.1:3000/admin/shifts/${shiftId}`);
|
|
expect(letter.body_text).not.toContain('/m/');
|
|
expect(letter.body_html).not.toContain('/m/');
|
|
expect(db.prepare(`SELECT body_text AS body_text FROM outbox WHERE kind = 'confirmation'`).get()).toEqual(
|
|
expect.objectContaining({ body_text: expect.stringContaining('/m/') }),
|
|
);
|
|
|
|
const again = reserveSignup(db, reservation(shiftId, {
|
|
name: 'Pat <script>\nReview',
|
|
email: 'pat@example.invalid',
|
|
phone: '712-555-0199',
|
|
groupSize: 3,
|
|
operationId: 'same-operation',
|
|
}));
|
|
expect(again.ok && again.replay).toBe(true);
|
|
expect(db.prepare(`SELECT COUNT(*) AS n FROM outbox WHERE kind = 'notice'`).get()).toEqual({ n: 2 });
|
|
});
|
|
|
|
it('tells coordinators about a cancellation without dropping the signup notice', async () => {
|
|
const { db, shiftId } = await buildDb();
|
|
insertCoordinator(db, 'ada@example.invalid', 'Ada Admin', 1);
|
|
const saved = reserveSignup(db, reservation(shiftId));
|
|
if (!saved.ok || !saved.manageToken) throw new Error('Signup was not saved.');
|
|
expect(cancelSignup(db, saved.manageToken, NOW, MAIL)).toEqual({ ok: true, already: false });
|
|
const notices = db.prepare(`SELECT subject, status FROM outbox WHERE kind = 'notice' ORDER BY subject`).all();
|
|
expect(notices).toEqual([
|
|
{ subject: 'Pat Review signed up for Sorting', status: 'pending' },
|
|
{ subject: "Pat Review's signup for Sorting was cancelled", status: 'pending' },
|
|
]);
|
|
expect(db.prepare(`SELECT status FROM outbox WHERE kind = 'confirmation'`).get()).toEqual({ status: 'cancelled' });
|
|
expect(cancelSignup(db, saved.manageToken, NOW, MAIL)).toEqual({ ok: true, already: true });
|
|
expect(db.prepare(`SELECT COUNT(*) AS n FROM outbox WHERE kind = 'notice'`).get()).toEqual({ n: 2 });
|
|
});
|
|
|
|
it('tells coordinators when a signup is switched, moved, or the shift changes', async () => {
|
|
const { db, eventId, shiftId } = await buildDb();
|
|
const ada = insertCoordinator(db, 'ada@example.invalid', 'Ada Admin', 1);
|
|
const other = insertShift(db, eventId, 'Distribution');
|
|
const saved = reserveSignup(db, reservation(shiftId, { email: 'pat@example.invalid', operationId: 'switch-source' }));
|
|
if (!saved.ok) throw new Error('Signup was not saved.');
|
|
const switched = switchSignup(db, saved.signupId, other, 'switch-op', NOW, MAIL);
|
|
expect(switched.ok).toBe(true);
|
|
const switchNotice = db.prepare(`SELECT body_text AS body_text FROM outbox WHERE kind = 'notice' AND subject = 'Pat Review switched to Distribution'`).get() as { body_text: string };
|
|
expect(switchNotice.body_text).toContain('switched from Sorting (');
|
|
expect(switchNotice.body_text).toContain('to Distribution');
|
|
expect(switchNotice.body_text).toContain(`/admin/shifts/${other}`);
|
|
expect(switchNotice.body_text).not.toContain('/m/');
|
|
|
|
const movedSource = reserveSignup(db, reservation(shiftId, { name: 'Sam Example', email: 'sam@example.invalid', operationId: 'move-source' }));
|
|
if (!movedSource.ok) throw new Error('Move signup was not saved.');
|
|
const moved = moveSignup(db, ada, movedSource.signupId, other, 'move-op', NOW, MAIL);
|
|
expect(moved.ok).toBe(true);
|
|
const moveNotice = db.prepare(`SELECT body_text AS body_text FROM outbox WHERE kind = 'notice' AND subject = 'Sam Example was moved to Distribution'`).get() as { body_text: string };
|
|
expect(moveNotice.body_text).toContain('was moved from Sorting (');
|
|
expect(moveNotice.body_text).toContain('to Distribution');
|
|
|
|
const changed = updateShift(db, ada, other, {
|
|
role: 'Packing',
|
|
date: '2026-10-15',
|
|
start: '10:00',
|
|
end: '13:00',
|
|
capacity: 4,
|
|
}, NOW, MAIL);
|
|
expect(changed.ok).toBe(true);
|
|
const changeNotices = db.prepare(`SELECT subject AS subject FROM outbox WHERE kind = 'notice' AND subject LIKE '%shift changed' ORDER BY subject`).all();
|
|
expect(changeNotices).toEqual([
|
|
{ subject: "Pat Review's Packing shift changed" },
|
|
{ subject: "Sam Example's Packing shift changed" },
|
|
]);
|
|
expect(db.prepare(`SELECT status FROM outbox WHERE kind = 'notice' AND subject = 'Pat Review signed up for Sorting'`).get()).toEqual({
|
|
status: 'pending',
|
|
});
|
|
});
|
|
|
|
it('sends a signup notice after the person cancels, and does not send it once the coordinator or the signup is gone', async () => {
|
|
const { db, shiftId } = await buildDb();
|
|
insertCoordinator(db, 'ada@example.invalid', 'Ada Admin', 1);
|
|
const saved = reserveSignup(db, reservation(shiftId));
|
|
if (!saved.ok || !saved.manageToken) throw new Error('Signup was not saved.');
|
|
cancelSignup(db, saved.manageToken, NOW, MAIL);
|
|
const sent: string[] = [];
|
|
const mailer: Mailer = {
|
|
async send(message) {
|
|
sent.push(message.subject);
|
|
return { result: 'sent', providerId: 'notice-1' };
|
|
},
|
|
};
|
|
expect(await deliverDueMail(db, mailer, NOW)).toBe(3);
|
|
expect(sent).toEqual([
|
|
'Pat Review signed up for Sorting',
|
|
'Your signup for Sorting is cancelled',
|
|
"Pat Review's signup for Sorting was cancelled",
|
|
]);
|
|
expect(db.prepare(`SELECT status FROM outbox WHERE kind = 'notice' AND subject = 'Pat Review signed up for Sorting'`).get()).toEqual({
|
|
status: 'sent',
|
|
});
|
|
|
|
const quiet = await buildDb();
|
|
insertCoordinator(quiet.db, 'ada@example.invalid', 'Ada Admin', 1);
|
|
reserveSignup(quiet.db, reservation(quiet.shiftId, { operationId: 'quiet-operation' }));
|
|
quiet.db.prepare(`UPDATE coordinators SET active = 0 WHERE email = 'ada@example.invalid'`).run();
|
|
const calls: string[] = [];
|
|
expect(await deliverDueMail(quiet.db, { async send(message) { calls.push(message.to); return { result: 'sent', providerId: null }; } }, NOW)).toBe(1);
|
|
expect(calls).toEqual(['pat.review@example.invalid']);
|
|
expect(quiet.db.prepare(`SELECT status, last_error FROM outbox WHERE kind = 'notice'`).get()).toEqual({
|
|
status: 'cancelled',
|
|
last_error: 'That coordinator no longer has access.',
|
|
});
|
|
|
|
const forgotten = await buildDb();
|
|
insertCoordinator(forgotten.db, 'ada@example.invalid', 'Ada Admin', 1);
|
|
const old = reserveSignup(forgotten.db, reservation(forgotten.shiftId, { operationId: 'old-operation' }));
|
|
if (!old.ok) throw new Error('Old signup was not saved.');
|
|
forgotten.db.prepare(
|
|
`UPDATE signups
|
|
SET name = NULL, email = NULL, email_norm = NULL, phone = NULL, is_minor = 0,
|
|
guardian_name = NULL, guardian_email = NULL, notes = NULL,
|
|
manage_token_hash = NULL, token_expires_at = NULL,
|
|
anonymized_at = ?, updated_at = ?
|
|
WHERE id = ?`,
|
|
).run(NOW, NOW, old.signupId);
|
|
let anonymizedCalls = 0;
|
|
expect(await deliverDueMail(forgotten.db, { async send() { anonymizedCalls += 1; return { result: 'sent', providerId: null }; } }, NOW)).toBe(0);
|
|
expect(anonymizedCalls).toBe(0);
|
|
expect(forgotten.db.prepare(`SELECT status, last_error FROM outbox WHERE kind = 'notice'`).get()).toEqual({
|
|
status: 'cancelled',
|
|
last_error: 'The signup is no longer available.',
|
|
});
|
|
});
|
|
|
|
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);
|
|
const bea = insertCoordinator(db, 'bea@example.invalid', 'Bea Admin', 1);
|
|
reserveSignup(db, reservation(shiftId));
|
|
expect(deactivateAdmin(db, ada, bea, NOW)).toEqual({ ok: true });
|
|
expect(db.prepare(`SELECT status, last_error FROM outbox WHERE kind = 'notice' AND to_email = 'bea@example.invalid'`).get()).toEqual({
|
|
status: 'cancelled',
|
|
last_error: 'That coordinator no longer has access.',
|
|
});
|
|
expect(db.prepare(`SELECT status FROM outbox WHERE kind = 'notice' AND to_email = 'ada@example.invalid'`).get()).toEqual({
|
|
status: 'pending',
|
|
});
|
|
|
|
recoverCoordinator(db, { email: 'ada@example.invalid', replaceWith: 'ada.next@example.invalid', now: NOW });
|
|
expect(db.prepare(`SELECT status, last_error FROM outbox WHERE kind = 'notice' AND to_email = 'ada@example.invalid'`).get()).toEqual({
|
|
status: 'cancelled',
|
|
last_error: 'That coordinator no longer has access.',
|
|
});
|
|
});
|
|
});
|
|
|
|
async function buildDb() {
|
|
const root = mkdtempSync(path.join(tmpdir(), 'hope-notice-'));
|
|
dirs.push(root);
|
|
const config = loadConfig(devEnv({ DATA_DIR: path.join(root, 'data') }));
|
|
const db = await openAppDatabase(config);
|
|
databases.push(db);
|
|
const eventId = Number(
|
|
db.prepare(
|
|
`INSERT INTO events (slug, title, description, location, tz, published, created_at)
|
|
VALUES ('weekly-pantry', 'Weekly pantry', 'Bring a hat.', 'Sioux Center, Iowa', 'America/Chicago', 1, ?)`,
|
|
).run(NOW).lastInsertRowid,
|
|
);
|
|
return { db, eventId, shiftId: insertShift(db, eventId, 'Sorting') };
|
|
}
|
|
|
|
function insertShift(db: Awaited<ReturnType<typeof openAppDatabase>>, eventId: number, role: string): number {
|
|
return Number(
|
|
db.prepare(
|
|
`INSERT INTO shifts (event_id, role, starts_at_utc, ends_at_utc, capacity, notes, cancelled)
|
|
VALUES (?, ?, '2026-10-15T14:00:00.000Z', '2026-10-15T17:00:00.000Z', 4, '', 0)`,
|
|
).run(eventId, role).lastInsertRowid,
|
|
);
|
|
}
|
|
|
|
function insertCoordinator(
|
|
db: Awaited<ReturnType<typeof openAppDatabase>>,
|
|
email: string,
|
|
name: string,
|
|
active: number,
|
|
role: 'admin' | 'coordinator' = 'admin',
|
|
): number {
|
|
return Number(
|
|
db.prepare(
|
|
`INSERT INTO coordinators (email, name, role, password_hash, active, created_at)
|
|
VALUES (?, ?, ?, NULL, ?, ?)`,
|
|
).run(email, name, role, active, NOW).lastInsertRowid,
|
|
);
|
|
}
|
|
|
|
function reservation(shiftId: number, overrides: Partial<ReserveInput> = {}): ReserveInput {
|
|
return {
|
|
shiftId,
|
|
name: 'Pat Review',
|
|
email: 'pat.review@example.invalid',
|
|
phone: '712-555-0100',
|
|
notes: null,
|
|
operationId: 'same-operation',
|
|
now: NOW,
|
|
mail: MAIL,
|
|
...overrides,
|
|
};
|
|
}
|