Files
crosscode-platform/apps/accounts/backend/src/links/messaging.ts
T
joshuaandClaude Opus 5.5 ad488c3280 Accounts 1.9.0, @crosscode/demo 1.0.0: messaging.get, WhatsApp opt-out, demo scenarios
Accounts 1.9.0 (one migration):
- messaging.get (sealed settings) and the messaging.changed hint to products
  that asked, on admin or product changes
- the WhatsApp opt-out for every product: whatsapp.optout, Accounts' own WAWP
  webhook, WhatsAppOptOut kept per number, whatsapp_opted_out in user.updated,
  no WhatsApp from Accounts to an opted-out person, "Turn WhatsApp back on"
- demo scenarios: npm run demo -- --profile <scenario> --force replaces the
  church profile (with an SVG logo and icon at /api/demo-assets/) and the demo
  people, ids kept across loads

@crosscode/demo 1.0.0 (new): the eight scenarios' churches, people and logos.

crosscode script: crosscode demo <church> accounts, --scenario on every demo,
Accounts installs and builds packages/demo.

Checked on PostgreSQL 18: migrations, scenario loads, opt-out, npm run demo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 11:05:22 +02:00

32 lines
1.8 KiB
TypeScript

/**
* 1.9.0 (docs/APP-1.0-PLAN.md §8, decision 4): one set of email and WhatsApp settings per church,
* kept here (or copied in from Cross Events). Products that asked for them (`messaging.get`, the
* Church App) are sent `messaging.changed` when they change, so they fetch them again.
*/
import { IDENTITY_REQUEST_SCHEMA, type MessagingChanged } from '@crosscode/contracts';
import type { Ctx } from '../context.js';
import { queueCall } from './deliveries.js';
/** The settings that make up the church's email and WhatsApp. */
const EMAIL_KEYS = new Set(['smtp_host', 'smtp_port', 'smtp_user', 'smtp_pass', 'smtp_from', 'smtp_secure']);
const WHATSAPP_KEYS = new Set(['wawp_access_token', 'wawp_instance_id']);
/** Which parts the changed setting keys touch. */
export function messagingParts(keys: string[]): { email: boolean; whatsapp: boolean } {
return { email: keys.some((k) => EMAIL_KEYS.has(k)), whatsapp: keys.some((k) => WHATSAPP_KEYS.has(k)) };
}
/**
* Records the change and tells every product that asked for the settings, except the one that
* made the change (`exceptLinkId`, e.g. Events saving its own into Accounts).
*/
export async function messagingChanged(ctx: Ctx, parts: { email: boolean; whatsapp: boolean }, exceptLinkId?: string) {
if (!parts.email && !parts.whatsapp) return;
const at = ctx.now().toISOString();
await ctx.settings.set('messaging_updated_at', at);
const wanted = await ctx.db.productLink.findMany({ where: { messagingWantedAt: { not: null } } });
const linkIds = wanted.map((l) => l.installId).filter((id) => id !== exceptLinkId);
if (!linkIds.length) return;
await queueCall(ctx, 'messaging.changed', (): MessagingChanged => ({ schema: IDENTITY_REQUEST_SCHEMA, type: 'messaging.changed', occurredAt: at, ...parts }), { linkIds });
}