50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { getServerSession } from 'next-auth';
|
|
import { authOptions } from "@/app/api/auth/options";
|
|
import { NotificationService } from '@/lib/services/notifications/notification-service';
|
|
|
|
// POST /api/notifications/{id}/read
|
|
export async function POST(
|
|
request: Request,
|
|
context: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
// Authenticate user
|
|
const session = await getServerSession(authOptions);
|
|
if (!session || !session.user?.id) {
|
|
return NextResponse.json(
|
|
{ error: "Not authenticated" },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// Await params as per Next.js requirements
|
|
const params = await context.params;
|
|
const id = params?.id;
|
|
if (!id) {
|
|
return NextResponse.json(
|
|
{ error: "Missing notification ID" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const userId = session.user.id;
|
|
const notificationService = NotificationService.getInstance();
|
|
const success = await notificationService.markAsRead(userId, id);
|
|
|
|
if (!success) {
|
|
return NextResponse.json(
|
|
{ error: "Failed to mark notification as read" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error: any) {
|
|
console.error('Error marking notification as read:', error);
|
|
return NextResponse.json(
|
|
{ error: "Internal server error", message: error.message },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|