app: email and WhatsApp's backend (APP-1.0-PLAN §8)

The church's one set of settings from Accounts (messaging.get; the SMTP
password and WAWP token sealed with the link's secret, kept encrypted in
AppSetting messaging_settings), fetched when the App links to Accounts, on
Accounts' signed messaging.changed, once a day and on the admin's Fetch again.

messages-send (every minute): the outbox by email (nodemailer) and WhatsApp
(WAWP), STOP checked before every WhatsApp (the member and anyone with the
same number), email instead for what a member asked for (never
announcements), retries after 2, 4, 8 and 16 minutes, a reminder never after
its slot started, nothing more than 3 days late. announcement-sends and
devotional-sends queue "Also send it" at the publish time and today's
devotional at each member's time. Members' choices (NotificationSettings),
news consent kept from Accounts, MessageCount for the heartbeat's messages,
the admin's Email and WhatsApp API with Send a test.

Migration messaging (checked on PostgreSQL 18). 110 backend tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 14:29:13 +02:00
co-authored by Claude Opus 5.5
parent 7ff318a682
commit 8bfd0b767d
23 changed files with 1503 additions and 34 deletions
@@ -0,0 +1,45 @@
-- Church App 1.0.0: email and WhatsApp (docs/APP-1.0-PLAN.md §8): members' news consent from Accounts,
-- what each member chose the App sends them, what each outbox message went by, and the counts per day.
-- CreateEnum
CREATE TYPE "NotifyChoice" AS ENUM ('off', 'email', 'whatsapp', 'both');
-- AlterTable
ALTER TABLE "Member" ADD COLUMN "newsEmail" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN "newsWhatsApp" BOOLEAN NOT NULL DEFAULT false;
-- AlterTable
ALTER TABLE "OutboxMessage" ADD COLUMN "sentVia" TEXT;
-- CreateTable
CREATE TABLE "NotificationSettings" (
"memberId" TEXT NOT NULL,
"requestUpdates" "NotifyChoice" NOT NULL DEFAULT 'email',
"announcements" "NotifyChoice" NOT NULL DEFAULT 'off',
"devotional" "NotifyChoice" NOT NULL DEFAULT 'off',
"devotionalTime" TEXT NOT NULL DEFAULT '06:00',
"devotionalOfferedAt" TIMESTAMP(3),
"lastDevotionalDate" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "NotificationSettings_pkey" PRIMARY KEY ("memberId")
);
-- CreateTable
CREATE TABLE "MessageCount" (
"key" TEXT NOT NULL,
"day" TEXT NOT NULL,
"channel" TEXT NOT NULL,
"count" INTEGER NOT NULL DEFAULT 0,
CONSTRAINT "MessageCount_pkey" PRIMARY KEY ("key")
);
-- CreateIndex
CREATE INDEX "NotificationSettings_devotional_idx" ON "NotificationSettings"("devotional");
-- CreateIndex
CREATE INDEX "MessageCount_day_idx" ON "MessageCount"("day");
-- AddForeignKey
ALTER TABLE "NotificationSettings" ADD CONSTRAINT "NotificationSettings_memberId_fkey" FOREIGN KEY ("memberId") REFERENCES "Member"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+56 -2
View File
@@ -39,6 +39,9 @@ model Member {
phone String?
/// The person replied STOP to the church's WhatsApp number (decision 11): no WhatsApp of any kind.
whatsappOptedOut Boolean @default(false)
/// News consent from Accounts (`news_consent`): announcements the church sends go only on these (§8).
newsEmail Boolean @default(false)
newsWhatsApp Boolean @default(false)
roles AppRole[] @default([])
/// The last day they used the App signed in (written at most once a day): active members (§10).
lastActiveAt DateTime?
@@ -50,6 +53,7 @@ model Member {
testimonies Testimony[]
outboxMessages OutboxMessage[]
prayerRequests PrayerRequest[]
notifications NotificationSettings?
@@index([lastActiveAt])
}
@@ -267,8 +271,11 @@ model AccessLog {
@@index([createdAt])
}
/// Email and WhatsApp to send (§8): reminders, renewal notices and later the modules' sends. Written
/// once (`dedupeKey`), sent by the messaging job; what's in `payload` is the message's facts only.
/// Email and WhatsApp to send (§8): reminders, renewal notices, the prayer team's and submitters'
/// notices, announcements and devotionals. Written once (`dedupeKey`), sent by the `messages-send`
/// job with the church's settings from Accounts; what's in `payload` is the message's facts only
/// (never a prayer request's text). `sentVia` says what went (`email`, `whatsapp`, `email,whatsapp`,
/// or `none` when there was nothing to send it on); `error` why a channel didn't.
model OutboxMessage {
id String @id @default(uuid())
memberId String
@@ -279,6 +286,7 @@ model OutboxMessage {
dedupeKey String @unique
sendAfter DateTime
sentAt DateTime?
sentVia String?
failedAt DateTime?
error String?
attempts Int @default(0)
@@ -287,6 +295,52 @@ model OutboxMessage {
@@index([sentAt, sendAfter])
}
// ── Email and WhatsApp (§8) ──────────────────────────────────────────────────────────────────
// The church's one set of settings comes from Accounts (`messaging.get`) and is kept encrypted in
// AppSetting `messaging_settings`; the App never asks for its own (decision 4).
/// How a member wants one kind of message: email, WhatsApp, both or not at all (§8).
enum NotifyChoice {
off
email
whatsapp
both
}
/// What a member chose the App sends them (§8). Prayer reminders and renewals follow each
/// commitment's own channel; these are the rest. No row: the defaults.
model NotificationSettings {
memberId String @id
member Member @relation(fields: [memberId], references: [id], onDelete: Cascade)
/// Approved, someone prayed, a reply (email by default).
requestUpdates NotifyChoice @default(email)
/// The announcements the church sends (off until the member turns them on, P13).
announcements NotifyChoice @default(off)
/// Today's devotional (offered at the first sign-in, §6).
devotional NotifyChoice @default(off)
/// When (HH:MM in the church's timezone), 06:00 by default.
devotionalTime String @default("06:00")
/// When the first-sign-in offer was answered or dismissed.
devotionalOfferedAt DateTime?
/// The last local day a devotional was queued for them (once a day).
lastDevotionalDate String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([devotional])
}
/// Messages sent per local day and channel, for the heartbeat (`messages.email30d`, `whatsapp30d`).
/// Counts only: kept when a member or their messages are deleted. `key` is `<day>:<channel>`.
model MessageCount {
key String @id
day String
channel String
count Int @default(0)
@@index([day])
}
// ── Prayer requests (§4) ─────────────────────────────────────────────────────────────────────
// Special personal information (POPIA s26): never in stats, the heartbeat, AI or an email's text.
// The rules (closing after 30 days, fully anonymous requests, emergency numbers) are in AppSetting