getalvi's picture
Upload 51 files
a92567e verified
Raw
History Blame Contribute Delete
938 Bytes
import { NextRequest } from 'next/server';
import { db } from '@/lib/db';
import { getAuthUser } from '@/lib/auth';
import { apiHandler, errorResponse, successResponse, paginatedResponse, getPagination } from '@/lib/api-utils';
/** GET /api/analytics/posts - Post-level analytics */
export const GET = apiHandler(async (request: NextRequest) => {
const authUser = await getAuthUser(request);
if (!authUser) return errorResponse('Authentication required', 401);
const { page, limit, skip } = getPagination(request);
const url = new URL(request.url);
const postId = url.searchParams.get('postId');
const where: Record<string, unknown> = {};
if (postId) where.postId = postId;
const [analytics, total] = await Promise.all([
db.postAnalytics.findMany({ where, skip, take: limit, orderBy: { date: 'desc' } }),
db.postAnalytics.count({ where }),
]);
return paginatedResponse(analytics, page, limit, total);
});