Files
2026-06-06 19:55:41 +00:00

38 lines
1.1 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { hashPassword } from "@/lib/auth";
export async function GET() {
const establishments = await prisma.establishment.findMany({
include: { subscription: true, _count: { select: { users: true, classes: true } } },
orderBy: { createdAt: "desc" },
});
return NextResponse.json(establishments);
}
export async function POST(req: NextRequest) {
const body = await req.json();
const { name, slug, adminEmail, adminPassword } = body;
const establishment = await prisma.establishment.create({
data: { name, slug },
});
await prisma.subscription.create({
data: { establishmentId: establishment.id, plan: "trial", status: "active" },
});
if (adminEmail && adminPassword) {
await prisma.user.create({
data: {
email: adminEmail,
password: await hashPassword(adminPassword),
role: "admin",
establishmentId: establishment.id,
},
});
}
return NextResponse.json(establishment, { status: 201 });
}