2023-03-30 05:54:54 +02:00
|
|
|
import { Prisma, Team, TeamUser } from '@prisma/client';
|
2023-05-06 06:16:13 +02:00
|
|
|
import { getRandomChars } from 'next-basics';
|
2022-12-13 04:45:38 +01:00
|
|
|
import cache from 'lib/cache';
|
2023-03-10 08:21:19 +01:00
|
|
|
import { ROLES } from 'lib/constants';
|
2022-12-13 04:45:38 +01:00
|
|
|
import prisma from 'lib/prisma';
|
2023-03-03 07:48:30 +01:00
|
|
|
import { Website, User, Roles } from 'lib/types';
|
2022-12-13 04:45:38 +01:00
|
|
|
|
|
|
|
export async function getUser(
|
2023-02-28 01:01:34 +01:00
|
|
|
where: Prisma.UserWhereInput | Prisma.UserWhereUniqueInput,
|
|
|
|
options: { includePassword?: boolean; showDeleted?: boolean } = {},
|
2022-12-13 04:45:38 +01:00
|
|
|
): Promise<User> {
|
2023-02-28 01:01:34 +01:00
|
|
|
const { includePassword = false, showDeleted = false } = options;
|
2022-12-13 04:45:38 +01:00
|
|
|
|
2023-02-28 01:01:34 +01:00
|
|
|
return prisma.client.user.findFirst({
|
|
|
|
where: { ...where, ...(showDeleted ? {} : { deletedAt: null }) },
|
2022-12-13 04:45:38 +01:00
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
username: true,
|
|
|
|
password: includePassword,
|
|
|
|
role: true,
|
2023-04-13 21:08:53 +02:00
|
|
|
createdAt: true,
|
2022-12-13 04:45:38 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getUsers(): Promise<User[]> {
|
|
|
|
return prisma.client.user.findMany({
|
2023-04-12 22:40:19 +02:00
|
|
|
take: 100,
|
2023-01-25 16:42:46 +01:00
|
|
|
where: {
|
|
|
|
deletedAt: null,
|
|
|
|
},
|
2022-12-13 04:45:38 +01:00
|
|
|
orderBy: [
|
|
|
|
{
|
|
|
|
username: 'asc',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
username: true,
|
|
|
|
role: true,
|
|
|
|
createdAt: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-30 05:54:54 +02:00
|
|
|
export async function getUserTeams(userId: string): Promise<
|
|
|
|
(Team & {
|
|
|
|
teamUser: (TeamUser & {
|
|
|
|
user: { id: string; username: string };
|
|
|
|
})[];
|
|
|
|
})[]
|
|
|
|
> {
|
2023-02-02 03:39:54 +01:00
|
|
|
return prisma.client.team.findMany({
|
|
|
|
where: {
|
2023-02-02 11:54:43 +01:00
|
|
|
teamUser: {
|
2023-02-02 03:39:54 +01:00
|
|
|
some: {
|
|
|
|
userId,
|
|
|
|
},
|
2022-12-13 04:45:38 +01:00
|
|
|
},
|
2023-02-02 03:39:54 +01:00
|
|
|
},
|
|
|
|
include: {
|
2023-02-02 11:54:43 +01:00
|
|
|
teamUser: {
|
2023-02-02 03:39:54 +01:00
|
|
|
include: {
|
2023-03-30 05:54:54 +02:00
|
|
|
user: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
username: true,
|
|
|
|
},
|
|
|
|
},
|
2023-02-02 03:39:54 +01:00
|
|
|
},
|
2022-12-13 04:45:38 +01:00
|
|
|
},
|
2023-02-02 03:39:54 +01:00
|
|
|
},
|
|
|
|
});
|
2022-12-13 04:45:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getUserWebsites(userId: string): Promise<Website[]> {
|
|
|
|
return prisma.client.website.findMany({
|
|
|
|
where: {
|
|
|
|
userId,
|
2023-02-28 01:01:34 +01:00
|
|
|
deletedAt: null,
|
2022-12-13 04:45:38 +01:00
|
|
|
},
|
|
|
|
orderBy: [
|
|
|
|
{
|
|
|
|
name: 'asc',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createUser(data: {
|
|
|
|
id: string;
|
|
|
|
username: string;
|
|
|
|
password: string;
|
2023-03-03 07:48:30 +01:00
|
|
|
role: Roles;
|
2022-12-13 04:45:38 +01:00
|
|
|
}): 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,
|
|
|
|
]
|
|
|
|
> {
|
2022-12-13 04:45:38 +01:00
|
|
|
const { client } = prisma;
|
2023-02-28 01:01:34 +01:00
|
|
|
const cloudMode = process.env.CLOUD_MODE;
|
2022-12-13 04:45:38 +01:00
|
|
|
|
|
|
|
const websites = await client.website.findMany({
|
|
|
|
where: { userId },
|
|
|
|
});
|
|
|
|
|
|
|
|
let websiteIds = [];
|
|
|
|
|
|
|
|
if (websites.length > 0) {
|
|
|
|
websiteIds = websites.map(a => a.id);
|
|
|
|
}
|
|
|
|
|
2023-03-10 08:21:19 +01:00
|
|
|
const teams = await client.team.findMany({
|
|
|
|
where: {
|
|
|
|
teamUser: {
|
|
|
|
some: {
|
|
|
|
userId,
|
|
|
|
role: ROLES.teamOwner,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const teamIds = teams.map(a => a.id);
|
|
|
|
|
2022-12-13 04:45:38 +01:00
|
|
|
return prisma
|
|
|
|
.transaction([
|
2023-04-07 20:40:28 +02:00
|
|
|
client.eventData.deleteMany({
|
|
|
|
where: { websiteId: { in: websiteIds } },
|
|
|
|
}),
|
2022-12-13 04:45:38 +01:00
|
|
|
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: {
|
2023-03-10 08:21:19 +01:00
|
|
|
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-03-10 08:21:19 +01:00
|
|
|
teamId: {
|
|
|
|
in: teamIds,
|
2023-03-03 07:48:30 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
client.team.deleteMany({
|
|
|
|
where: {
|
2023-03-10 08:21:19 +01:00
|
|
|
id: {
|
|
|
|
in: teamIds,
|
|
|
|
},
|
2023-03-03 07:48:30 +01:00
|
|
|
},
|
|
|
|
}),
|
2023-05-29 06:37:34 +02: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: {
|
2023-05-06 06:16:13 +02:00
|
|
|
username: getRandomChars(32),
|
2023-02-28 01:01:34 +01:00
|
|
|
deletedAt: new Date(),
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
id: userId,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
: client.user.delete({
|
|
|
|
where: {
|
|
|
|
id: userId,
|
|
|
|
},
|
|
|
|
}),
|
2022-12-13 04:45:38 +01:00
|
|
|
])
|
|
|
|
.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;
|
|
|
|
});
|
|
|
|
}
|