56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
|
|
import { NextRequest, NextResponse } from "next/server";
|
||
|
|
import {
|
||
|
|
getNotifications,
|
||
|
|
getUnreadCount,
|
||
|
|
markRead,
|
||
|
|
markAllRead,
|
||
|
|
dismissNotification,
|
||
|
|
} from "@/lib/notifications";
|
||
|
|
|
||
|
|
export async function GET(request: NextRequest) {
|
||
|
|
try {
|
||
|
|
const { searchParams } = new URL(request.url);
|
||
|
|
const unreadOnly = searchParams.get("unreadOnly") === "true";
|
||
|
|
const limit = Math.min(100, parseInt(searchParams.get("limit") || "50"));
|
||
|
|
|
||
|
|
const [notifications, unreadCount] = await Promise.all([
|
||
|
|
getNotifications({ unreadOnly, limit }),
|
||
|
|
getUnreadCount(),
|
||
|
|
]);
|
||
|
|
|
||
|
|
return NextResponse.json({ notifications, unreadCount });
|
||
|
|
} catch (error) {
|
||
|
|
console.error("[notifications GET]", error);
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: "Failed to fetch notifications" },
|
||
|
|
{ status: 500 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function PUT(request: NextRequest) {
|
||
|
|
try {
|
||
|
|
const body = await request.json().catch(() => ({}));
|
||
|
|
const action = body.action as string;
|
||
|
|
|
||
|
|
if (action === "mark_read" && body.id) {
|
||
|
|
await markRead(body.id);
|
||
|
|
} else if (action === "mark_all_read") {
|
||
|
|
await markAllRead();
|
||
|
|
} else if (action === "dismiss" && body.id) {
|
||
|
|
await dismissNotification(body.id);
|
||
|
|
} else {
|
||
|
|
return NextResponse.json({ error: "Invalid action" }, { status: 400 });
|
||
|
|
}
|
||
|
|
|
||
|
|
const unreadCount = await getUnreadCount();
|
||
|
|
return NextResponse.json({ ok: true, unreadCount });
|
||
|
|
} catch (error) {
|
||
|
|
console.error("[notifications PUT]", error);
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: "Failed to update notification" },
|
||
|
|
{ status: 500 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|