umami/pages/api/websites/[id]/active.ts

43 lines
1 KiB
TypeScript
Raw Normal View History

2023-01-02 20:26:10 +00:00
import { WebsiteActive, NextApiRequestQueryBody } from 'lib/types';
import { canViewWebsite } from 'lib/auth';
2023-08-20 05:23:15 +00:00
import { useAuth, useCors, useValidate } from 'lib/middleware';
2022-11-15 21:21:14 +00:00
import { NextApiResponse } from 'next';
2022-11-19 02:49:58 +00:00
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getActiveVisitors } from 'queries';
2023-08-20 05:23:15 +00:00
import * as yup from 'yup';
2020-08-18 07:51:32 +00:00
2022-11-15 21:21:14 +00:00
export interface WebsiteActiveRequestQuery {
id: string;
}
2023-08-20 05:23:15 +00:00
const schema = {
GET: yup.object().shape({
id: yup.string().uuid().required(),
}),
};
2022-11-15 21:21:14 +00:00
export default async (
req: NextApiRequestQueryBody<WebsiteActiveRequestQuery>,
res: NextApiResponse<WebsiteActive>,
) => {
2022-10-12 20:11:44 +00:00
await useCors(req, res);
await useAuth(req, res);
2022-04-04 07:33:20 +00:00
2023-08-20 05:23:15 +00:00
req.yup = schema;
await useValidate(req, res);
const { id: websiteId } = req.query;
2022-10-12 20:11:44 +00:00
if (req.method === 'GET') {
if (!(await canViewWebsite(req.auth, websiteId))) {
return unauthorized(res);
}
2022-10-11 00:01:48 +00:00
const result = await getActiveVisitors(websiteId);
2020-09-11 20:49:43 +00:00
return ok(res, result);
}
return methodNotAllowed(res);
2020-08-18 07:51:32 +00:00
};