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.
This commit is contained in:
1 parent
cd71fa2e59
commit
a53389d7a8
5 files changed
+130
-13
No files matched your search
+8
-1
@@ -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;
|
||||
};
|
||||
+3
-2
@@ -423,6 +423,7 @@ export function ManagePage(props: {
|
||||
<time dateTime={signup.when.date}>{signup.when.dateLabel}</time> {signup.when.timeLabel} {signup.when.abbreviation}
|
||||
</p>
|
||||
{signup.location ? <p class="location">{signup.location}</p> : null}
|
||||
{!cancelled && signup.ended ? <p>This shift has ended.</p> : null}
|
||||
</header>
|
||||
{props.note ? <p class="status">{props.note}</p> : null}
|
||||
{props.notice ? (
|
||||
@@ -558,13 +559,13 @@ export function ManagePage(props: {
|
||||
</div>
|
||||
</div>
|
||||
</dialog>
|
||||
) : (
|
||||
) : cancelled ? (
|
||||
<p>
|
||||
{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.'}
|
||||
</p>
|
||||
)}
|
||||
) : null}
|
||||
</article>
|
||||
);
|
||||
}
|
||||
|
||||
+17
-7
@@ -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',
|
||||
<ManagePage
|
||||
signup={signup}
|
||||
cancel={cancelForm(services, token, signup.status)}
|
||||
cancel={cancelForm(services, token, signup.status, signup.ended)}
|
||||
switches={switchForm(services, token, signup)}
|
||||
party={partyForm(services, token, signup, partyError)}
|
||||
reminders={reminderForm(services, token, signup)}
|
||||
@@ -372,6 +373,7 @@ async function submitCancel(c: Context, services: SignupServices): Promise<Respo
|
||||
}
|
||||
try {
|
||||
const result = cancelSignup(services.db, token, services.now(), mailIdentity(services.db, services.config));
|
||||
if (!result.ok && result.reason === 'ended') return showManage(c, services, token, 400, SHIFT_ENDED);
|
||||
if (!result.ok) return missingManage(c, services);
|
||||
} catch (err) {
|
||||
if (!isBusy(err)) throw err;
|
||||
@@ -395,6 +397,7 @@ async function submitSwitch(c: Context, services: SignupServices): Promise<Respo
|
||||
return showManage(c, services, token, 400, SWITCH_REJECTED);
|
||||
}
|
||||
if (signup.status !== 'confirmed') return showManage(c, services, token, 400, 'This signup is cancelled.');
|
||||
if (signup.ended) return showManage(c, services, token, 400, SHIFT_ENDED);
|
||||
const destination = Number(textField(body, 'destination'));
|
||||
if (!Number.isSafeInteger(destination) || destination <= 0) return showManage(c, services, token, 400, 'Choose a shift.');
|
||||
const formToken = readFormToken(services.config.sessionSecret, textField(body, 'form_token'), signup.shiftId, epochMs(services.now()), 0);
|
||||
@@ -432,10 +435,10 @@ function switchedAlready(c: Context, services: SignupServices, token: string, bo
|
||||
function partyForm(
|
||||
services: SignupServices,
|
||||
token: string,
|
||||
signup: { status: 'confirmed' | 'cancelled'; shiftId: number; groupSize: number },
|
||||
signup: { status: 'confirmed' | 'cancelled'; ended: boolean; shiftId: number; groupSize: number },
|
||||
error?: string,
|
||||
): PartyForm | null {
|
||||
if (signup.status !== 'confirmed') return null;
|
||||
if (signup.status !== 'confirmed' || signup.ended) return null;
|
||||
return {
|
||||
action: appPath(services.config, `/m/${token}/party`),
|
||||
token: issuePartyToken(services.config.sessionSecret, token),
|
||||
@@ -457,6 +460,7 @@ async function submitParty(c: Context, services: SignupServices): Promise<Respon
|
||||
return showManage(c, services, token, 400, PARTY_REJECTED);
|
||||
}
|
||||
if (signup.status !== 'confirmed') return showManage(c, services, token, 400, 'This signup is cancelled.');
|
||||
if (signup.ended) return showManage(c, services, token, 400, SHIFT_ENDED);
|
||||
const raw = textField(body, 'party_size');
|
||||
const parsed = raw.trim().length === 0
|
||||
? { ok: false as const, error: 'Choose how many people are coming.' }
|
||||
@@ -465,6 +469,7 @@ async function submitParty(c: Context, services: SignupServices): Promise<Respon
|
||||
try {
|
||||
const result = changePartySize(services.db, token, parsed.groupSize, services.now(), mailIdentity(services.db, services.config));
|
||||
if (!result.ok && result.reason === 'cancelled') return showManage(c, services, token, 400, 'This signup is cancelled.');
|
||||
if (!result.ok && result.reason === 'ended') return showManage(c, services, token, 400, SHIFT_ENDED);
|
||||
if (!result.ok && result.reason === 'full') {
|
||||
const spotsLeft = result.spotsLeft ?? 0;
|
||||
return showManage(c, services, token, 409, undefined, spotsLeft > 0 ? spotsLeftMessage(spotsLeft) : FULL);
|
||||
@@ -479,8 +484,8 @@ async function submitParty(c: Context, services: SignupServices): Promise<Respon
|
||||
return c.redirect(appPath(services.config, `/m/${token}?notice=party`), 303);
|
||||
}
|
||||
|
||||
function switchForm(services: SignupServices, token: string, signup: { status: 'confirmed' | 'cancelled'; shiftId: number; eventId: number; name: string; email: string; groupSize: number }): SwitchForm | null {
|
||||
if (signup.status !== 'confirmed') return null;
|
||||
function switchForm(services: SignupServices, token: string, signup: { status: 'confirmed' | 'cancelled'; ended: boolean; shiftId: number; eventId: number; name: string; email: string; groupSize: number }): SwitchForm | null {
|
||||
if (signup.status !== 'confirmed' || signup.ended) return null;
|
||||
const targets = listMoveTargets(services.db, signup.eventId, signup.shiftId, services.now(), {
|
||||
publishedOnly: true,
|
||||
signupWindow: true,
|
||||
@@ -539,8 +544,13 @@ async function submitReminders(c: Context, services: SignupServices): Promise<Re
|
||||
return c.redirect(appPath(services.config, `/m/${token}`), 303);
|
||||
}
|
||||
|
||||
function cancelForm(services: SignupServices, token: string, status: 'confirmed' | 'cancelled'): { action: string; token: string } | null {
|
||||
if (status !== 'confirmed') return null;
|
||||
function cancelForm(
|
||||
services: SignupServices,
|
||||
token: string,
|
||||
status: 'confirmed' | 'cancelled',
|
||||
ended: boolean,
|
||||
): { action: string; token: string } | null {
|
||||
if (status !== 'confirmed' || ended) return null;
|
||||
return {
|
||||
action: appPath(services.config, `/m/${token}`),
|
||||
token: issueCancelToken(services.config.sessionSecret, token),
|
||||
|
||||
+23
-3
@@ -47,6 +47,8 @@ export type ManageView = {
|
||||
location: string;
|
||||
eventTitle: string;
|
||||
when: ZonedClock;
|
||||
/** True once the shift's end instant has been reached. The private link can no longer change this signup. */
|
||||
ended: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -85,12 +87,13 @@ export function confirmedOperationShift(db: Database.Database, operationId: stri
|
||||
return row?.status === 'confirmed' ? found.shiftId : null;
|
||||
}
|
||||
|
||||
export type CancelResult = { ok: true; already: boolean } | { ok: false; reason: 'invalid' };
|
||||
export type CancelResult = { ok: true; already: boolean } | { ok: false; reason: 'invalid' | 'ended' };
|
||||
|
||||
/**
|
||||
* Marks this signup cancelled, frees its spot, and queues cancellation mail.
|
||||
* Cancelling the same link again is the same result and does not queue a second letter.
|
||||
* A pending confirmation is cancelled in this transaction. Mail already accepted cannot be recalled.
|
||||
* Once the shift has ended, a confirmed signup stays confirmed and no mail is queued.
|
||||
*/
|
||||
export function cancelSignup(db: Database.Database, token: string, now: string, mail: MailIdentity): CancelResult {
|
||||
if (!manageTokenShape(token)) return { ok: false, reason: 'invalid' };
|
||||
@@ -100,13 +103,14 @@ export function cancelSignup(db: Database.Database, token: string, now: string,
|
||||
|
||||
export type PartySizeResult =
|
||||
| { ok: true; groupSize: number; changed: boolean }
|
||||
| { ok: false; reason: 'invalid' | 'cancelled' | 'full'; spotsLeft?: number };
|
||||
| { ok: false; reason: 'invalid' | 'cancelled' | 'full' | 'ended'; spotsLeft?: number };
|
||||
|
||||
/**
|
||||
* Changes how many spots this one signup holds. The signup stays confirmed, under the same name.
|
||||
* A smaller count frees the difference. A larger count is saved only when the shift still has room
|
||||
* and the new count is within the stored maximum. Saving the current count does not write again.
|
||||
* A real change tells each active coordinator once for that new count.
|
||||
* Once the shift has ended, the count stays as it is and no mail is queued.
|
||||
*/
|
||||
export function changePartySize(
|
||||
db: Database.Database,
|
||||
@@ -148,7 +152,8 @@ export function readManageSignup(db: Database.Database, token: string, now: stri
|
||||
e.title AS title,
|
||||
e.location AS location,
|
||||
e.tz AS tz,
|
||||
s.starts_at_utc AS starts_at_utc
|
||||
s.starts_at_utc AS starts_at_utc,
|
||||
s.ends_at_utc AS ends_at_utc
|
||||
FROM signups su
|
||||
JOIN shifts s ON s.id = su.shift_id
|
||||
JOIN events e ON e.id = s.event_id
|
||||
@@ -174,6 +179,7 @@ export function readManageSignup(db: Database.Database, token: string, now: stri
|
||||
location: row.location,
|
||||
eventTitle: row.title,
|
||||
when: formatInstantInZone(row.starts_at_utc, row.tz),
|
||||
ended: row.ends_at_utc <= now,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -355,6 +361,9 @@ function writePartySize(
|
||||
}
|
||||
if (row.status === 'cancelled') return { ok: false, reason: 'cancelled' };
|
||||
if (row.status !== 'confirmed') 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' };
|
||||
if (!Number.isInteger(groupSize) || groupSize < 1 || groupSize > 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;
|
||||
};
|
||||
@@ -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('<dd>3</dd>');
|
||||
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 <volunteer@example.invalid>',
|
||||
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 }) {
|
||||
|
||||
Reference in new issue
Block a user