diff --git a/README.md b/README.md index 2bf94ab..a96cc35 100644 --- a/README.md +++ b/README.md @@ -284,6 +284,7 @@ Each team can remind its members in Slack to fill in their availability. Admins | Days that must be filled in | Weekdays that count, e.g. only Wednesday and Saturday | | Look ahead | From tomorrow, how many days ahead those weekdays must already be answered (1–28) | | Send at | Time of day in `TZ` | +| Don't remind members of | Teams whose members (sub-teams included) are never reminded, for people who don't need to fill in, such as mentors. Put them in a team, which can join them by Keycloak role (auto-join roles), and pick it here | Every day after its time, the reminder tags each member of the team, sub-teams included, who hasn't answered one of those days, with the days missing. A team with nobody missing sends nothing. A big team's reminder covers everyone diff --git a/messages/en.json b/messages/en.json index 1bc0cd3..3832f1e 100644 --- a/messages/en.json +++ b/messages/en.json @@ -326,6 +326,8 @@ "enabled": "Send a reminder every day", "weekdays": "Days that must be filled in", "weekdaysHelp": "Only these days of the week count.", + "exemptTeams": "Don't remind members of", + "exemptTeamsHelp": "For people who don't need to fill in, such as mentors: put them in a team (it can join them by Keycloak role) and pick it here. Its sub-teams count too.", "daysAhead": "Look ahead", "daysAheadHelp": "Days from tomorrow that must already be answered, 1 to {max}.", "time": "Send at", diff --git a/messages/zh-TW.json b/messages/zh-TW.json index 5e16777..25882b4 100644 --- a/messages/zh-TW.json +++ b/messages/zh-TW.json @@ -326,6 +326,8 @@ "enabled": "每天送提醒", "weekdays": "必填的日子", "weekdaysHelp": "只算這幾個星期幾。", + "exemptTeams": "不用提醒這些隊伍的成員", + "exemptTeamsHelp": "給不需要填的人,例如 mentor:把他們放進一個隊伍(可以用 Keycloak 角色自動加入),再在這裡選它。子隊伍也算。", "daysAhead": "往後看幾天", "daysAheadHelp": "從明天起,這幾天內的必填日子要先填好,1 到 {max} 天。", "time": "送出時間", diff --git a/src/app/admin/teams/[id]/ReminderForm.tsx b/src/app/admin/teams/[id]/ReminderForm.tsx index 88d5b9c..351a076 100644 --- a/src/app/admin/teams/[id]/ReminderForm.tsx +++ b/src/app/admin/teams/[id]/ReminderForm.tsx @@ -13,8 +13,9 @@ import { saveTeamReminderAction } from "../actions"; const WEEKDAYS = [1, 2, 3, 4, 5, 6, 7]; -export default function ReminderForm(props: { reminder: TeamReminderDTO }) { - const { reminder } = props; +/** `teams`: the teams whose members may be exempted (every team but this one). */ +export default function ReminderForm(props: { reminder: TeamReminderDTO; teams: { id: string; name: string }[] }) { + const { reminder, teams } = props; const t = useTranslations("TeamReminders"); const router = useRouter(); const [settings, setSettings] = useState(reminder.settings); @@ -26,6 +27,10 @@ export default function ReminderForm(props: { reminder: TeamReminderDTO }) { const toggleDay = (day: number) => set({ weekdays: settings.weekdays.includes(day) ? settings.weekdays.filter(d => d !== day) : [...settings.weekdays, day].sort(), }); + const exempt = settings.exemptTeamIds ?? []; + const toggleExempt = (id: string) => set({ + exemptTeamIds: teams.map(team => team.id).filter(teamId => (teamId === id) !== exempt.includes(teamId)), + }); const changed = JSON.stringify(settings) !== JSON.stringify(reminder.settings); const save = () => startSaving(async () => { @@ -90,6 +95,23 @@ export default function ReminderForm(props: { reminder: TeamReminderDTO }) { + {teams.length > 0 && ( + + {t("exemptTeams")} + + {teams.map(team => { + const on = exempt.includes(team.id); + return ( + + ); + })} + + {t("exemptTeamsHelp")} + + )} + diff --git a/src/app/admin/teams/[id]/TeamDetail.tsx b/src/app/admin/teams/[id]/TeamDetail.tsx index 2cfc0a0..0b86b4b 100644 --- a/src/app/admin/teams/[id]/TeamDetail.tsx +++ b/src/app/admin/teams/[id]/TeamDetail.tsx @@ -15,7 +15,7 @@ import { toaster } from "@/components/ui/toaster"; import type { TeamDTO, TeamMemberDTO } from "@/lib/data/team-dto"; import type { TeamReminderDTO } from "@/lib/data/team-reminder-dto"; import { parseAutoJoinRoles } from "@/lib/team-roles"; -import { wouldCreateCycle } from "@/lib/team-tree"; +import { getTeamAncestors, wouldCreateCycle } from "@/lib/team-tree"; import { type TeamActionResult, addTeamMembersAction, deleteTeamAction, moveTeamAction, removeTeamMemberAction, renameTeamAction, setAutoJoinRolesAction, setTeamReminderWebhookAction, @@ -290,10 +290,14 @@ function TeamMembers(props: { team: TeamDTO; members: TeamMemberDTO[]; users: { ); } -function TeamReminder(props: { team: TeamDTO; reminder: TeamReminderDTO }) { - const { team, reminder } = props; +function TeamReminder(props: { team: TeamDTO; teams: TeamDTO[]; reminder: TeamReminderDTO }) { + const { team, teams, reminder } = props; const { pending, run } = useTeamAction(); const [webhook, setWebhook] = useState(""); + const exemptable = useMemo(() => { + const excluded = new Set([team.id, ...getTeamAncestors(teams, team.id).map(ancestor => ancestor.id)]); + return teams.filter(other => !excluded.has(other.id)); + }, [team.id, teams]); return ( @@ -302,7 +306,8 @@ function TeamReminder(props: { team: TeamDTO; reminder: TeamReminderDTO }) { - + {/* Not this team or its parents: exempting them would leave nobody to remind. */} + @@ -422,7 +427,7 @@ export default function TeamDetail(props: { - + ); } diff --git a/src/lib/data/team-reminder-dto.test.ts b/src/lib/data/team-reminder-dto.test.ts index 3e74dbe..bd84519 100644 --- a/src/lib/data/team-reminder-dto.test.ts +++ b/src/lib/data/team-reminder-dto.test.ts @@ -142,6 +142,30 @@ describe("sendDueReminders", () => { expect(fetchMock).not.toHaveBeenCalled(); }); + it("doesn't remind members of an exempt team or its sub-teams", async () => { + const MENTORS = "0000000000000000000000b1"; + const SENIOR = "0000000000000000000000b2"; + db.teams.push({ id: MENTORS, name: "Mentors", parentTeamId: null }, { id: SENIOR, name: "Senior mentors", parentTeamId: MENTORS }); + db.users.push( + { id: "u3", email: "three@example.test", name: "Three", teamIds: [CHILD, MENTORS] }, + { id: "u4", email: "four@example.test", name: "Four", teamIds: [PARENT, SENIOR] }, + ); + db.rules[0].config = { weekdays: [6], daysAhead: 7, exemptTeamIds: [MENTORS] }; + + await sendDueReminders(evening, "https://x.test"); + const body = fetchMock.mock.calls[0][1].body as string; + expect(body).toContain("Two"); + expect(body).not.toContain("Three"); + expect(body).not.toContain("Four"); + expect(JSON.parse(body).text).toContain("1 member hasn't"); + }); + + it("sends nothing when everyone left is exempt", async () => { + db.rules[0].config = { weekdays: [6], daysAhead: 7, exemptTeamIds: [CHILD] }; + await expect(sendDueReminders(evening, "https://x.test")).resolves.toEqual([{ ruleId: "r1", teamId: PARENT, result: "nobodyMissing" }]); + expect(fetchMock).not.toHaveBeenCalled(); + }); + it("sends nothing when everyone answered, and records the run", async () => { db.entries.push({ userId: "u2", date: "2026-11-14" }); await expect(sendDueReminders(evening, "https://x.test")).resolves.toEqual([{ ruleId: "r1", teamId: PARENT, result: "nobodyMissing" }]); @@ -308,7 +332,7 @@ describe("getTeamReminder", () => { const reminder = await getTeamReminder(PARENT); expect(reminder).toEqual({ team: { id: PARENT, name: "Robot" }, - settings: { enabled: true, weekdays: [6], daysAhead: 7, time: "20:00" }, + settings: { enabled: true, weekdays: [6], daysAhead: 7, exemptTeamIds: [], time: "20:00" }, hasWebhook: true, missing: expect.any(Array), }); @@ -332,13 +356,13 @@ describe("saveTeamReminder", () => { const settings = { enabled: true, weekdays: [3], daysAhead: 3, time: "21:30" }; it("lets an admin change a team's rule, or create it", async () => { - await saveTeamReminder(CHILD, settings); - expect(db.rules[1]).toMatchObject({ enabled: true, sendTime: "21:30", config: { weekdays: [3], daysAhead: 3 } }); + await saveTeamReminder(CHILD, { ...settings, exemptTeamIds: [PARENT] }); + expect(db.rules[1]).toMatchObject({ enabled: true, sendTime: "21:30", config: { weekdays: [3], daysAhead: 3, exemptTeamIds: [PARENT] } }); db.rules = []; await saveTeamReminder(CHILD, settings); expect(db.rules).toEqual([expect.objectContaining({ - teamId: CHILD, kind: "unanswered", channel: "slack-webhook", target: {}, config: { weekdays: [3], daysAhead: 3 }, + teamId: CHILD, kind: "unanswered", channel: "slack-webhook", target: {}, config: { weekdays: [3], daysAhead: 3, exemptTeamIds: [] }, })]); }); diff --git a/src/lib/data/team-reminder-dto.ts b/src/lib/data/team-reminder-dto.ts index e637321..7e62e0c 100644 --- a/src/lib/data/team-reminder-dto.ts +++ b/src/lib/data/team-reminder-dto.ts @@ -20,7 +20,7 @@ export type TeamReminderDTO = { missing: { name: string; dates: string[] }[]; }; -const DEFAULT_SETTINGS: ReminderSettings = { enabled: false, weekdays: [], daysAhead: 7, time: "20:00" }; +const DEFAULT_SETTINGS: ReminderSettings = { enabled: false, weekdays: [], daysAhead: 7, exemptTeamIds: [], time: "20:00" }; function assertValidId(id: string) { if (!objectIdSchema.safeParse(id).success) throw new TeamError("Invalid team id."); @@ -46,7 +46,9 @@ export async function getTeamReminder(teamId: string): Promise { const required = requiredDates(on, config.weekdays, config.daysAhead); if (required.length === 0) return []; - const members = await prisma.user.findMany({ + const exempt = new Set((config.exemptTeamIds ?? []).flatMap(id => [id, ...getTeamDescendants(tree, id).map(team => team.id)])); + const members = (await prisma.user.findMany({ where: { teamIds: { hasSome: [teamId, ...getTeamDescendants(tree, teamId).map(team => team.id)] } }, - select: { id: true, email: true, name: true }, + select: { id: true, email: true, name: true, teamIds: true }, orderBy: { name: "asc" }, - }); + })).filter(member => !member.teamIds.some(id => exempt.has(id))); if (members.length === 0) return []; const answered = await prisma.attendanceEntry.findMany({ diff --git a/src/lib/team-reminders.test.ts b/src/lib/team-reminders.test.ts index b10a561..acbbb88 100644 --- a/src/lib/team-reminders.test.ts +++ b/src/lib/team-reminders.test.ts @@ -73,4 +73,13 @@ describe("reminderSettingsSchema", () => { expect(reminderSettingsSchema.safeParse({ enabled: true, weekdays: [], daysAhead: 0, time: "08:30" }).success).toBe(false); expect(reminderSettingsSchema.safeParse({ enabled: true, weekdays: [], daysAhead: 7, time: "8:30" }).success).toBe(false); }); + + it("takes optional exempt teams, deduped, and rejects ids that aren't ObjectIds", () => { + const team = "0000000000000000000000a1"; + expect(reminderSettingsSchema.parse({ enabled: true, weekdays: [], daysAhead: 7, time: "08:30" }).exemptTeamIds).toBeUndefined(); + expect(reminderSettingsSchema.parse({ enabled: true, weekdays: [], daysAhead: 7, time: "08:30", exemptTeamIds: [team, team] }).exemptTeamIds) + .toEqual([team]); + expect(reminderSettingsSchema.safeParse({ enabled: true, weekdays: [], daysAhead: 7, time: "08:30", exemptTeamIds: ["mentors"] }).success) + .toBe(false); + }); }); diff --git a/src/lib/team-reminders.ts b/src/lib/team-reminders.ts index bf20f81..602549f 100644 --- a/src/lib/team-reminders.ts +++ b/src/lib/team-reminders.ts @@ -4,6 +4,7 @@ import * as z from "zod"; import { addDays, isoWeekday } from "./attendance"; +import { objectIdSchema } from "./objectid"; export const MAX_DAYS_AHEAD = 28; const TIME_PATTERN = /^([01]\d|2[0-3]):[0-5]\d$/; @@ -12,6 +13,8 @@ const TIME_PATTERN = /^([01]\d|2[0-3]):[0-5]\d$/; export const unansweredConfigSchema = z.object({ weekdays: z.array(z.number().int().min(1).max(7)).max(7).transform(days => [...new Set(days)].sort()), daysAhead: z.number().int().min(1).max(MAX_DAYS_AHEAD), + /** Members of these teams (sub-teams included) are never reminded, e.g. a mentors team joined by Keycloak role. */ + exemptTeamIds: z.array(objectIdSchema).max(50).transform(ids => [...new Set(ids)]).optional(), }); /** A rule's condition: a new kind is a new branch here. An unknown kind fails to parse. */