pelagia-portal/App/app/api/notifications/read/route.ts
2026-05-18 23:18:58 +05:30

32 lines
904 B
TypeScript

import { auth } from "@/auth";
import { db } from "@/lib/db";
import { NextRequest, NextResponse } from "next/server";
// PATCH /api/notifications/read
// Body: { ids?: string[] } — if omitted, mark all unread as read
export async function PATCH(request: NextRequest) {
const session = await auth();
if (!session?.user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
let ids: string[] | undefined;
try {
const body = await request.json();
ids = body.ids;
} catch {
// no body — mark all
}
if (ids && ids.length > 0) {
await db.notification.updateMany({
where: { id: { in: ids }, userId: session.user.id },
data: { isRead: true },
});
} else {
await db.notification.updateMany({
where: { userId: session.user.id, isRead: false },
data: { isRead: true },
});
}
return NextResponse.json({ ok: true });
}