2022-12-07 03:36:41 +01:00
|
|
|
import { WebsiteActive } from 'lib/types';
|
|
|
|
import { NextApiRequestQueryBody } from 'lib/types';
|
2022-12-02 05:53:37 +01:00
|
|
|
import { canViewWebsite } from 'lib/auth';
|
2022-11-19 03:49:58 +01:00
|
|
|
import { useAuth, useCors } from 'lib/middleware';
|
2022-11-15 22:21:14 +01:00
|
|
|
import { NextApiResponse } from 'next';
|
2022-11-19 03:49:58 +01:00
|
|
|
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
|
|
|
|
import { getActiveVisitors } from 'queries';
|
2020-08-18 09:51:32 +02:00
|
|
|
|
2022-11-15 22:21:14 +01:00
|
|
|
export interface WebsiteActiveRequestQuery {
|
|
|
|
id: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default async (
|
|
|
|
req: NextApiRequestQueryBody<WebsiteActiveRequestQuery>,
|
|
|
|
res: NextApiResponse<WebsiteActive>,
|
|
|
|
) => {
|
2022-10-12 22:11:44 +02:00
|
|
|
await useCors(req, res);
|
|
|
|
await useAuth(req, res);
|
2022-04-04 09:33:20 +02:00
|
|
|
|
2022-12-02 05:53:37 +01:00
|
|
|
const { id: websiteId } = req.query;
|
|
|
|
|
2022-10-12 22:11:44 +02:00
|
|
|
if (req.method === 'GET') {
|
2022-12-28 00:18:58 +01:00
|
|
|
if (await canViewWebsite(req.auth, websiteId)) {
|
2020-09-18 07:52:20 +02:00
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
2022-10-11 02:01:48 +02:00
|
|
|
const result = await getActiveVisitors(websiteId);
|
2020-09-11 22:49:43 +02:00
|
|
|
|
|
|
|
return ok(res, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
2020-08-18 09:51:32 +02:00
|
|
|
};
|