1
0
Fork 0
polaris-task-force/src/lib/intelligence/imminentReminders.ts
Z8MB1E 74564afb2b feat(missions): auto-start and auto-complete ops, plus side-op scheduling
- The mission tick now runs two idempotent sweeps: auto-start flips Scheduled
  missions whose start time has arrived to Active, and auto-complete sets
  missions whose scheduled window (start plus duration) has fully passed to
  Completed with a stamped completedAt.
- Side ops are created as quiet Ready drafts and committed with a "Mark as
  Scheduled" control, which is what posts the Discord roll-call and hands the
  op to the lifecycle above.
- Daily schedule reminders nudge the creator to set a draft live (web
  notification plus Discord DM).
- Adds the mission lifecycle/commit event types and the matching notification
  labels, and updates the bot docs.
2026-10-03 03:29:35 -04:00

57 lines
2.2 KiB
TypeScript

import type { Payload } from "payload";
import type { Mission } from "@/payload-types";
/**
* Imminent-op reminder for the Discord bot.
*
* When a posted op (roll-call already live in the ops channel) is 15 minutes
* from starting, the bot opens a thread on the roll-call message and pings
* everyone who RSVP'd yes, as a last-call reminder that the op is imminent.
*
* A one-shot marker on the mission (imminentReminderSentAt) keeps it to a
* single thread per occurrence; the bot's cancel flow clears the marker so a
* rescheduled op reminds again for its new start time.
*
* The shared query lives here (not in the bot) to keep the window logic
* testable and free of discord.js imports, mirroring readyReminders.ts.
*/
/** How far before the start time the reminder fires. */
export const IMMINENT_REMINDER_WINDOW_MS = 15 * 60 * 1000;
/**
* How far past the start time a still-unsent reminder may fire (bounds staleness
* when the bot was down through the whole window).
*/
export const IMMINENT_REMINDER_GRACE_MS = 15 * 60 * 1000;
/**
* Ops due for the imminent reminder: unit-visible, Scheduled, with a live
* roll-call message, starting within the reminder window (or just past it,
* within the grace bound), and without the one-shot marker yet.
*/
export const getImminentReminderMissions = async (
payload: Payload,
now = new Date(),
): Promise<Mission[]> => {
const windowStartIso = new Date(now.getTime() - IMMINENT_REMINDER_GRACE_MS).toISOString();
const windowEndIso = new Date(now.getTime() + IMMINENT_REMINDER_WINDOW_MS).toISOString();
const res = await payload.find({
collection: "missions",
overrideAccess: true,
depth: 0,
sort: "classification.startDateTime",
limit: 50,
where: {
and: [
{ "ownershipAndStatus.visibility": { equals: "unit" } },
{ "ownershipAndStatus.status": { in: ["Scheduled"] } },
{ discordMessageId: { exists: true } },
{ imminentReminderSentAt: { exists: false } },
{ "classification.startDateTime": { greater_than: windowStartIso } },
{ "classification.startDateTime": { less_than_equal: windowEndIso } },
],
},
});
return res.docs as unknown as Mission[];
};