Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions client/e2e/tests/38-exclusions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import { test, expect } from "@playwright/test";
import type { RowDataPacket } from "mysql2";

import {
assignRole,
cleanupAllTestData,
closePool,
getPool,
insertFullShift,
insertRole,
insertVolunteer,
insertVolunteerShift,
} from "../helpers/db";
import { buildSessionCookieHeader } from "../helpers/session";
import {
FUTURE_SHIFT,
makeTestVolunteer,
ROLE_BEHAVIORAL_STANDARDS_ID,
signInAsBuiltinAdmin,
} from "../fixtures/test-data";

// #848 (Mew 2026-11-29): exclusions are roles. A shift type or position can
// be hidden from a role; NoLogin ends the session; DoNotContact gets no email.
const ROLE_NO_LOGIN_ID = 2000040;
const ROLE_DO_NOT_CONTACT_ID = 2000043;
const HIDE_ROLE_ID = 9_000_510;
const volunteer = makeTestVolunteer({
shiftboardId: 9_000_511,
playaName: "E2E Excluded",
worldName: "Excluded Tester",
email: "e2e-excluded@test.local",
passcode: "5110",
});
const HIDDEN_SHIFT = {
...FUTURE_SHIFT,
categoryId: 9_000_512,
categoryName: "E2E Hidden Category",
shiftNameId: 9_000_513,
shiftName: "E2E Hidden Shift",
shiftTimesId: 9_000_514,
positionTypeId: 9_000_515,
positionName: "E2E Hidden Position",
timePositionId: 9_000_516,
};
const asVolunteer = {
Cookie: buildSessionCookieHeader(volunteer.shiftboardId),
};
const addBody = (timePositionId: number) =>
JSON.stringify({ shiftboardId: volunteer.shiftboardId, timePositionId });

test.describe("Exclusions (#848)", () => {
test.beforeAll(async () => {
await cleanupAllTestData();
const db = getPool();
await db.execute("DELETE FROM op_email_queue WHERE `to` = ?", [
volunteer.email,
]);
await insertVolunteer(volunteer);
await assignRole(volunteer.shiftboardId, ROLE_BEHAVIORAL_STANDARDS_ID);
await insertRole(HIDE_ROLE_ID, "E2E No Hidden Shift");
await insertFullShift(FUTURE_SHIFT);
await insertFullShift(HIDDEN_SHIFT);
await db.execute(
"UPDATE op_position_type SET hidden_role_id = ? WHERE position_type_id = ?",
[HIDE_ROLE_ID, HIDDEN_SHIFT.positionTypeId]
);
});

test.afterAll(async () => {
await getPool().execute("DELETE FROM op_email_queue WHERE `to` = ?", [
volunteer.email,
]);
await cleanupAllTestData();
await closePool();
});

test("hidden position: not listed, not signable, flagged for admins", async ({
page,
request,
}) => {
const listed = async (headers?: Record<string, string>) => {
const res = await request.get("/api/shifts", { headers });
const list: { id: number }[] = await res.json();
return list.some(({ id }) => id === HIDDEN_SHIFT.shiftTimesId);
};
expect(await listed(asVolunteer)).toBe(true);

await assignRole(volunteer.shiftboardId, HIDE_ROLE_ID);
expect(await listed(asVolunteer)).toBe(false);
const detail = await request.get(
`/api/shifts/${HIDDEN_SHIFT.shiftTimesId}/volunteers`,
{ headers: asVolunteer }
);
expect(detail.status()).toBe(404);

// a hard block: neither the volunteer nor an admin can add them
const self = await request.post(
`/api/shifts/${HIDDEN_SHIFT.shiftTimesId}/volunteers`,
{ data: addBody(HIDDEN_SHIFT.timePositionId), headers: asVolunteer }
);
expect(self.ok()).toBe(false);
await signInAsBuiltinAdmin(page);
const byAdmin = await page.request.post(
`/api/shifts/${HIDDEN_SHIFT.shiftTimesId}/volunteers`,
{ data: addBody(HIDDEN_SHIFT.timePositionId) }
);
expect(byAdmin.ok()).toBe(false);
expect(await byAdmin.text()).toContain("excluded");

// signed up before the exclusion: the admin roster flags the row
await insertVolunteerShift(
volunteer.shiftboardId,
HIDDEN_SHIFT.timePositionId
);
const roster = await page.request.get(
`/api/shifts/${HIDDEN_SHIFT.shiftTimesId}/volunteers`
);
const { volunteerList } = await roster.json();
expect(volunteerList).toEqual([
expect.objectContaining({
isExcluded: true,
shiftboardId: volunteer.shiftboardId,
}),
]);
});

test("DoNotContact: no email, even when only the address is known", async ({
page,
}) => {
await assignRole(volunteer.shiftboardId, ROLE_DO_NOT_CONTACT_ID);
await signInAsBuiltinAdmin(page);
const res = await page.request.post(
`/api/shifts/${FUTURE_SHIFT.shiftTimesId}/volunteers`,
{ data: addBody(FUTURE_SHIFT.timePositionId) }
);
expect(res.status()).toBe(201);
// give the fire-and-forget notify a moment before asserting nothing came
await page.waitForTimeout(1500);
const [rows] = await getPool().query<RowDataPacket[]>(
"SELECT COUNT(*) AS n FROM op_email_queue WHERE `to` LIKE ?",
[`%${volunteer.email}%`]
);
expect(Number(rows[0].n)).toBe(0);
});

test("NoLogin: a live session gets 401 on its next request", async ({
request,
}) => {
const before = await request.get("/api/auth/session", {
headers: asVolunteer,
});
expect(before.status()).toBe(200);
await assignRole(volunteer.shiftboardId, ROLE_NO_LOGIN_ID);
const after = await request.get("/api/auth/session", {
headers: asVolunteer,
});
expect(after.status()).toBe(401);
});
});
45 changes: 42 additions & 3 deletions client/lib/mail/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type { EmailRow, EnqueueArgs, MailConfig } from "./types";
// ROLE_EMAIL_UNSUBSCRIBED_ID in client/src/constants.ts and the seed in
// migrations/003_email_unsubscribed_role.sql.
const ROLE_EMAIL_UNSUBSCRIBED_ID = 2000020;
// ROLE_DO_NOT_CONTACT_ID (#848, migration 031)
const ROLE_DO_NOT_CONTACT_ID = 2000043;

