mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-16 02:05:04 +01:00
55 lines
1.3 KiB
TypeScript
55 lines
1.3 KiB
TypeScript
import { WebsiteMetric, NextApiRequestQueryBody } from 'lib/types';
|
|
import { canViewWebsite } from 'lib/auth';
|
|
import { useAuth, useCors } from 'lib/middleware';
|
|
import { NextApiResponse } from 'next';
|
|
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
|
import { getEventData } from 'queries';
|
|
|
|
export interface WebsiteEventDataRequestQuery {
|
|
id: string;
|
|
}
|
|
|
|
export interface WebsiteEventDataRequestBody {
|
|
startAt: string;
|
|
endAt: string;
|
|
eventName: string;
|
|
columns: { [key: string]: 'count' | 'max' | 'min' | 'avg' | 'sum' };
|
|
filters?: { [key: string]: any };
|
|
}
|
|
|
|
export default async (
|
|
req: NextApiRequestQueryBody<WebsiteEventDataRequestQuery, WebsiteEventDataRequestBody>,
|
|
res: NextApiResponse<WebsiteMetric>,
|
|
) => {
|
|
await useCors(req, res);
|
|
await useAuth(req, res);
|
|
|
|
const {
|
|
user: { id: userId },
|
|
} = req.auth;
|
|
const { id: websiteId } = req.query;
|
|
|
|
if (req.method === 'POST') {
|
|
if (canViewWebsite(userId, websiteId)) {
|
|
return unauthorized(res);
|
|
}
|
|
|
|
const { startAt, endAt, event_name: eventName, columns, filters } = req.body;
|
|
|
|
const startDate = new Date(+startAt);
|
|
const endDate = new Date(+endAt);
|
|
|
|
const events = await getEventData(websiteId, {
|
|
startDate,
|
|
endDate,
|
|
eventName,
|
|
columns,
|
|
filters,
|
|
});
|
|
|
|
return ok(res, events);
|
|
}
|
|
|
|
return methodNotAllowed(res);
|
|
};
|