app: the Church App's foundation; docs for Hub 2.26.1 and two years of devotionals
apps/app (not released; app@1.0.0 is built in slices, APP-1.0-PLAN §15): - backend (Express, Prisma, pg-boss, as the Website): the Hub client (CCP-, product app, the active-member band in the heartbeat), links and church.changed, sign-in through Accounts (everyone a Member; 8 days renewed up to 90; every role only with 2FA; the first Owner from HUB_ADMIN_EMAIL), Accounts' user.updated/export/deleted, active members once a day, modules on and off, GET /api/v1/app. Migration init. - frontend (Next.js 16): header, tab bar, footer, manifest and icons from the church profile, service worker, Add to home screen, Me, the admin's first dashboard. - env.template.json, PM2 template, README, CHANGELOG, WHATS-NEW; 27 tests. Docs: the Hub runs 2.26.1 with two years of Cross Code's devotionals; an App gets 2 weeks, today included. HANDOVER lists the App's slices. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* The admin API (APP-1.0-PLAN §10), under /api/v1/admin. Every route needs a role above Member and
|
||||
* a sign-in with 2FA (auth/sessions.ts `requireRole`).
|
||||
*
|
||||
* GET /overview the dashboard: active members and band, modules, the Hub and Accounts links
|
||||
* PUT /modules switch modules on or off (Owner, Church admin)
|
||||
*
|
||||
* The rest comes with each module (§10): House of Prayer, requests, announcements, devotionals,
|
||||
* events, members and roles, settings, audit and access logs.
|
||||
*/
|
||||
import express from 'express';
|
||||
import { z } from 'zod';
|
||||
import { APP_MODULES } from '@crosscode/contracts';
|
||||
import type { Ctx } from '../context.js';
|
||||
import { ApiError, handle, sameOrigin } from '../http.js';
|
||||
import { audit, clientIp } from '../lib/audit.js';
|
||||
import { requireRole } from '../auth/sessions.js';
|
||||
import { licenceStatus } from '../hub/licence.js';
|
||||
import { activeMembers } from '../members/activity.js';
|
||||
import { enabledModules, setModules } from '../modules.js';
|
||||
|
||||
const modulesBody = z.object(Object.fromEntries(APP_MODULES.map((m) => [m, z.boolean().optional()]))).strict();
|
||||
|
||||
export function adminRouter(ctx: Ctx) {
|
||||
const r = express.Router();
|
||||
r.use(sameOrigin(ctx.config.publicUrl, ctx.config.production));
|
||||
r.use(express.json({ limit: '64kb' }));
|
||||
r.use((_req, res, next) => {
|
||||
res.set('Cache-Control', 'no-store');
|
||||
next();
|
||||
});
|
||||
|
||||
r.get('/overview', requireRole(ctx), handle(async () => {
|
||||
const [members, modules, licence, accounts, lastHeartbeatAt] = await Promise.all([
|
||||
activeMembers(ctx),
|
||||
enabledModules(ctx),
|
||||
licenceStatus(ctx),
|
||||
ctx.db.productLink.findFirst({ where: { product: 'accounts' } }),
|
||||
ctx.settings.get('hub_last_heartbeat_at'),
|
||||
]);
|
||||
return {
|
||||
activeMembers90d: members,
|
||||
modules,
|
||||
hub: { connected: licence.connected, state: licence.state, endsAt: licence.endsAt, daysLeft: licence.daysLeft, lastHeartbeatAt },
|
||||
accounts: accounts ? { url: accounts.url, connectedAt: accounts.connectedAt } : null,
|
||||
};
|
||||
}));
|
||||
|
||||
r.put('/modules', requireRole(ctx, ['owner', 'church_admin']), handle(async (req) => {
|
||||
const parsed = modulesBody.safeParse(req.body);
|
||||
if (!parsed.success) throw new ApiError(400, 'VALIDATION', `Send the modules to switch as { "<module>": true | false }: ${APP_MODULES.join(', ')}.`);
|
||||
const change = Object.fromEntries(Object.entries(parsed.data).filter(([, v]) => typeof v === 'boolean')) as Record<string, boolean>;
|
||||
const modules = await setModules(ctx, change);
|
||||
await audit(ctx, { actorId: req.signed!.member.id, actorRole: req.signed!.member.roles.join(','), action: 'modules_changed', targetType: 'settings', targetId: 'modules', metadata: change, ip: clientIp(req) });
|
||||
return { modules };
|
||||
}));
|
||||
|
||||
return r;
|
||||
}
|
||||
Reference in New Issue
Block a user