Events 3.8.0, @crosscode/demo 1.1.0: STOP stops all WhatsApp, demo scenarios

Events 3.8.0 (one migration):
- STOP to the church's WhatsApp number stops all WhatsApp, not only news:
  checked before every sendText/sendPdf against WhatsAppStop (by number) and
  User.whatsappOptedOut (from Accounts' user.updated); 2FA codes go by email
- a STOP received here is passed on to Accounts (whatsapp.optout); START turns
  it back on without Accounts; Accounts turning it back on clears it here
- the eight demo scenarios as set-demo profiles, around each church's own
  people (@crosscode/demo, same accounts as Accounts) and campuses; the
  scenario's people are never removed from Accounts

@crosscode/demo 1.1.0: each person's gender.
crosscode script: scenarios as Events demo profiles; Events builds packages/demo.

Checked on PostgreSQL 18: the migration and every scenario loaded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 11:42:17 +02:00
co-authored by Claude Opus 5.5
parent 5b9dc559b6
commit 49153e6f07
26 changed files with 549 additions and 48 deletions
+2 -1
View File
@@ -270,7 +270,8 @@ function whatsAppNewsFooter(recipient) {
/** Recognises an opt-out reply ("STOP", "stop.", "Unsubscribe"). */
function isStopKeyword(text) {
return /^\s*(stop|unsubscribe|stop all|opt out|optout)\s*[.!]?\s*$/i.test(String(text || ''));
// 3.8.0: STOPP and UITTEKEN (Afrikaans) too, as the church's Accounts reads them.
return /^\s*(stop|stopp|unsubscribe|stop all|opt out|optout|uitteken)\s*[.!]?\s*$/i.test(String(text || ''));
}
/**
@@ -186,6 +186,8 @@ async function deliverCode(user, method, code, { allowFallback = true } = {}) {
try {
// sendText silently no-ops when WhatsApp isn't set up, so check first.
if (!isValidZAPhone(user.phoneNumber) || !(await isConfigured())) throw new Error('WhatsApp unavailable');
// 3.8.0: they replied STOP, so no WhatsApp: the code goes by email.
if (await require('./whatsappStop').isWhatsAppStopped(user.phoneNumber)) throw new Error('WhatsApp turned off (STOP)');
await sendText(user.phoneNumber, buildWATwoFactorCode({ name: user.name, code }));
return { method: 'whatsapp', destination: maskPhone(user.phoneNumber) };
} catch (e) {
+13 -1
View File
@@ -222,10 +222,18 @@ async function requestPairingCode(phoneNumber) {
// ─── Messaging ────────────────────────────────────────────────────────────────
async function sendText(toPhone, message) {
/**
* 3.8.0: nothing goes to a number that replied STOP (utils/whatsappStop.js), unless `evenIfStopped`
* (only the one reply that confirms the STOP). Returns false when it skipped.
*/
async function sendText(toPhone, message, { evenIfStopped = false } = {}) {
if (!(await isConfigured())) return;
const chatId = toChatId(toPhone);
if (!chatId) { console.warn('[whatsapp] Invalid phone, skipping text:', toPhone); return; }
if (!evenIfStopped && (await require('./whatsappStop').isWhatsAppStopped(toPhone))) {
console.info('[whatsapp] Not sent: the number replied STOP.');
return false;
}
const { token, instanceId } = await getConfig();
try {
await axios.post(`${BASE}/send/text`, {
@@ -251,6 +259,10 @@ async function sendPdf(toPhone, localPdfPath, filename, caption) {
if (!(await isConfigured())) return;
const chatId = toChatId(toPhone);
if (!chatId) { console.warn('[whatsapp] Invalid phone, skipping PDF:', toPhone); return; }
if (await require('./whatsappStop').isWhatsAppStopped(toPhone)) {
console.info('[whatsapp] PDF not sent: the number replied STOP.');
return false;
}
const tempDir = path.join(__dirname, '../../public/uploads/tickets-temp');
if (!fs.existsSync(tempDir)) fs.mkdirSync(tempDir, { recursive: true });
@@ -0,0 +1,63 @@
/**
* 3.8.0 (docs/APP-1.0-PLAN.md §8, decision 11): a STOP to the church's WhatsApp number means no
* WhatsApp from any of the church's products, event updates and codes included, until the person
* turns it back on (in the church's Accounts; or, without Accounts, by replying START here).
*
* Two records, either of which stops a message:
* - `WhatsAppStop`, by number: STOPs this site's webhook received (with or without an account).
* - `User.whatsappOptedOut`: from Accounts (`user.updated` carries `whatsapp_opted_out`), so a STOP
* another product received reaches Events too.
*
* `sendText` / `sendPdf` (utils/whatsapp.js) check `isWhatsAppStopped` before every message.
*/
'use strict';
const prisma = require('../config/db');
function normalise(raw) {
return require('./whatsapp').normalizeZAPhone(raw);
}
/** The ways a number may be stored on an account: 27821234567, +27821234567, 0821234567. */
function storedForms(n) {
return [n, `+${n}`, `0${n.slice(2)}`];
}
/** Whether nothing may go to this number on WhatsApp. */
async function isWhatsAppStopped(rawPhone) {
const n = normalise(rawPhone);
if (!n) return false;
if (await prisma.whatsAppStop.findUnique({ where: { phone: n } })) return true;
const user = await prisma.user.findFirst({ where: { whatsappOptedOut: true, phoneNumber: { in: storedForms(n) } }, select: { id: true } });
return !!user;
}
/** A STOP from this number: kept, and every account with it opted out. Returns the number (27…) or null. */
async function recordStop(rawPhone, { keyword, source = 'events' }) {
const n = normalise(rawPhone);
if (!n) return null;
const word = String(keyword || 'STOP').trim().toUpperCase().slice(0, 40);
await prisma.whatsAppStop.upsert({
where: { phone: n },
update: { keyword: word, source, receivedAt: new Date() },
create: { phone: n, keyword: word, source, receivedAt: new Date() },
});
await prisma.user.updateMany({ where: { phoneNumber: { in: storedForms(n) } }, data: { whatsappOptedOut: true } });
return n;
}
/** WhatsApp back on for this number (Accounts said so, or a START here without Accounts). */
async function clearStop(rawPhone) {
const n = normalise(rawPhone);
if (!n) return null;
await prisma.whatsAppStop.deleteMany({ where: { phone: n } });
await prisma.user.updateMany({ where: { phoneNumber: { in: storedForms(n) } }, data: { whatsappOptedOut: false } });
return n;
}
/** "START" (or "BEGIN", Afrikaans "BEGIN"/"AAN"): WhatsApp back on, for a site without Accounts. */
function isStartKeyword(text) {
return /^\s*(start|unstop|begin|aan)\s*[.!]?\s*$/i.test(String(text || ''));
}
module.exports = { isWhatsAppStopped, recordStop, clearStop, isStartKeyword, storedForms };