48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
import { z } from 'zod';
|
|
import { canCreateTeamWebsite, canCreateWebsite } from '@/validations';
|
|
import { json, unauthorized } from '@/lib/response';
|
|
import { uuid } from '@/lib/crypto';
|
|
import { parseRequest } from '@/lib/request';
|
|
import { createWebsite } from '@/queries';
|
|
|
|
export { GET } from '@/app/api/users/[userId]/websites/route';
|
|
|
|
export async function POST(request: Request) {
|
|
const schema = z.object({
|
|
name: z.string().max(100),
|
|
domain: z.string().max(500),
|
|
shareId: z.string().max(50).nullable().optional(),
|
|
teamId: z.string().nullable().optional(),
|
|
id: z.string().uuid().nullable().optional(),
|
|
});
|
|
|
|
const { auth, body, error } = await parseRequest(request, schema);
|
|
|
|
if (error) {
|
|
return error();
|
|
}
|
|
|
|
const { id, name, domain, shareId, teamId } = body;
|
|
|
|
if ((teamId && !(await canCreateTeamWebsite(auth, teamId))) || !(await canCreateWebsite(auth))) {
|
|
return unauthorized();
|
|
}
|
|
|
|
const data: any = {
|
|
id: id ?? uuid(),
|
|
createdBy: auth.user.id,
|
|
name,
|
|
domain,
|
|
shareId,
|
|
teamId,
|
|
};
|
|
|
|
if (!teamId) {
|
|
data.userId = auth.user.id;
|
|
}
|
|
|
|
const website = await createWebsite(data);
|
|
|
|
return json(website);
|
|
}
|