/** * Messages to send (APP-1.0-PLAN §8), written once each (`dedupeKey`) by whatever decides one is * due: prayer reminders and renewal notices (prayer/jobs.ts), and the later modules' sends. The * messaging job sends them by email and/or WhatsApp with the church's settings from Accounts, * checking the WhatsApp STOP first (§8, decision 11). * * `payload` holds the facts the message is built from (times, links), never a prayer request's text. */ import type { Ctx } from '../context.js'; export type OutboxKind = 'prayer-reminder' | 'prayer-renewal' | 'testimony-removed' | 'request-new' | 'request-update' | 'request-escalated'; export type Channel = 'email' | 'whatsapp' | 'both'; /** Queues a message; false when one with the same key was already queued (never sent twice). */ export async function enqueue(ctx: Ctx, m: { memberId: string; kind: OutboxKind; channel: Channel; dedupeKey: string; payload: object; sendAfter?: Date }): Promise { try { await ctx.db.outboxMessage.create({ data: { memberId: m.memberId, kind: m.kind, channel: m.channel, dedupeKey: m.dedupeKey, payload: m.payload as never, sendAfter: m.sendAfter ?? ctx.now() } }); return true; } catch (e) { if ((e as { code?: string }).code === 'P2002') return false; throw e; } }