app: the admin's backend: members and roles, jobs, logs, the setup checklist (APP-1.0-PLAN §10)

Members and roles: members searched by name or email with what they hold;
roles given and taken by an Owner or Church admin (only an Owner makes or
changes an Owner, nobody changes their own), or someone added by email with
a role through Accounts' people.ensure. Background jobs: each job's switch
and schedule (winning over the .env) and Run now. The audit log and the
access log in plain words, 100 at a time. The dashboard's overview adds the
setup checklist (the church's details and icon, the wall's rules, prayer
coordinators, a first announcement, sharing the App), this week's coverage
and gaps, what waits for the prayer team and recent changes, each for the
roles it's about; Share the App marks the checklist.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 14:42:30 +02:00
co-authored by Claude Opus 5.5
parent 5619a65936
commit f4cf5587be
5 changed files with 431 additions and 12 deletions
+119
View File
@@ -0,0 +1,119 @@
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import request from 'supertest';
import { cookieHeader, harness, MEMBER, PASTOR, signInAs, type Harness } from './support/harness.js';
const ADMIN = '22222222-2222-4222-8222-222222222222';
const COORD = '55555555-5555-4555-8555-555555555555';
const ORIGIN = 'https://app.hope.test';
async function setup() {
const h = await harness() as Harness;
const as = (cookies: Record<string, string>) => ({
get: (path: string) => request(h.app).get(path).set('Cookie', cookieHeader(cookies)),
post: (path: string, body: object = {}) => request(h.app).post(path).set('Cookie', cookieHeader(cookies)).set('Origin', ORIGIN).send(body),
put: (path: string, body: object) => request(h.app).put(path).set('Cookie', cookieHeader(cookies)).set('Origin', ORIGIN).send(body),
});
const owner = as(await signInAs(h, { sub: PASTOR, name: 'Pastor Sam', email: 'pastor@hope.test', roles: ['owner'] }, true));
const admin = as(await signInAs(h, { sub: ADMIN, name: 'Nomsa Admin', email: 'nomsa@hope.test', roles: ['church_admin'] }, true));
const coord = as(await signInAs(h, { sub: COORD, name: 'Lerato Dlamini', email: 'lerato@hope.test', roles: ['prayer_coordinator'] }, true));
const member = as(await signInAs(h, { sub: MEMBER, name: 'Thandi Mokoena', email: 'thandi@hope.test' }));
return { h, owner, admin, coord, member };
}
describe('members and roles (§10)', () => {
it('lists members with their roles, searched by name or email, for an Owner or Church admin only', async () => {
const { owner, coord } = await setup();
assert.equal((await coord.get('/api/v1/admin/members')).status, 403);
const all = await owner.get('/api/v1/admin/members');
assert.equal(all.status, 200);
assert.equal(all.body.total, 4);
assert.equal(all.body.roleCounts.prayer_coordinator, 1);
const found = await owner.get('/api/v1/admin/members?q=THANDI');
assert.deepEqual(found.body.members.map((m: { name: string }) => m.name), ['Thandi Mokoena']);
const withRoles = await owner.get('/api/v1/admin/members?role=any');
assert.equal(withRoles.body.total, 3);
});
it('gives and takes roles: only an Owner makes an Owner, nobody changes their own', async () => {
const { h, owner, admin } = await setup();
assert.equal((await admin.put(`/api/v1/admin/members/${MEMBER}/roles`, { roles: ['prayer_coordinator', 'devotional_writer'] })).status, 200);
assert.deepEqual((await h.db.member.findUnique({ where: { id: MEMBER } }))!.roles, ['devotional_writer', 'prayer_coordinator']);
assert.equal((await admin.put(`/api/v1/admin/members/${MEMBER}/roles`, { roles: ['owner'] })).body.error.code, 'OWNERS_ONLY');
assert.equal((await admin.put(`/api/v1/admin/members/${PASTOR}/roles`, { roles: [] })).body.error.code, 'OWNERS_ONLY');
assert.equal((await owner.put(`/api/v1/admin/members/${PASTOR}/roles`, { roles: [] })).body.error.code, 'SELF');
assert.equal((await owner.put(`/api/v1/admin/members/${MEMBER}/roles`, { roles: ['owner'] })).status, 200);
assert.equal((await owner.put(`/api/v1/admin/members/${MEMBER}/roles`, { roles: ['pope'] })).status, 400);
const log = [...h.db.tables.get('auditLog')!.values()].filter((r) => r.action === 'member_roles_changed');
assert.equal(log.length, 2);
});
it('adds someone by email through Accounts, with their roles', async () => {
const { h, admin } = await setup();
const res = await admin.post('/api/v1/admin/members', { name: 'Johan Pretorius', email: 'Johan@Hope.test', roles: ['prayer_coordinator'] });
assert.equal(res.status, 200);
assert.equal(res.body.newAccount, true);
const ensure = h.fake.requests.find((r) => r.type === 'people.ensure')!;
assert.equal(ensure.email, 'johan@hope.test');
assert.equal(ensure.actor, ADMIN);
const m = await h.db.member.findUnique({ where: { id: res.body.id } });
assert.deepEqual(m!.roles, ['prayer_coordinator']);
// Again with another role: added to what they have.
const again = await admin.post('/api/v1/admin/members', { name: 'Johan Pretorius', email: 'johan@hope.test', roles: ['content_admin'] });
assert.equal(again.body.alreadyMember, true);
assert.deepEqual(again.body.roles, ['content_admin', 'prayer_coordinator']);
assert.equal((await admin.post('/api/v1/admin/members', { name: 'X', email: 'x@hope.test', roles: [] })).status, 400);
assert.equal((await admin.post('/api/v1/admin/members', { name: 'X', email: 'x@hope.test', roles: ['owner'] })).body.error.code, 'OWNERS_ONLY');
});
});
describe('the dashboard (§10)', () => {
it('has the setup checklist for an Owner, ticked as things get done', async () => {
const { owner, coord } = await setup();
const o = await owner.get('/api/v1/admin/overview');
assert.equal(o.status, 200);
const done = (body: { checklist: { items: { key: string; done: boolean }[] } }) => Object.fromEntries(body.checklist.items.map((i) => [i.key, i.done]));
assert.deepEqual(done(o.body), { church: false, wall: false, coordinators: true, announcement: false, share: false });
assert.equal(o.body.week.days.length, 7);
assert.equal(o.body.waiting.requests, 0);
assert.equal((await owner.post('/api/v1/admin/share')).status, 200);
assert.equal(done((await owner.get('/api/v1/admin/overview')).body).share, true);
// A coordinator: the week and the queue, not the checklist or the recent changes.
const c = await coord.get('/api/v1/admin/overview');
assert.equal(c.body.checklist, null);
assert.ok(c.body.week);
assert.deepEqual(c.body.recent, []);
});
});
describe('background jobs and the logs (§10)', () => {
it('lists the jobs, switches one off and runs one now', async () => {
const { h, owner, coord } = await setup();
assert.equal((await coord.get('/api/v1/admin/jobs')).status, 403);
const list = await owner.get('/api/v1/admin/jobs');
assert.ok(list.body.jobs.some((j: { name: string }) => j.name === 'messages-send'));
const off = await owner.put('/api/v1/admin/jobs/devotional-sends', { enabled: false });
assert.equal(off.body.override.enabled, false);
assert.equal((await owner.put('/api/v1/admin/jobs/devotional-sends', { cron: 'every minute' })).status, 400);
assert.equal((await owner.put('/api/v1/admin/jobs/nope', { enabled: false })).status, 404);
const run = await owner.post('/api/v1/admin/jobs/announcement-sends/run');
assert.equal(run.status, 200);
assert.equal(run.body.queued, false);
await owner.put('/api/v1/admin/jobs/devotional-sends', { enabled: null });
const log = [...h.db.tables.get('auditLog')!.values()].map((r) => r.action);
assert.ok(log.includes('job_settings_changed') && log.includes('job_run'));
});
it('shows the audit log and the access log with names, to an Owner or Church admin', async () => {
const { owner, coord } = await setup();
await owner.put(`/api/v1/admin/members/${MEMBER}/roles`, { roles: ['content_admin'] });
await coord.get('/api/v1/admin/prayer/bookings?date=2026-12-02');
const audit = await owner.get('/api/v1/admin/audit');
assert.equal(audit.body.entries[0].action, 'Changed someone\'s roles');
assert.equal(audit.body.entries[0].who, 'Pastor Sam');
const access = await owner.get('/api/v1/admin/access');
assert.equal(access.body.entries[0].who, 'Lerato Dlamini');
assert.equal(access.body.entries[0].action, 'Saw who prays when');
assert.equal((await coord.get('/api/v1/admin/access')).status, 403);
});
});
+12 -1
View File
@@ -3,6 +3,7 @@
* tested against Accounts' provider in apps/accounts/backend/tests/authClient.test.ts), linked to a
* church's Accounts and registered with the Hub, and a clock the tests can move.
*/
import crypto from 'node:crypto';
import { IDENTITY_REQUEST_SCHEMA, type IdentityClaims } from '@crosscode/contracts';
import { sealLinkValue } from '@crosscode/contracts/signing';
import type { AuthClientModule } from '../../src/auth/accounts.js';
@@ -27,6 +28,8 @@ export type FakeAccounts = {
/** Requests the App sent Accounts (`crosscode.identity-request/1`). */
requests: Record<string, unknown>[];
/** What `messaging.get` answers: the church's settings in the clear (sealed on the way), or an error. */
/** Accounts' accounts by email, for people.ensure. */
accountsByEmail: Map<string, string>;
messaging: { email: (Omit<Smtp, 'pass'> & { pass: string | null }) | null; whatsapp: Wawp | null } | Error;
};
@@ -107,6 +110,14 @@ function fakeAuthClient(fake: FakeAccounts): AuthClientModule {
updatedAt: '2026-11-30T10:00:00.000Z',
};
}
if (input.type === 'people.ensure') {
// The same account for the same email, as Accounts does; new ones get a fresh id.
const email = String(input.email);
const known = fake.accountsByEmail.get(email);
const sub = known ?? crypto.randomUUID();
fake.accountsByEmail.set(email, sub);
return { schema: IDENTITY_REQUEST_SCHEMA, sub, created: !known, emailSent: !known };
}
return { schema: IDENTITY_REQUEST_SCHEMA, ok: true };
},
verifyAccountsCall: (raw: Buffer, headers: Record<string, unknown>) => {
@@ -126,7 +137,7 @@ export function testClock(start = '2026-12-01T06:00:00Z') {
export async function harness(opts: { linked?: boolean; env?: Record<string, string> } = {}) {
const db = memoryDb();
const clock = testClock();
const fake: FakeAccounts = { next: { sub: PASTOR }, started: [], requests: [], messaging: { email: CHURCH_SMTP, whatsapp: CHURCH_WAWP } };
const fake: FakeAccounts = { next: { sub: PASTOR }, started: [], requests: [], accountsByEmail: new Map(), messaging: { email: CHURCH_SMTP, whatsapp: CHURCH_WAWP } };
const transport = new MemoryTransport();
const config = loadConfig({ APP_URL: SITE, HUB_ADMIN_EMAIL: ADMIN_EMAIL.toUpperCase(), ...opts.env });
const ctx = createCtx(config, db as unknown as Db, { authClient: async () => fakeAuthClient(fake), now: clock.now, transport });