feat: enhance shift cancellation process and update templates
Improved the shift cancellation functionality to ensure that selected shifts are properly marked as cancelled, with corresponding emails queued for affected signups. Updated the admin interface to reflect these changes, including clearer messaging for cancelled shifts. Enhanced the handling of weekly templates to ensure that shifts are accurately updated or removed based on their status. Adjusted related tests to verify the new behavior and ensure consistency across the application.
This commit is contained in:
1 parent
e363cdaef9
commit
1b78a06f72
10 files changed
+956
-352
No files matched your search
+72
-11
@@ -380,12 +380,16 @@ describe('coordinator screens', () => {
|
||||
|
||||
const closed = await postForm(app, `/admin/shifts/${shiftId}/cancel`, cookie, {});
|
||||
expect(closed.status).toBe(303);
|
||||
expect(closed.headers.get('location')).toBe(`/admin/events/${eventId}?notice=shift-cancelled`);
|
||||
expect(db.prepare('SELECT cancelled AS cancelled FROM shifts WHERE id = ?').get(shiftId)).toEqual({ cancelled: 1 });
|
||||
expect(db.prepare("SELECT status AS status FROM signups WHERE email_norm = 'sam@example.invalid'").get()).toEqual({ status: 'cancelled' });
|
||||
expect(count(db, 'SELECT COUNT(*) AS n FROM signups')).toBe(2);
|
||||
expect(await (await app.request('/')).text()).not.toContain('Ladle');
|
||||
const eventPage = await (await app.request(closed.headers.get('location') ?? '', { headers: { cookie } })).text();
|
||||
expect(eventPage).toContain('The shift is cancelled and off this list. Cancellation emails are queued.');
|
||||
expect(eventPage).not.toContain('Ladle');
|
||||
const closedAdmin = await (await app.request(`/admin/shifts/${shiftId}`, { headers: { cookie } })).text();
|
||||
expect(closedAdmin).toContain('Cancelled. The roster stays until you remove it.');
|
||||
expect(closedAdmin).toContain('Cancelled. It is off the upcoming list.');
|
||||
expect(closedAdmin).not.toContain('Edit details');
|
||||
expect(closedAdmin).toContain('Remove this shift');
|
||||
const closedPage = await (await app.request(`/s/${shiftId}`)).text();
|
||||
@@ -665,7 +669,7 @@ describe('coordinator screens', () => {
|
||||
expect(db.prepare('SELECT status, shift_id FROM signups WHERE id = ?').get(signupId)).toEqual({ status: 'cancelled', shift_id: sourceId });
|
||||
});
|
||||
|
||||
it('keeps the next two weeks of shifts in view and tucks later ones under see more', async () => {
|
||||
it('shows the next two weeks as day cards and tucks the rest under see more', async () => {
|
||||
const { app, db } = await buildApp();
|
||||
const cookie = await signIn(app, db);
|
||||
const eventId = await createEvent(app, cookie, 'Pantry', true);
|
||||
@@ -678,20 +682,22 @@ describe('coordinator screens', () => {
|
||||
expect((await postForm(app, `/admin/shifts/${eveningId}/cancel`, cookie, {})).status).toBe(303);
|
||||
|
||||
const html = await (await app.request(`/admin/events/${eventId}`, { headers: { cookie } })).text();
|
||||
const moreAt = html.indexOf('Show 2 more shifts');
|
||||
const moreAt = html.indexOf('See more');
|
||||
expect(moreAt).toBeGreaterThan(0);
|
||||
const before = html.slice(0, moreAt);
|
||||
const after = html.slice(moreAt);
|
||||
expect(before).toContain('Morning');
|
||||
expect(before).toContain('need-day');
|
||||
expect(before).toContain('Sorting');
|
||||
expect(before).toContain('Distribution');
|
||||
expect(before).not.toContain('Morning');
|
||||
expect(before).not.toContain('Intake');
|
||||
expect(before).not.toContain('Packing');
|
||||
expect(before).not.toContain('Evening');
|
||||
expect(after.indexOf('Intake')).toBeGreaterThan(-1);
|
||||
expect(after.indexOf('Morning')).toBeGreaterThan(-1);
|
||||
expect(after.indexOf('Intake')).toBeGreaterThan(after.indexOf('Morning'));
|
||||
expect(after.indexOf('Packing')).toBeGreaterThan(after.indexOf('Intake'));
|
||||
expect(after.indexOf('Evening')).toBeGreaterThan(after.indexOf('Packing'));
|
||||
expect(after).toContain('Show fewer');
|
||||
expect(html).not.toContain('Evening');
|
||||
expect(after).toContain('See less');
|
||||
expect(html).not.toContain('<details class="shift-more" open');
|
||||
expect(count(db, 'SELECT COUNT(*) AS n FROM shifts')).toBe(6);
|
||||
|
||||
@@ -699,17 +705,72 @@ describe('coordinator screens', () => {
|
||||
await addShift(app, cookie, soon, { role: 'Ladle', date: '2026-10-16', capacity: '2' });
|
||||
const short = await (await app.request(`/admin/events/${soon}`, { headers: { cookie } })).text();
|
||||
expect(short).toContain('Ladle');
|
||||
expect(short).not.toContain('Show 1 more shift');
|
||||
expect(short).toContain('need-day');
|
||||
expect(short).not.toContain('See more');
|
||||
expect(short).not.toContain('shift-more');
|
||||
|
||||
const distant = await createEvent(app, cookie, 'Holiday', true);
|
||||
await addShift(app, cookie, distant, { role: 'Early', date: '2026-11-04', capacity: '2' });
|
||||
await addShift(app, cookie, distant, { role: 'Later', date: '2026-11-18', capacity: '2' });
|
||||
const far = await (await app.request(`/admin/events/${distant}`, { headers: { cookie } })).text();
|
||||
const farMore = far.indexOf('Show 1 more shift');
|
||||
const farMore = far.indexOf('See more');
|
||||
expect(farMore).toBeGreaterThan(0);
|
||||
expect(far.slice(0, farMore)).toContain('Early');
|
||||
expect(far.slice(farMore)).toContain('Later');
|
||||
expect(far.slice(0, farMore)).toContain('No shifts in the next 2 weeks.');
|
||||
expect(far.slice(0, farMore)).not.toContain('Early');
|
||||
expect(far.slice(farMore).indexOf('Later')).toBeGreaterThan(far.slice(farMore).indexOf('Early'));
|
||||
});
|
||||
|
||||
it('cancels the open shifts that are selected and refuses one that has ended', async () => {
|
||||
const { app, db } = await buildApp();
|
||||
const cookie = await signIn(app, db);
|
||||
const eventId = await createEvent(app, cookie, 'Pantry', true);
|
||||
const endedId = await addShift(app, cookie, eventId, { role: 'Morning', date: '2026-10-14', capacity: '2' });
|
||||
const first = await addShift(app, cookie, eventId, { role: 'Sorting', date: '2026-10-21', capacity: '2' });
|
||||
const second = await addShift(app, cookie, eventId, { role: 'Packing', date: '2026-10-22', capacity: '2' });
|
||||
reserve(db, first, 'Bea Example', 'bea@example.invalid', 'bea-operation');
|
||||
const page = await app.request(`/admin/events/${eventId}`, { headers: { cookie } });
|
||||
const html = await page.text();
|
||||
expect(html).toContain('Cancel selected');
|
||||
expect(html).toContain('>Templates<');
|
||||
expect(html).toContain('Add a template');
|
||||
expect(html).toContain('data-confirm-gate="open"');
|
||||
const token = field(html, 'csrf');
|
||||
|
||||
const refused = await app.request(`/admin/events/${eventId}/shifts/cancel`, {
|
||||
method: 'POST',
|
||||
headers: { origin: ORIGIN, 'content-type': 'application/x-www-form-urlencoded', cookie },
|
||||
body: new URLSearchParams([
|
||||
['csrf', token],
|
||||
['shift[]', String(endedId)],
|
||||
['shift[]', String(first)],
|
||||
]),
|
||||
});
|
||||
expect(refused.status).toBe(400);
|
||||
expect(await refused.text()).toContain('That shift has ended.');
|
||||
expect(db.prepare('SELECT cancelled AS cancelled FROM shifts WHERE id = ?').get(first)).toEqual({ cancelled: 0 });
|
||||
expect(db.prepare('SELECT cancelled AS cancelled FROM shifts WHERE id = ?').get(endedId)).toEqual({ cancelled: 0 });
|
||||
|
||||
const cancelled = await app.request(`/admin/events/${eventId}/shifts/cancel`, {
|
||||
method: 'POST',
|
||||
headers: { origin: ORIGIN, 'content-type': 'application/x-www-form-urlencoded', cookie },
|
||||
body: new URLSearchParams([
|
||||
['csrf', token],
|
||||
['shift[]', String(first)],
|
||||
['shift[]', String(second)],
|
||||
]),
|
||||
});
|
||||
expect(cancelled.status).toBe(303);
|
||||
expect(cancelled.headers.get('location')).toBe(`/admin/events/${eventId}?notice=shifts-cancelled`);
|
||||
expect(db.prepare('SELECT cancelled AS cancelled FROM shifts WHERE id = ?').get(first)).toEqual({ cancelled: 1 });
|
||||
expect(db.prepare('SELECT cancelled AS cancelled FROM shifts WHERE id = ?').get(second)).toEqual({ cancelled: 1 });
|
||||
expect(db.prepare("SELECT status AS status FROM signups WHERE email_norm = 'bea@example.invalid'").get()).toEqual({ status: 'cancelled' });
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM outbox WHERE kind = 'cancelled' AND to_email = 'bea@example.invalid' AND status = 'pending'")).toBe(1);
|
||||
const after = await (await app.request(`/admin/events/${eventId}?notice=shifts-cancelled`, { headers: { cookie } })).text();
|
||||
expect(after).toContain('Those shifts are cancelled and off this list. Cancellation emails are queued.');
|
||||
expect(after).toContain('Morning');
|
||||
expect(after).not.toContain('Sorting');
|
||||
expect(after).not.toContain('Packing');
|
||||
expect(count(db, 'SELECT COUNT(*) AS n FROM shifts')).toBe(3);
|
||||
});
|
||||
|
||||
it('treats an expired session as signed out', async () => {
|
||||
|
||||
+13
-41
@@ -131,34 +131,25 @@ describe('weekly template expansion', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('add weekly shifts', () => {
|
||||
it('adds from the event page once and refuses a form that is out of date', async () => {
|
||||
describe('weekly schedule', () => {
|
||||
it('does not offer a separate button to fill shifts', async () => {
|
||||
const { app, db } = await buildApp();
|
||||
const eventId = insertEvent(db, 'weekly-pantry', ZONE);
|
||||
insertTemplate(db, eventId, { weekday: 3, startsOn: '2026-10-14' });
|
||||
const empty = insertEvent(db, 'plain', ZONE);
|
||||
const cookie = await signIn(app, db);
|
||||
|
||||
const missing = await postGenerate(app, eventId, cookie, { csrf: 'wrong' });
|
||||
expect(missing.status).toBe(403);
|
||||
expect(count(db, 'SELECT COUNT(*) AS n FROM shifts')).toBe(0);
|
||||
|
||||
const added = await postGenerate(app, eventId, cookie);
|
||||
expect(added.status).toBe(303);
|
||||
expect(added.headers.get('location')).toBe(`/admin/events/${eventId}?notice=generated`);
|
||||
const page = await app.request(added.headers.get('location') ?? '', { headers: { cookie } });
|
||||
const page = await app.request(`/admin/events/${eventId}`, { headers: { cookie } });
|
||||
const html = await page.text();
|
||||
expect(html).toContain('Weekly shifts for the next three months are on the schedule.');
|
||||
expect(html).toContain('Add weekly shifts');
|
||||
expect(count(db, 'SELECT COUNT(*) AS n FROM shifts')).toBe(14);
|
||||
expect(html).toContain('Add a template');
|
||||
expect(html).toContain('Each one keeps three months of shifts filled.');
|
||||
expect(html).not.toContain('Fill missing shifts');
|
||||
expect(html).not.toContain('/generate');
|
||||
|
||||
const repeat = await postGenerate(app, eventId, cookie);
|
||||
expect(repeat.headers.get('location')).toBe(`/admin/events/${eventId}?notice=generated`);
|
||||
expect(count(db, 'SELECT COUNT(*) AS n FROM shifts')).toBe(14);
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM audit WHERE action = 'template.expand'")).toBe(1);
|
||||
|
||||
const none = await postGenerate(app, empty, cookie);
|
||||
expect(none.headers.get('location')).toBe(`/admin/events/${empty}?notice=no-template`);
|
||||
const generate = await app.request(`/admin/events/${eventId}/generate`, {
|
||||
method: 'POST',
|
||||
headers: { origin: ORIGIN, 'content-type': 'application/x-www-form-urlencoded', cookie },
|
||||
body: new URLSearchParams({ csrf: 'irrelevant' }),
|
||||
});
|
||||
expect(generate.status).toBe(404);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -248,22 +239,3 @@ async function signIn(app: Awaited<ReturnType<typeof buildApp>>['app'], db: Awai
|
||||
const cookie = /hope_session=([^;]+)/.exec(signedIn.headers.get('set-cookie') ?? '')?.[1] ?? '';
|
||||
return `hope_session=${cookie}`;
|
||||
}
|
||||
|
||||
async function postGenerate(
|
||||
app: Awaited<ReturnType<typeof buildApp>>['app'],
|
||||
eventId: number,
|
||||
cookie: string,
|
||||
options?: { csrf?: string },
|
||||
) {
|
||||
const current = await app.request(`/admin/events/${eventId}`, { headers: { cookie } });
|
||||
const csrf = options?.csrf ?? (/name="csrf"[^>]*value="([^"]*)"/.exec(await current.text())?.[1] ?? '');
|
||||
return app.request(`/admin/events/${eventId}/generate`, {
|
||||
method: 'POST',
|
||||
headers: { origin: ORIGIN, 'content-type': 'application/x-www-form-urlencoded', cookie },
|
||||
body: new URLSearchParams({ csrf }),
|
||||
});
|
||||
}
|
||||
|
||||
function count(db: Awaited<ReturnType<typeof openDb>>, sql: string): number {
|
||||
return Number((db.prepare(sql).get() as { n: number }).n);
|
||||
}
|
||||
+155
-28
@@ -3,6 +3,7 @@ import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
import { createTemplate, endTemplate, updateTemplate } from '../src/admin/store.js';
|
||||
import type { MailIdentity } from '../src/mail/types.js';
|
||||
import { createApp } from '../src/app.js';
|
||||
import { bootstrapAdmin } from '../src/auth/bootstrap.js';
|
||||
import { loadConfig } from '../src/config.js';
|
||||
@@ -12,6 +13,12 @@ import { devEnv } from './env.js';
|
||||
|
||||
const NOW = '2026-10-14T18:00:00.000Z';
|
||||
const ORIGIN = 'http://127.0.0.1:3000';
|
||||
const MAIL: MailIdentity = {
|
||||
baseUrl: ORIGIN,
|
||||
basePath: '',
|
||||
from: 'Hope Food Pantry <volunteer@example.invalid>',
|
||||
replyTo: 'volunteer@example.invalid',
|
||||
};
|
||||
const ZONE = 'America/Chicago';
|
||||
const dirs: string[] = [];
|
||||
const databases: { close: () => void }[] = [];
|
||||
@@ -22,19 +29,27 @@ afterEach(() => {
|
||||
});
|
||||
|
||||
describe('weekly template editing', () => {
|
||||
it('keeps generated shifts when a template changes or ends', async () => {
|
||||
it('updates shifts that have not ended when a template changes, and clears them when it ends', async () => {
|
||||
const db = await openDb();
|
||||
const eventId = insertEvent(db);
|
||||
const created = createTemplate(db, 1, eventId, templateInput({ weekday: 3, role: 'Sorting' }), NOW);
|
||||
expect(created.ok).toBe(true);
|
||||
if (!created.ok) return;
|
||||
expandTemplates(db, NOW, { eventId, coordinatorId: 1 });
|
||||
const before = shiftRows(db);
|
||||
expect(shiftRows(db)).toHaveLength(14);
|
||||
|
||||
const updated = updateTemplate(db, 1, eventId, created.id, templateInput({ weekday: 3, role: 'Intake', capacity: 9 }), NOW);
|
||||
expect(updated.ok).toBe(true);
|
||||
expandTemplates(db, NOW, { eventId, coordinatorId: 1 });
|
||||
expect(shiftRows(db)).toEqual(before);
|
||||
const updated = updateTemplate(db, 1, eventId, created.id, templateInput({ weekday: 3, role: 'Intake', capacity: 9 }), NOW, MAIL);
|
||||
expect(updated).toMatchObject({ ok: true, mailed: 0 });
|
||||
const changed = shiftRows(db);
|
||||
expect(changed.find((row) => row.occurrence_date === '2026-10-14')).toEqual({
|
||||
occurrence_date: '2026-10-14',
|
||||
role: 'Sorting',
|
||||
capacity: 6,
|
||||
template_revision: 1,
|
||||
cancelled: 0,
|
||||
});
|
||||
const upcoming = changed.filter((row) => row.occurrence_date !== '2026-10-14');
|
||||
expect(upcoming).toHaveLength(13);
|
||||
expect(upcoming.every((row) => row.role === 'Intake' && row.capacity === 9 && row.template_revision === 2 && row.cancelled === 0)).toBe(true);
|
||||
const template = db.prepare('SELECT role, capacity, revision, active FROM shift_templates WHERE id = ?').get(created.id) as {
|
||||
role: string;
|
||||
capacity: number;
|
||||
@@ -45,12 +60,118 @@ describe('weekly template editing', () => {
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM audit WHERE action = 'template.update'")).toBe(1);
|
||||
expect(count(db, 'SELECT COUNT(*) AS n FROM outbox')).toBe(0);
|
||||
|
||||
const ended = endTemplate(db, 1, eventId, created.id, NOW);
|
||||
const ended = endTemplate(db, 1, eventId, created.id, NOW, MAIL);
|
||||
expect(ended.ok).toBe(true);
|
||||
expandTemplates(db, NOW, { eventId, coordinatorId: 1 });
|
||||
expect(shiftRows(db)).toEqual(before);
|
||||
const after = shiftRows(db);
|
||||
expect(after).toEqual(changed.filter((row) => row.occurrence_date !== '2026-10-14').map((row) => ({ ...row, cancelled: 1 })));
|
||||
expect(after.some((row) => row.occurrence_date === '2026-10-14')).toBe(false);
|
||||
expect((db.prepare('SELECT active FROM shift_templates WHERE id = ?').get(created.id) as { active: number }).active).toBe(0);
|
||||
expect(updateTemplate(db, 1, eventId, created.id, templateInput({ weekday: 3, role: 'Sorting' }), NOW).ok).toBe(false);
|
||||
expect(updateTemplate(db, 1, eventId, created.id, templateInput({ weekday: 3, role: 'Sorting' }), NOW, MAIL).ok).toBe(false);
|
||||
});
|
||||
|
||||
it('leaves a shift that was edited on its own, and a cancelled date, alone', async () => {
|
||||
const db = await openDb();
|
||||
const eventId = insertEvent(db);
|
||||
const created = createTemplate(db, 1, eventId, templateInput({ weekday: 3, role: 'Sorting' }), NOW);
|
||||
expect(created.ok).toBe(true);
|
||||
if (!created.ok) return;
|
||||
db.prepare(`UPDATE shifts SET role = 'Custom', manually_modified = 1 WHERE occurrence_date = '2026-10-21'`).run();
|
||||
db.prepare(`UPDATE shifts SET cancelled = 1 WHERE occurrence_date = '2026-10-28'`).run();
|
||||
insertSignup(db, shiftId(db, '2026-10-21'), 'Ada Example', 'ada@example.invalid');
|
||||
|
||||
const updated = updateTemplate(db, 1, eventId, created.id, templateInput({ weekday: 3, role: 'Intake' }), NOW, MAIL);
|
||||
expect(updated).toMatchObject({ ok: true, mailed: 0 });
|
||||
expect(db.prepare(`SELECT role, manually_modified, cancelled FROM shifts WHERE occurrence_date = '2026-10-21'`).get()).toEqual({
|
||||
role: 'Custom',
|
||||
manually_modified: 1,
|
||||
cancelled: 0,
|
||||
});
|
||||
expect(db.prepare(`SELECT role, cancelled FROM shifts WHERE occurrence_date = '2026-10-28'`).get()).toEqual({
|
||||
role: 'Sorting',
|
||||
cancelled: 1,
|
||||
});
|
||||
expect(count(db, `SELECT COUNT(*) AS n FROM shifts WHERE occurrence_date = '2026-10-28'`)).toBe(1);
|
||||
expect(db.prepare(`SELECT role FROM shifts WHERE occurrence_date = '2026-11-04'`).get()).toEqual({ role: 'Intake' });
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM outbox WHERE to_email = 'ada@example.invalid'")).toBe(0);
|
||||
});
|
||||
|
||||
it('emails a changed shift, cancels a date that no longer fits, and refuses a capacity below the roster', async () => {
|
||||
const db = await openDb();
|
||||
const eventId = insertEvent(db);
|
||||
const created = createTemplate(db, 1, eventId, templateInput({ weekday: 3, role: 'Sorting' }), NOW);
|
||||
expect(created.ok).toBe(true);
|
||||
if (!created.ok) return;
|
||||
insertSignup(db, shiftId(db, '2026-10-21'), 'Bea Example', 'bea@example.invalid');
|
||||
insertSignup(db, shiftId(db, '2026-11-04'), 'Cy Example', 'cy@example.invalid');
|
||||
insertSignup(db, shiftId(db, '2026-11-04'), 'Dee Example', 'dee@example.invalid');
|
||||
|
||||
const tooSmall = updateTemplate(db, 1, eventId, created.id, templateInput({ weekday: 3, capacity: 1 }), NOW, MAIL);
|
||||
expect(tooSmall).toEqual({ ok: false, error: 'Capacity cannot be lower than the number of people signed up.' });
|
||||
expect((db.prepare('SELECT capacity, revision FROM shift_templates WHERE id = ?').get(created.id) as { capacity: number; revision: number })).toEqual({
|
||||
capacity: 6,
|
||||
revision: 1,
|
||||
});
|
||||
|
||||
const timed = updateTemplate(db, 1, eventId, created.id, templateInput({ weekday: 3, start: '10:00', end: '13:00' }), NOW, MAIL);
|
||||
expect(timed).toMatchObject({ ok: true, mailed: 3 });
|
||||
expect(db.prepare(`SELECT starts_at_utc, ends_at_utc, role FROM shifts WHERE occurrence_date = '2026-10-21'`).get()).toEqual({
|
||||
starts_at_utc: '2026-10-21T15:00:00.000Z',
|
||||
ends_at_utc: '2026-10-21T18:00:00.000Z',
|
||||
role: 'Sorting',
|
||||
});
|
||||
expect(db.prepare(`SELECT starts_at_utc FROM shifts WHERE occurrence_date = '2026-10-14'`).get()).toEqual({
|
||||
starts_at_utc: '2026-10-14T14:00:00.000Z',
|
||||
});
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM outbox WHERE kind = 'changed' AND status = 'pending'")).toBe(3);
|
||||
|
||||
const moved = updateTemplate(db, 1, eventId, created.id, templateInput({ weekday: 5, start: '10:00', end: '13:00' }), NOW, MAIL);
|
||||
expect(moved).toMatchObject({ ok: true, mailed: 3 });
|
||||
expect(db.prepare(`SELECT cancelled FROM shifts WHERE occurrence_date = '2026-10-21'`).get()).toEqual({ cancelled: 1 });
|
||||
expect(db.prepare(`SELECT cancelled FROM shifts WHERE occurrence_date = '2026-11-04'`).get()).toEqual({ cancelled: 1 });
|
||||
expect(db.prepare(`SELECT cancelled, role FROM shifts WHERE occurrence_date = '2026-10-14'`).get()).toEqual({ cancelled: 0, role: 'Sorting' });
|
||||
expect(count(db, `SELECT COUNT(*) AS n FROM shifts WHERE occurrence_date >= '2026-10-16' AND cancelled = 0`)).toBe(13);
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM outbox WHERE kind = 'cancelled' AND status = 'pending'")).toBe(3);
|
||||
});
|
||||
|
||||
it('puts a removed date back when the template is saved again', async () => {
|
||||
const db = await openDb();
|
||||
const eventId = insertEvent(db);
|
||||
const created = createTemplate(db, 1, eventId, templateInput({ weekday: 3, role: 'Sorting' }), NOW);
|
||||
expect(created.ok).toBe(true);
|
||||
if (!created.ok) return;
|
||||
db.prepare(`DELETE FROM shifts WHERE occurrence_date = '2026-10-21'`).run();
|
||||
const again = updateTemplate(db, 1, eventId, created.id, templateInput({ weekday: 3, role: 'Sorting' }), NOW, MAIL);
|
||||
expect(again).toMatchObject({ ok: true, mailed: 0 });
|
||||
expect(count(db, `SELECT COUNT(*) AS n FROM shifts WHERE occurrence_date = '2026-10-21' AND role = 'Sorting'`)).toBe(1);
|
||||
expect((db.prepare('SELECT revision FROM shift_templates WHERE id = ?').get(created.id) as { revision: number }).revision).toBe(1);
|
||||
});
|
||||
|
||||
it('cancels upcoming template shifts and deletes shifts that already ended', async () => {
|
||||
const db = await openDb();
|
||||
const eventId = insertEvent(db);
|
||||
const created = createTemplate(db, 1, eventId, templateInput({ weekday: 3, role: 'Sorting' }), NOW);
|
||||
expect(created.ok).toBe(true);
|
||||
if (!created.ok) return;
|
||||
expandTemplates(db, NOW, { eventId, coordinatorId: 1 });
|
||||
const past = db.prepare(`SELECT id AS id FROM shifts WHERE occurrence_date = '2026-10-14'`).get() as { id: number };
|
||||
const future = db.prepare(`SELECT id AS id FROM shifts WHERE occurrence_date = '2026-10-21'`).get() as { id: number };
|
||||
insertSignup(db, past.id, 'Ada Example', 'ada@example.invalid');
|
||||
insertSignup(db, future.id, 'Bea Example', 'bea@example.invalid');
|
||||
|
||||
const ended = endTemplate(db, 1, eventId, created.id, NOW, MAIL);
|
||||
expect(ended).toMatchObject({ ok: true, already: false, mailed: 1 });
|
||||
expect(count(db, 'SELECT COUNT(*) AS n FROM shifts WHERE id = ?', past.id)).toBe(0);
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM signups WHERE email_norm = 'ada@example.invalid'")).toBe(0);
|
||||
expect(db.prepare('SELECT cancelled AS cancelled FROM shifts WHERE id = ?').get(future.id)).toEqual({ cancelled: 1 });
|
||||
expect(db.prepare("SELECT status AS status FROM signups WHERE email_norm = 'bea@example.invalid'").get()).toEqual({ status: 'cancelled' });
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM outbox WHERE kind = 'cancelled' AND to_email = 'bea@example.invalid' AND status = 'pending'")).toBe(1);
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM outbox WHERE to_email = 'ada@example.invalid'")).toBe(0);
|
||||
expandTemplates(db, NOW, { eventId, coordinatorId: 1 });
|
||||
expect(count(db, `SELECT COUNT(*) AS n FROM shifts WHERE occurrence_date = '2026-10-14'`)).toBe(0);
|
||||
expect(db.prepare('PRAGMA foreign_key_check').all()).toEqual([]);
|
||||
const audit = db.prepare(`SELECT detail_json AS detail_json FROM audit WHERE action = 'template.end'`).get() as { detail_json: string };
|
||||
expect(audit.detail_json).not.toContain('@');
|
||||
});
|
||||
|
||||
it('rejects an overnight template and an end date before the first date', async () => {
|
||||
@@ -74,17 +195,13 @@ describe('weekly template editing', () => {
|
||||
const saved = await postTemplate(app, eventId, cookie, { weekday: '3', role: 'Sorting' });
|
||||
expect(saved.status).toBe(303);
|
||||
expect(saved.headers.get('location')).toBe(`/admin/events/${eventId}?notice=template-saved`);
|
||||
const generated = await postGenerate(app, eventId, cookie);
|
||||
expect(generated.status).toBe(303);
|
||||
const wednesdays = count(db, "SELECT COUNT(*) AS n FROM shifts WHERE role = 'Sorting'");
|
||||
expect(wednesdays).toBe(14);
|
||||
|
||||
const friday = await postTemplate(app, eventId, cookie, { weekday: '5', role: 'Intake', capacity: '4' });
|
||||
expect(friday.headers.get('location')).toBe(`/admin/events/${eventId}?notice=template-saved`);
|
||||
await postGenerate(app, eventId, cookie);
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM shifts WHERE role = 'Sorting'")).toBe(wednesdays);
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM shifts WHERE role = 'Intake' AND capacity = 4")).toBe(13);
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM shifts WHERE template_revision = 1")).toBe(wednesdays + 13);
|
||||
|
||||
const templateId = (
|
||||
db.prepare("SELECT id AS id FROM shift_templates WHERE role = 'Intake'").get() as { id: number }
|
||||
@@ -95,14 +212,21 @@ describe('weekly template editing', () => {
|
||||
revision: 2,
|
||||
role: 'Packing',
|
||||
});
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM shifts WHERE role = 'Intake'")).toBe(13);
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM shifts WHERE role = 'Intake'")).toBe(0);
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM shifts WHERE role = 'Packing' AND template_revision = 2")).toBe(13);
|
||||
|
||||
const ended = await postEnd(app, eventId, templateId, cookie);
|
||||
expect(ended.headers.get('location')).toBe(`/admin/events/${eventId}?notice=template-ended`);
|
||||
const page = await app.request(`/admin/events/${eventId}?notice=template-ended`, { headers: { cookie } });
|
||||
const html = await page.text();
|
||||
expect(html).toContain('This weekly template has ended. Shifts already on the schedule stay.');
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM shifts WHERE role = 'Intake'")).toBe(13);
|
||||
expect(html).toContain('This template has ended. The shifts it added are off this list.');
|
||||
expect(html).toContain('Templates');
|
||||
expect(html).toContain('Add a template');
|
||||
expect(html).toContain('Each one keeps three months of shifts filled.');
|
||||
expect(html).not.toContain('Repeating shifts');
|
||||
expect(html).not.toContain('Fill missing shifts');
|
||||
expect(html).not.toContain('Intake');
|
||||
expect(count(db, "SELECT COUNT(*) AS n FROM shifts WHERE role = 'Packing' AND cancelled = 1")).toBe(13);
|
||||
expect(count(db, 'SELECT COUNT(*) AS n FROM outbox')).toBe(1);
|
||||
});
|
||||
});
|
||||
@@ -211,15 +335,6 @@ async function postTemplate(
|
||||
});
|
||||
}
|
||||
|
||||
async function postGenerate(app: Awaited<ReturnType<typeof buildApp>>['app'], eventId: number, cookie: string) {
|
||||
const csrf = await csrfFor(app, eventId, cookie);
|
||||
return app.request(`/admin/events/${eventId}/generate`, {
|
||||
method: 'POST',
|
||||
headers: { origin: ORIGIN, 'content-type': 'application/x-www-form-urlencoded', cookie },
|
||||
body: new URLSearchParams({ csrf }),
|
||||
});
|
||||
}
|
||||
|
||||
async function postEnd(app: Awaited<ReturnType<typeof buildApp>>['app'], eventId: number, templateId: number, cookie: string) {
|
||||
const csrf = await csrfFor(app, eventId, cookie);
|
||||
return app.request(`/admin/events/${eventId}/templates/${templateId}/end`, {
|
||||
@@ -229,6 +344,18 @@ async function postEnd(app: Awaited<ReturnType<typeof buildApp>>['app'], eventId
|
||||
});
|
||||
}
|
||||
|
||||
function count(db: Awaited<ReturnType<typeof openDb>>, sql: string): number {
|
||||
return Number((db.prepare(sql).get() as { n: number }).n);
|
||||
function count(db: Awaited<ReturnType<typeof openDb>>, sql: string, ...params: Array<number | string>): number {
|
||||
return Number((db.prepare(sql).get(...params) as { n: number }).n);
|
||||
}
|
||||
|
||||
function shiftId(db: Awaited<ReturnType<typeof openDb>>, date: string): number {
|
||||
return (db.prepare('SELECT id AS id FROM shifts WHERE occurrence_date = ?').get(date) as { id: number }).id;
|
||||
}
|
||||
|
||||
function insertSignup(db: Awaited<ReturnType<typeof openDb>>, shiftId: number, name: string, email: string): void {
|
||||
db.prepare(
|
||||
`INSERT INTO signups (
|
||||
shift_id, name, email, email_norm, is_minor, group_size, status, reminders_off, created_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, 0, 1, 'confirmed', 0, ?, ?)`,
|
||||
).run(shiftId, name, email, email, NOW, NOW);
|
||||
}
|
||||
Reference in new issue
Block a user