Files
crosscode-platform/apps/accounts/backend/src/app.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

76 lines
3.4 KiB
TypeScript

/**
* The Express app (docs/ACCOUNTS-1.0-PLAN.md §1). Everything is under /api, so the site and its API
* share one address (as Events 2.2): nginx sends /api to this backend and the rest to Next.js.
*
* /api/health version and database, for monitoring and the Hub
* /api/oidc/… the OpenID Connect provider (discovery, auth, token, …)
* /api/self/… the account page's own sign-in
* /api/church/v1, /api/links/v1/… for the church's other products
* /api/accounts/v1/requests what a linked product asks (import, people.*; contracts 1.8.0)
* /api/v2/webhooks/whatsapp WAWP's webhook: STOP replies (1.9.0)
* /api/v2/… the pages' API (sign-in steps, account, admin, setup)
*/
import express from 'express';
import type Provider from 'oidc-provider';
import type { Ctx } from './context.js';
import { sameOrigin, sendError } from './http.js';
import { adminRouter } from './admin/routes.js';
import { interactionRouter, selfRouter } from './interaction/routes.js';
import { meRouter } from './me/routes.js';
import { LOGOUT_JS } from './oidc/pages.js';
import { publicRouter } from './public/routes.js';
import { requestsRouter } from './links/requests.js';
import { whatsappWebhookRouter } from './links/whatsappWebhook.js';
import { setupRouter } from './setup/routes.js';
import { health } from './hub/client.js';
import { appVersion } from './lib/version.js';
export function createApp(ctx: Ctx, provider: Provider) {
const app = express();
app.disable('x-powered-by');
if (ctx.config.trustProxySetting !== false) app.set('trust proxy', ctx.config.trustProxySetting);
app.use((_req, res, next) => {
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('Referrer-Policy', 'same-origin');
res.setHeader('X-Frame-Options', 'DENY');
res.setHeader('X-Robots-Tag', 'noindex, nofollow');
next();
});
app.get('/api/health', async (_req, res) => {
const h = await health(ctx);
res.status(h.db ? 200 : 503).json({ status: h.db ? 'ok' : 'error', product: 'accounts', version: appVersion(), db: h.db });
});
app.get('/api/static/logout.js', (_req, res) => {
res.type('application/javascript').set('Cache-Control', 'public, max-age=3600').send(LOGOUT_JS);
});
// The provider parses its own bodies: mounted before express.json.
app.use('/api/oidc', provider.callback());
app.use('/api/self', selfRouter(ctx));
app.use('/api', requestsRouter(ctx, provider));
app.use('/api', publicRouter(ctx));
// 1.9.0: WAWP's webhook (STOP replies), before the pages' API: no same-origin check, its own secret.
app.use(whatsappWebhookRouter(ctx));
const v2 = express.Router();
v2.use(express.json({ limit: '64kb' }));
v2.use(sameOrigin(ctx));
v2.use(interactionRouter(ctx, provider));
v2.use(setupRouter(ctx));
v2.use(meRouter(ctx, provider));
v2.use('/admin', adminRouter(ctx, provider));
v2.use((_req, res) => res.status(404).json({ error: { code: 'NOT_FOUND', message: 'There is no such API endpoint.' } }));
app.use('/api/v2', v2);
app.use((err: unknown, _req: express.Request, res: express.Response, next: express.NextFunction) => {
if (res.headersSent) return next(err);
const e = err as { type?: string };
if (e?.type === 'entity.parse.failed') return res.status(400).json({ error: { code: 'VALIDATION', message: 'The body must be JSON.' } });
return sendError(res, err);
});
return app;
}