34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { requireAdmin } from "@/lib/session";
|
|
|
|
export async function PATCH(req: NextRequest) {
|
|
const admin = await requireAdmin();
|
|
if (!admin) return NextResponse.json({ error: "Admin requis" }, { status: 403 });
|
|
|
|
const { userId, sessionsToAdd, sessionsToRemove } = await req.json();
|
|
if (!userId) return NextResponse.json({ error: "userId requis" }, { status: 400 });
|
|
|
|
try {
|
|
const profile = await prisma.yogaProfile.upsert({
|
|
where: { userId },
|
|
update: {},
|
|
create: { userId },
|
|
});
|
|
|
|
let newCount = profile.sessionsRemaining;
|
|
if (sessionsToAdd) newCount += Number(sessionsToAdd);
|
|
if (sessionsToRemove) newCount = Math.max(0, newCount - Number(sessionsToRemove));
|
|
|
|
const updated = await prisma.yogaProfile.update({
|
|
where: { id: profile.id },
|
|
data: { sessionsRemaining: newCount },
|
|
});
|
|
|
|
return NextResponse.json(updated);
|
|
} catch (error) {
|
|
console.error(error);
|
|
return NextResponse.json({ error: "Erreur" }, { status: 500 });
|
|
}
|
|
}
|