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 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { requireAdmin } from "@/lib/session";
|
||||
import bcrypt from "bcryptjs";
|
||||
import { Role } from "@prisma/client";
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const admin = await requireAdmin();
|
||||
if (!admin) return NextResponse.json({ error: "Admin requis" }, { status: 403 });
|
||||
|
||||
try {
|
||||
const users = await prisma.user.findMany({
|
||||
orderBy: { createdAt: "asc" },
|
||||
include: {
|
||||
muayThai: true,
|
||||
yoga: true,
|
||||
certificates: true,
|
||||
},
|
||||
});
|
||||
return NextResponse.json(users);
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Erreur" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const admin = await requireAdmin();
|
||||
if (!admin) return NextResponse.json({ error: "Admin requis" }, { status: 403 });
|
||||
|
||||
const { name, email, password, isMuayThai, isYoga, role } = await req.json();
|
||||
|
||||
if (!name || !email || !password) {
|
||||
return NextResponse.json({ error: "Nom, email et mot de passe requis" }, { status: 400 });
|
||||
}
|
||||
|
||||
const existing = await prisma.user.findUnique({ where: { email: email.toLowerCase() } });
|
||||
if (existing) {
|
||||
return NextResponse.json({ error: "Un compte avec cet email existe déjà" }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const hashed = await bcrypt.hash(password, 10);
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
name,
|
||||
email: email.toLowerCase(),
|
||||
password: hashed,
|
||||
role: (role as Role) || "MEMBER",
|
||||
isMuayThai: !!isMuayThai,
|
||||
isYoga: !!isYoga,
|
||||
muayThai: isMuayThai ? { create: {} } : undefined,
|
||||
yoga: isYoga ? { create: {} } : undefined,
|
||||
},
|
||||
});
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { password: _, ...safeUser } = user;
|
||||
return NextResponse.json(safeUser, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ error: "Erreur" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user