umami/queries/admin/team.ts

60 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-11-20 09:48:13 +01:00
import { Prisma, Team } from '@prisma/client';
2022-11-18 07:27:33 +01:00
import prisma from 'lib/prisma';
2022-11-20 09:48:13 +01:00
export async function createTeam(
data: Prisma.TeamCreateInput,
searchDeleted = false,
): Promise<Team> {
return prisma.client.team.create({
data: { ...data, isDeleted: searchDeleted ? null : false },
2022-11-18 07:27:33 +01:00
});
}
2022-11-20 09:48:13 +01:00
export async function getTeam(where: Prisma.TeamWhereInput): Promise<Team> {
return prisma.client.team.findFirst({
2022-11-18 07:27:33 +01:00
where,
});
}
2022-11-18 09:27:42 +01:00
export async function getTeams(where: Prisma.TeamWhereInput): Promise<Team[]> {
2022-11-20 09:48:13 +01:00
return prisma.client.team.findMany({
2022-11-18 07:27:33 +01:00
where,
});
}
2022-11-20 09:48:13 +01:00
export async function getTeamsByUserId(userId: string): Promise<Team[]> {
return prisma.client.teamUser
.findMany({
where: {
userId,
},
include: {
team: true,
},
})
.then(data => {
return data.map(a => a.team);
});
2022-11-18 07:27:33 +01:00
}
export async function updateTeam(
2022-11-18 09:27:42 +01:00
data: Prisma.TeamUpdateInput,
where: Prisma.TeamWhereUniqueInput,
): Promise<Team> {
2022-11-20 09:48:13 +01:00
return prisma.client.team.update({
2022-11-18 07:27:33 +01:00
data,
where,
});
}
2022-11-18 09:27:42 +01:00
export async function deleteTeam(teamId: string): Promise<Team> {
2022-11-20 09:48:13 +01:00
return prisma.client.team.update({
2022-11-18 07:27:33 +01:00
data: {
isDeleted: true,
},
where: {
id: teamId,
},
});
}