mirror of
https://github.com/kremalicious/umami.git
synced 2024-06-30 13:41:50 +02:00
Add some api/team endpoints.
This commit is contained in:
parent
f5eb974d8d
commit
25279271ce
|
@ -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);
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
import { Prisma, Role, Team, TeamUser } from '@prisma/client';
|
||||
import { Prisma, Team, TeamUser } from '@prisma/client';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createTeam(data: Prisma.RoleCreateInput): Promise<Role> {
|
||||
export async function createTeam(data: Prisma.TeamCreateInput): Promise<Team> {
|
||||
return prisma.client.role.create({
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getTeam(where: Prisma.RoleWhereUniqueInput): Promise<Role> {
|
||||
export async function getTeam(where: Prisma.TeamWhereUniqueInput): Promise<Team> {
|
||||
return prisma.client.role.findUnique({
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function getTeams(where: Prisma.RoleWhereInput): Promise<Role[]> {
|
||||
export async function getTeams(where: Prisma.TeamWhereInput): Promise<Team[]> {
|
||||
return prisma.client.role.findMany({
|
||||
where,
|
||||
});
|
||||
|
@ -35,16 +35,16 @@ export async function getTeamsByUserId(userId: string): Promise<
|
|||
}
|
||||
|
||||
export async function updateTeam(
|
||||
data: Prisma.RoleUpdateInput,
|
||||
where: Prisma.RoleWhereUniqueInput,
|
||||
): Promise<Role> {
|
||||
data: Prisma.TeamUpdateInput,
|
||||
where: Prisma.TeamWhereUniqueInput,
|
||||
): Promise<Team> {
|
||||
return prisma.client.role.update({
|
||||
data,
|
||||
where,
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteTeam(teamId: string): Promise<Role> {
|
||||
export async function deleteTeam(teamId: string): Promise<Team> {
|
||||
return prisma.client.role.update({
|
||||
data: {
|
||||
isDeleted: true,
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
import { Prisma, TeamUser } from '@prisma/client';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createTeamUser(data: Prisma.TeamUserCreateInput): Promise<TeamUser> {
|
||||
export async function createTeamUser(
|
||||
data: Prisma.TeamUserCreateInput | Prisma.TeamUserUncheckedCreateInput,
|
||||
): Promise<TeamUser> {
|
||||
return prisma.client.teamUser.create({
|
||||
data,
|
||||
});
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
import { Prisma, TeamWebsite } from '@prisma/client';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createTeamWebsite(data: Prisma.TeamWebsiteCreateInput): Promise<TeamWebsite> {
|
||||
export async function createTeamWebsite(
|
||||
data: Prisma.TeamWebsiteCreateInput | Prisma.TeamWebsiteUncheckedCreateInput,
|
||||
): Promise<TeamWebsite> {
|
||||
return prisma.client.teamWebsite.create({
|
||||
data,
|
||||
});
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
import { Prisma, UserRole } from '@prisma/client';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createUserRole(data: Prisma.UserRoleCreateInput): Promise<UserRole> {
|
||||
export async function createUserRole(
|
||||
data: Prisma.UserRoleCreateInput | Prisma.UserRoleUncheckedCreateInput,
|
||||
): Promise<UserRole> {
|
||||
return prisma.client.userRole.create({
|
||||
data,
|
||||
});
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
import { Prisma, UserWebsite } from '@prisma/client';
|
||||
import prisma from 'lib/prisma';
|
||||
|
||||
export async function createUserWebsite(data: Prisma.UserWebsiteCreateInput): Promise<UserWebsite> {
|
||||
export async function createUserWebsite(
|
||||
data: Prisma.UserWebsiteCreateInput | Prisma.UserWebsiteUncheckedCreateInput,
|
||||
): Promise<UserWebsite> {
|
||||
return prisma.client.userWebsite.create({
|
||||
data,
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user