umami/queries/admin/user.ts

279 lines
5.9 KiB
TypeScript
Raw Normal View History

2023-08-10 22:26:33 +02:00
import { Prisma } from '@prisma/client';
import cache from 'lib/cache';
2023-08-10 22:26:33 +02:00
import { ROLES, USER_FILTER_TYPES } from 'lib/constants';
import prisma from 'lib/prisma';
2023-08-10 22:26:33 +02:00
import { FilterResult, Role, User, UserSearchFilter } from 'lib/types';
import { getRandomChars } from 'next-basics';
2023-07-30 07:03:34 +02:00
export interface GetUserOptions {
includePassword?: boolean;
showDeleted?: boolean;
}
async function getUser(
2023-02-28 01:01:34 +01:00
where: Prisma.UserWhereInput | Prisma.UserWhereUniqueInput,
2023-07-30 07:03:34 +02:00
options: GetUserOptions = {},
): Promise<User> {
2023-02-28 01:01:34 +01:00
const { includePassword = false, showDeleted = false } = options;
2023-02-28 01:01:34 +01:00
return prisma.client.user.findFirst({
where: { ...where, ...(showDeleted ? {} : { deletedAt: null }) },
select: {
id: true,
username: true,
password: includePassword,
role: true,
2023-04-13 21:08:53 +02:00
createdAt: true,
},
});
}
2023-07-30 07:03:34 +02:00
export async function getUserById(userId: string, options: GetUserOptions = {}) {
return getUser({ id: userId }, options);
}
export async function getUserByUsername(username: string, options: GetUserOptions = {}) {
return getUser({ username }, options);
}
2023-08-10 22:26:33 +02:00
export async function getUsers(
UserSearchFilter: UserSearchFilter = {},
options?: { include?: Prisma.UserInclude },
): Promise<FilterResult<User[]>> {
const { teamId, filter, filterType = USER_FILTER_TYPES.all } = UserSearchFilter;
2023-08-15 19:57:25 +02:00
const mode = prisma.getSearchMode();
2023-08-10 22:26:33 +02:00
const where: Prisma.UserWhereInput = {
...(teamId && {
2023-02-02 11:54:43 +01:00
teamUser: {
2023-02-02 03:39:54 +01:00
some: {
2023-08-10 22:26:33 +02:00
teamId,
2023-02-02 03:39:54 +01:00
},
},
2023-08-10 22:26:33 +02:00
}),
...(filter && {
AND: {
OR: [
{
...((filterType === USER_FILTER_TYPES.all ||
filterType === USER_FILTER_TYPES.username) && {
username: {
startsWith: filter,
2023-08-15 19:57:25 +02:00
...mode,
2023-08-10 22:26:33 +02:00
},
}),
2023-03-30 05:54:54 +02:00
},
2023-08-10 22:26:33 +02:00
],
},
2023-08-10 22:26:33 +02:00
}),
};
const [pageFilters, getParameters] = prisma.getPageFilters({
orderBy: 'username',
...UserSearchFilter,
2023-02-02 03:39:54 +01:00
});
2023-07-27 22:20:22 +02:00
2023-08-10 22:26:33 +02:00
const users = await prisma.client.user.findMany({
where: {
2023-08-10 22:26:33 +02:00
...where,
deletedAt: null,
},
...pageFilters,
...(options?.include && { include: options.include }),
});
const count = await prisma.client.user.count({
where: {
...where,
2023-02-28 01:01:34 +01:00
deletedAt: null,
},
});
2023-08-10 22:26:33 +02:00
return { data: users as any, count, ...getParameters };
}
export async function getUsersByTeamId(teamId: string, filter?: UserSearchFilter) {
return getUsers({ teamId, ...filter });
}
export async function createUser(data: {
id: string;
username: string;
password: string;
role: Role;
}): Promise<{
id: string;
username: string;
role: string;
}> {
return prisma.client.user.create({
data,
select: {
id: true,
username: true,
role: true,
},
});
}
export async function updateUser(
data: Prisma.UserUpdateInput,
where: Prisma.UserWhereUniqueInput,
): Promise<User> {
return prisma.client.user.update({
where,
data,
select: {
id: true,
username: true,
role: true,
createdAt: true,
},
});
}
export async function deleteUser(
userId: string,
2023-03-03 07:48:30 +01:00
): Promise<
[
Prisma.BatchPayload,
Prisma.BatchPayload,
Prisma.BatchPayload,
Prisma.BatchPayload,
Prisma.BatchPayload,
Prisma.BatchPayload,
User,
]
> {
const { client } = prisma;
2023-02-28 01:01:34 +01:00
const cloudMode = process.env.CLOUD_MODE;
const websites = await client.website.findMany({
where: { userId },
});
let websiteIds = [];
if (websites.length > 0) {
websiteIds = websites.map(a => a.id);
}
const teams = await client.team.findMany({
where: {
teamUser: {
some: {
userId,
role: ROLES.teamOwner,
},
},
},
});
const teamIds = teams.map(a => a.id);
return prisma
.transaction([
client.eventData.deleteMany({
where: { websiteId: { in: websiteIds } },
}),
client.websiteEvent.deleteMany({
where: { websiteId: { in: websiteIds } },
}),
client.session.deleteMany({
where: { websiteId: { in: websiteIds } },
}),
2023-03-03 07:48:30 +01:00
client.teamWebsite.deleteMany({
where: {
OR: [
{
websiteId: {
in: websiteIds,
},
},
{
teamId: {
in: teamIds,
},
},
],
},
}),
client.teamWebsite.deleteMany({
where: {
teamId: {
in: teamIds,
2023-03-03 07:48:30 +01:00
},
},
}),
client.teamUser.deleteMany({
where: {
2023-05-17 12:01:41 +02:00
OR: [
{
teamId: {
in: teamIds,
},
},
{
userId,
},
],
2023-03-03 07:48:30 +01:00
},
}),
client.team.deleteMany({
where: {
id: {
in: teamIds,
},
2023-03-03 07:48:30 +01:00
},
}),
client.report.deleteMany({
2023-05-18 22:13:18 +02:00
where: {
OR: [
{
websiteId: {
in: websiteIds,
},
},
{
userId,
},
],
},
}),
2023-02-28 01:01:34 +01:00
cloudMode
? client.website.updateMany({
data: {
deletedAt: new Date(),
},
where: { id: { in: websiteIds } },
})
: client.website.deleteMany({
where: { id: { in: websiteIds } },
}),
cloudMode
? client.user.update({
data: {
username: getRandomChars(32),
2023-02-28 01:01:34 +01:00
deletedAt: new Date(),
},
where: {
id: userId,
},
})
: client.user.delete({
where: {
id: userId,
},
}),
])
.then(async data => {
if (cache.enabled) {
const ids = websites.map(a => a.id);
for (let i = 0; i < ids.length; i++) {
await cache.deleteWebsite(`website:${ids[i]}`);
}
}
return data;
});
}