2023-02-02 12:30:09 +01:00
|
|
|
import { Prisma, Team, TeamWebsite } from '@prisma/client';
|
2022-12-13 04:45:38 +01:00
|
|
|
import prisma from 'lib/prisma';
|
|
|
|
import { uuid } from 'lib/crypto';
|
|
|
|
import { ROLES } from 'lib/constants';
|
|
|
|
|
|
|
|
export async function getTeam(where: Prisma.TeamWhereInput): Promise<Team> {
|
|
|
|
return prisma.client.team.findFirst({
|
|
|
|
where,
|
2023-02-02 20:59:38 +01:00
|
|
|
include: {
|
|
|
|
teamUser: true,
|
|
|
|
},
|
2022-12-13 04:45:38 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getTeams(where: Prisma.TeamWhereInput): Promise<Team[]> {
|
|
|
|
return prisma.client.team.findMany({
|
|
|
|
where,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-09 21:42:12 +01:00
|
|
|
export async function createTeam(data: Prisma.TeamCreateInput, userId: string): Promise<Team> {
|
|
|
|
const { id } = data;
|
2022-12-13 04:45:38 +01:00
|
|
|
|
|
|
|
return prisma.transaction([
|
|
|
|
prisma.client.team.create({
|
|
|
|
data,
|
|
|
|
}),
|
|
|
|
prisma.client.teamUser.create({
|
|
|
|
data: {
|
|
|
|
id: uuid(),
|
|
|
|
teamId: id,
|
|
|
|
userId,
|
|
|
|
role: ROLES.teamOwner,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function updateTeam(
|
|
|
|
data: Prisma.TeamUpdateInput,
|
|
|
|
where: Prisma.TeamWhereUniqueInput,
|
|
|
|
): Promise<Team> {
|
|
|
|
return prisma.client.team.update({
|
|
|
|
data: {
|
|
|
|
...data,
|
|
|
|
updatedAt: new Date(),
|
|
|
|
},
|
|
|
|
where,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-03 07:48:30 +01:00
|
|
|
export async function deleteTeam(
|
|
|
|
teamId: string,
|
|
|
|
): Promise<Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Team]>> {
|
2023-03-10 08:21:19 +01:00
|
|
|
const { client, transaction } = prisma;
|
2023-03-03 07:48:30 +01:00
|
|
|
|
2023-03-10 08:21:19 +01:00
|
|
|
return transaction([
|
2023-03-03 07:48:30 +01:00
|
|
|
client.teamWebsite.deleteMany({
|
|
|
|
where: {
|
2023-03-10 08:21:19 +01:00
|
|
|
teamId,
|
2023-03-03 07:48:30 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
client.teamUser.deleteMany({
|
|
|
|
where: {
|
2023-03-10 08:21:19 +01:00
|
|
|
teamId,
|
2023-03-03 07:48:30 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
client.team.delete({
|
|
|
|
where: {
|
|
|
|
id: teamId,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
]);
|
2022-12-13 04:45:38 +01:00
|
|
|
}
|