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>
This commit is contained in:
@@ -20,14 +20,17 @@ const DEFAULTS: Record<string, () => Row> = {
|
||||
testimony: () => ({ campaignId: null, showName: false, status: 'pending', removedReason: null, moderatedById: null, moderatedAt: null }),
|
||||
accessLog: () => ({ targetId: null, metadata: null, ip: null }),
|
||||
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 }),
|
||||
};
|
||||
const KEYS: Record<string, string> = { appSetting: 'key', productLink: 'installId', linkNonce: 'nonce' };
|
||||
/** 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']] };
|
||||
const HAS_UPDATED_AT = new Set(['member', 'appSetting', 'productLink', 'prayerCommitment', 'prayerCampaign']);
|
||||
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']);
|
||||
/** `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' } },
|
||||
prayerRequest: { updates: { model: 'prayerRequestUpdate', many: true, key: 'requestId' }, prayers: { model: 'requestPrayer', many: true, key: 'requestId' } },
|
||||
prayerCommitment: { skips: { model: 'prayerSkip', many: true, key: 'commitmentId' }, marks: { model: 'prayedMark', many: true, key: 'commitmentId' } },
|
||||
};
|
||||
|
||||
@@ -48,6 +51,7 @@ function matchValue(actual: unknown, cond: unknown): boolean {
|
||||
case 'lt': return actual != null && (v(actual) as number) < (v(x) as number);
|
||||
case 'lte': return actual != null && (v(actual) as number) <= (v(x) as number);
|
||||
case 'not': return x === null ? actual != null : !matchValue(actual, x);
|
||||
case 'isEmpty': return Array.isArray(actual) && (actual.length === 0) === x;
|
||||
case 'has': return Array.isArray(actual) && actual.map(v).includes(v(x));
|
||||
case 'contains': return typeof actual === 'string' && (insensitive ? actual.toLowerCase().includes(String(x).toLowerCase()) : actual.includes(String(x)));
|
||||
default: throw new Error(`memoryDb: no operator ${op}`);
|
||||
@@ -159,9 +163,10 @@ function model(name: string, tables: Map<string, Map<unknown, Row>>) {
|
||||
const cascade = (from: string, id: unknown) => {
|
||||
const drop = (model: string, field: string) => { for (const [k, r] of tables.get(model) ?? []) if (r[field] === id) tables.get(model)!.delete(k); };
|
||||
const unset = (model: string, field: string) => { for (const r of tables.get(model)?.values() ?? []) if (r[field] === id) r[field] = null; };
|
||||
if (from === 'member') { drop('session', 'memberId'); drop('testimony', 'memberId'); drop('outboxMessage', 'memberId'); unset('prayerCommitment', 'memberId'); }
|
||||
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 === 'prayerRequest') { drop('prayerRequestUpdate', 'requestId'); drop('requestPrayer', 'requestId'); }
|
||||
};
|
||||
const withIncludes = (row: Row, include?: Row) => {
|
||||
for (const [rel, on] of Object.entries(include ?? {})) {
|
||||
|
||||
Reference in New Issue
Block a user