umami/queries/admin/team.ts

165 lines
3.8 KiB
TypeScript
Raw Normal View History

2023-07-29 02:21:34 +02:00
import { Prisma, Team } from '@prisma/client';
import prisma from 'lib/prisma';
2023-08-10 22:26:33 +02:00
import { ROLES, TEAM_FILTER_TYPES } from 'lib/constants';
2023-07-29 02:21:34 +02:00
import { uuid } from 'lib/crypto';
2023-08-10 22:26:33 +02:00
import { FilterResult, TeamSearchFilter, TeamSearchFilterType, SearchFilter } from 'lib/types';
2023-07-30 07:03:34 +02:00
export interface GetTeamOptions {
includeTeamUser?: boolean;
}
async function getTeam(where: Prisma.TeamWhereInput, options: GetTeamOptions = {}): Promise<Team> {
const { includeTeamUser = false } = options;
return prisma.client.team.findFirst({
where,
2023-02-02 20:59:38 +01:00
include: {
2023-07-30 07:03:34 +02:00
teamUser: includeTeamUser,
2023-02-02 20:59:38 +01:00
},
});
}
2023-07-30 07:03:34 +02:00
export function getTeamById(teamId: string, options: GetTeamOptions = {}) {
return getTeam({ id: teamId }, options);
}
export function getTeamByAccessCode(accessCode: string, options: GetTeamOptions = {}) {
return getTeam({ accessCode }, options);
}
export async function createTeam(data: Prisma.TeamCreateInput, userId: string): Promise<Team> {
const { id } = data;
return prisma.transaction([
prisma.client.team.create({
data,
}),
prisma.client.teamUser.create({
data: {
id: uuid(),
teamId: id,
userId,
role: ROLES.teamOwner,
},
}),
]);
}
2023-07-30 07:03:34 +02:00
export async function updateTeam(teamId: string, data: Prisma.TeamUpdateInput): Promise<Team> {
return prisma.client.team.update({
2023-07-30 07:03:34 +02:00
where: {
id: teamId,
},
data: {
...data,
updatedAt: new Date(),
},
});
}
2023-03-03 07:48:30 +01:00
export async function deleteTeam(
teamId: string,
): Promise<Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Team]>> {
const { client, transaction } = prisma;
2023-03-03 07:48:30 +01:00
return transaction([
2023-03-03 07:48:30 +01:00
client.teamWebsite.deleteMany({
where: {
teamId,
2023-03-03 07:48:30 +01:00
},
}),
client.teamUser.deleteMany({
where: {
teamId,
2023-03-03 07:48:30 +01:00
},
}),
client.team.delete({
where: {
id: teamId,
},
}),
]);
}
2023-08-10 22:26:33 +02:00
export async function getTeams(
TeamSearchFilter: TeamSearchFilter,
options?: { include?: Prisma.TeamInclude },
): Promise<FilterResult<Team[]>> {
const { userId, filter, filterType = TEAM_FILTER_TYPES.all } = TeamSearchFilter;
2023-08-15 19:57:25 +02:00
const mode = prisma.getSearchMode();
2023-08-10 22:26:33 +02:00
const where: Prisma.TeamWhereInput = {
...(userId && {
teamUser: {
some: { userId },
},
}),
...(filter && {
AND: {
OR: [
{
...((filterType === TEAM_FILTER_TYPES.all || filterType === TEAM_FILTER_TYPES.name) && {
2023-08-15 19:57:25 +02:00
name: { startsWith: filter, ...mode },
2023-08-10 22:26:33 +02:00
}),
},
{
...((filterType === TEAM_FILTER_TYPES.all ||
filterType === TEAM_FILTER_TYPES['user:username']) && {
teamUser: {
2023-08-11 20:37:01 +02:00
some: {
2023-08-10 22:26:33 +02:00
role: ROLES.teamOwner,
user: {
username: {
startsWith: filter,
2023-08-15 19:57:25 +02:00
...mode,
2023-08-10 22:26:33 +02:00
},
},
},
},
}),
},
],
},
}),
};
const [pageFilters, getParameters] = prisma.getPageFilters({
orderBy: 'name',
...TeamSearchFilter,
});
const teams = await prisma.client.team.findMany({
where: {
...where,
},
...pageFilters,
...(options?.include && { include: options?.include }),
});
const count = await prisma.client.team.count({ where });
return { data: teams, count, ...getParameters };
}
export async function getTeamsByUserId(
userId: string,
filter?: SearchFilter<TeamSearchFilterType>,
): Promise<FilterResult<Team[]>> {
return getTeams(
{ userId, ...filter },
{
include: {
teamUser: {
include: {
user: {
select: {
id: true,
username: true,
},
},
},
},
},
},
);
}