NeahStable/app/api/notifications/mark-all-read/route.ts
2026-01-16 00:27:07 +01:00

57 lines
1.9 KiB
TypeScript

import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from "@/app/api/auth/options";
import { NotificationRegistry } from '@/lib/services/notifications/notification-registry';
import { logger } from '@/lib/logger';
// POST /api/notifications/mark-all-read
export async function POST(request: Request) {
try {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
return NextResponse.json({ error: "Not authenticated" }, { status: 401 });
}
const registry = NotificationRegistry.getInstance();
// Reset all counts to 0 for this user
try {
const redis = await import('@/lib/redis').then(m => m.getRedisClient());
const countKey = `notifications:count:${session.user.id}`;
// Set all counts to 0
const emptyCount = {
total: 0,
unread: 0,
sources: {
email: { total: 0, unread: 0 },
rocketchat: { total: 0, unread: 0 },
leantime: { total: 0, unread: 0 },
calendar: { total: 0, unread: 0 },
},
};
await redis.set(countKey, JSON.stringify(emptyCount), 'EX', 30);
logger.debug('[NOTIFICATIONS_MARK_ALL_READ] All notifications marked as read', {
userId: session.user.id,
});
} catch (error) {
logger.error('[NOTIFICATIONS_MARK_ALL_READ] Error marking all as read', {
userId: session.user.id,
error: error instanceof Error ? error.message : String(error),
});
}
return NextResponse.json({ success: true });
} catch (error: any) {
logger.error('[NOTIFICATIONS_MARK_ALL_READ] Error', {
error: error instanceof Error ? error.message : String(error),
});
return NextResponse.json(
{ error: "Internal server error", message: error.message },
{ status: 500 }
);
}
}