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:
blake committed 2026-11-02 12:10:00 -06:00
1 parent cd71fa2e59
commit a53389d7a8
5 files changed
+130 -13

No files matched your search

+8 -1
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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;
};