mirror of
https://github.com/umami-software/umami.git
synced 2026-02-09 23:27:12 +01:00
Convert /api/users.
This commit is contained in:
parent
090abcff81
commit
baa3851fb4
61 changed files with 1064 additions and 70 deletions
71
src/app/api/websites/route.ts
Normal file
71
src/app/api/websites/route.ts
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
import { z } from 'zod';
|
||||
import { canCreateTeamWebsite, canCreateWebsite, checkAuth } from 'lib/auth';
|
||||
import { json, badRequest, unauthorized } from 'lib/response';
|
||||
import { uuid } from 'lib/crypto';
|
||||
import { checkRequest } from 'lib/request';
|
||||
import { createWebsite, getUserWebsites } from 'queries';
|
||||
import { pagingParams } from 'lib/schema';
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const schema = z.object({ ...pagingParams });
|
||||
|
||||
const { query, error } = await checkRequest(request, schema);
|
||||
|
||||
if (error) {
|
||||
return badRequest(error);
|
||||
}
|
||||
|
||||
const auth = await checkAuth(request);
|
||||
|
||||
if (!auth) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const websites = await getUserWebsites(auth.user.userId, query);
|
||||
|
||||
return json(websites);
|
||||
}
|
||||
|
||||
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(),
|
||||
teamId: z.string().nullable(),
|
||||
});
|
||||
|
||||
const { body, error } = await checkRequest(request, schema);
|
||||
|
||||
if (error) {
|
||||
return badRequest(error);
|
||||
}
|
||||
|
||||
const auth = await checkAuth(request);
|
||||
|
||||
if (!auth) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const { name, domain, shareId, teamId } = body;
|
||||
|
||||
if ((teamId && !(await canCreateTeamWebsite(auth, teamId))) || !(await canCreateWebsite(auth))) {
|
||||
return unauthorized();
|
||||
}
|
||||
|
||||
const data: any = {
|
||||
id: uuid(),
|
||||
createdBy: auth.user.userId,
|
||||
name,
|
||||
domain,
|
||||
shareId,
|
||||
teamId,
|
||||
};
|
||||
|
||||
if (!teamId) {
|
||||
data.userId = auth.user.userId;
|
||||
}
|
||||
|
||||
const website = await createWebsite(data);
|
||||
|
||||
return json(website);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue