mirror of
https://github.com/umami-software/umami.git
synced 2025-12-08 05:12:36 +01:00
Add some api/team endpoints.
This commit is contained in:
parent
f5eb974d8d
commit
25279271ce
10 changed files with 172 additions and 43 deletions
|
|
@ -1,17 +1,17 @@
|
|||
import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { getTeam, deleteTeam, updateTeam } from 'queries';
|
||||
import { Team } from '@prisma/client';
|
||||
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
||||
import { useAuth } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
||||
import { Team } from '@prisma/client';
|
||||
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||
import { deleteTeam, getTeam, updateTeam } from 'queries';
|
||||
|
||||
export interface TeamRequestQuery {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface TeamRequestBody {
|
||||
username: string;
|
||||
password: string;
|
||||
name?: string;
|
||||
is_deleted?: boolean;
|
||||
}
|
||||
|
||||
export default async (
|
||||
|
|
@ -36,35 +36,13 @@ export default async (
|
|||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const { username, password } = req.body;
|
||||
const { name, is_deleted: isDeleted } = req.body;
|
||||
|
||||
if (id !== userId && !isAdmin) {
|
||||
return unauthorized(res);
|
||||
}
|
||||
|
||||
const user = await getTeam({ id });
|
||||
|
||||
const data: any = {};
|
||||
|
||||
if (password) {
|
||||
data.password = hashPassword(password);
|
||||
}
|
||||
|
||||
// Only admin can change these fields
|
||||
if (isAdmin) {
|
||||
data.username = username;
|
||||
}
|
||||
|
||||
// Check when username changes
|
||||
if (data.username && user.username !== data.username) {
|
||||
const userByTeamname = await getTeam({ username });
|
||||
|
||||
if (userByTeamname) {
|
||||
return badRequest(res, 'Team already exists');
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await updateTeam(data, { id });
|
||||
const updated = await updateTeam({ name, isDeleted }, { id });
|
||||
|
||||
return ok(res, updated);
|
||||
}
|
||||
|
|
|
|||
48
pages/api/teams/[id]/user.ts
Normal file
48
pages/api/teams/[id]/user.ts
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
||||
import { uuid } from 'lib/crypto';
|
||||
import { useAuth } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok } from 'next-basics';
|
||||
import { createTeamUser, deleteTeamUser, getUsersByTeamId } from 'queries';
|
||||
|
||||
export interface TeamUserRequestQuery {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface TeamUserRequestBody {
|
||||
user_id: string;
|
||||
team_user_id?: string;
|
||||
}
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<TeamUserRequestQuery, TeamUserRequestBody>,
|
||||
res: NextApiResponse,
|
||||
) => {
|
||||
await useAuth(req, res);
|
||||
|
||||
const { id: teamId } = req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
const user = await getUsersByTeamId({ teamId });
|
||||
|
||||
return ok(res, user);
|
||||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const { user_id: userId } = req.body;
|
||||
|
||||
const updated = await createTeamUser({ id: uuid(), userId, teamId });
|
||||
|
||||
return ok(res, updated);
|
||||
}
|
||||
|
||||
if (req.method === 'DELETE') {
|
||||
const { team_user_id } = req.body;
|
||||
|
||||
await deleteTeamUser(team_user_id);
|
||||
|
||||
return ok(res);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
48
pages/api/teams/[id]/website.ts
Normal file
48
pages/api/teams/[id]/website.ts
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
||||
import { uuid } from 'lib/crypto';
|
||||
import { useAuth } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { methodNotAllowed, ok } from 'next-basics';
|
||||
import { createTeamWebsite, deleteTeamWebsite, getWebsitesByTeamId } from 'queries';
|
||||
|
||||
export interface TeamWebsiteRequestQuery {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface TeamWebsiteRequestBody {
|
||||
website_id: string;
|
||||
team_website_id?: string;
|
||||
}
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<TeamWebsiteRequestQuery, TeamWebsiteRequestBody>,
|
||||
res: NextApiResponse,
|
||||
) => {
|
||||
await useAuth(req, res);
|
||||
|
||||
const { id: teamId } = req.query;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
const website = await getWebsitesByTeamId({ teamId });
|
||||
|
||||
return ok(res, website);
|
||||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const { website_id: websiteId } = req.body;
|
||||
|
||||
const updated = await createTeamWebsite({ id: uuid(), websiteId, teamId });
|
||||
|
||||
return ok(res, updated);
|
||||
}
|
||||
|
||||
if (req.method === 'DELETE') {
|
||||
const { team_website_id } = req.body;
|
||||
|
||||
await deleteTeamWebsite(team_website_id);
|
||||
|
||||
return ok(res);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
import { Team } from '@prisma/client';
|
||||
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
||||
import { uuid } from 'lib/crypto';
|
||||
import { useAuth } from 'lib/middleware';
|
||||
import { NextApiResponse } from 'next';
|
||||
import { badRequest, methodNotAllowed, ok } from 'next-basics';
|
||||
import { createTeam, getTeam, getTeamsByUserId } from 'queries';
|
||||
export interface TeamsRequestBody {
|
||||
name: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<any, TeamsRequestBody>,
|
||||
res: NextApiResponse<Team[] | Team>,
|
||||
) => {
|
||||
await useAuth(req, res);
|
||||
|
||||
const {
|
||||
user: { id },
|
||||
} = req.auth;
|
||||
|
||||
if (req.method === 'GET') {
|
||||
const users = await getTeamsByUserId(id);
|
||||
|
||||
return ok(res, users);
|
||||
}
|
||||
|
||||
if (req.method === 'POST') {
|
||||
const { name } = req.body;
|
||||
|
||||
const user = await getTeam({ name });
|
||||
|
||||
if (user) {
|
||||
return badRequest(res, 'Team already exists');
|
||||
}
|
||||
|
||||
const created = await createTeam({
|
||||
id: id || uuid(),
|
||||
name,
|
||||
});
|
||||
|
||||
return ok(res, created);
|
||||
}
|
||||
|
||||
return methodNotAllowed(res);
|
||||
};
|
||||
|
|
@ -13,7 +13,7 @@ export interface UsersRequestBody {
|
|||
}
|
||||
|
||||
export default async (
|
||||
req: NextApiRequestQueryBody<UsersRequestBody>,
|
||||
req: NextApiRequestQueryBody<any, UsersRequestBody>,
|
||||
res: NextApiResponse<User[] | User>,
|
||||
) => {
|
||||
await useAuth(req, res);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue