mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 15:58:38 +01:00
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import { EventModel } from "@schema/metrics/EventSchema";
|
|
import { VisitModel } from "@schema/metrics/VisitSchema";
|
|
import { ProjectModel } from "@schema/project/ProjectSchema";
|
|
|
|
export default defineEventHandler(async event => {
|
|
|
|
const user = getRequestUser(event);
|
|
if (!user?.logged) return;
|
|
const project_id = getRequestProjectId(event);
|
|
if (!project_id) return;
|
|
const project = await ProjectModel.findOne({ _id: project_id });
|
|
if (!project) return;
|
|
|
|
const [hasAccess] = await hasAccessToProject(user.id, project_id, user.user.email, project)
|
|
if (!hasAccess) return;
|
|
|
|
const query = getQuery(event);
|
|
|
|
const { orderBy, order, page, limit, type } = query;
|
|
|
|
const limitValue = limit ? parseInt(limit.toString()) : 20;
|
|
const skipValue = page ? (parseInt(page.toString()) - 1) * limitValue : 0;
|
|
|
|
if (type == '0') {
|
|
const visits = await VisitModel.find({ project_id: project }, {}, {
|
|
limit: limitValue,
|
|
skip: skipValue,
|
|
sort: { [(orderBy || '').toString()]: order == 'asc' ? 1 : -1 }
|
|
});
|
|
return visits;
|
|
} else {
|
|
const events = await EventModel.find({ project_id: project }, {}, {
|
|
limit: limitValue,
|
|
skip: skipValue,
|
|
sort: { [(orderBy || '').toString()]: order == 'asc' ? 1 : -1 }
|
|
});
|
|
return events;
|
|
}
|
|
|
|
}); |