/** * News consent lives in the church's Accounts once sign-in goes through it (3.0.0, * docs/EVENTS-3.0-PLAN.md decision 7). An unsubscribe link or a WhatsApp STOP here turns news off * there too (`people.newsOff`), so it's off for every product, and Accounts' next `user.updated` * doesn't turn it back on here. Never fails the unsubscribe itself. */ import { accounts } from './client'; import { viaAccounts } from './mode'; /* eslint-disable @typescript-eslint/no-require-imports -- CommonJS */ const prisma = require('../config/db'); /* eslint-enable @typescript-eslint/no-require-imports */ export async function newsOffInAccounts(userId: string, channels: string[]): Promise { try { if (!(await viaAccounts())) return; const u = await prisma.user.findUnique({ where: { id: userId }, select: { accountId: true } }); if (!u?.accountId) return; const both = channels.includes('email') && channels.includes('whatsapp'); const channel = both ? 'all' : channels.includes('whatsapp') ? 'whatsapp' : 'email'; await (await accounts()).request({ type: 'people.newsOff', actor: null, sub: u.accountId, channel }); } catch (e) { console.warn('[accounts] news off not passed on:', (e as Error).message); } }