Initial commit

This commit is contained in:
Developer
2026-02-06 21:44:04 -06:00
commit f85e93c7a6
151 changed files with 22916 additions and 0 deletions

View File

@@ -0,0 +1,142 @@
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { getPrisma } from "../../../../lib/db";
import { getSession } from "../../../../lib/auth/session";
export const runtime = "nodejs";
const UpdateBody = z.object({
title: z.string().min(3).optional(),
description: z.string().min(1).optional(),
speaker: z.string().min(1).optional(),
startAt: z.string().optional(),
duration: z.number().int().positive().optional(),
bannerUrl: z.string().url().optional().or(z.literal("")).optional(),
category: z.string().min(1).optional(),
visibility: z.enum(["PUBLIC", "PRIVATE"]).optional(),
isActive: z.boolean().optional(),
capacity: z.number().int().positive().optional(),
priceCents: z.number().int().min(0).optional(),
learningPoints: z.array(z.string()).optional(),
meetingInfo: z.any().optional(),
});
export async function GET(
req: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params;
const prisma = await getPrisma();
if (!prisma) {
return NextResponse.json({ ok: false, message: "Database not configured" }, { status: 503 });
}
const session = await getSession();
const isAdmin = session?.role === "ADMIN";
try {
const webinar = await prisma.webinar.findUnique({
where: { id: id },
include: {
_count: {
select: { registrations: true },
},
},
});
if (!webinar) {
return NextResponse.json({ ok: false, message: "Webinar not found" }, { status: 404 });
}
// Check visibility
if (!isAdmin && (webinar.visibility !== "PUBLIC" || !webinar.isActive)) {
return NextResponse.json({ ok: false, message: "Webinar not found" }, { status: 404 });
}
return NextResponse.json({ ok: true, webinar });
} catch (error) {
console.error("Error fetching webinar:", error);
return NextResponse.json({ ok: false, message: "Failed to fetch webinar" }, { status: 500 });
}
}
export async function PATCH(
req: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params;
const session = await getSession();
if (!session || session.role !== "ADMIN") {
return NextResponse.json({ ok: false, message: "Unauthorized" }, { status: 403 });
}
const prisma = await getPrisma();
if (!prisma) {
return NextResponse.json({ ok: false, message: "Database not configured" }, { status: 503 });
}
try {
const body = await req.json();
const parsed = UpdateBody.safeParse(body);
if (!parsed.success) {
return NextResponse.json(
{ ok: false, message: "Invalid input", errors: parsed.error.errors },
{ status: 400 }
);
}
const updateData: any = { ...parsed.data };
if (parsed.data.startAt) {
updateData.startAt = new Date(parsed.data.startAt);
}
if (parsed.data.learningPoints) {
updateData.learningPoints = parsed.data.learningPoints;
}
const webinar = await prisma.webinar.update({
where: { id: id },
data: updateData,
});
return NextResponse.json({ ok: true, webinar, message: "Webinar updated successfully" });
} catch (error: any) {
console.error("Error updating webinar:", error);
if (error.code === "P2025") {
return NextResponse.json({ ok: false, message: "Webinar not found" }, { status: 404 });
}
return NextResponse.json({ ok: false, message: "Failed to update webinar" }, { status: 500 });
}
}
export async function DELETE(
req: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const { id } = await params;
const session = await getSession();
if (!session || session.role !== "ADMIN") {
return NextResponse.json({ ok: false, message: "Unauthorized" }, { status: 403 });
}
const prisma = await getPrisma();
if (!prisma) {
return NextResponse.json({ ok: false, message: "Database not configured" }, { status: 503 });
}
try {
await prisma.webinar.delete({
where: { id: id },
});
return NextResponse.json({ ok: true, message: "Webinar deleted successfully" });
} catch (error: any) {
console.error("Error deleting webinar:", error);
if (error.code === "P2025") {
return NextResponse.json({ ok: false, message: "Webinar not found" }, { status: 404 });
}
return NextResponse.json({ ok: false, message: "Failed to delete webinar" }, { status: 500 });
}
}

72
app/api/webinars/route.ts Normal file
View File

@@ -0,0 +1,72 @@
import { NextRequest } from "next/server";
import { z } from "zod";
import { getPrisma } from "../../../lib/db";
import { getSession } from "../../../lib/auth/session";
import { ok, fail } from "../../../lib/http";
export const runtime = "nodejs";
export async function GET(req: NextRequest) {
const prisma = await getPrisma();
if (!prisma) return fail(new Error("Database not configured"), { status: 503 });
const session = await getSession();
const isAdmin = session?.role === "ADMIN";
const url = new URL(req.url);
const page = parseInt(url.searchParams.get("page") || "0");
const limit = parseInt(url.searchParams.get("limit") || "20");
const offset = page * limit;
const where = isAdmin ? {} : { visibility: "PUBLIC" as const, isActive: true };
const [webinars, total] = await Promise.all([
prisma.webinar.findMany({
where,
orderBy: { startAt: "asc" },
take: limit,
skip: offset,
}),
prisma.webinar.count({ where }),
]);
return ok({ webinars, total, page, limit });
}
const CreateBody = z.object({
title: z.string().min(3),
description: z.string().min(1),
speaker: z.string().min(1),
startAt: z.string(),
duration: z.number().int().positive(),
bannerUrl: z.string().url().optional().or(z.literal("")),
category: z.string().min(1),
visibility: z.enum(["PUBLIC", "PRIVATE"]),
isActive: z.boolean(),
capacity: z.number().int().positive(),
priceCents: z.number().int().min(0),
learningPoints: z.array(z.string()).optional(),
});
export async function POST(req: NextRequest) {
const session = await getSession();
if (!session || session.role !== "ADMIN") return fail(new Error("Forbidden"), { status: 403 });
const prisma = await getPrisma();
if (!prisma) return fail(new Error("Database not configured"), { status: 503, isAdmin: true });
const parsed = CreateBody.safeParse(await req.json().catch(() => ({})));
if (!parsed.success) return fail(new Error("Invalid input"), { isAdmin: true });
const w = await prisma.webinar.create({
data: {
...parsed.data,
bannerUrl: parsed.data.bannerUrl ? parsed.data.bannerUrl : null,
startAt: new Date(parsed.data.startAt),
meetingInfo: {},
learningPoints: parsed.data.learningPoints || [],
},
});
return ok({ webinar: w });
}