umami/src/queries/admin/user.ts

236 lines
4.6 KiB
TypeScript
Raw Normal View History

2023-08-10 20:26:33 +00:00
import { Prisma } from '@prisma/client';
import cache from 'lib/cache';
import { ROLES } from 'lib/constants';
import prisma from 'lib/prisma';
2023-08-10 20:26:33 +00:00
import { FilterResult, Role, User, UserSearchFilter } from 'lib/types';
import { getRandomChars } from 'next-basics';
2024-01-30 08:10:25 +00:00
import UserFindManyArgs = Prisma.UserFindManyArgs;
2023-07-30 05:03:34 +00:00
export interface GetUserOptions {
includePassword?: boolean;
showDeleted?: boolean;
}
2024-01-30 08:10:25 +00:00
async function findUser(
criteria: Prisma.UserFindUniqueArgs,
2023-07-30 05:03:34 +00:00
options: GetUserOptions = {},
): Promise<User> {
2023-02-28 00:01:34 +00:00
const { includePassword = false, showDeleted = false } = options;
2023-11-28 18:22:24 +00:00
return prisma.client.user.findUnique({
2024-01-30 08:10:25 +00:00
...criteria,
where: {
...criteria.where,
...(showDeleted && { deletedAt: null }),
2024-01-30 08:10:25 +00:00
},
select: {
id: true,
username: true,
password: includePassword,
role: true,
2023-04-13 19:08:53 +00:00
createdAt: true,
},
});
}
2024-01-30 08:10:25 +00:00
export async function getUser(userId: string, options: GetUserOptions = {}) {
return findUser(
{
where: {
id: userId,
},
},
options,
);
2023-07-30 05:03:34 +00:00
}
export async function getUserByUsername(username: string, options: GetUserOptions = {}) {
2024-01-30 08:10:25 +00:00
return findUser({ where: { username } }, options);
2023-07-30 05:03:34 +00:00
}
2023-08-10 20:26:33 +00:00
export async function getUsers(
2024-01-30 08:10:25 +00:00
criteria: UserFindManyArgs,
filters?: UserSearchFilter,
2023-08-10 20:26:33 +00:00
): Promise<FilterResult<User[]>> {
2024-02-04 07:19:29 +00:00
const { query } = filters;
2023-08-15 17:57:25 +00:00
2023-08-10 20:26:33 +00:00
const where: Prisma.UserWhereInput = {
2024-02-04 07:19:29 +00:00
...criteria.where,
...prisma.getSearchParameters(query, [{ username: 'contains' }]),
2024-01-30 08:10:25 +00:00
deletedAt: null,
2023-08-10 20:26:33 +00:00
};
2023-08-19 04:52:59 +00:00
2024-01-30 08:10:25 +00:00
return prisma.pagedQuery(
'user',
{
...criteria,
where,
},
2023-09-06 18:37:41 +00:00
{
2024-01-30 08:10:25 +00:00
orderBy: 'createdAt',
sortDescending: true,
...filters,
2023-09-06 18:37:41 +00:00
},
);
}
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,
},
});
}
2024-02-01 06:08:48 +00:00
export async function updateUser(userId: string, data: Prisma.UserUpdateInput): Promise<User> {
return prisma.client.user.update({
2024-02-01 06:08:48 +00:00
where: {
id: userId,
},
data,
select: {
id: true,
username: true,
role: true,
createdAt: true,
},
});
}
export async function deleteUser(
userId: string,
2023-03-03 06:48:30 +00:00
): Promise<
[
Prisma.BatchPayload,
Prisma.BatchPayload,
Prisma.BatchPayload,
Prisma.BatchPayload,
Prisma.BatchPayload,
Prisma.BatchPayload,
User,
]
> {
const { client, transaction } = prisma;
2023-02-28 00:01:34 +00: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);
2024-01-26 07:20:53 +00:00
if (cloudMode) {
return transaction([
2024-01-26 07:20:53 +00:00
client.website.updateMany({
data: {
deletedAt: new Date(),
},
where: { id: { in: websiteIds } },
}),
client.user.update({
data: {
username: getRandomChars(32),
deletedAt: new Date(),
},
where: {
id: userId,
},
}),
]);
}
return transaction([
client.eventData.deleteMany({
where: { websiteId: { in: websiteIds } },
}),
client.websiteEvent.deleteMany({
where: { websiteId: { in: websiteIds } },
}),
client.session.deleteMany({
where: { websiteId: { in: websiteIds } },
}),
client.teamUser.deleteMany({
where: {
OR: [
{
teamId: {
in: teamIds,
2023-05-17 10:01:41 +00:00
},
},
{
userId,
},
],
},
}),
client.team.deleteMany({
where: {
id: {
in: teamIds,
2023-03-03 06:48:30 +00:00
},
},
}),
client.report.deleteMany({
where: {
OR: [
{
websiteId: {
in: websiteIds,
2023-05-18 20:13:18 +00:00
},
},
{
userId,
},
],
},
}),
client.website.deleteMany({
where: { id: { in: websiteIds } },
}),
client.user.delete({
where: {
id: userId,
},
}),
]).then(async (data: any) => {
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;
});
}