From a53389d7a810c699fdf8ab92abf8a55fbc89fcde Mon Sep 17 00:00:00 2001 From: blake Date: Mon, 2 Nov 2026 12:10:00 -0600 Subject: [PATCH] feat: implement shift end handling in signup management Enhanced the ManagePage and related functions to handle scenarios where a shift has ended. Added logic to prevent modifications to signups after the shift's end, including hiding party size options and cancel buttons. Updated the cancelSignup and changePartySize functions to return appropriate error messages when attempting to modify signups for ended shifts. Added tests to verify the new behavior and ensure proper handling of ended shifts across the application. --- src/admin/store.ts | 9 ++++- src/pages.tsx | 5 +-- src/signup-http.tsx | 24 ++++++++++---- src/signup.ts | 26 +++++++++++++-- tests/cancel.test.ts | 79 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 130 insertions(+), 13 deletions(-) diff --git a/src/admin/store.ts b/src/admin/store.ts index 36e4343..c8e2d7b 100644 --- a/src/admin/store.ts +++ b/src/admin/store.ts @@ -1382,6 +1382,9 @@ function relocate( if (source.status !== 'confirmed') { return { ok: false, error: transferError(options.audience, 'cancelled'), shiftId: source.shift_id }; } + if (options.audience === 'volunteer' && source.ends_at_utc <= now) { + return { ok: false, error: transferError(options.audience, 'ended'), shiftId: source.shift_id }; + } if (!Number.isSafeInteger(destinationShiftId) || destinationShiftId <= 0) { return { ok: false, error: transferError(options.audience, 'choose'), shiftId: source.shift_id }; } @@ -1534,6 +1537,7 @@ function transferError(audience: TransferOptions['audience'], code: TransferErro missing: 'That shift is not available.', event: 'Choose a shift in this event.', closed: 'That shift is closed.', + ended: 'This shift has ended.', early: 'That shift is not open for sign-up yet.', duplicate: 'That person is already signed up for this shift.', full: 'That shift is full.', @@ -1550,6 +1554,7 @@ function transferError(audience: TransferOptions['audience'], code: TransferErro missing: 'That shift is not available.', event: 'Choose a shift in this event.', closed: 'That shift is closed.', + ended: 'This shift has ended.', early: 'Sign-up for that shift opens three months before the date.', duplicate: 'You are already signed up for that shift.', full: 'That shift is full.', @@ -1569,6 +1574,7 @@ type TransferError = | 'missing' | 'event' | 'closed' + | 'ended' | 'early' | 'duplicate' | 'full' @@ -1583,7 +1589,7 @@ function loadMoveSource(db: Database.Database, signupId: number): MoveSource | n `SELECT su.id AS id, su.shift_id AS shift_id, s.event_id AS event_id, su.name AS name, su.email AS email, su.email_norm AS email_norm, su.phone AS phone, su.notes AS notes, su.is_minor AS is_minor, su.guardian_name AS guardian_name, su.guardian_email AS guardian_email, su.reminders_off AS reminders_off, - su.group_size AS group_size, su.status AS status + su.group_size AS group_size, su.status AS status, s.ends_at_utc AS ends_at_utc FROM signups su JOIN shifts s ON s.id = su.shift_id WHERE su.id = ? AND su.anonymized_at IS NULL`, @@ -1976,4 +1982,5 @@ type MoveSource = { reminders_off: number; group_size: number; status: string; + ends_at_utc: string; }; diff --git a/src/pages.tsx b/src/pages.tsx index a363e52..7d8558f 100644 --- a/src/pages.tsx +++ b/src/pages.tsx @@ -423,6 +423,7 @@ export function ManagePage(props: { {signup.when.timeLabel} {signup.when.abbreviation}

