mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-14 21:10:34 +01:00
Allow user to be added to team with role.
This commit is contained in:
parent
d85b2be5f8
commit
502facd426
@ -1,18 +1,18 @@
|
|||||||
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
|
||||||
import { allowQuery } from 'lib/auth';
|
import { allowQuery } from 'lib/auth';
|
||||||
import { UmamiApi } from 'lib/constants';
|
import { UmamiApi } from 'lib/constants';
|
||||||
import { uuid } from 'lib/crypto';
|
|
||||||
import { useAuth } from 'lib/middleware';
|
import { useAuth } from 'lib/middleware';
|
||||||
import { NextApiResponse } from 'next';
|
import { NextApiResponse } from 'next';
|
||||||
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
||||||
import { createTeamUser, deleteTeamUser, getUsersByTeamId, getTeamUser } from 'queries';
|
import { createTeamUser, deleteTeamUser, getUser, getUsersByTeamId } from 'queries';
|
||||||
|
|
||||||
export interface TeamUserRequestQuery {
|
export interface TeamUserRequestQuery {
|
||||||
id: string;
|
id: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface TeamUserRequestBody {
|
export interface TeamUserRequestBody {
|
||||||
user_id: string;
|
email: string;
|
||||||
|
role_id: string;
|
||||||
team_user_id?: string;
|
team_user_id?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,16 +39,16 @@ export default async (
|
|||||||
return unauthorized(res, 'You must be the owner of this team.');
|
return unauthorized(res, 'You must be the owner of this team.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const { user_id: userId } = req.body;
|
const { email, role_id: roleId } = req.body;
|
||||||
|
|
||||||
// Check for TeamUser
|
// Check for User
|
||||||
const teamUser = getTeamUser({ userId, teamId });
|
const user = await getUser({ username: email });
|
||||||
|
|
||||||
if (!teamUser) {
|
if (!user) {
|
||||||
return badRequest(res, 'The User already exists on this Team.');
|
return badRequest(res, 'The User does not exists.');
|
||||||
}
|
}
|
||||||
|
|
||||||
const updated = await createTeamUser({ id: uuid(), userId, teamId });
|
const updated = await createTeamUser(user.id, teamId, roleId);
|
||||||
|
|
||||||
return ok(res, updated);
|
return ok(res, updated);
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,31 @@
|
|||||||
import { Prisma, TeamUser } from '@prisma/client';
|
import { Prisma, TeamUser, UserRole } from '@prisma/client';
|
||||||
|
import { uuid } from 'lib/crypto';
|
||||||
import prisma from 'lib/prisma';
|
import prisma from 'lib/prisma';
|
||||||
|
|
||||||
export async function createTeamUser(
|
export async function createTeamUser(
|
||||||
data: Prisma.TeamUserCreateInput | Prisma.TeamUserUncheckedCreateInput,
|
userId: string,
|
||||||
): Promise<TeamUser> {
|
teamId: string,
|
||||||
return prisma.client.teamUser.create({
|
roleId: string,
|
||||||
data,
|
): Promise<[TeamUser, UserRole]> {
|
||||||
});
|
const { client } = prisma;
|
||||||
|
|
||||||
|
return client.$transaction([
|
||||||
|
client.teamUser.create({
|
||||||
|
data: {
|
||||||
|
id: uuid(),
|
||||||
|
userId,
|
||||||
|
teamId,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
client.userRole.create({
|
||||||
|
data: {
|
||||||
|
id: uuid(),
|
||||||
|
userId,
|
||||||
|
teamId,
|
||||||
|
roleId,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getTeamUser(where: Prisma.TeamUserWhereInput): Promise<TeamUser> {
|
export async function getTeamUser(where: Prisma.TeamUserWhereInput): Promise<TeamUser> {
|
||||||
|
Loading…
Reference in New Issue
Block a user