Files
crosscode-platform/apps/events/backend/tests/accountsWhatsappOptOut.test.ts
T
joshuaandClaude Opus 5.5 49153e6f07 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>
2026-11-30 11:42:17 +02:00

43 lines
2.0 KiB
TypeScript

// 3.8.0 (docs/APP-1.0-PLAN.md §8, decision 11): the WhatsApp opt-out Accounts keeps for every
// product reaches Events in user.updated (`whatsapp_opted_out`), both ways.
const mockUpdates: { id: string; data: Record<string, unknown> }[] = [];
let mockBefore: { whatsappOptedOut: boolean; phoneNumber: string | null } | null = null;
jest.mock('../src/config/db', () => ({
user: {
findFirst: async () => null,
findUnique: async () => mockBefore,
update: async ({ where, data }: { where: { id: string }; data: Record<string, unknown> }) => { mockUpdates.push({ id: where.id, data }); return {}; },
},
}));
jest.mock('../src/utils/whatsappStop', () => ({ clearStop: jest.fn().mockResolvedValue('27821234567') }));
import { updateLocalCopy } from '../src/accounts/signin';
// eslint-disable-next-line @typescript-eslint/no-require-imports
const { clearStop } = require('../src/utils/whatsappStop') as { clearStop: jest.Mock };
const profile = (over: Record<string, unknown> = {}) => ({
name: 'Thandi Example', email: 'thandi@hope.test', phone_number: '+27821234567', news_consent: { email: true, whatsapp: false }, ...over,
});
beforeEach(() => { mockUpdates.length = 0; mockBefore = null; clearStop.mockClear(); });
test('opted out in Accounts: no WhatsApp here either', async () => {
await updateLocalCopy('u1', profile({ whatsapp_opted_out: true }));
expect(mockUpdates[0]!.data.whatsappOptedOut).toBe(true);
expect(clearStop).not.toHaveBeenCalled();
});
test('turned back on in Accounts: on here, and this site\'s own STOP for the number goes', async () => {
mockBefore = { whatsappOptedOut: true, phoneNumber: '+27821234567' };
await updateLocalCopy('u1', profile({ whatsapp_opted_out: false }));
expect(mockUpdates[0]!.data.whatsappOptedOut).toBe(false);
expect(clearStop).toHaveBeenCalledWith('+27821234567');
});
test('an Accounts before 1.9.0 (no field) changes nothing about it', async () => {
await updateLocalCopy('u1', profile());
expect('whatsappOptedOut' in mockUpdates[0]!.data).toBe(false);
});