Files
litlyx/dashboard/server/api/raw/events.ts
2025-11-28 16:49:20 +01:00

26 lines
991 B
TypeScript

import { EventModel } from "~/shared/schema/metrics/EventSchema";
import { parseNumberInt } from "~/utils/parseNumber";
export default defineEventHandler(async event => {
const ctx = await getRequestContext(event, 'pid', 'domain', 'range', 'permission:webAnalytics');
const { project_id, domain, from, to } = ctx;
const { page, limit } = getQuery(event);
const pageValue = parseNumberInt(page, 1);
const limitValue = parseNumberInt(limit, 10);
const skipValue = (pageValue - 1) * limitValue;
const websiteMatch = domain ? { website: domain } : {};
const count = await EventModel.countDocuments({ project_id, ...websiteMatch, created_at: { $gte: new Date(from), $lte: new Date(to) } });
const result = await EventModel.find({
project_id, ...websiteMatch, created_at: { $gte: new Date(from), $lte: new Date(to) }
}, {}, {
skip: skipValue, limit: limitValue, sort: { created_at: -1 }
})
return { count, data: result };
});