NeahNew/app/api/notifications/[id]/read/route.ts
2026-01-06 16:58:03 +01:00

79 lines
2.3 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 }> }
) {
const startTime = Date.now();
try {
console.log('[NOTIFICATION_API] Mark as read endpoint called');
// Authenticate user
const session = await getServerSession(authOptions);
if (!session || !session.user?.id) {
console.log('[NOTIFICATION_API] Mark as read - Authentication failed');
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) {
console.log('[NOTIFICATION_API] Mark as read - Missing notification ID');
return NextResponse.json(
{ error: "Missing notification ID" },
{ status: 400 }
);
}
const userId = session.user.id;
console.log('[NOTIFICATION_API] Mark as read - Processing', {
userId,
notificationId: id,
timestamp: new Date().toISOString()
});
const notificationService = NotificationService.getInstance();
const success = await notificationService.markAsRead(userId, id);
const duration = Date.now() - startTime;
if (!success) {
console.log('[NOTIFICATION_API] Mark as read - Failed', {
userId,
notificationId: id,
duration: `${duration}ms`
});
return NextResponse.json(
{ error: "Failed to mark notification as read" },
{ status: 400 }
);
}
console.log('[NOTIFICATION_API] Mark as read - Success', {
userId,
notificationId: id,
duration: `${duration}ms`
});
return NextResponse.json({ success: true });
} catch (error: any) {
const duration = Date.now() - startTime;
console.error('[NOTIFICATION_API] Mark as read - Error', {
error: error.message,
stack: error.stack,
duration: `${duration}ms`
});
return NextResponse.json(
{ error: "Internal server error", message: error.message },
{ status: 500 }
);
}
}