app: announcements, devotionals and events' backend (APP-1.0-PLAN §5-§7)

Announcements: public or members-only, pinned, a campus, a publish time and an
end, an optional button, markdown text; Content admins write them; the
heartbeat counts the last 30 days; "also send it" flags kept for §8.

Devotionals: Cross Code's from the Hub each night and on Sync now (the next
2 weeks from today in the church's timezone, with the licence; exactly those
days replaced, the rest kept, a Hub outage changes nothing), or the church's
own by its Devotional writers, by hand or the same CSV import as the Hub's
(preview, then replace or skip days already filled). Only KJV and BSB; the
passage with its copyright line and Read in YouVersion; today's or the most
recent; the archive by month; members-only ones; the acknowledgements.

Events: simple events in the church's timezone, and the feed both ways as the
Website's (publish its own as crosscode.event/1 with removals; read-only copies
of Cross Events' and the Website's, Register on the Events site, canonical to
the origin). Jobs event-feed (15 min) and crosscode-devotionals (01:15).
Migration 20261204000000_content (checked on PostgreSQL 18: applies, no diff).
93 backend tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 11:20:31 +02:00
co-authored by Claude Opus 5.5
parent 710b762ddf
commit a9079f1195
14 changed files with 1514 additions and 21 deletions
+10 -3
View File
@@ -22,11 +22,15 @@ const DEFAULTS: Record<string, () => Row> = {
outboxMessage: () => ({ sentAt: null, failedAt: null, error: null, attempts: 0 }),
prayerRequest: () => ({ visibility: 'team', showName: false, anonymous: false, moderation: 'none', redactedAt: null, redactionNote: null, removedReason: null, moderatedById: null, moderatedAt: null, escalatedAt: null, escalatedById: null, escalationNote: null, closedAt: null }),
prayerRequestUpdate: () => ({ authorId: null }),
announcement: () => ({ imageUrl: null, buttonLabel: null, buttonUrl: null, campus: null, audience: 'everyone', pinned: false, publishAt: null, endsAt: null, sendEmail: false, sendWhatsApp: false, sentAt: null, createdById: null }),
devotional: () => ({ originId: null, prayer: null, author: null, membersOnly: false, createdById: null }),
simpleEvent: () => ({ endsAt: null, allDay: false, timezone: 'Africa/Johannesburg', location: null, description: null, imageUrl: null, link: null, cancelled: false, publishedAt: null, updatedBy: null }),
importedEvent: () => ({ syncedAt: new Date() }),
};
const KEYS: Record<string, string> = { appSetting: 'key', productLink: 'installId', linkNonce: 'nonce' };
const KEYS: Record<string, string> = { appSetting: 'key', productLink: 'installId', linkNonce: 'nonce', feedRemoval: 'eventId' };
/** Unique fields: one field, or several together (`@@unique([a, b])`). */
const UNIQUE: Record<string, (string | string[])[]> = { session: ['tokenHash'], outboxMessage: ['dedupeKey'], prayerSkip: [['commitmentId', 'date']], prayedMark: [['commitmentId', 'date']], requestPrayer: [['requestId', 'memberId']] };
const HAS_UPDATED_AT = new Set(['member', 'appSetting', 'productLink', 'prayerCommitment', 'prayerCampaign', 'prayerRequest']);
const UNIQUE: Record<string, (string | string[])[]> = { session: ['tokenHash'], outboxMessage: ['dedupeKey'], prayerSkip: [['commitmentId', 'date']], prayedMark: [['commitmentId', 'date']], requestPrayer: [['requestId', 'memberId']], devotional: [['source', 'date']], announcement: ['slug'], simpleEvent: ['slug'], importedEvent: [['originProduct', 'originId']] };
const HAS_UPDATED_AT = new Set(['member', 'appSetting', 'productLink', 'prayerCommitment', 'prayerCampaign', 'prayerRequest', 'announcement', 'devotional', 'simpleEvent']);
/** `include` support: model → relation → [other model, foreign key on the other (many) or on this row (one)]. */
const RELATIONS: Record<string, Record<string, { model: string; many: boolean; key: string }>> = {
member: { sessions: { model: 'session', many: true, key: 'memberId' } },
@@ -101,6 +105,8 @@ function model(name: string, tables: Map<string, Map<unknown, Row>>) {
};
const findWhere = (where: Row) => {
if (where && key in where && typeof where[key] !== 'object') return table().get(where[key]) ?? null;
// A compound unique (`source_date: { source, date }`) is looked up by its fields.
for (const u of UNIQUE[name] ?? []) if (Array.isArray(u) && where && u.join('_') in where) { const w = where[u.join('_')] as Row; return [...table().values()].find((r) => u.every((f) => v(r[f]) === v(w[f]))) ?? null; }
for (const u of UNIQUE[name] ?? []) if (typeof u === 'string' && where && u in where) return [...table().values()].find((r) => r[u] === where[u]) ?? null;
return [...table().values()].find((r) => matches(r, where)) ?? null;
};
@@ -166,6 +172,7 @@ function model(name: string, tables: Map<string, Map<unknown, Row>>) {
if (from === 'member') { drop('session', 'memberId'); drop('testimony', 'memberId'); drop('outboxMessage', 'memberId'); drop('prayerRequest', 'memberId'); unset('prayerCommitment', 'memberId'); }
if (from === 'prayerCommitment') { drop('prayerSkip', 'commitmentId'); drop('prayedMark', 'commitmentId'); }
if (from === 'prayerCampaign') unset('testimony', 'campaignId');
if (from === 'productLink') drop('importedEvent', 'linkId');
if (from === 'prayerRequest') { drop('prayerRequestUpdate', 'requestId'); drop('requestPrayer', 'requestId'); }
};
const withIncludes = (row: Row, include?: Row) => {