umami/pages/api/websites/[id]/active.ts
Brian Cao 561cde6e7e
Add admin check. (#1716)
* Add admin check.

* Fix teamId check.
2022-12-27 15:18:58 -08:00

34 lines
873 B
TypeScript

import { WebsiteActive } from 'lib/types';
import { 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 { getActiveVisitors } from 'queries';
export interface WebsiteActiveRequestQuery {
id: string;
}
export default async (
req: NextApiRequestQueryBody<WebsiteActiveRequestQuery>,
res: NextApiResponse<WebsiteActive>,
) => {
await useCors(req, res);
await useAuth(req, res);
const { id: websiteId } = req.query;
if (req.method === 'GET') {
if (await canViewWebsite(req.auth, websiteId)) {
return unauthorized(res);
}
const result = await getActiveVisitors(websiteId);
return ok(res, result);
}
return methodNotAllowed(res);
};