Enable an existing AI difficulty for a human seat through admin controls without granting private access or advancing paused play. Preserve custody, credentials, takeback and separate paced resume. Bind atomic writes and outgoing-grant revocation to live authority, room/pause versions and active seat access with durable exact receipts. Validation: types/lint, 5408 offline tests, build and 52 HTTP tests passed on source fingerprint 2157de0f4bb0b74d9760d52102601c52344cf674dee8ccd1d559f0a246a1d4ff; 22 authenticated admin HTTP groups, independent SQL/privacy/UI review, desktop/mobile and refresh checks. All 2098 baseline rooms and 4455 seats preserved. Migration 0015 is additive. Positive production admin QA awaits authorized access; full admin/rules scope remains unfinished.
34 lines
1.8 KiB
TypeScript
34 lines
1.8 KiB
TypeScript
import { env } from 'cloudflare:workers';
|
|
import { AdminError, requireAdmin } from '@/db/admin-access';
|
|
import { applyAdminSeatAi, readAdminSeatAi } from '@/db/admin-seat-ai';
|
|
import { validAdminSeatAiInput } from '@/lib/admin-seat-ai';
|
|
import { adminBody, adminFailure, adminResponse, adminToken } from '@/lib/admin-http';
|
|
|
|
function code(request: Request) {
|
|
const code = new URL(request.url).pathname.split('/').at(-2)?.toUpperCase() ?? '';
|
|
if (code.length !== 8 || !/^[A-Z2-9]{8}$/.test(code)) throw new AdminError('Enter the eight-character room code.', 400);
|
|
return code;
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
try {
|
|
const identity = await requireAdmin(env.DB, adminToken(request));
|
|
if (request.headers.has('X-Dune-Admin-Id') && request.headers.get('X-Dune-Admin-Id') !== identity.id)
|
|
throw new AdminError('The signed-in administrator changed. Reload administration before continuing.', 401);
|
|
return adminResponse(await readAdminSeatAi(env.DB, identity, code(request)));
|
|
} catch (error) { return adminFailure(error); }
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const input = await adminBody(request, env.DUNE_PUBLIC_ORIGIN);
|
|
const identity = await requireAdmin(env.DB, adminToken(request), ['owner', 'operator']);
|
|
if (request.headers.get('X-Dune-Admin-Id') !== identity.id)
|
|
throw new AdminError('The signed-in administrator changed. Reload administration before continuing.', 401);
|
|
if (!validAdminSeatAiInput(input)) throw new AdminError('Choose a participant, difficulty, current versions and an operational reason.', 400);
|
|
const room = code(request);
|
|
const result = await applyAdminSeatAi(env.DB, identity, room, input);
|
|
return adminResponse(result);
|
|
} catch (error) { return adminFailure(error); }
|
|
}
|