chore(api): add explicit type casting for request payloads
This commit is contained in:
@@ -13,11 +13,13 @@ export async function GET() {
|
||||
take: 20,
|
||||
});
|
||||
|
||||
return NextResponse.json(results.map((r, i) => ({
|
||||
rank: i + 1,
|
||||
name: r.user.name,
|
||||
clicks: r.clicks,
|
||||
timeSeconds: r.timeSeconds,
|
||||
userId: r.userId,
|
||||
})));
|
||||
return NextResponse.json(
|
||||
results.map((r, i) => ({
|
||||
rank: i + 1,
|
||||
name: r.user.name,
|
||||
clicks: r.clicks,
|
||||
timeSeconds: r.timeSeconds,
|
||||
userId: r.userId,
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
+39
-9
@@ -13,7 +13,9 @@ async function getOrCreatePuzzle() {
|
||||
if (existing) return existing;
|
||||
|
||||
const { start, target } = await pickTwoArticles();
|
||||
return prisma.dailyPuzzle.create({ data: { date, startArticle: start, targetArticle: target } });
|
||||
return prisma.dailyPuzzle.create({
|
||||
data: { date, startArticle: start, targetArticle: target },
|
||||
});
|
||||
}
|
||||
|
||||
// GET - retourne le puzzle du jour + si l'utilisateur a déjà joué
|
||||
@@ -24,28 +26,49 @@ export async function GET() {
|
||||
let myResult = null;
|
||||
if (session?.user?.id) {
|
||||
myResult = await prisma.dailyResult.findUnique({
|
||||
where: { puzzleId_userId: { puzzleId: puzzle.id, userId: session.user.id } },
|
||||
where: {
|
||||
puzzleId_userId: { puzzleId: puzzle.id, userId: session.user.id },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
puzzle: { id: puzzle.id, date: puzzle.date, startArticle: puzzle.startArticle, targetArticle: puzzle.targetArticle },
|
||||
puzzle: {
|
||||
id: puzzle.id,
|
||||
date: puzzle.date,
|
||||
startArticle: puzzle.startArticle,
|
||||
targetArticle: puzzle.targetArticle,
|
||||
},
|
||||
alreadyPlayed: !!myResult,
|
||||
myResult: myResult ? { clicks: myResult.clicks, timeSeconds: myResult.timeSeconds, won: myResult.won, path: JSON.parse(myResult.path) } : null,
|
||||
myResult: myResult
|
||||
? {
|
||||
clicks: myResult.clicks,
|
||||
timeSeconds: myResult.timeSeconds,
|
||||
won: myResult.won,
|
||||
path: JSON.parse(myResult.path),
|
||||
}
|
||||
: null,
|
||||
});
|
||||
}
|
||||
|
||||
// POST - soumettre un résultat
|
||||
export async function POST(req: Request) {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) return NextResponse.json({ error: "Non connecté" }, { status: 401 });
|
||||
if (!session?.user?.id)
|
||||
return NextResponse.json({ error: "Non connecté" }, { status: 401 });
|
||||
|
||||
const { puzzleId, path, clicks, timeSeconds, won } = await req.json() as {
|
||||
puzzleId: string; path: string[]; clicks: number; timeSeconds: number; won: boolean;
|
||||
const { puzzleId, path, clicks, timeSeconds, won } = (await req.json()) as {
|
||||
puzzleId: string;
|
||||
path: string[];
|
||||
clicks: number;
|
||||
timeSeconds: number;
|
||||
won: boolean;
|
||||
};
|
||||
|
||||
// Vérifier que le puzzle est bien celui du jour
|
||||
const puzzle = await prisma.dailyPuzzle.findUnique({ where: { id: puzzleId } });
|
||||
const puzzle = await prisma.dailyPuzzle.findUnique({
|
||||
where: { id: puzzleId },
|
||||
});
|
||||
if (!puzzle || puzzle.date !== todayKey()) {
|
||||
return NextResponse.json({ error: "Puzzle invalide" }, { status: 400 });
|
||||
}
|
||||
@@ -53,7 +76,14 @@ export async function POST(req: Request) {
|
||||
// Upsert - on n'enregistre qu'une fois
|
||||
const result = await prisma.dailyResult.upsert({
|
||||
where: { puzzleId_userId: { puzzleId, userId: session.user.id } },
|
||||
create: { puzzleId, userId: session.user.id, path: JSON.stringify(path), clicks, timeSeconds, won },
|
||||
create: {
|
||||
puzzleId,
|
||||
userId: session.user.id,
|
||||
path: JSON.stringify(path),
|
||||
clicks,
|
||||
timeSeconds,
|
||||
won,
|
||||
},
|
||||
update: {},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user