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
4 changes: 2 additions & 2 deletions client/e2e/tests/39-forms.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ test.describe("Yearly forms (#844)", () => {
const { formId } = await made.json();
const patched = await request.patch(`/api/forms/${formId}`, {
data: JSON.stringify({
hiddenRoleId: HIDE_ROLE_ID,
inviteRoleId: INVITE_ROLE_ID,
hiddenRoleIds: [9_000_524, HIDE_ROLE_ID],
inviteRoleIds: [9_000_525, INVITE_ROLE_ID],
questions: QUESTIONS,
status: "published",
}),
Expand Down
31 changes: 20 additions & 11 deletions client/src/app/forms/[formId]/edit/FormEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ export const FormEdit = ({ formId }: { formId: number }) => {
arg: {
body: {
closesAt: form.closesAt || null,
hiddenRoleId: form.hiddenRoleId,
hiddenRoleIds: form.hiddenRoleIds,
intro: form.intro,
inviteRoleId: form.inviteRoleId,
inviteRoleIds: form.inviteRoleIds,
opensAt: form.opensAt || null,
questions: form.questions,
status,
Expand Down Expand Up @@ -148,14 +148,14 @@ export const FormEdit = ({ formId }: { formId: number }) => {
{(
[
[
"inviteRoleId",
"Only for role",
"Blank = everyone. Invite people by giving them this role; they get the invite email.",
"inviteRoleIds",
"Only for roles",
"Blank = everyone. Invite people by giving them any of these roles; they get the invite email.",
],
[
"hiddenRoleId",
"Hidden from role",
"e.g. NoCamping: never shown to them, even if invited.",
"hiddenRoleIds",
"Hidden from roles",
"e.g. NoCamping: anyone with one of these never sees it, even if invited.",
],
] as const
).map(([key, label, help]) => (
Expand All @@ -165,12 +165,21 @@ export const FormEdit = ({ formId }: { formId: number }) => {
key={key}
label={label}
onChange={(e) =>
setForm({ ...form, [key]: Number(e.target.value) || null })
setForm({
...form,
[key]: (e.target.value as unknown as number[]).map(Number),
})
}
select
value={form[key] ?? ""}
SelectProps={{
multiple: true,
renderValue: (ids) =>
(ids as number[])
.map((id) => roles?.find((r) => r.id === id)?.name ?? id)
.join(", "),
}}
value={form[key]}
>
<MenuItem value="">None</MenuItem>
{(roles ?? []).map((r) => (
<MenuItem key={r.id} value={r.id}>
{r.name}
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/layout/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export const Footer = () => {
color: theme.palette.common.white,
}}
>
2027.G.00895.C
2027.G.00896.C
</Typography>
</Stack>
</Container>
Expand Down
12 changes: 6 additions & 6 deletions client/src/lib/formInvites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { RowDataPacket } from "mysql2";

import { NOW_PT } from "@/lib/content";
import { emailBody } from "@/lib/emailTemplates";
import { holds } from "@/lib/exclusions";
import { holdsAnySql } from "@/lib/formsDb";
import { pool } from "lib/database";
import { enqueueEmail } from "lib/mail";

Expand All @@ -23,25 +23,25 @@ export const sendFormInvites = async (scope: {
}) => {
const filters = [
["f.form_id", scope.formId],
["f.invite_role_id", scope.roleId],
["ivr.role_id", scope.roleId],
["v.shiftboard_id", scope.shiftboardId],
] as const;
const where = filters.filter(([, v]) => v).map(([col]) => `AND ${col} = ?`);
const params = filters.flatMap(([, v]) => (v ? [v] : []));
try {
const [due] = await pool.query<RowDataPacket[]>(
`SELECT f.form_id, f.title, f.opens_at, (f.opens_at > ${NOW_PT}) AS not_yet,
`SELECT DISTINCT f.form_id, f.title, f.opens_at, (f.opens_at > ${NOW_PT}) AS not_yet,
v.shiftboard_id, v.email, v.playa_name, v.world_name
FROM op_forms f
JOIN op_volunteer_roles ivr
ON ivr.role_id = f.invite_role_id AND ivr.remove_role = false
ON JSON_CONTAINS(f.invite_role_ids, CAST(ivr.role_id AS CHAR))
AND ivr.remove_role = false
JOIN op_volunteers v
ON v.shiftboard_id = ivr.shiftboard_id AND v.delete_volunteer = false
WHERE f.status = 'published'
AND (f.closes_at IS NULL OR f.closes_at > ${NOW_PT})
AND COALESCE(v.email, '') <> ''
AND (f.hidden_role_id IS NULL
OR NOT ${holds("f.hidden_role_id", "v.shiftboard_id")})
AND NOT ${holdsAnySql("f.hidden_role_ids", "v.shiftboard_id")}
AND NOT EXISTS (SELECT 1 FROM op_email_queue q
WHERE q.category = CONCAT('form-invite:', f.form_id) AND q.\`to\` = v.email)
${where.join(" ")}`,
Expand Down
4 changes: 2 additions & 2 deletions client/src/lib/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export type FormAnswers = Record<string, FormAnswer>;
export interface IForm {
closesAt: string | null;
formId: number;
hiddenRoleId: number | null;
hiddenRoleIds: number[];
intro: string;
inviteRoleId: number | null;
inviteRoleIds: number[];
isOpen: boolean;
opensAt: string | null;
questions: IFormQuestion[];
Expand Down
21 changes: 14 additions & 7 deletions client/src/lib/formsDb.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import type { RowDataPacket } from "mysql2";

import { NOW_PT } from "@/lib/content";
import { holds } from "@/lib/exclusions";
import type { IForm } from "@/lib/forms";

export const FORM_COLS = `f.form_id, f.slug, f.year, f.title, f.intro, f.questions,
f.status, f.opens_at, f.closes_at, f.invite_role_id, f.hidden_role_id,
f.status, f.opens_at, f.closes_at, f.invite_role_ids, f.hidden_role_ids,
(f.status = 'published'
AND (f.opens_at IS NULL OR f.opens_at <= ${NOW_PT})
AND (f.closes_at IS NULL OR f.closes_at > ${NOW_PT})) AS is_open`;

// MariaDB JSON is LONGTEXT, so mysql2 hands it back as a string
// SQL: does `who` (a column or integer) hold any role in JSON list `listCol`?
// NULL / [] lists count as "no limit" in canSeeSql, not here.
export const holdsAnySql = (listCol: string, who: string) =>
`EXISTS (SELECT 1 FROM op_volunteer_roles lvr
WHERE lvr.shiftboard_id = ${who} AND lvr.remove_role = false
AND JSON_CONTAINS(${listCol}, CAST(lvr.role_id AS CHAR)))`;
const noLimit = (listCol: string) => `COALESCE(JSON_LENGTH(${listCol}), 0) = 0`;

// SQL: may volunteer `who` (an integer) see form `f`? Invite-only forms need
// the invite role; the hidden role (e.g. NoCamping) always wins (#848).
// any invite role; any hidden role (e.g. NoCamping) always wins (#848).
export const canSeeSql = (who: number) => {
const id = String(Math.trunc(who));
return `((f.invite_role_id IS NULL OR ${holds("f.invite_role_id", id)})
AND (f.hidden_role_id IS NULL OR NOT ${holds("f.hidden_role_id", id)}))`;
return `((${noLimit("f.invite_role_ids")} OR ${holdsAnySql("f.invite_role_ids", id)})
AND (${noLimit("f.hidden_role_ids")} OR NOT ${holdsAnySql("f.hidden_role_ids", id)}))`;
};

export const parseJson = <T>(v: unknown, fallback: T): T =>
Expand All @@ -25,8 +32,8 @@ export const parseJson = <T>(v: unknown, fallback: T): T =>
export const toForm = (r: RowDataPacket): IForm => ({
closesAt: r.closes_at,
formId: r.form_id,
hiddenRoleId: r.hidden_role_id ?? null,
inviteRoleId: r.invite_role_id ?? null,
hiddenRoleIds: parseJson<number[] | null>(r.hidden_role_ids, null) ?? [],
inviteRoleIds: parseJson<number[] | null>(r.invite_role_ids, null) ?? [],
intro: r.intro ?? "",
isOpen: Boolean(r.is_open),
opensAt: r.opens_at,
Expand Down
12 changes: 6 additions & 6 deletions client/src/pages/api/forms/[formId]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const DATETIME_RE = /^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}(:\d{2})?$/;
// PUT { answers } → save the signed-in volunteer's answers; open forms only.
// Editable until the form closes.
// PATCH (SuperAdmin / FormEditor) { title?, intro?, questions?, status?,
// opensAt?, closesAt?, inviteRoleId?, hiddenRoleId? }; then sends any
// opensAt?, closesAt?, inviteRoleIds?, hiddenRoleIds? }; then sends any
// invite emails now due (lib/formInvites)
const form = async (
req: NextApiRequest,
Expand Down Expand Up @@ -136,15 +136,15 @@ const form = async (
params.push(v === null ? null : v.replace("T", " "));
}
for (const [key, col] of [
["inviteRoleId", "invite_role_id"],
["hiddenRoleId", "hidden_role_id"],
["inviteRoleIds", "invite_role_ids"],
["hiddenRoleIds", "hidden_role_ids"],
] as const) {
const v = body[key];
if (v === undefined) continue;
if (v !== null && !Number.isSafeInteger(v))
return bad("Pick a role, or none.");
if (!Array.isArray(v) || !v.every((id) => Number.isSafeInteger(id)))
return bad("Roles must be a list of role ids.");
sets.push(`${col} = ?`);
params.push(v || null);
params.push(v.length ? JSON.stringify([...new Set(v)]) : null);
}
if (!sets.length) return bad("Nothing to change.");
await pool.query(
Expand Down
13 changes: 13 additions & 0 deletions migrations/034_form_role_lists.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- #844 (Mew 2026-11-29): a form can be for several invite roles and hidden
-- from several roles. JSON arrays of role ids replace the single columns
-- from 033 (unused on prod: no form had a role set). NULL / [] = no limit.
SET @@system_versioning_alter_history = KEEP;
ALTER TABLE op_forms
ADD COLUMN IF NOT EXISTS invite_role_ids JSON NULL AFTER closes_at,
ADD COLUMN IF NOT EXISTS hidden_role_ids JSON NULL AFTER invite_role_ids;
UPDATE op_forms SET
invite_role_ids = IF(invite_role_id IS NULL, NULL, JSON_ARRAY(invite_role_id)),
hidden_role_ids = IF(hidden_role_id IS NULL, NULL, JSON_ARRAY(hidden_role_id)),
changed_by = -9
WHERE invite_role_id IS NOT NULL OR hidden_role_id IS NOT NULL;
ALTER TABLE op_forms DROP COLUMN IF EXISTS invite_role_id, DROP COLUMN IF EXISTS hidden_role_id;
Loading