64 lines
2.0 KiB
TypeScript
64 lines
2.0 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';
|
|
|
|
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 body = await request.json();
|
|
const { source, count, items } = body;
|
|
|
|
// Validate request
|
|
if (!source || typeof count !== 'number' || count < 0) {
|
|
return NextResponse.json(
|
|
{ error: "Invalid request: source (string) and count (number >= 0) required" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Validate source
|
|
const validSources = ['email', 'rocketchat', 'leantime', 'calendar'];
|
|
if (!validSources.includes(source)) {
|
|
return NextResponse.json(
|
|
{ error: `Invalid source. Must be one of: ${validSources.join(', ')}` },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Validate items if provided
|
|
if (items && !Array.isArray(items)) {
|
|
return NextResponse.json(
|
|
{ error: "Invalid request: items must be an array" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const registry = NotificationRegistry.getInstance();
|
|
|
|
// Record the count and items
|
|
await registry.recordCount(session.user.id, source, count, items);
|
|
|
|
logger.debug('[NOTIFICATIONS_UPDATE] Count updated', {
|
|
userIdHash: Buffer.from(session.user.id).toString('base64').slice(0, 12),
|
|
count,
|
|
itemsCount: items?.length || 0,
|
|
});
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error: any) {
|
|
logger.error('[NOTIFICATIONS_UPDATE] Error', {
|
|
error: error instanceof Error ? error.message : String(error),
|
|
});
|
|
return NextResponse.json(
|
|
{ error: "Internal server error", message: error.message },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|