Repository Public
1// Auteur : Simon-Pierre Boucher — contact@spboucher.ai2// /api/admin/team — création d'un compte admin (super_admin seulement).3import { NextRequest, NextResponse } from "next/server";4import { createAdmin, findAdminByUsername } from "@/lib/comms/db";5import { getAdminSession, isSuperAdmin, ADMIN_ROLES } from "@/lib/comms/admin-auth";67export async function POST(req: NextRequest) {8 const session = await getAdminSession();9 if (!session)10 return NextResponse.json({ error: "Non autorisé." }, { status: 401 });11 if (!isSuperAdmin(session))12 return NextResponse.json({ error: "Réservé au super administrateur." }, { status: 403 });1314 const body = (await req.json().catch(() => null)) as {15 username?: string;16 display_name?: string;17 email?: string;18 password?: string;19 roles?: string[];20 } | null;21 const username = (body?.username ?? "").trim().toLowerCase().slice(0, 40);22 const displayName = (body?.display_name ?? "").trim().slice(0, 120);23 const password = body?.password ?? "";24 const roles = (body?.roles ?? []).filter((r) => r in ADMIN_ROLES);2526 if (!/^[a-z0-9._-]{3,40}$/.test(username))27 return NextResponse.json(28 { error: "Identifiant invalide (3-40 caractères : a-z, 0-9, . _ -)." },29 { status: 422 },30 );31 if (!displayName)32 return NextResponse.json({ error: "Nom d'affichage requis." }, { status: 422 });33 if (password.length < 8)34 return NextResponse.json(35 { error: "Mot de passe trop court (8 caractères minimum)." },36 { status: 422 },37 );38 if (!roles.length)39 return NextResponse.json({ error: "Au moins un rôle requis." }, { status: 422 });40 if (findAdminByUsername(username))41 return NextResponse.json({ error: "Cet identifiant existe déjà." }, { status: 409 });4243 const id = createAdmin({44 username,45 display_name: displayName,46 email: (body?.email ?? "").trim().slice(0, 200) || null,47 password,48 roles,49 });50 return NextResponse.json({ ok: true, id });51}52