const APP_BASE_URL =
process.env.APP_BASE_URL ?? "https://volunteers.census.burningman.org";
Expand Down Expand Up @@ -36,6 +38,37 @@ async function isUnsubscribed(
return rows.length > 0;
}

const addrOf = (a: string) =>
(/<([^>]+)>/.exec(a)?.[1] ?? a).trim().toLowerCase();

// #848: DoNotContact drops a person from EVERY email, whether the caller
// passed recipientShiftboardId or only addresses. Returns the address list
// with them removed ("" if nobody is left).
async function withoutDoNotContact(
pool: Pool,
addrs: string | null,
shiftboardId: number | undefined
): Promise<string | null> {
if (!addrs) return addrs;
const list = addrs
.split(",")
.map((a) => a.trim())
.filter(Boolean);
const [rows] = await pool.query<RowDataPacket[]>(
`SELECT LOWER(TRIM(v.email)) AS email, v.shiftboard_id
FROM op_volunteers v
JOIN op_volunteer_roles vr
ON vr.shiftboard_id = v.shiftboard_id
AND vr.role_id = ?
AND vr.remove_role = false
WHERE LOWER(TRIM(v.email)) IN (?) OR v.shiftboard_id = ?`,
[ROLE_DO_NOT_CONTACT_ID, list.map(addrOf), shiftboardId ?? 0]
);
if (rows.some((r) => r.shiftboard_id === shiftboardId)) return "";
const blocked = new Set(rows.map((r) => r.email));
return list.filter((a) => !blocked.has(addrOf(a))).join(", ");
}

