2022-11-28 22:08:48 +01:00
|
|
|
import { Prisma, TeamUser } from '@prisma/client';
|
2022-11-23 07:17:49 +01:00
|
|
|
import { uuid } from 'lib/crypto';
|
2022-11-18 07:27:33 +01:00
|
|
|
import prisma from 'lib/prisma';
|
|
|
|
|
2023-03-10 08:21:19 +01:00
|
|
|
export async function getTeamUserById(teamUserId: string): Promise<TeamUser> {
|
|
|
|
return prisma.client.teamUser.findUnique({
|
|
|
|
where: {
|
|
|
|
id: teamUserId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-07 03:36:41 +01:00
|
|
|
export async function getTeamUser(teamId: string, userId: string): Promise<TeamUser> {
|
|
|
|
return prisma.client.teamUser.findFirst({
|
|
|
|
where: {
|
|
|
|
teamId,
|
|
|
|
userId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-30 05:54:54 +02:00
|
|
|
export async function getTeamUsers(
|
|
|
|
teamId: string,
|
|
|
|
): Promise<(TeamUser & { user: { id: string; username: string } })[]> {
|
2022-12-07 03:36:41 +01:00
|
|
|
return prisma.client.teamUser.findMany({
|
|
|
|
where: {
|
|
|
|
teamId,
|
|
|
|
},
|
2022-12-29 00:43:22 +01:00
|
|
|
include: {
|
2023-03-30 05:54:54 +02:00
|
|
|
user: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
username: true,
|
|
|
|
},
|
|
|
|
},
|
2022-12-29 00:43:22 +01:00
|
|
|
},
|
2022-12-07 03:36:41 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-18 09:27:42 +01:00
|
|
|
export async function createTeamUser(
|
2022-11-23 07:17:49 +01:00
|
|
|
userId: string,
|
|
|
|
teamId: string,
|
2022-12-02 05:53:37 +01:00
|
|
|
role: string,
|
2022-11-28 22:08:48 +01:00
|
|
|
): Promise<TeamUser> {
|
|
|
|
return prisma.client.teamUser.create({
|
|
|
|
data: {
|
|
|
|
id: uuid(),
|
|
|
|
userId,
|
|
|
|
teamId,
|
2022-12-02 05:53:37 +01:00
|
|
|
role,
|
2022-11-28 22:08:48 +01:00
|
|
|
},
|
|
|
|
});
|
2022-11-18 07:27:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function updateTeamUser(
|
|
|
|
data: Prisma.TeamUserUpdateInput,
|
|
|
|
where: Prisma.TeamUserWhereUniqueInput,
|
|
|
|
): Promise<TeamUser> {
|
|
|
|
return prisma.client.teamUser.update({
|
|
|
|
data,
|
|
|
|
where,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-08 07:45:46 +02:00
|
|
|
export async function deleteTeamUser(teamId: string, userId: string): Promise<TeamUser> {
|
2023-03-10 08:21:19 +01:00
|
|
|
const { client, transaction } = prisma;
|
|
|
|
|
|
|
|
return transaction([
|
|
|
|
client.teamWebsite.deleteMany({
|
|
|
|
where: {
|
2023-04-08 07:45:46 +02:00
|
|
|
teamId: teamId,
|
2023-03-10 08:21:19 +01:00
|
|
|
website: {
|
2023-04-08 07:45:46 +02:00
|
|
|
userId: userId,
|
2023-03-10 08:21:19 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
client.teamUser.deleteMany({
|
|
|
|
where: {
|
2023-04-08 07:45:46 +02:00
|
|
|
teamId,
|
|
|
|
userId,
|
2023-03-10 08:21:19 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
]);
|
2022-11-18 07:27:33 +01:00
|
|
|
}
|
2023-03-03 07:48:30 +01:00
|
|
|
|
|
|
|
export async function deleteTeamUserByUserId(
|
|
|
|
userId: string,
|
|
|
|
teamId: string,
|
|
|
|
): Promise<Prisma.BatchPayload> {
|
|
|
|
return prisma.client.teamUser.deleteMany({
|
|
|
|
where: {
|
|
|
|
userId,
|
|
|
|
teamId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|