umami/queries/admin/user.ts

160 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-12-07 03:36:41 +01:00
import { Prisma, Team } from '@prisma/client';
2022-11-18 07:27:33 +01:00
import cache from 'lib/cache';
import prisma from 'lib/prisma';
2022-12-07 03:36:41 +01:00
import { Website } from 'lib/types';
2022-11-18 07:27:33 +01:00
export interface User {
id: string;
username: string;
password?: string;
createdAt?: Date;
}
export async function getUser(
where: Prisma.UserWhereUniqueInput,
2022-12-07 03:36:41 +01:00
options: { includePassword?: boolean } = {},
2022-11-18 07:27:33 +01:00
): Promise<User> {
2022-12-07 03:36:41 +01:00
const { includePassword = false } = options;
2022-11-18 07:27:33 +01:00
return prisma.client.user.findUnique({
where,
select: {
id: true,
username: true,
password: includePassword,
2022-12-07 03:36:41 +01:00
role: true,
2022-11-18 07:27:33 +01:00
},
});
}
export async function getUsers(): Promise<User[]> {
return prisma.client.user.findMany({
orderBy: [
{
username: 'asc',
},
],
select: {
id: true,
username: true,
2022-12-07 03:36:41 +01:00
role: true,
2022-11-18 07:27:33 +01:00
createdAt: true,
},
});
}
2022-12-07 03:36:41 +01:00
export async function getUserTeams(userId: string): Promise<Team[]> {
return prisma.client.teamUser
.findMany({
where: {
userId,
},
include: {
team: true,
2022-11-18 07:27:33 +01:00
},
2022-12-07 03:36:41 +01:00
})
.then(data => {
return data.map(a => a.team);
});
}
export async function getUserWebsites(userId: string): Promise<Website[]> {
return prisma.client.website.findMany({
where: {
userId,
2022-11-18 07:27:33 +01:00
},
2022-12-07 03:36:41 +01:00
orderBy: [
{
name: 'asc',
},
],
});
}
export async function createUser(data: {
id: string;
username: string;
password: string;
role: string;
}): Promise<{
id: string;
username: string;
role: string;
}> {
return prisma.client.user.create({
data,
2022-11-18 07:27:33 +01:00
select: {
id: true,
username: true,
2022-12-07 03:36:41 +01:00
role: true,
2022-11-18 07:27:33 +01:00
},
});
}
export async function updateUser(
data: Prisma.UserUpdateInput,
where: Prisma.UserWhereUniqueInput,
): Promise<User> {
2022-11-18 20:22:03 +01:00
return prisma.client.user.update({
where,
data,
select: {
id: true,
username: true,
2022-12-07 03:36:41 +01:00
role: true,
2022-11-18 20:22:03 +01:00
createdAt: true,
},
});
2022-11-18 07:27:33 +01:00
}
export async function deleteUser(
userId: string,
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Prisma.BatchPayload, User]> {
const { client } = prisma;
2022-11-20 09:48:13 +01:00
const websites = await client.website.findMany({
2022-11-18 07:27:33 +01:00
where: { userId },
});
let websiteIds = [];
if (websites.length > 0) {
2022-11-20 09:48:13 +01:00
websiteIds = websites.map(a => a.id);
2022-11-18 07:27:33 +01:00
}
2022-12-07 03:36:41 +01:00
return prisma
.transaction([
2022-11-18 07:27:33 +01:00
client.websiteEvent.deleteMany({
where: { websiteId: { in: websiteIds } },
}),
client.session.deleteMany({
where: { websiteId: { in: websiteIds } },
}),
client.website.updateMany({
data: {
2022-12-07 03:36:41 +01:00
deletedAt: new Date(),
2022-11-18 07:27:33 +01:00
},
where: { id: { in: websiteIds } },
}),
client.user.update({
data: {
2022-12-07 03:36:41 +01:00
deletedAt: new Date(),
2022-11-18 07:27:33 +01:00
},
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;
});
}