Files
crosscode-platform/apps/app/backend/src/lib/outbox.ts
T
joshuaandClaude Opus 5.5 6425a6a30d app: prayer requests' backend (APP-1.0-PLAN §4)
Private by default (the prayer team only) or shared with members after a
coordinator approves it, with the first name or "Anonymous"; fully anonymous
requests when the church allows them (off by default). Coordinators approve,
redact (the sender told what changed) or remove (with a reason); anyone on the
team can escalate to Pastoral care (email and WhatsApp at once, an Owner or
Church admin when nobody has the role): it leaves the shared list and the sender
sees nothing different. "I prayed" counts without who, "Answered!" updates,
private replies from the team, closing after 30 days (7-90) and deletion 12
months later (prayer-requests-tidy). Every list with a sender is access-logged;
messages never carry a request's text; nothing in the heartbeat. Export and
delete for a member's requests. Shared helpers: parse (http.ts), accessLog
(lib/accessLog.ts). Migration 20261203000000_prayer_requests (checked on
PostgreSQL 18: applies, no diff). 78 backend tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 10:56:53 +02:00

24 lines
1.3 KiB
TypeScript

/**
* 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<boolean> {
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;
}
}