1
0
Fork 0

feat(evaluations): mark mission evaluable after scheduled window

isMissionEvaluable now considers the full scheduled window (start +
estimated duration) rather than the start alone, so evaluation reminders
fire once a mission's window has genuinely closed. Extends the profile page,
the remindEvaluations command and the evaluationReminders service to match,
and adds integration tests. Updates the bot evaluation-reminders docs.
This commit is contained in:
Jason Fraley 2026-10-05 03:36:12 -04:00
parent f4a0c8e378
commit 665992a7ea
6 changed files with 27 additions and 11 deletions

View file

@ -381,7 +381,7 @@ A Discord bot living in `src/bot/`, run as a standalone long-running process via
- **Env / run**: requires `DISCORD_TOKEN` and `DISCORD_GUILD_ID` (fail-fast on missing required vars in `src/bot/config.ts`); optional `DISCORD_OPS_CHANNEL_ID`, `DISCORD_ANNOUNCE_CHANNEL_ID`, `DISCORD_STAFF_ROLE_IDS`, `DISCORD_ATTENDANCE_POLL_MS` (default 60s), `DISCORD_NOTIFICATION_POLL_MS` (default 20s), `DISCORD_EVALUATION_POLL_MS` (default 60s), `DISCORD_IMMINENT_POLL_MS` (default 60s), `DISCORD_SIDE_OPS_CHANNEL_ID` (Friday open-slot notice channel), `DISCORD_COMMUNITY_ZEUS_ROLE_ID` (role ping for that notice), `DISCORD_SIDE_OPS_POLL_MS` (default 30 min), plus existing `APP_URL`. Logs through `payload.logger`.
- **Structure**: `commands/` — `ping`, `signup`, `link`, `announce`, `remindEvaluations`; `events/interactionCreate.ts` — routes `ptf-att:` RSVP buttons; `services/` — `missionEmbeds` (attendance embed lifecycle + reconcile loop), `notificationBridge` (poll → Discord DMs), `evaluationReminders` (post-mission evaluation reminder DMs), `imminentReminders` (pre-op thread + attendee pings), and `fridayOpsNotice` (weekly #side-ops ping when the upcoming Friday has no op, edited to credit the community claimer); `lib/` — `roles.ts` (`isStaff`), `resolve.ts` (discordId ↔ Payload user lookups). Command registration scope: `signup`/`link`/`ping` are **global** (DM-usable — guild-scoped commands never appear in DMs), `announce`/`remind-evaluations` are guild-only.
- **Evaluation reminders**: once a mission is evaluable (`Completed`, or `Scheduled`/`Active` past its start — same rule as `isMissionEvaluable`), the bot sends a one-shot DM (per user with a linked `discordId`) asking them to rate their leadership, plus a "rate your subordinates" section for leaders whose members RSVP'd yes. Links go to each ratee's profile page where the rating dialog lives. Recipient computation: `computeEvaluationReminderPlan` in `src/lib/evaluations/reminders.ts`; state marker: `evaluationRemindersSentAt` on Missions (one-shot, same pattern as `discordAttendanceSentAt`; withheld if the 40-DM/tick cap is hit — retries next tick). These DMs deliberately ignore `preferences.discord.enabled` (defaults false, not yet exposed in the web UI — honoring it would DM nobody). Staff can trigger manually via `/remind-evaluations` (optional `mission` option accepts an ID, name, or code name and re-sends even if the marker is set; omitted, it sweeps all pending missions and reports `{processed, sent}`).
- **Evaluation reminders**: once a mission is evaluable (`Completed`, or `Scheduled`/`Active` whose full scheduled window (start + estimated duration, default 240 min) has passed — same rule as `isMissionEvaluable`), the bot sends a one-shot DM (per user with a linked `discordId`) asking them to rate their leadership, plus a "rate your subordinates" section for leaders whose members RSVP'd yes. Links go to each ratee's profile page where the rating dialog lives. Recipient computation: `computeEvaluationReminderPlan` in `src/lib/evaluations/reminders.ts`; state marker: `evaluationRemindersSentAt` on Missions (one-shot, same pattern as `discordAttendanceSentAt`; withheld if the 40-DM/tick cap is hit — retries next tick). These DMs deliberately ignore `preferences.discord.enabled` (defaults false, not yet exposed in the web UI — honoring it would DM nobody). Staff can trigger manually via `/remind-evaluations` (optional `mission` option accepts an ID, name, or code name and re-sends even if the marker is set; omitted, it sweeps all pending missions and reports `{processed, sent}`).
- **Sign-up / linking (feature 1)**: `/signup` is **DM-only** (the temp password flows through the DM). Creates the Payload user with `username` = `discordUsername` = the caller's Discord username, plus `discordId`, `displayName`, `steamId`, and a random temp password. The ephemeral reply carries the password as the guaranteed delivery path; `interaction.user.send()` is a best-effort persistent copy, so a blocked DM never orphans the account. `/link` works in servers **and** DMs (credential-free, ephemeral reply only) — matches `discordUsername` → sets `discordId`.
- **DM gotcha**: a user with "Allow direct messages from server members" off in Discord privacy settings can neither receive the bot's DMs nor open a DM with the bot. The `/signup` rejection message explains how to enable it.
- **Attendance (feature 2)**: `mission-attendances` collection + `src/lib/attendance/` (single write path, emits `mission:attendance-change`). The bot posts RSVP embeds (Yes/Tentative/No) for future, `Scheduled`, `visibility: "unit"` missions into the ops channel; stores `discordMessageId` + `discordAttendanceHash` on the mission; reconciles hash changes every poll tick (web ↔ Discord two-way sync, loop-safe). Web UI: `MissionAttendance` component on the mission detail page. `bun run payload generate-mission` clones the next weekly main mission.

View file

@ -188,7 +188,7 @@ async function findEligibleEvaluationMissions(
depth: 0,
select: {
name: true,
classification: { startDateTime: true },
classification: { startDateTime: true, estimatedDuration: true },
ownershipAndStatus: { status: true },
},
overrideAccess: true,
@ -199,6 +199,7 @@ async function findEligibleEvaluationMissions(
isMissionEvaluable(
mission.ownershipAndStatus.status,
mission.classification.startDateTime,
mission.classification.estimatedDuration ?? null,
now,
),
)

View file

@ -89,10 +89,11 @@ export const remindEvaluationsCommand: BotCommand = {
!isMissionEvaluable(
mission.ownershipAndStatus?.status ?? "",
mission.classification?.startDateTime ?? null,
mission.classification?.estimatedDuration ?? null,
)
) {
await interaction.editReply(
`**${missionLabel(mission)}** isn't evaluable yet: missions must be completed or past their scheduled start before reminders go out.`,
`**${missionLabel(mission)}** isn't evaluable yet: missions must be completed or past their scheduled window before reminders go out.`,
);
return;
}

View file

@ -186,6 +186,7 @@ export const reconcile = async (client: Client, payload: Payload): Promise<Recon
isMissionEvaluable(
mission.ownershipAndStatus?.status ?? "",
mission.classification?.startDateTime ?? null,
mission.classification?.estimatedDuration ?? null,
),
);
for (const mission of missions) {

View file

@ -5,6 +5,7 @@ import {
type PerformanceLevel,
type PerformanceLevelId,
} from "./levels";
import { missionEndTime } from "@/lib/intelligence/missionLifecycle";
type PayloadType = Awaited<ReturnType<typeof import("payload").getPayload>>;
@ -34,20 +35,22 @@ export const ANONYMITY_THRESHOLD = 3;
/**
* Pure eligibility check for whether a mission is "completed/past" enough to be
* evaluated. Draft statuses (Concept/Planning/Ready) and Cancelled are never
* eligible. Scheduled/Active missions become eligible once their scheduled start
* has passed.
* eligible. Scheduled/Active missions become eligible only once their scheduled
* window (start + estimated duration, defaulting to 240 minutes when missing —
* same rule as the mission-lifecycle auto-complete) has fully passed, so an op
* that just started is not treated as evaluable while it is still running.
*/
export function isMissionEvaluable(
status: string,
startDateTimeISO?: string | null,
estimatedDurationMinutes?: number | null,
now: Date = new Date(),
): boolean {
if (status === "Completed") return true;
if (status === "Scheduled" || status === "Active") {
if (!startDateTimeISO) return false;
const start = new Date(startDateTimeISO).getTime();
if (Number.isNaN(start)) return false;
return start < now.getTime();
const end = missionEndTime(startDateTimeISO ?? null, estimatedDurationMinutes ?? null);
if (!end) return false;
return end.getTime() <= now.getTime();
}
return false;
}
@ -158,14 +161,15 @@ export async function evaluateEligibility(
const mission = missions.docs[0] as
| {
ownershipAndStatus?: { status?: string };
classification?: { startDateTime?: string | null };
classification?: { startDateTime?: string | null; estimatedDuration?: number | null };
}
| undefined;
if (!mission) return { eligible: false, reason: "Mission not found." };
const status = mission.ownershipAndStatus?.status ?? "";
const startDateTime = mission.classification?.startDateTime;
if (!isMissionEvaluable(status, startDateTime)) {
const estimatedDuration = mission.classification?.estimatedDuration ?? null;
if (!isMissionEvaluable(status, startDateTime, estimatedDuration)) {
return { eligible: false, reason: "This mission is not completed or past yet." };
}

View file

@ -229,6 +229,9 @@ describe("Leadership evaluations", () => {
it("classifies mission statuses for evaluability (pure)", () => {
const past = new Date(Date.now() - 86_400_000).toISOString();
const future = new Date(Date.now() + 86_400_000).toISOString();
// A mission that just started: past its start but inside the default
// 240-minute window, so it is still running and not yet evaluable.
const justStarted = new Date(Date.now() - 60 * 60 * 1000).toISOString();
expect(isMissionEvaluable("Completed")).toBe(true);
expect(isMissionEvaluable("Scheduled", past)).toBe(true);
@ -238,6 +241,12 @@ describe("Leadership evaluations", () => {
expect(isMissionEvaluable("Cancelled", past)).toBe(false);
expect(isMissionEvaluable("Scheduled")).toBe(false);
expect(isMissionEvaluable("Scheduled", "not-a-date")).toBe(false);
// Still running: start has passed but the scheduled window has not.
expect(isMissionEvaluable("Active", justStarted)).toBe(false);
expect(isMissionEvaluable("Scheduled", justStarted)).toBe(false);
// Explicit duration: evaluable once start + duration has fully passed.
expect(isMissionEvaluable("Active", justStarted, 30)).toBe(true);
expect(isMissionEvaluable("Active", justStarted, 240)).toBe(false);
});
it("derives the evaluation kind from the rater/ratee/mission relationship", async () => {