74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { cookies } from "next/headers";
|
|
import { evaluationService } from "@/services/evaluation-service";
|
|
import { UserEvaluation, UserProfile } from "@/lib/types";
|
|
import { COOKIE_NAME } from "@/lib/auth-utils";
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const cookieStore = await cookies();
|
|
const userUuid = cookieStore.get(COOKIE_NAME)?.value;
|
|
|
|
// Support pour l'ancien mode avec paramètres (pour la compatibilité)
|
|
if (!userUuid) {
|
|
const { searchParams } = new URL(request.url);
|
|
const firstName = searchParams.get("firstName");
|
|
const lastName = searchParams.get("lastName");
|
|
const teamId = searchParams.get("teamId");
|
|
|
|
if (!firstName || !lastName || !teamId) {
|
|
return NextResponse.json(
|
|
{ error: "Utilisateur non authentifié" },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const profile: UserProfile = { firstName, lastName, teamId };
|
|
const evaluation = await evaluationService.loadUserEvaluation(profile);
|
|
return NextResponse.json({ evaluation });
|
|
}
|
|
|
|
// Mode authentifié par cookie UUID
|
|
const userProfile = await evaluationService.getUserByUuid(userUuid);
|
|
if (!userProfile) {
|
|
return NextResponse.json(
|
|
{ error: "Utilisateur non trouvé" },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
const evaluation = await evaluationService.loadUserEvaluation(userProfile);
|
|
return NextResponse.json({ evaluation });
|
|
} catch (error) {
|
|
console.error("Erreur lors du chargement de l'évaluation:", error);
|
|
return NextResponse.json(
|
|
{ error: "Erreur interne du serveur" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json();
|
|
const evaluation: UserEvaluation = body.evaluation;
|
|
|
|
if (!evaluation || !evaluation.profile) {
|
|
return NextResponse.json(
|
|
{ error: "Évaluation invalide" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
await evaluationService.saveUserEvaluation(evaluation);
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error("Erreur lors de la sauvegarde de l'évaluation:", error);
|
|
return NextResponse.json(
|
|
{ error: "Erreur interne du serveur" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|