This commit is contained in:
Mike Cao 2022-11-21 22:33:13 -08:00
commit f98a97ef83
5 changed files with 20 additions and 26 deletions

View File

@ -18,8 +18,7 @@ model User {
groupUser GroupUser[] groupUser GroupUser[]
userRole UserRole[] userRole UserRole[]
teamUser TeamUser[] teamUser TeamUser[]
Website Website? @relation(fields: [websiteId], references: [id]) Website Website[]
websiteId String? @db.Uuid
@@map("user") @@map("user")
} }
@ -52,8 +51,8 @@ model Website {
createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6) createdAt DateTime? @default(now()) @map("created_at") @db.Timestamptz(6)
isDeleted Boolean @default(false) @map("is_deleted") isDeleted Boolean @default(false) @map("is_deleted")
team Team[] team Team? @relation(fields: [teamId], references: [id])
user User[] user User? @relation(fields: [userId], references: [id])
@@index([createdAt]) @@index([createdAt])
@@index([shareId]) @@index([shareId])
@ -183,8 +182,7 @@ model Team {
teamUsers TeamUser[] teamUsers TeamUser[]
UserRole UserRole[] UserRole UserRole[]
Website Website? @relation(fields: [websiteId], references: [id]) Website Website[]
websiteId String? @db.Uuid
@@map("team") @@map("team")
} }

View File

@ -4,8 +4,8 @@ import { UmamiApi } from 'lib/constants';
import { uuid } from 'lib/crypto'; import { uuid } from 'lib/crypto';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics'; import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { createTeamUser, deleteTeamUser, getUsersByTeamId } from 'queries'; import { createTeamUser, deleteTeamUser, getUsersByTeamId, getTeamUser } from 'queries';
export interface TeamUserRequestQuery { export interface TeamUserRequestQuery {
id: string; id: string;
@ -41,6 +41,13 @@ export default async (
const { user_id: userId } = req.body; const { user_id: userId } = req.body;
// Check for TeamUser
const teamUser = getTeamUser({ userId, teamId });
if (!teamUser) {
return badRequest(res, 'The User already exists on this Team.');
}
const updated = await createTeamUser({ id: uuid(), userId, teamId }); const updated = await createTeamUser({ id: uuid(), userId, teamId });
return ok(res, updated); return ok(res, updated);
@ -50,7 +57,6 @@ export default async (
if (!(await allowQuery(req, UmamiApi.AuthType.TeamOwner))) { if (!(await allowQuery(req, UmamiApi.AuthType.TeamOwner))) {
return unauthorized(res, 'You must be the owner of this team.'); return unauthorized(res, 'You must be the owner of this team.');
} }
const { team_user_id } = req.body; const { team_user_id } = req.body;
await deleteTeamUser(team_user_id); await deleteTeamUser(team_user_id);

View File

@ -42,7 +42,6 @@ export default async (
if (req.method === 'POST') { if (req.method === 'POST') {
const { roleId, teamId } = req.body; const { roleId, teamId } = req.body;
// Check when userRolename changes
const userRole = getUserRole({ userId: id, roleId, teamId }); const userRole = getUserRole({ userId: id, roleId, teamId });
if (userRole) { if (userRole) {
@ -57,13 +56,6 @@ export default async (
if (req.method === 'DELETE') { if (req.method === 'DELETE') {
const { userRoleId } = req.body; const { userRoleId } = req.body;
// Check when userRolename changes
const userRole = getUserRole({ id: userRoleId });
if (userRole) {
return badRequest(res, 'Role already exists for User.');
}
const updated = await deleteUserRole(userRoleId); const updated = await deleteUserRole(userRoleId);
return ok(res, updated); return ok(res, updated);

View File

@ -15,8 +15,6 @@ export interface WebsiteRequestBody {
name: string; name: string;
domain: string; domain: string;
shareId: string; shareId: string;
userId?: string;
teamId?: string;
} }
export default async ( export default async (
@ -39,14 +37,10 @@ export default async (
} }
if (req.method === 'POST') { if (req.method === 'POST') {
const { ...data } = req.body; const { name, domain, shareId } = req.body;
if (!data.userId && !data.teamId) {
badRequest(res, 'A website must be assigned to a User or Team.');
}
try { try {
await updateWebsite(websiteId, data); await updateWebsite(websiteId, { name, domain, shareId });
} catch (e: any) { } catch (e: any) {
if (e.message.includes('Unique constraint') && e.message.includes('share_id')) { if (e.message.includes('Unique constraint') && e.message.includes('share_id')) {
return serverError(res, 'That share ID is already taken.'); return serverError(res, 'That share ID is already taken.');

View File

@ -5,6 +5,8 @@ import { useAuth, useCors } from 'lib/middleware';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { methodNotAllowed, ok } from 'next-basics'; import { methodNotAllowed, ok } from 'next-basics';
import { createWebsite, getAllWebsites, getWebsitesByUserId } from 'queries'; import { createWebsite, getAllWebsites, getWebsitesByUserId } from 'queries';
import { checkPermission } from 'lib/auth';
import { UmamiApi } from 'lib/constants';
export interface WebsitesRequestQuery { export interface WebsitesRequestQuery {
include_all?: boolean; include_all?: boolean;
@ -25,12 +27,14 @@ export default async (
await useAuth(req, res); await useAuth(req, res);
const { const {
user: { id: userId, isAdmin }, user: { id: userId },
} = req.auth; } = req.auth;
if (req.method === 'GET') { if (req.method === 'GET') {
const { include_all } = req.query; const { include_all } = req.query;
const isAdmin = await checkPermission(req, UmamiApi.Permission.Admin);
const websites = const websites =
isAdmin && include_all ? await getAllWebsites() : await getWebsitesByUserId(userId); isAdmin && include_all ? await getAllWebsites() : await getWebsitesByUserId(userId);