umami/src/queries/admin/website.ts

202 lines
4.1 KiB
TypeScript
Raw Normal View History

import { Prisma, Website } from '@prisma/client';
import cache from 'lib/cache';
import prisma from 'lib/prisma';
2023-08-10 20:26:33 +00:00
import { FilterResult, WebsiteSearchFilter } from 'lib/types';
2024-01-30 08:10:25 +00:00
import WebsiteFindManyArgs = Prisma.WebsiteFindManyArgs;
2024-01-30 08:10:25 +00:00
async function findWebsite(criteria: Prisma.WebsiteFindManyArgs): Promise<Website> {
return prisma.client.website.findUnique(criteria);
}
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,
2023-09-22 07:59:00 +00:00
filters: WebsiteSearchFilter,
2023-12-27 22:20:36 +00:00
): Promise<FilterResult<Website[]>> {
2024-02-03 01:49:17 +00:00
const { query } = filters;
2024-01-14 10:21:39 +00:00
const mode = prisma.getQueryMode();
2023-08-10 20:26:33 +00:00
const where: Prisma.WebsiteWhereInput = {
2024-01-30 08:10:25 +00:00
...criteria.where,
2024-02-03 01:49:17 +00:00
name: {
contains: query ? query : undefined,
mode,
},
domain: {
contains: query ? query : undefined,
mode,
},
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-01-30 08:10:25 +00:00
export async function getUserWebsites(
2023-08-10 20:26:33 +00:00
userId: string,
2023-09-22 07:59:00 +00:00
filters?: WebsiteSearchFilter,
2023-12-27 22:20:36 +00:00
): Promise<FilterResult<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,
2023-09-22 07:59:00 +00:00
filters?: WebsiteSearchFilter,
2023-12-27 22:20:36 +00:00
): Promise<FilterResult<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: {
user: {
select: {
id: true,
username: true,
},
},
},
},
2024-01-30 08:10:25 +00:00
filters,
2023-08-10 20:26:33 +00:00
);
}
export async function createWebsite(
data: Prisma.WebsiteCreateInput | Prisma.WebsiteUncheckedCreateInput,
): Promise<Website> {
return prisma.client.website
.create({
data,
})
2024-02-04 08:09:15 +00:00
.then(async (data: { id: any }) => {
if (cache.enabled) {
await cache.storeWebsite(data);
}
return data;
});
}
export async function updateWebsite(
websiteId: string,
data: Prisma.WebsiteUpdateInput | Prisma.WebsiteUncheckedUpdateInput,
): Promise<Website> {
return prisma.client.website.update({
where: {
2024-02-05 03:53:06 +00:00
id: websiteId,
},
data,
});
}
export async function resetWebsite(
websiteId: string,
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
const { client, transaction } = prisma;
return transaction([
2023-05-30 04:19:53 +00:00
client.eventData.deleteMany({
where: { websiteId },
}),
client.websiteEvent.deleteMany({
where: { websiteId },
}),
client.session.deleteMany({
where: { websiteId },
}),
client.website.update({
2024-02-05 03:53:06 +00:00
where: { id: websiteId },
data: {
resetAt: new Date(),
},
}),
]).then(async data => {
if (cache.enabled) {
2023-09-20 19:43:59 +00:00
await cache.storeWebsite(data[3]);
}
return data;
});
}
2023-02-28 00:01:34 +00:00
export async function deleteWebsite(
2024-01-19 23:06:09 +00:00
websiteId: string,
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
const { client, transaction } = prisma;
2023-02-28 00:01:34 +00:00
const cloudMode = process.env.CLOUD_MODE;
return transaction([
client.eventData.deleteMany({
where: { websiteId },
}),
client.websiteEvent.deleteMany({
where: { websiteId },
}),
client.session.deleteMany({
where: { websiteId },
}),
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
}),
]).then(async data => {
if (cache.enabled) {
await cache.deleteWebsite(websiteId);
}
return data;
});
}