umami/queries/admin/team.ts

73 lines
1.5 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-12-07 03:36:41 +01:00
import { uuid } from 'lib/crypto';
import { ROLES } from 'lib/constants';
import { Website } from 'lib/types';
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-12-07 03:36:41 +01:00
export async function getTeamWebsites(teamId: string): Promise<Website[]> {
return prisma.client.website.findMany({
where: {
teamId,
},
orderBy: [
{
name: 'asc',
2022-11-20 09:48:13 +01:00
},
2022-12-07 03:36:41 +01:00
],
});
}
export async function createTeam(data: Prisma.TeamCreateInput): Promise<Team> {
const { id, userId } = data;
return prisma.transaction([
prisma.client.team.create({
data,
}),
prisma.client.teamUser.create({
data: {
id: uuid(),
teamId: id,
userId,
role: ROLES.teamOwner,
2022-11-20 09:48:13 +01:00
},
2022-12-07 03:36:41 +01:00
}),
]);
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-12-07 03:36:41 +01:00
data: {
...data,
updatedAt: new Date(),
},
2022-11-18 07:27:33 +01:00
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: {
2022-12-07 03:36:41 +01:00
deletedAt: new Date(),
2022-11-18 07:27:33 +01:00
},
where: {
id: teamId,
},
});
}