feat: enhance party size management with coordinator notifications
Updated the changePartySize function to include mail identity and notify coordinators when a signup's party size changes. Enhanced the writePartySize function to queue notices for coordinators, detailing changes in group size. Added new tests to verify the notification logic for party size adjustments, ensuring accurate communication of changes to coordinators.
This commit is contained in:
1 parent
17ca2f9dfb
commit
cd71fa2e59
5 files changed
+98
-9
No files matched your search
+5
-2
@@ -66,7 +66,8 @@ export function queueChanged(db: Database.Database, input: QueuedSignup, reason:
|
||||
|
||||
/**
|
||||
* One roster notice per active coordinator.
|
||||
* The private signup link is not included. The same signup and change does not add a second row.
|
||||
* The private signup link is not included.
|
||||
* The same signup, action, shift version, and party size does not add a second row.
|
||||
*/
|
||||
export function queueCoordinatorNotices(
|
||||
db: Database.Database,
|
||||
@@ -78,6 +79,7 @@ export function queueCoordinatorNotices(
|
||||
volunteerName: string;
|
||||
volunteerEmail: string;
|
||||
groupSize?: number;
|
||||
previousGroupSize?: number;
|
||||
shift: MailShift;
|
||||
fromShift?: MailShift;
|
||||
mail: MailIdentity;
|
||||
@@ -111,6 +113,7 @@ export function queueCoordinatorNotices(
|
||||
volunteerEmail: input.volunteerEmail,
|
||||
phone: phoneRow?.phone ?? null,
|
||||
groupSize: input.groupSize,
|
||||
previousGroupSize: input.previousGroupSize,
|
||||
action: input.action,
|
||||
shift: input.shift,
|
||||
fromShift: input.fromShift,
|
||||
@@ -118,7 +121,7 @@ export function queueCoordinatorNotices(
|
||||
replyTo: input.mail.replyTo,
|
||||
});
|
||||
insert.run(
|
||||
`notice:${input.action}:v${input.shiftVersion}:${input.signupId}:${coordinator.email}`,
|
||||
`notice:${input.action}:v${input.shiftVersion}:${input.groupSize ?? 1}:${input.signupId}:${coordinator.email}`,
|
||||
input.signupId,
|
||||
input.shiftId,
|
||||
coordinator.email,
|
||||
|
||||
+27
-1
@@ -165,7 +165,7 @@ export function renderReminder(input: {
|
||||
return { subject, text: lines.join('\n'), html };
|
||||
}
|
||||
|
||||
export type CoordinatorNoticeAction = 'signup' | 'cancel' | 'switch' | 'move' | 'change';
|
||||
export type CoordinatorNoticeAction = 'signup' | 'cancel' | 'switch' | 'move' | 'change' | 'party';
|
||||
|
||||
/** A roster notice for coordinators. It names the person and links to the shift. It does not include the private signup link. */
|
||||
export function renderCoordinatorNotice(input: {
|
||||
@@ -174,6 +174,7 @@ export function renderCoordinatorNotice(input: {
|
||||
volunteerEmail: string;
|
||||
phone: string | null;
|
||||
groupSize?: number;
|
||||
previousGroupSize?: number;
|
||||
action: CoordinatorNoticeAction;
|
||||
shift: MailShift;
|
||||
fromShift?: MailShift;
|
||||
@@ -250,6 +251,7 @@ export function renderMagicLink(input: { signInUrl: string; replyTo: string }):
|
||||
function noticeCopy(input: {
|
||||
volunteerName: string;
|
||||
groupSize?: number;
|
||||
previousGroupSize?: number;
|
||||
action: CoordinatorNoticeAction;
|
||||
shift: MailShift;
|
||||
fromShift?: MailShift;
|
||||
@@ -260,6 +262,7 @@ function noticeCopy(input: {
|
||||
const from = input.fromShift
|
||||
? ` from ${singleLine(input.fromShift.role)} (${singleLine(input.fromShift.whenLabel)})`
|
||||
: '';
|
||||
if (input.action === 'party') return partyChangeCopy(name, role, input.previousGroupSize, input.groupSize ?? 1);
|
||||
if (input.action === 'signup') {
|
||||
return { subject: `${name} signed up for ${role}`, hello: `${name} signed up for ${role}.${party}` };
|
||||
}
|
||||
@@ -285,6 +288,29 @@ function noticeCopy(input: {
|
||||
};
|
||||
}
|
||||
|
||||
function partyChangeCopy(
|
||||
name: string,
|
||||
role: string,
|
||||
previous: number | undefined,
|
||||
next: number,
|
||||
): { subject: string; hello: string } {
|
||||
const nextPeople = next === 1 ? '1 person' : `${next} people`;
|
||||
const subject = `${name} changed to ${nextPeople} for ${role}`;
|
||||
if (!previous || previous === next) {
|
||||
const spots = next === 1 ? '1 spot' : `${next} spots`;
|
||||
return { subject, hello: `${name} changed this signup for ${role} to ${nextPeople}. This signup holds ${spots}.` };
|
||||
}
|
||||
const previousPeople = previous === 1 ? '1 person' : `${previous} people`;
|
||||
const difference = next - previous;
|
||||
const delta = difference > 0
|
||||
? `That takes ${difference} more ${difference === 1 ? 'spot' : 'spots'}.`
|
||||
: `That opens ${-difference} ${difference === -1 ? 'spot' : 'spots'}.`;
|
||||
return {
|
||||
subject,
|
||||
hello: `${name} changed this signup for ${role} from ${previousPeople} to ${nextPeople}. ${delta}`,
|
||||
};
|
||||
}
|
||||
|
||||
function partyNote(groupSize: number | undefined): string {
|
||||
if (!groupSize || groupSize <= 1) return '';
|
||||
return ` This signup holds ${groupSize} spots.`;
|
||||
|
||||
+1
-1
@@ -463,7 +463,7 @@ async function submitParty(c: Context, services: SignupServices): Promise<Respon
|
||||
: parsePartySize(raw);
|
||||
if (!parsed.ok) return showManage(c, services, token, 400, undefined, parsed.error);
|
||||
try {
|
||||
const result = changePartySize(services.db, token, parsed.groupSize, services.now());
|
||||
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 === 'full') {
|
||||
const spotsLeft = result.spotsLeft ?? 0;
|
||||
|
||||
+34
-4
@@ -106,10 +106,17 @@ export type PartySizeResult =
|
||||
* 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.
|
||||
*/
|
||||
export function changePartySize(db: Database.Database, token: string, groupSize: number, now: string): PartySizeResult {
|
||||
export function changePartySize(
|
||||
db: Database.Database,
|
||||
token: string,
|
||||
groupSize: number,
|
||||
now: string,
|
||||
mail: MailIdentity,
|
||||
): PartySizeResult {
|
||||
if (!manageTokenShape(token)) return { ok: false, reason: 'invalid' };
|
||||
return db.transaction(() => writePartySize(db, sha256(token), groupSize, now)).immediate();
|
||||
return db.transaction(() => writePartySize(db, sha256(token), groupSize, now, mail)).immediate();
|
||||
}
|
||||
|
||||
/** How many people this signup may offer, including the spots it already holds. */
|
||||
@@ -317,11 +324,17 @@ export function confirmedPersonShiftIds(db: Database.Database, emailNorm: string
|
||||
return rows.map((row) => row.shift_id);
|
||||
}
|
||||
|
||||
function writePartySize(db: Database.Database, hash: string, groupSize: number, now: string): PartySizeResult {
|
||||
function writePartySize(
|
||||
db: Database.Database,
|
||||
hash: string,
|
||||
groupSize: number,
|
||||
now: string,
|
||||
mail: MailIdentity,
|
||||
): PartySizeResult {
|
||||
const row = db
|
||||
.prepare(
|
||||
`SELECT id AS id, status AS status, token_expires_at AS token_expires_at, anonymized_at AS anonymized_at,
|
||||
shift_id AS shift_id, group_size AS group_size, name AS name
|
||||
shift_id AS shift_id, group_size AS group_size, name AS name, email_norm AS email_norm
|
||||
FROM signups
|
||||
WHERE manage_token_hash = ?`,
|
||||
)
|
||||
@@ -334,6 +347,7 @@ function writePartySize(db: Database.Database, hash: string, groupSize: number,
|
||||
shift_id: number;
|
||||
group_size: number;
|
||||
name: string | null;
|
||||
email_norm: string | null;
|
||||
}
|
||||
| undefined;
|
||||
if (!row || row.anonymized_at || !row.token_expires_at || row.token_expires_at <= now || !row.name) {
|
||||
@@ -357,6 +371,22 @@ function writePartySize(db: Database.Database, hash: string, groupSize: number,
|
||||
)
|
||||
.run(groupSize, now, row.id, now, current);
|
||||
if (updated.changes !== 1) return { ok: false, reason: 'invalid' };
|
||||
const shift = row.email_norm ? loadShiftFacts(db, row.shift_id) : null;
|
||||
if (shift && row.email_norm) {
|
||||
queueCoordinatorNotices(db, {
|
||||
action: 'party',
|
||||
signupId: row.id,
|
||||
shiftId: row.shift_id,
|
||||
shiftVersion: shift.version,
|
||||
volunteerName: row.name,
|
||||
volunteerEmail: row.email_norm,
|
||||
groupSize,
|
||||
previousGroupSize: current,
|
||||
shift: shift.facts,
|
||||
mail,
|
||||
now,
|
||||
});
|
||||
}
|
||||
return { ok: true, groupSize, changed: true };
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user