2022-12-13 03:45:38 +00:00
|
|
|
import { Prisma, Website } from '@prisma/client';
|
2024-05-09 18:55:54 +00:00
|
|
|
import redis from '@umami/redis-client';
|
2022-12-13 03:45:38 +00:00
|
|
|
import prisma from 'lib/prisma';
|
2024-04-29 05:45:58 +00:00
|
|
|
import { PageResult, PageParams } from 'lib/types';
|
2024-01-30 08:10:25 +00:00
|
|
|
import WebsiteFindManyArgs = Prisma.WebsiteFindManyArgs;
|
2024-05-13 22:43:53 +00:00
|
|
|
import { ROLES } from 'lib/constants';
|
2022-12-13 03:45:38 +00:00
|
|
|
|
2024-02-07 05:50:48 +00:00
|
|
|
async function findWebsite(criteria: Prisma.WebsiteFindUniqueArgs): Promise<Website> {
|
2024-01-30 08:10:25 +00:00
|
|
|
return prisma.client.website.findUnique(criteria);
|
2022-12-13 03:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-30 08:10:25 +00:00
|
|
|
export async function getWebsite(websiteId: string) {
|
|
|
|
|
return findWebsite({
|
|
|
|
|
where: {
|
|
|
|
|
id: websiteId,
|
|
|
|
|
},
|
|
|
|
|
});
|
2023-07-30 05:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-30 08:10:25 +00:00
|
|
|
export async function getSharedWebsite(shareId: string) {
|
|
|
|
|
return findWebsite({
|
|
|
|
|
where: {
|
|
|
|
|
shareId,
|
|
|
|
|
},
|
|
|
|
|
});
|
2023-07-30 05:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-10 20:26:33 +00:00
|
|
|
export async function getWebsites(
|
2024-01-30 08:10:25 +00:00
|
|
|
criteria: WebsiteFindManyArgs,
|
2024-04-29 05:45:58 +00:00
|
|
|
filters: PageParams,
|
|
|
|
|
): Promise<PageResult<Website[]>> {
|
2024-02-03 01:49:17 +00:00
|
|
|
const { query } = filters;
|
2023-08-10 20:26:33 +00:00
|
|
|
|
|
|
|
|
const where: Prisma.WebsiteWhereInput = {
|
2024-01-30 08:10:25 +00:00
|
|
|
...criteria.where,
|
2024-02-05 23:48:45 +00:00
|
|
|
...prisma.getSearchParameters(query, [
|
|
|
|
|
{
|
|
|
|
|
name: 'contains',
|
|
|
|
|
},
|
|
|
|
|
{ domain: 'contains' },
|
|
|
|
|
]),
|
2024-01-30 08:10:25 +00:00
|
|
|
deletedAt: null,
|
2023-08-10 20:26:33 +00:00
|
|
|
};
|
|
|
|
|
|
2024-02-03 01:49:17 +00:00
|
|
|
return prisma.pagedQuery('website', { ...criteria, where }, filters);
|
2024-01-30 08:10:25 +00:00
|
|
|
}
|
2023-08-10 20:26:33 +00:00
|
|
|
|
2024-01-30 08:10:25 +00:00
|
|
|
export async function getAllWebsites(userId: string) {
|
|
|
|
|
return prisma.client.website.findMany({
|
2023-08-10 20:26:33 +00:00
|
|
|
where: {
|
2024-01-30 08:10:25 +00:00
|
|
|
userId,
|
2023-08-10 20:26:33 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-13 22:43:53 +00:00
|
|
|
export async function getAllUserWebsitesIncludingTeamOwner(userId: string) {
|
|
|
|
|
return prisma.client.website.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
OR: [
|
|
|
|
|
{ userId },
|
|
|
|
|
{
|
|
|
|
|
team: {
|
|
|
|
|
deletedAt: null,
|
|
|
|
|
teamUser: {
|
|
|
|
|
some: {
|
|
|
|
|
role: ROLES.teamOwner,
|
|
|
|
|
userId,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-30 08:10:25 +00:00
|
|
|
export async function getUserWebsites(
|
2023-08-10 20:26:33 +00:00
|
|
|
userId: string,
|
2024-04-29 05:45:58 +00:00
|
|
|
filters?: PageParams,
|
|
|
|
|
): Promise<PageResult<Website[]>> {
|
2023-08-14 05:21:49 +00:00
|
|
|
return getWebsites(
|
|
|
|
|
{
|
2024-01-30 08:10:25 +00:00
|
|
|
where: {
|
|
|
|
|
userId,
|
|
|
|
|
},
|
2023-08-14 05:21:49 +00:00
|
|
|
include: {
|
|
|
|
|
user: {
|
|
|
|
|
select: {
|
|
|
|
|
username: true,
|
2023-08-14 05:37:04 +00:00
|
|
|
id: true,
|
2023-08-14 05:21:49 +00:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-01-30 08:10:25 +00:00
|
|
|
{
|
|
|
|
|
orderBy: 'name',
|
|
|
|
|
...filters,
|
|
|
|
|
},
|
2023-08-14 05:21:49 +00:00
|
|
|
);
|
2023-08-10 20:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-30 08:10:25 +00:00
|
|
|
export async function getTeamWebsites(
|
2023-08-10 20:26:33 +00:00
|
|
|
teamId: string,
|
2024-04-29 05:45:58 +00:00
|
|
|
filters?: PageParams,
|
|
|
|
|
): Promise<PageResult<Website[]>> {
|
2023-08-10 20:26:33 +00:00
|
|
|
return getWebsites(
|
|
|
|
|
{
|
2024-01-30 08:10:25 +00:00
|
|
|
where: {
|
|
|
|
|
teamId,
|
|
|
|
|
},
|
2023-08-10 20:26:33 +00:00
|
|
|
include: {
|
2024-02-22 23:31:32 +00:00
|
|
|
createUser: {
|
2023-08-10 20:26:33 +00:00
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
username: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-01-30 08:10:25 +00:00
|
|
|
filters,
|
2023-08-10 20:26:33 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-13 03:45:38 +00:00
|
|
|
export async function createWebsite(
|
|
|
|
|
data: Prisma.WebsiteCreateInput | Prisma.WebsiteUncheckedCreateInput,
|
|
|
|
|
): Promise<Website> {
|
2024-04-26 05:26:20 +00:00
|
|
|
return prisma.client.website.create({
|
|
|
|
|
data,
|
|
|
|
|
});
|
2022-12-13 03:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function updateWebsite(
|
2024-01-03 18:17:27 +00:00
|
|
|
websiteId: string,
|
2022-12-13 03:45:38 +00:00
|
|
|
data: Prisma.WebsiteUpdateInput | Prisma.WebsiteUncheckedUpdateInput,
|
|
|
|
|
): Promise<Website> {
|
|
|
|
|
return prisma.client.website.update({
|
|
|
|
|
where: {
|
2024-02-05 03:53:06 +00:00
|
|
|
id: websiteId,
|
2022-12-13 03:45:38 +00:00
|
|
|
},
|
|
|
|
|
data,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function resetWebsite(
|
2024-01-03 18:17:27 +00:00
|
|
|
websiteId: string,
|
2022-12-13 03:45:38 +00:00
|
|
|
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
|
|
|
|
|
const { client, transaction } = prisma;
|
2024-05-09 18:55:54 +00:00
|
|
|
const cloudMode = !!process.env.cloudMode;
|
2022-12-13 03:45:38 +00:00
|
|
|
|
|
|
|
|
return transaction([
|
2023-05-30 04:19:53 +00:00
|
|
|
client.eventData.deleteMany({
|
|
|
|
|
where: { websiteId },
|
|
|
|
|
}),
|
2022-12-13 03:45:38 +00:00
|
|
|
client.websiteEvent.deleteMany({
|
|
|
|
|
where: { websiteId },
|
|
|
|
|
}),
|
|
|
|
|
client.session.deleteMany({
|
|
|
|
|
where: { websiteId },
|
|
|
|
|
}),
|
2023-03-27 18:25:16 +00:00
|
|
|
client.website.update({
|
2024-02-05 03:53:06 +00:00
|
|
|
where: { id: websiteId },
|
2023-03-27 18:25:16 +00:00
|
|
|
data: {
|
|
|
|
|
resetAt: new Date(),
|
|
|
|
|
},
|
|
|
|
|
}),
|
2024-05-09 18:55:54 +00:00
|
|
|
]).then(async data => {
|
|
|
|
|
if (cloudMode) {
|
|
|
|
|
await redis.client.set(`website:${websiteId}`, data[3]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
});
|
2022-12-13 03:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
2023-02-28 00:01:34 +00:00
|
|
|
export async function deleteWebsite(
|
2024-01-19 23:06:09 +00:00
|
|
|
websiteId: string,
|
2022-12-13 03:45:38 +00:00
|
|
|
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
|
|
|
|
|
const { client, transaction } = prisma;
|
2024-05-09 18:55:54 +00:00
|
|
|
const cloudMode = !!process.env.CLOUD_MODE;
|
2022-12-13 03:45:38 +00:00
|
|
|
|
|
|
|
|
return transaction([
|
2023-04-07 18:40:28 +00:00
|
|
|
client.eventData.deleteMany({
|
|
|
|
|
where: { websiteId },
|
|
|
|
|
}),
|
2022-12-13 03:45:38 +00:00
|
|
|
client.websiteEvent.deleteMany({
|
|
|
|
|
where: { websiteId },
|
|
|
|
|
}),
|
|
|
|
|
client.session.deleteMany({
|
|
|
|
|
where: { websiteId },
|
|
|
|
|
}),
|
2023-05-29 04:37:34 +00:00
|
|
|
client.report.deleteMany({
|
2023-05-18 20:13:18 +00:00
|
|
|
where: {
|
|
|
|
|
websiteId,
|
|
|
|
|
},
|
|
|
|
|
}),
|
2023-02-28 00:01:34 +00:00
|
|
|
cloudMode
|
2024-01-26 07:20:53 +00:00
|
|
|
? client.website.update({
|
2023-02-28 00:01:34 +00:00
|
|
|
data: {
|
|
|
|
|
deletedAt: new Date(),
|
|
|
|
|
},
|
2024-02-05 03:53:06 +00:00
|
|
|
where: { id: websiteId },
|
2023-02-28 00:01:34 +00:00
|
|
|
})
|
|
|
|
|
: client.website.delete({
|
2024-02-05 03:53:06 +00:00
|
|
|
where: { id: websiteId },
|
2023-02-28 00:01:34 +00:00
|
|
|
}),
|
2024-05-09 18:55:54 +00:00
|
|
|
]).then(async data => {
|
|
|
|
|
if (cloudMode) {
|
|
|
|
|
await redis.client.del(`website:${websiteId}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
});
|
2022-12-13 03:45:38 +00:00
|
|
|
}
|