Events 3.0: auto-connect, demo people in Accounts, e2e; import.finish checks the subs
- contracts 1.8.0 / Accounts 1.1.0: import.finish lists the subs the product relies on; Accounts checks each is held, not deleted and known to it (the count+hash failed a second move after Undo once someone had joined through Accounts). - Events: connects to the church's Accounts once the Hub lists it; moveNewPeople (demo data, people added after the move); set-demo moves demo people; e2e/sign-in-accounts.spec.ts with Accounts in memory and an e2e-only /api rewrite. test:accounts 14/14; Jest auto-connect tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
import crypto from 'node:crypto';
|
||||
import { IMPORT_BATCH_MAX, phoneSchema, type ImportPerson, type ImportResult } from '@crosscode/contracts';
|
||||
import { importIdsHash } from '@crosscode/contracts/signing';
|
||||
import { siteUrl } from '../feed/context';
|
||||
import { accounts, accountsLink, AccountsUnavailable } from './client';
|
||||
import { authMode, CLEARED_KEY, getSetting, MODE_KEY, MOVED_AT_KEY, REPORT_KEY, setSetting, UNDO_DAYS } from './mode';
|
||||
@@ -214,6 +213,52 @@ export type MoveReport = {
|
||||
notices: 'events' | 'accounts';
|
||||
};
|
||||
|
||||
/**
|
||||
* Ends a move: Accounts checks it holds every account this site now relies on (everyone linked,
|
||||
* including a move run again after a failure, and people who signed up through Accounts since).
|
||||
*/
|
||||
async function finishMove(auth: Awaited<ReturnType<typeof accounts>>, actor: string | null) {
|
||||
const linkedRows: { accountId: string }[] = await prisma.user.findMany({ where: { accountId: { not: null } }, select: { accountId: true } });
|
||||
const subs = [...new Set(linkedRows.map((x) => x.accountId))];
|
||||
const finish = await auth.request<{ matched: boolean; count: number; missing: string[] }>({ type: 'import.finish', actor, subs });
|
||||
if (!finish.matched) {
|
||||
throw new MoveError(`Accounts holds ${finish.count} of the ${subs.length} people this site relies on (missing, for example, ${finish.missing.slice(0, 3).join(', ')}). Nothing was switched; run the move again.`, 'MISMATCH');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* People added here after sign-in moved, without signing in through Accounts (demo data, an
|
||||
* import): moved into Accounts the same way, so they can sign in. Only while sign-in goes through
|
||||
* Accounts; returns how many were moved.
|
||||
*/
|
||||
export async function moveNewPeople(actor: string | null = null): Promise<{ moved: number; refused: { email: string; reason: string }[] }> {
|
||||
if ((await authMode()) !== 'accounts') return { moved: 0, refused: [] };
|
||||
const people = await peopleToMove();
|
||||
if (!people.length) return { moved: 0, refused: [] };
|
||||
const auth = await accounts();
|
||||
await auth.request({ type: 'import.begin', actor, check: false, notices: [], church: { name: null, primaryColour: null } });
|
||||
try {
|
||||
const byId = new Map(people.map((p) => [p.id, p]));
|
||||
let moved = 0;
|
||||
const refused: { email: string; reason: string }[] = [];
|
||||
for (const batch of chunk(people, IMPORT_BATCH_MAX)) {
|
||||
const built = await Promise.all(batch.map((u) => person(u, { admins: new Set(), seal: (v) => auth.seal(v), keepAccountsNotices: false })));
|
||||
const r = await auth.request<{ results: ImportResult[] }>({ type: 'import.batch', actor, check: false, people: built }, { timeoutMs: 120000 });
|
||||
for (const res of r.results) {
|
||||
if (res.sub && res.outcome !== 'refused') {
|
||||
await prisma.user.update({ where: { id: res.id }, data: { accountId: res.sub } });
|
||||
moved++;
|
||||
} else refused.push({ email: byId.get(res.id)?.email ?? res.id, reason: res.reason ?? '' });
|
||||
}
|
||||
}
|
||||
await finishMove(auth, actor);
|
||||
return { moved, refused };
|
||||
} catch (e) {
|
||||
await auth.request({ type: 'import.abort', actor }).catch(() => {});
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/** The move. Throws MoveError when it doesn't finish; nothing changes for anyone then. */
|
||||
export async function move(opts: { actor: { id: string; role: string; name?: string }; adminIds: string[]; keepAccountsNotices?: boolean; ip?: string | null }): Promise<MoveReport> {
|
||||
const auth = await connected();
|
||||
@@ -245,11 +290,7 @@ export async function move(opts: { actor: { id: string; role: string; name?: str
|
||||
}
|
||||
results.push(...r.results);
|
||||
}
|
||||
// Everyone this site has linked, including a move run again after a failure.
|
||||
const linkedRows: { accountId: string }[] = await prisma.user.findMany({ where: { accountId: { not: null } }, select: { accountId: true } });
|
||||
const subs = [...new Set(linkedRows.map((x) => x.accountId))];
|
||||
const finish = await auth.request<{ matched: boolean; count: number }>({ type: 'import.finish', actor: opts.actor.id, count: subs.length, idsHash: importIdsHash(subs) });
|
||||
if (!finish.matched) throw new MoveError(`Accounts holds ${finish.count} people from this site, but ${subs.length} were sent. Nothing was switched; run the move again.`, 'MISMATCH');
|
||||
await finishMove(auth, opts.actor.id);
|
||||
|
||||
const who = (id: string) => ({ name: byId.get(id)?.name ?? '', email: byId.get(id)?.email ?? '' });
|
||||
const report: MoveReport = {
|
||||
|
||||
Reference in New Issue
Block a user