umami/src/pages/api/reports/index.ts

111 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-08-10 20:26:33 +00:00
import { uuid } from 'lib/crypto';
2023-08-20 05:23:15 +00:00
import { useAuth, useCors, useValidate } from 'lib/middleware';
2024-02-01 06:08:48 +00:00
import { NextApiRequestQueryBody } from 'lib/types';
2023-09-22 07:59:00 +00:00
import { pageInfo } from 'lib/schema';
2023-05-18 20:13:18 +00:00
import { NextApiResponse } from 'next';
2024-02-04 07:19:29 +00:00
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
2024-01-30 10:10:23 +00:00
import { createReport, getReports } from 'queries';
2023-08-20 05:23:15 +00:00
import * as yup from 'yup';
2024-02-22 21:47:28 +00:00
import { canUpdateWebsite, canViewTeam, canViewWebsite } from 'lib/auth';
2023-08-10 20:26:33 +00:00
export interface ReportRequestBody {
2023-05-18 20:13:18 +00:00
websiteId: string;
name: string;
type: string;
description: string;
parameters: {
2023-08-20 05:23:15 +00:00
[key: string]: any;
};
2023-05-18 20:13:18 +00:00
}
2023-08-20 05:23:15 +00:00
const schema = {
GET: yup.object().shape({
2023-09-22 07:59:00 +00:00
...pageInfo,
2023-08-20 05:23:15 +00:00
}),
POST: yup.object().shape({
websiteId: yup.string().uuid().required(),
name: yup.string().max(200).required(),
type: yup
.string()
.matches(/funnel|insights|retention/i)
.required(),
description: yup.string().max(500),
parameters: yup
.object()
.test('len', 'Must not exceed 6000 characters.', val => JSON.stringify(val).length < 6000),
}),
};
2023-05-18 20:13:18 +00:00
export default async (
req: NextApiRequestQueryBody<any, ReportRequestBody>,
2023-05-18 20:13:18 +00:00
res: NextApiResponse,
) => {
await useCors(req, res);
await useAuth(req, res);
2023-09-30 03:24:48 +00:00
await useValidate(schema, req, res);
2023-08-20 05:23:15 +00:00
2023-05-18 20:13:18 +00:00
const {
user: { id: userId },
} = req.auth;
if (req.method === 'GET') {
2024-01-30 10:10:23 +00:00
const { page, query, pageSize, websiteId, teamId } = req.query;
const filters = {
2023-08-10 20:26:33 +00:00
page,
2024-02-04 07:19:29 +00:00
pageSize,
query,
2024-01-30 10:10:23 +00:00
};
2024-02-04 07:19:29 +00:00
if (
(websiteId && !(await canViewWebsite(req.auth, websiteId))) ||
(teamId && !(await canViewTeam(req.auth, teamId)))
) {
return unauthorized(res);
}
2024-01-30 10:10:23 +00:00
const data = await getReports(
{
where: {
2024-02-04 07:19:29 +00:00
userId: !teamId && !websiteId ? userId : undefined,
websiteId,
2024-02-01 06:08:48 +00:00
website: {
teamId,
},
2024-01-30 10:10:23 +00:00
},
include: {
2024-02-01 06:08:48 +00:00
website: {
select: {
domain: true,
},
},
2024-01-30 10:10:23 +00:00
},
},
filters,
);
2023-05-18 20:13:18 +00:00
return ok(res, data);
}
if (req.method === 'POST') {
const { websiteId, type, name, description, parameters } = req.body;
2024-02-22 21:47:28 +00:00
if (!(await canUpdateWebsite(req.auth, websiteId))) {
return unauthorized(res);
}
const result = await createReport({
2023-05-18 20:13:18 +00:00
id: uuid(),
userId,
websiteId,
type,
name,
description,
parameters: JSON.stringify(parameters),
} as any);
return ok(res, result);
2023-05-18 20:13:18 +00:00
}
return methodNotAllowed(res);
};