feat: implement notifications system with API endpoints for fetching and marking notifications as read
This commit is contained in:
73
src/app/api/notifications/route.js
Normal file
73
src/app/api/notifications/route.js
Normal file
@@ -0,0 +1,73 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { auth } from "@/lib/auth";
|
||||
import {
|
||||
getUserNotifications,
|
||||
markNotificationsAsRead,
|
||||
getUnreadNotificationCount,
|
||||
} from "@/lib/notifications";
|
||||
|
||||
export async function GET(request) {
|
||||
try {
|
||||
const session = await auth();
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const includeRead = searchParams.get("includeRead") === "true";
|
||||
const limit = parseInt(searchParams.get("limit") || "50");
|
||||
const offset = parseInt(searchParams.get("offset") || "0");
|
||||
|
||||
const notifications = await getUserNotifications(session.user.id, {
|
||||
includeRead,
|
||||
limit,
|
||||
offset,
|
||||
});
|
||||
|
||||
const unreadCount = await getUnreadNotificationCount(session.user.id);
|
||||
|
||||
return NextResponse.json({
|
||||
notifications,
|
||||
unreadCount,
|
||||
hasMore: notifications.length === limit,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error fetching notifications:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function PATCH(request) {
|
||||
try {
|
||||
const session = await auth();
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { action, notificationIds } = body;
|
||||
|
||||
if (action === "markAsRead") {
|
||||
await markNotificationsAsRead(session.user.id, notificationIds);
|
||||
const unreadCount = await getUnreadNotificationCount(session.user.id);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
unreadCount,
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({ error: "Invalid action" }, { status: 400 });
|
||||
} catch (error) {
|
||||
console.error("Error updating notifications:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
23
src/app/api/notifications/unread-count/route.js
Normal file
23
src/app/api/notifications/unread-count/route.js
Normal file
@@ -0,0 +1,23 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { getUnreadNotificationCount } from "@/lib/notifications";
|
||||
|
||||
export async function GET(request) {
|
||||
try {
|
||||
const session = await auth();
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const unreadCount = await getUnreadNotificationCount(session.user.id);
|
||||
|
||||
return NextResponse.json({ unreadCount });
|
||||
} catch (error) {
|
||||
console.error("Error fetching unread notification count:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user