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

82 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-02-04 08:09:15 +00:00
import { canCreateTeamWebsite, canCreateWebsite } from 'lib/auth';
2023-07-29 00:21:34 +00:00
import { uuid } from 'lib/crypto';
2023-08-20 05:23:15 +00:00
import { useAuth, useCors, useValidate } from 'lib/middleware';
import { NextApiRequestQueryBody, SearchFilter } from 'lib/types';
2022-11-15 21:21:14 +00:00
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics';
2023-12-13 05:23:12 +00:00
import { createWebsite } from 'queries';
2024-02-01 06:08:48 +00:00
import userWebsites from 'pages/api/users/[userId]/websites';
2023-08-20 05:23:15 +00:00
import * as yup from 'yup';
2023-09-22 07:59:00 +00:00
import { pageInfo } from 'lib/schema';
2022-11-15 21:21:14 +00:00
export interface WebsitesRequestQuery extends SearchFilter {}
2023-08-10 20:26:33 +00:00
2022-11-18 06:46:05 +00:00
export interface WebsitesRequestBody {
2022-11-15 21:21:14 +00:00
name: string;
domain: string;
2022-11-20 08:48:13 +00:00
shareId: string;
2024-02-04 08:09:15 +00:00
teamId: string;
2022-11-15 21:21:14 +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({
name: yup.string().max(100).required(),
domain: yup.string().max(500).required(),
2023-10-05 02:22:26 +00:00
shareId: yup.string().max(50).nullable(),
2024-02-04 08:09:15 +00:00
teamId: yup.string().nullable(),
2023-08-20 05:23:15 +00:00
}),
};
2022-11-15 21:21:14 +00:00
export default async (
2023-08-10 20:26:33 +00:00
req: NextApiRequestQueryBody<WebsitesRequestQuery, WebsitesRequestBody>,
2022-11-15 21:21:14 +00:00
res: NextApiResponse,
) => {
await useCors(req, res);
2020-08-12 05:24:41 +00:00
await useAuth(req, res);
2023-09-30 03:24:48 +00:00
await useValidate(schema, req, res);
2020-08-12 05:24:41 +00:00
2022-11-09 15:40:17 +00:00
const {
2022-11-22 00:44:42 +00:00
user: { id: userId },
2022-11-09 15:40:17 +00:00
} = req.auth;
2020-08-12 05:24:41 +00:00
if (req.method === 'GET') {
2024-02-01 06:08:48 +00:00
if (!req.query.userId) {
req.query.userId = userId;
2023-08-30 22:23:08 +00:00
}
2024-02-01 06:08:48 +00:00
return userWebsites(req, res);
2020-08-12 05:24:41 +00:00
}
if (req.method === 'POST') {
2024-02-04 08:09:15 +00:00
const { name, domain, shareId, teamId } = req.body;
2022-11-20 08:48:13 +00:00
2024-02-04 08:09:15 +00:00
if (
(teamId && !(await canCreateTeamWebsite(req.auth, teamId))) ||
!(await canCreateWebsite(req.auth))
) {
return unauthorized(res);
}
const data: any = {
2022-11-20 08:48:13 +00:00
id: uuid(),
name,
domain,
shareId,
2024-02-04 08:09:15 +00:00
teamId,
2022-11-20 08:48:13 +00:00
};
2024-02-04 08:09:15 +00:00
if (!teamId) {
data.userId = userId;
}
2022-11-20 08:48:13 +00:00
const website = await createWebsite(data);
return ok(res, website);
}
2020-08-12 05:24:41 +00:00
return methodNotAllowed(res);
};