- Added `pg` and `@types/pg` dependencies for PostgreSQL integration. - Updated `useEvaluation` hook to load user evaluations from the API, improving data management. - Refactored `saveUserEvaluation` and `loadUserEvaluation` functions to interact with the API instead of localStorage. - Introduced error handling for profile loading and evaluation saving to enhance robustness. - Added new methods for managing user profiles and evaluations, including `clearUserProfile` and `loadEvaluationForProfile`.
55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { evaluationService } from "@/services/evaluation-service";
|
|
import { UserEvaluation, UserProfile } from "@/lib/types";
|
|
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
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: "firstName, lastName et teamId sont requis" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const profile: UserProfile = { firstName, lastName, teamId };
|
|
const evaluation = await evaluationService.loadUserEvaluation(profile);
|
|
|
|
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 }
|
|
);
|
|
}
|
|
}
|