mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-14 21:10:34 +01:00
39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import { canViewWebsite } from 'lib/auth';
|
|
import { useAuth, useCors } from 'lib/middleware';
|
|
import { NextApiRequestQueryBody, ReportSearchFilterType, SearchFilter } from 'lib/types';
|
|
import { NextApiResponse } from 'next';
|
|
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
|
import { getReportsByWebsiteId } from 'queries';
|
|
|
|
export interface ReportsRequestQuery extends SearchFilter<ReportSearchFilterType> {
|
|
id: string;
|
|
}
|
|
|
|
export default async (
|
|
req: NextApiRequestQueryBody<ReportsRequestQuery, any>,
|
|
res: NextApiResponse,
|
|
) => {
|
|
await useCors(req, res);
|
|
await useAuth(req, res);
|
|
|
|
const { id: websiteId } = req.query;
|
|
|
|
if (req.method === 'GET') {
|
|
if (!(websiteId && (await canViewWebsite(req.auth, websiteId)))) {
|
|
return unauthorized(res);
|
|
}
|
|
|
|
const { page, filter, pageSize } = req.query;
|
|
|
|
const data = await getReportsByWebsiteId(websiteId, {
|
|
page,
|
|
filter,
|
|
pageSize: +pageSize || null,
|
|
});
|
|
|
|
return ok(res, data);
|
|
}
|
|
|
|
return methodNotAllowed(res);
|
|
};
|