37 lines
946 B
TypeScript
37 lines
946 B
TypeScript
import { NextResponse } from "next/server";
|
|
import { auth } from "@/auth";
|
|
import { db } from "@/lib/db";
|
|
|
|
export async function GET() {
|
|
const session = await auth();
|
|
if (!session?.user) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
const notifications = await db.notification.findMany({
|
|
where: { userId: session.user.id },
|
|
orderBy: { createdAt: "desc" },
|
|
take: 50,
|
|
});
|
|
|
|
const unreadCount = await db.notification.count({
|
|
where: { userId: session.user.id, isRead: false },
|
|
});
|
|
|
|
return NextResponse.json({ notifications, unreadCount });
|
|
}
|
|
|
|
export async function PATCH() {
|
|
const session = await auth();
|
|
if (!session?.user) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
await db.notification.updateMany({
|
|
where: { userId: session.user.id, isRead: false },
|
|
data: { isRead: true },
|
|
});
|
|
|
|
return NextResponse.json({ success: true });
|
|
}
|