{signup.location ?

{signup.location}

: null} + {!cancelled && signup.ended ?

This shift has ended.

: null} {props.note ?

{props.note}

: null} {props.notice ? ( @@ -558,13 +559,13 @@ export function ManagePage(props: { - ) : ( + ) : cancelled ? (

{signup.groupSize > 1 ? `This signup is cancelled. The ${signup.groupSize} spots it held are open for someone else. A cancellation email is queued.` : 'This signup is cancelled. The spot is open for someone else. A cancellation email is queued.'}

- )} + ) : null} ); } diff --git a/src/signup-http.tsx b/src/signup-http.tsx index 7ce6589..2744e97 100644 --- a/src/signup-http.tsx +++ b/src/signup-http.tsx @@ -58,6 +58,7 @@ const PARTY_ORIGIN = 'The update could not be sent from this page.'; const PARTY_REJECTED = 'The update could not be confirmed. Please try again.'; const PARTY_UNAVAILABLE = 'The update could not be saved. Please try again in a little while.'; const PARTY_NOTE = 'The number of people on this signup is saved. The signup stays.'; +const SHIFT_ENDED = 'This shift has ended.'; const EXPIRED = 'This form expired. Please submit it again.'; export type SignupServices = { @@ -332,7 +333,7 @@ function showManage( 'Your signup ยท Hope Volunteer', 0 ? spotsLeftMessage(spotsLeft) : FULL); @@ -479,8 +484,8 @@ async function submitParty(c: Context, services: SignupServices): Promise MAX_PARTY_SIZE) return { ok: false, reason: 'invalid' }; const current = Number(row.group_size); @@ -390,6 +399,13 @@ function writePartySize( return { ok: true, groupSize, changed: true }; } +function shiftEndsAt(db: Database.Database, shiftId: number): string | null { + const shift = db.prepare('SELECT ends_at_utc AS ends_at_utc FROM shifts WHERE id = ?').get(shiftId) as + | { ends_at_utc: string } + | undefined; + return shift?.ends_at_utc ?? null; +} + function partyRoom( db: Database.Database, shiftId: number, @@ -433,6 +449,9 @@ function cancelConfirmed(db: Database.Database, hash: string, token: string, now } if (row.status === 'cancelled') return { ok: true, already: true }; if (row.status !== 'confirmed' || !row.name || !row.email_norm) return { ok: false, reason: 'invalid' }; + const endsAt = shiftEndsAt(db, row.shift_id); + if (endsAt === null) return { ok: false, reason: 'invalid' }; + if (endsAt <= now) return { ok: false, reason: 'ended' }; const updated = db .prepare( `UPDATE signups @@ -558,4 +577,5 @@ type ManageRow = { location: string; tz: string; starts_at_utc: string; + ends_at_utc: string; }; diff --git a/tests/cancel.test.ts b/tests/cancel.test.ts index f592b69..7dca8c7 100644 --- a/tests/cancel.test.ts +++ b/tests/cancel.test.ts @@ -4,8 +4,10 @@ import { tmpdir } from 'node:os'; import path from 'node:path'; import { Temporal } from '@js-temporal/polyfill'; import { afterEach, describe, expect, it } from 'vitest'; +import { switchSignup } from '../src/admin/store.js'; import { createApp } from '../src/app.js'; import { issueCancelToken, issuePartyToken } from '../src/cancel-token.js'; +import { cancelSignup, changePartySize } from '../src/signup.js'; import { loadConfig } from '../src/config.js'; import { openAppDatabase } from '../src/db.js'; import { issueFormToken } from '../src/form-token.js'; @@ -458,6 +460,83 @@ describe('manage-link party size', () => { status: 'cancelled', }]); }); + + it('hides party size, cancel, and switch once the shift has ended', async () => { + const { app, db, shiftId } = await buildApp({ capacity: 6 }); + const eventId = Number((db.prepare('SELECT id AS id FROM events').get() as { id: number }).id); + const destinationId = insertShift(db, eventId, 'Distribution', '2026-10-17T14:00:00.000Z', '2026-10-17T17:00:00.000Z', 4); + const saved = await postSignup(app, shiftId, { name: 'Pat Party', email: 'pat.late@example.invalid', partySize: '3' }, '203.0.113.150'); + const managePath = await managePathFrom(app, saved); + const openHtml = await (await app.request(managePath)).text(); + expect(openHtml).toContain('How many people?'); + expect(openHtml).toContain('Cancel this signup'); + expect(optionValues(openHtml)).toEqual([String(destinationId)]); + const signupId = Number((db.prepare('SELECT id AS id FROM signups').get() as { id: number }).id); + db.prepare( + `INSERT INTO coordinators (email, name, role, password_hash, active, created_at) + VALUES ('ada@example.invalid', 'Ada Admin', 'coordinator', NULL, 1, ?)`, + ).run(NOW); + // 13 hours after the shift ended, the private link is still valid. + db.prepare('UPDATE shifts SET starts_at_utc = ?, ends_at_utc = ? WHERE id = ?').run( + '2026-10-14T02:00:00.000Z', + '2026-10-14T05:00:00.000Z', + shiftId, + ); + db.prepare("UPDATE signups SET updated_at = '2026-10-14T04:00:00.000Z' WHERE id = ?").run(signupId); + + const ended = await app.request(managePath); + const html = await ended.text(); + expect(ended.status).toBe(200); + expect(html).toContain('Confirmed'); + expect(html).toContain('This shift has ended.'); + expect(html).toContain('
3
'); + expect(html).not.toContain('How many people?'); + expect(html).not.toContain('Cancel this signup'); + expect(html).not.toContain('name="party_size"'); + expect(html).not.toContain('name="cancel_token"'); + expect(html).not.toContain('name="destination"'); + expect(html).not.toContain('The spot is open for someone else'); + expect(html).not.toContain('spots are open for someone else'); + + const token = managePath.slice(3); + const party = await postParty(app, managePath, openHtml, '1', { ip: '203.0.113.151' }); + expect(party.status).toBe(400); + expect(await party.text()).toContain('This shift has ended.'); + const cancelled = await postCancel(app, managePath, field(openHtml, 'cancel_token'), { ip: '203.0.113.152' }); + expect(cancelled.status).toBe(400); + expect(await cancelled.text()).toContain('This shift has ended.'); + const switched = await postSwitch(app, managePath, openHtml, String(destinationId)); + expect(switched.status).toBe(400); + expect(await switched.text()).toContain('This shift has ended.'); + + const mail = { + baseUrl: 'http://127.0.0.1:3000', + basePath: '', + from: 'Hope Food Pantry ', + replyTo: 'volunteer@example.invalid', + }; + expect(changePartySize(db, token, 1, NOW, mail)).toEqual({ ok: false, reason: 'ended' }); + expect(changePartySize(db, token, 3, NOW, mail)).toEqual({ ok: false, reason: 'ended' }); + expect(cancelSignup(db, token, NOW, mail)).toEqual({ ok: false, reason: 'ended' }); + expect(switchSignup(db, signupId, destinationId, 'ended-switch-operation', NOW, mail)).toEqual({ + ok: false, + error: 'This shift has ended.', + shiftId, + }); + + expect(db.prepare('SELECT name, group_size, status, shift_id, updated_at FROM signups').all()).toEqual([{ + name: 'Pat Party', + group_size: 3, + status: 'confirmed', + shift_id: shiftId, + updated_at: '2026-10-14T04:00:00.000Z', + }]); + expect(db.prepare('SELECT kind, status FROM outbox ORDER BY id').all()).toEqual([ + { kind: 'confirmation', status: 'pending' }, + ]); + const letters = db.prepare('SELECT body_text FROM outbox').all() as { body_text: string }[]; + expect(letters.map((letter) => letter.body_text).join('\n')).not.toContain('open for someone else'); + }); }); async function buildApp(options?: { capacity?: number }) {