Initial commit: JuanitoSport - Muay Thai & Yog4Fight PWA
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
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 { id, ...data } = await req.json();
|
||||
if (!id) return NextResponse.json({ error: "id requis" }, { status: 400 });
|
||||
|
||||
const allowed = [
|
||||
"name", "email", "isMuayThai", "isYoga", "role", "phone", "avatar"
|
||||
];
|
||||
|
||||
const updateData: any = {};
|
||||
for (const key of allowed) {
|
||||
if (data[key] !== undefined) updateData[key] = data[key];
|
||||
}
|
||||
|
||||
if (updateData.email) updateData.email = updateData.email.toLowerCase();
|
||||
|
||||
try {
|
||||
if (updateData.isMuayThai === true) {
|
||||
await prisma.muayThaiProfile.upsert({
|
||||
where: { userId: id },
|
||||
update: {},
|
||||
create: { userId: id },
|
||||
});
|
||||
}
|
||||
if (updateData.isYoga === true) {
|
||||
await prisma.yogaProfile.upsert({
|
||||
where: { userId: id },
|
||||
update: {},
|
||||
create: { userId: id },
|
||||
});
|
||||
}
|
||||
|
||||
const user = await prisma.user.update({
|
||||
where: { id },
|
||||
data: updateData,
|
||||
});
|
||||
const { password: _, ...safeUser } = user;
|
||||
return NextResponse.json(safeUser);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ error: "Erreur" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(req: NextRequest) {
|
||||
const admin = await requireAdmin();
|
||||
if (!admin) return NextResponse.json({ error: "Admin requis" }, { status: 403 });
|
||||
|
||||
const { id } = await req.json();
|
||||
if (!id) return NextResponse.json({ error: "id requis" }, { status: 400 });
|
||||
|
||||
try {
|
||||
await prisma.user.delete({ where: { id } });
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Erreur" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user