umami/src/queries/admin/website.ts

227 lines
4.6 KiB
TypeScript
Raw Normal View History

import { Prisma, Website } from '@prisma/client';
import redis from '@umami/redis-client';
import prisma from 'lib/prisma';
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';
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);
}
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,
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: {
OR: [
{ userId },
{
team: {
deletedAt: null,
teamUser: {
some: {
userId,
},
},
},
},
],
2024-05-15 00:19:10 +00:00
deletedAt: null,
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,
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,
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: {
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
);
}
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,
});
}
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;
const cloudMode = !!process.env.cloudMode;
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 (cloudMode) {
await redis.client.set(`website:${websiteId}`, 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;
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 (cloudMode) {
await redis.client.del(`website:${websiteId}`);
}
return data;
});
}