function appendUnsubscribeFooter(
bodyText: string,
bodyHtml: string | undefined,
Expand All @@ -54,8 +87,14 @@ export async function enqueueEmail(
config: MailConfig,
args: EnqueueArgs
): Promise<{ id: number; skipped?: boolean }> {
const to = joinAddrs(args.to);
if (!to) throw new Error("enqueueEmail: `to` is required");
if (!joinAddrs(args.to)) throw new Error("enqueueEmail: `to` is required");
const to = await withoutDoNotContact(
pool,
joinAddrs(args.to),
args.recipientShiftboardId ?? undefined
);
if (!to) return { id: 0, skipped: true };
const cc = await withoutDoNotContact(pool, joinAddrs(args.cc), undefined);

let bodyText = args.bodyText;
let bodyHtml = args.bodyHtml;
Expand All @@ -79,7 +118,7 @@ export async function enqueueEmail(
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
to,
joinAddrs(args.cc),
cc || null,
args.replyTo ?? config.defaultReplyTo,
config.from,
args.subject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ interface IDialogCurrentState {
}

const BS_UNSIGNED_TEXT = "Has not signed Behavioral Standards";
// #848: signed up before an exclusion role hid this shift from them
const EXCLUDED_TEXT = "(excluded)";

const socket = io();
export const ShiftVolunteers = ({
Expand Down Expand Up @@ -202,6 +204,7 @@ export const ShiftVolunteers = ({
// right after, which fills in the real value
critical: false,
isCheckedIn,
isExcluded: false,
notes,
playaName,
positionName,
Expand Down Expand Up @@ -549,6 +552,7 @@ export const ShiftVolunteers = ({
bsSigned,
critical,
isCheckedIn,
isExcluded,
notes,
playaName,
positionName,
Expand All @@ -563,7 +567,7 @@ export const ShiftVolunteers = ({
const row: React.ReactNode[] = [
playaName,
worldName,
positionName,
isExcluded ? `${positionName} ${EXCLUDED_TEXT}` : positionName,
// Check-in only appears once the shift's check-in window is open
// (getCheckInType !== SHIFT_FUTURE); before that it's meaningless.
isCheckInAvailable ? (
Expand Down Expand Up @@ -920,6 +924,7 @@ export const ShiftVolunteers = ({
bsSigned,
critical,
isCheckedIn,
isExcluded,
notes,
playaName,
positionName,
Expand Down Expand Up @@ -968,6 +973,7 @@ export const ShiftVolunteers = ({
sx={{ color: "text.secondary" }}
>
{positionName}
{isExcluded && ` ${EXCLUDED_TEXT}`}
</Typography>
<Stack alignItems="center" direction="row">
{isCheckInAvailable && (
Expand Down
37 changes: 36 additions & 1 deletion client/src/app/shifts/positions/ShiftPositionsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface IFormValues {
name: string;
};
role: { name: string };
hiddenRole: { name: string };
startTimeOffset: number;
}
interface IShiftPositionsFormProps {
Expand Down Expand Up @@ -63,7 +64,15 @@ export const findList = (
).id
: null;

return [prerequisiteIdFound, roleIdFound];
const hiddenRoleIdFound = formValues.hiddenRole.name
? ensure(
dataDefaults.roleList.find(({ name }: { name: string }) => {
return name === formValues.hiddenRole.name;
})
).id
: null;

return [prerequisiteIdFound, roleIdFound, hiddenRoleIdFound];
};

export const defaultValues: IFormValues = {
Expand All @@ -77,6 +86,7 @@ export const defaultValues: IFormValues = {
name: "",
},
role: { name: "" },
hiddenRole: { name: "" },
startTimeOffset: 0,
};
export const ShiftPositionsForm = ({
Expand Down Expand Up @@ -197,6 +207,31 @@ export const ShiftPositionsForm = ({
)}
/>
</Grid>
<Grid size={6}>
<Controller
control={control}
name="hiddenRole.name"
render={({ field }) => (
<FormControl fullWidth variant="standard">
<InputLabel id="hiddenRole">Hidden from role</InputLabel>
<Select
{...field}
label="Hidden from role"
labelId="hiddenRole"
>
<MenuItem key={0} value="">
<em>None</em>
</MenuItem>
{dataDefaults.roleList.map(({ id, name }) => (
<MenuItem key={id} value={name}>
{name}
</MenuItem>
))}
</Select>
</FormControl>
)}
/>
</Grid>
<Grid size={6}>
<Controller
control={control}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const ShiftPositionsCreate = () => {
// ------------------------------------------------------------
const onSubmit: SubmitHandler<IFormValues> = async (formValues) => {
try {
const [prerequisiteIdFound, roleIdFound] = findList(
const [prerequisiteIdFound, roleIdFound, hiddenRoleIdFound] = findList(
dataDefaults,
formValues
);
Expand All @@ -91,6 +91,9 @@ export const ShiftPositionsCreate = () => {
role: {
id: roleIdFound,
},
hiddenRole: {
id: hiddenRoleIdFound,
},
startTimeOffset: formValues.startTimeOffset,
};

Expand Down
Loading
Loading