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 => { 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[]; };