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.
This commit is contained in:
1 parent
a520352071
commit
77a9a0caff
12 files changed
+725
-84
No files matched your search
@@ -773,6 +773,78 @@ describe('coordinator screens', () => {
|
||||
expect(count(db, 'SELECT COUNT(*) AS n FROM shifts')).toBe(3);
|
||||
});
|
||||
|
||||
it('shows how many people a location change emails before saving it', async () => {
|
||||
const { app, db } = await buildApp();
|
||||
const cookie = await signIn(app, db);
|
||||
const created = await postForm(app, '/admin/events', cookie, {
|
||||
title: 'Soup Night',
|
||||
slug: 'soup-night',
|
||||
description: 'Bring a spoon.',
|
||||
location: 'Sioux Center, Iowa',
|
||||
tz: 'America/Chicago',
|
||||
published: 'yes',
|
||||
});
|
||||
const eventId = Number(/\/admin\/events\/(\d+)/.exec(created.headers.get('location') ?? '')?.[1]);
|
||||
const shift = await postForm(app, `/admin/events/${eventId}/shifts`, cookie, {
|
||||
role: 'Ladle',
|
||||
date: '2026-10-15',
|
||||
start: '09:00',
|
||||
end: '12:00',
|
||||
capacity: '4',
|
||||
});
|
||||
const shiftId = Number(/\/admin\/shifts\/(\d+)/.exec(shift.headers.get('location') ?? '')?.[1]);
|
||||
const fields = {
|
||||
title: 'Soup Night',
|
||||
slug: 'soup-night',
|
||||
description: 'Bring a spoon.',
|
||||
tz: 'America/Chicago',
|
||||
published: 'yes',
|
||||
};
|
||||
|
||||
const empty = await postForm(app, `/admin/events/${eventId}`, cookie, { ...fields, location: 'Sioux Center, IA' });
|
||||
expect(empty.status).toBe(200);
|
||||
const emptyHtml = await empty.text();
|
||||
expect(emptyHtml).toContain('No one is signed up for a shift that has not ended.');
|
||||
expect(emptyHtml).toContain('0 people');
|
||||
expect(emptyHtml).toContain('Save location');
|
||||
expect(db.prepare('SELECT location AS location FROM events WHERE id = ?').get(eventId)).toEqual({ location: 'Sioux Center, Iowa' });
|
||||
|
||||
const savedEmpty = await postForm(app, `/admin/events/${eventId}`, cookie, {
|
||||
...fields,
|
||||
location: 'Sioux Center, IA',
|
||||
confirm_location: 'yes',
|
||||
});
|
||||
expect(savedEmpty.status).toBe(303);
|
||||
expect(savedEmpty.headers.get('location')).toBe(`/admin/events/${eventId}?notice=saved`);
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM outbox WHERE kind = 'location'")).toBe(0);
|
||||
|
||||
const person = reserve(db, shiftId, 'Pat Example', 'pat@example.invalid', 'pat-location');
|
||||
const hash = db.prepare('SELECT manage_token_hash AS hash FROM signups').get();
|
||||
const preview = await postForm(app, `/admin/events/${eventId}`, cookie, { ...fields, location: '409 1st Ave NE' });
|
||||
expect(preview.status).toBe(200);
|
||||
const previewHtml = await preview.text();
|
||||
expect(previewHtml).toContain('1 person');
|
||||
expect(previewHtml).toContain('This emails 1 person about the new location.');
|
||||
expect(previewHtml).toContain('Save and email');
|
||||
expect(previewHtml).toContain('name="confirm_location"');
|
||||
expect(db.prepare('SELECT location AS location FROM events WHERE id = ?').get(eventId)).toEqual({ location: 'Sioux Center, IA' });
|
||||
|
||||
const saved = await postForm(app, `/admin/events/${eventId}`, cookie, {
|
||||
...fields,
|
||||
location: '409 1st Ave NE',
|
||||
confirm_location: 'yes',
|
||||
});
|
||||
expect(saved.status).toBe(303);
|
||||
expect(saved.headers.get('location')).toBe(`/admin/events/${eventId}?notice=saved-location`);
|
||||
expect(db.prepare('SELECT location AS location FROM events WHERE id = ?').get(eventId)).toEqual({ location: '409 1st Ave NE' });
|
||||
expect(db.prepare('SELECT manage_token_hash AS hash FROM signups').get()).toEqual(hash);
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM outbox WHERE kind = 'location' AND to_email = 'pat@example.invalid'")).toBe(1);
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM outbox WHERE kind = 'notice' AND subject LIKE '%location%'")).toBe(0);
|
||||
const page = await (await app.request(`/admin/events/${eventId}?notice=saved-location`, { headers: { cookie } })).text();
|
||||
expect(page).toContain('One email is queued for each person');
|
||||
expect(readManageSignup(db, person.manageToken ?? '', NOW)?.status).toBe('confirmed');
|
||||
});
|
||||
|
||||
it('treats an expired session as signed out', async () => {
|
||||
const { app, db } = await buildApp();
|
||||
const cookie = await signIn(app, db);
|
||||
|
||||
@@ -2,7 +2,8 @@ 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 { addWalkIn, cancelShift, moveSignup, switchSignup, updateEvent, updateShift } from '../src/admin/store.js';
|
||||
import { queueDueReminders } from '../src/reminders.js';
|
||||
import { deactivateAdmin } from '../src/admins.js';
|
||||
import { recoverCoordinator } from '../src/auth/recover.js';
|
||||
import { loadConfig } from '../src/config.js';
|
||||
@@ -92,7 +93,7 @@ describe('coordinator roster notices', () => {
|
||||
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 () => {
|
||||
it('tells coordinators when a volunteer switches, and stays quiet when a coordinator moves or edits a shift', async () => {
|
||||
const { db, eventId, shiftId } = await buildDb();
|
||||
const ada = insertCoordinator(db, 'ada@example.invalid', 'Ada Admin', 1);
|
||||
const other = insertShift(db, eventId, 'Distribution');
|
||||
@@ -110,9 +111,8 @@ describe('coordinator roster notices', () => {
|
||||
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');
|
||||
expect(db.prepare(`SELECT COUNT(*) AS n FROM outbox WHERE kind = 'notice' AND subject = 'Sam Example was moved to Distribution'`).get()).toEqual({ n: 0 });
|
||||
expect(db.prepare(`SELECT COUNT(*) AS n FROM outbox WHERE kind = 'changed' AND to_email = 'sam@example.invalid'`).get()).toEqual({ n: 1 });
|
||||
|
||||
const changed = updateShift(db, ada, other, {
|
||||
role: 'Packing',
|
||||
@@ -122,11 +122,8 @@ describe('coordinator roster notices', () => {
|
||||
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 subject AS subject FROM outbox WHERE kind = 'notice' AND subject LIKE '%shift changed'`).all()).toEqual([]);
|
||||
expect(db.prepare(`SELECT COUNT(*) AS n FROM outbox WHERE kind = 'changed' AND subject LIKE 'Your Packing shift has changed'`).get()).toEqual({ n: 2 });
|
||||
expect(db.prepare(`SELECT status FROM outbox WHERE kind = 'notice' AND subject = 'Pat Review signed up for Sorting'`).get()).toEqual({
|
||||
status: 'pending',
|
||||
});
|
||||
@@ -218,6 +215,119 @@ describe('coordinator roster notices', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('does not email a coordinator about a walk-in or a shift they cancel', async () => {
|
||||
const { db, shiftId } = await buildDb();
|
||||
const ada = insertCoordinator(db, 'ada@example.invalid', 'Ada Admin', 1);
|
||||
db.prepare('UPDATE shifts SET capacity = 6 WHERE id = ?').run(shiftId);
|
||||
const added = addWalkIn(db, ada, {
|
||||
shiftId,
|
||||
name: 'Pat Review',
|
||||
email: 'pat.review@example.invalid',
|
||||
phone: '712-555-0100',
|
||||
notes: null,
|
||||
operationId: 'walk-in',
|
||||
now: NOW,
|
||||
mail: MAIL,
|
||||
});
|
||||
expect(added.ok).toBe(true);
|
||||
expect(db.prepare(`SELECT COUNT(*) AS n FROM outbox WHERE kind = 'notice'`).get()).toEqual({ n: 0 });
|
||||
expect(db.prepare(`SELECT COUNT(*) AS n FROM outbox WHERE kind = 'confirmation' AND to_email = 'pat.review@example.invalid'`).get()).toEqual({ n: 1 });
|
||||
|
||||
for (let index = 0; index < 5; index += 1) {
|
||||
const saved = reserveSignup(db, reservation(shiftId, {
|
||||
name: `Person ${index}`,
|
||||
email: `person${index}@example.invalid`,
|
||||
operationId: `person-${index}`,
|
||||
}));
|
||||
expect(saved.ok).toBe(true);
|
||||
}
|
||||
expect(db.prepare(`SELECT COUNT(*) AS n FROM outbox WHERE kind = 'notice'`).get()).toEqual({ n: 5 });
|
||||
const cancelled = cancelShift(db, ada, shiftId, NOW, MAIL);
|
||||
expect(cancelled).toEqual({ ok: true, already: false, mailed: 6 });
|
||||
expect(db.prepare(`SELECT COUNT(*) AS n FROM outbox WHERE kind = 'notice'`).get()).toEqual({ n: 5 });
|
||||
expect(db.prepare(`SELECT COUNT(*) AS n FROM outbox WHERE kind = 'cancelled' AND status = 'pending'`).get()).toEqual({ n: 6 });
|
||||
});
|
||||
|
||||
it('emails each person once when the location changes and leaves private links in place', async () => {
|
||||
const { db, eventId, shiftId } = await buildDb();
|
||||
const ada = insertCoordinator(db, 'ada@example.invalid', 'Ada Admin', 1);
|
||||
const second = insertShift(db, eventId, 'Sorting', '2026-10-22T14:00:00.000Z', '2026-10-22T17:00:00.000Z');
|
||||
const third = insertShift(db, eventId, 'Sorting', '2026-10-29T14:00:00.000Z', '2026-10-29T17:00:00.000Z');
|
||||
const people = [
|
||||
['Pat Review', 'pat@example.invalid'],
|
||||
['Sam Example', 'sam@example.invalid'],
|
||||
['Kim Example', 'kim@example.invalid'],
|
||||
] as const;
|
||||
const tokens = new Map<string, string>();
|
||||
for (const shift of [shiftId, second, third]) {
|
||||
for (const [name, email] of people) {
|
||||
const saved = reserveSignup(db, reservation(shift, { name, email, operationId: `${email}-${shift}` }));
|
||||
expect(saved.ok).toBe(true);
|
||||
if (saved.ok && saved.manageToken && shift === shiftId) tokens.set(email, saved.manageToken);
|
||||
}
|
||||
}
|
||||
const hashes = db.prepare('SELECT id, manage_token_hash AS hash FROM signups ORDER BY id').all();
|
||||
expect(queueDueReminders(db, MAIL, 'location-secret', '2026-10-15T12:00:00.000Z')).toEqual({ queued: 3, suppressed: 0 });
|
||||
const noticesBefore = db.prepare(`SELECT COUNT(*) AS n FROM outbox WHERE kind = 'notice'`).get();
|
||||
|
||||
const saved = updateEvent(db, ada, eventId, {
|
||||
title: 'Weekly pantry',
|
||||
slug: 'weekly-pantry',
|
||||
description: 'Bring a hat.',
|
||||
location: 'Sioux Center, IA',
|
||||
tz: 'America/Chicago',
|
||||
published: true,
|
||||
}, '2026-10-15T12:00:00.000Z', MAIL, 'location-secret');
|
||||
expect(saved).toEqual({ ok: true, id: eventId, mailed: 3 });
|
||||
expect(db.prepare('SELECT id, manage_token_hash AS hash FROM signups ORDER BY id').all()).toEqual(hashes);
|
||||
expect(db.prepare('SELECT version AS version FROM shifts ORDER BY id').all()).toEqual([
|
||||
{ version: 1 },
|
||||
{ version: 1 },
|
||||
{ version: 1 },
|
||||
]);
|
||||
expect(db.prepare(`SELECT COUNT(*) AS n FROM outbox WHERE kind = 'notice'`).get()).toEqual(noticesBefore);
|
||||
|
||||
const letters = db.prepare(`SELECT to_email, body_text, body_html FROM outbox WHERE kind = 'location' ORDER BY to_email`).all() as Array<{
|
||||
to_email: string;
|
||||
body_text: string;
|
||||
body_html: string;
|
||||
}>;
|
||||
expect(letters.map((row) => row.to_email)).toEqual(['kim@example.invalid', 'pat@example.invalid', 'sam@example.invalid']);
|
||||
const pat = letters.find((row) => row.to_email === 'pat@example.invalid');
|
||||
if (!pat) throw new Error('Pat did not get a location email.');
|
||||
expect(pat.body_text).toContain('The location for Weekly pantry is now Sioux Center, IA.');
|
||||
expect(pat.body_text).toContain('October 15, 2026');
|
||||
expect(pat.body_text).toContain('October 22, 2026');
|
||||
expect(pat.body_text).toContain('October 29, 2026');
|
||||
expect(pat.body_text).not.toContain('/m/');
|
||||
expect(pat.body_html).not.toContain('/m/');
|
||||
expect(pat.body_text.match(/October/g)).toHaveLength(3);
|
||||
|
||||
const confirmation = db.prepare(`SELECT body_text AS body_text FROM outbox WHERE kind = 'confirmation' AND to_email = 'pat@example.invalid' ORDER BY id LIMIT 1`).get() as { body_text: string };
|
||||
expect(confirmation.body_text).toContain('Sioux Center, IA');
|
||||
expect(confirmation.body_text).toContain(`/m/${tokens.get('pat@example.invalid')}`);
|
||||
expect(confirmation.body_text).not.toContain('Sioux Center, Iowa');
|
||||
const reminder = db.prepare(`SELECT body_text AS body_text FROM outbox WHERE kind = 'reminder' AND status = 'pending' AND to_email = 'pat@example.invalid'`).get() as { body_text: string };
|
||||
expect(reminder.body_text).toContain('Sioux Center, IA');
|
||||
expect(reminder.body_text).not.toContain('Sioux Center, Iowa');
|
||||
|
||||
const again = updateEvent(db, ada, eventId, {
|
||||
title: 'Weekly pantry',
|
||||
slug: 'weekly-pantry',
|
||||
description: 'Bring a hat.',
|
||||
location: '409 1st Ave NE',
|
||||
tz: 'America/Chicago',
|
||||
published: true,
|
||||
}, '2026-10-15T12:30:00.000Z', MAIL, 'location-secret');
|
||||
expect(again).toEqual({ ok: true, id: eventId, mailed: 3 });
|
||||
expect(db.prepare(`SELECT status, last_error FROM outbox WHERE kind = 'location' AND body_text LIKE '%Sioux Center, IA%' LIMIT 1`).get()).toEqual({
|
||||
status: 'cancelled',
|
||||
last_error: 'A newer location email replaced this message.',
|
||||
});
|
||||
expect(db.prepare(`SELECT COUNT(*) AS n FROM outbox WHERE kind = 'location' AND status = 'pending'`).get()).toEqual({ n: 3 });
|
||||
expect(db.prepare('SELECT id, manage_token_hash AS hash FROM signups ORDER BY id').all()).toEqual(hashes);
|
||||
});
|
||||
|
||||
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);
|
||||
@@ -255,12 +365,18 @@ async function buildDb() {
|
||||
return { db, eventId, shiftId: insertShift(db, eventId, 'Sorting') };
|
||||
}
|
||||
|
||||
function insertShift(db: Awaited<ReturnType<typeof openAppDatabase>>, eventId: number, role: string): number {
|
||||
function insertShift(
|
||||
db: Awaited<ReturnType<typeof openAppDatabase>>,
|
||||
eventId: number,
|
||||
role: string,
|
||||
startsAt = '2026-10-15T14:00:00.000Z',
|
||||
endsAt = '2026-10-15T17:00:00.000Z',
|
||||
): 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,
|
||||
VALUES (?, ?, ?, ?, 4, '', 0)`,
|
||||
).run(eventId, role, startsAt, endsAt).lastInsertRowid,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user