// Auteur : Simon-Pierre Boucher — contact@spboucher.ai // Profil enrichi du KA ID — saisi ICI, au hub, et affiché par toutes les // plateformes du groupe (via /api/sso/profile). POST = mise à jour. import { NextRequest, NextResponse } from "next/server"; import { db, SOCIAL_KEYS } from "@/lib/db"; import { getSessionUser } from "@/lib/auth"; const clip = (v: unknown, max: number): string => typeof v === "string" ? v.trim().slice(0, max) : ""; export async function POST(req: NextRequest) { const user = await getSessionUser(); if (!user) return NextResponse.json({ error: "Non connecté." }, { status: 401 }); const body = (await req.json().catch(() => ({}))) as Record; const name = clip(body.name, 80); const bio = clip(body.bio, 280); const city = clip(body.city, 60); const phone = clip(body.phone, 40); const website = clip(body.website, 300); const jobTitle = clip(body.job_title, 80); const company = clip(body.company, 80); const birthDate = clip(body.birth_date, 10); if (birthDate && !/^\d{4}-\d{2}-\d{2}$/.test(birthDate)) return NextResponse.json( { error: "Date de naissance invalide (AAAA-MM-JJ)." }, { status: 400 }, ); if (website && !/^https?:\/\//i.test(website) && website.length > 0) return NextResponse.json( { error: "Le site web doit commencer par http(s)://" }, { status: 400 }, ); const socialsIn = (body.socials ?? {}) as Record; const socials: Record = {}; for (const k of SOCIAL_KEYS) { const v = clip(socialsIn[k], 200); if (v) socials[k] = v; } db.prepare( `UPDATE users SET name = CASE WHEN ? != '' THEN ? ELSE name END, bio = ?, city = ?, phone = ?, website = ?, job_title = ?, company = ?, birth_date = ?, socials = ? WHERE id = ?`, ).run( name, name, bio, city, phone, website, jobTitle, company, birthDate, JSON.stringify(socials), user.id, ); return NextResponse.json({ ok: true }); }