-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Expand file tree
/
Copy pathroute.ts
More file actions
103 lines (82 loc) · 2.57 KB
/
route.ts
File metadata and controls
103 lines (82 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { z } from 'zod';
import { ENTITY_TYPE } from '@/lib/constants';
import { uuid } from '@/lib/crypto';
import { fetchAccount } from '@/lib/load';
import { getQueryFilters, parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { pagingParams, searchParams } from '@/lib/schema';
import { canCreateTeamWebsite, canCreateWebsite } from '@/permissions';
import { createShare, createWebsite, getWebsiteCount } from '@/queries/prisma';
import {
getAllUserWebsitesIncludingTeamOwner,
getUserWebsitesIncludingAdminOwned,
} from '@/queries/prisma/website';
const CLOUD_WEBSITE_LIMIT = 3;
export async function GET(request: Request) {
const schema = z.object({
...pagingParams,
...searchParams,
includeTeams: z.string().optional(),
});
const { auth, query, error } = await parseRequest(request, schema);
if (error) {
return error();
}
const userId = auth.user.id;
const filters = await getQueryFilters(query);
if (query.includeTeams) {
return json(await getAllUserWebsitesIncludingTeamOwner(userId, filters));
}
return json(await getUserWebsitesIncludingAdminOwned(userId, filters));
}
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.uuid().nullable().optional(),
id: z.uuid().nullable().optional(),
});
const { auth, body, error } = await parseRequest(request, schema);
if (error) {
return error();
}
const { id, name, domain, shareId, teamId } = body;
if (process.env.CLOUD_MODE && !teamId) {
const account = await fetchAccount(auth.user.id);
if (!account?.hasSubscription) {
const count = await getWebsiteCount(auth.user.id);
if (count >= CLOUD_WEBSITE_LIMIT) {
return unauthorized({ message: 'Website limit reached.' });
}
}
}
if ((teamId && !(await canCreateTeamWebsite(auth, teamId))) || !(await canCreateWebsite(auth))) {
return unauthorized();
}
const data: any = {
id: id ?? uuid(),
createdBy: auth.user.id,
name,
domain,
teamId,
};
if (!teamId) {
data.userId = auth.user.id;
}
const website = await createWebsite(data);
const share = shareId
? await createShare({
id: uuid(),
entityId: website.id,
shareType: ENTITY_TYPE.website,
name: website.name,
slug: shareId,
parameters: { overview: true, events: true },
})
: null;
return json({
...website,
shareId: share?.slug ?? null,
});
}