2022-12-13 04:45:38 +01:00
|
|
|
import { Prisma, Website } from '@prisma/client';
|
|
|
|
import cache from 'lib/cache';
|
2023-08-10 22:26:33 +02:00
|
|
|
import { ROLES, WEBSITE_FILTER_TYPES } from 'lib/constants';
|
2022-12-13 04:45:38 +01:00
|
|
|
import prisma from 'lib/prisma';
|
2023-08-10 22:26:33 +02:00
|
|
|
import { FilterResult, WebsiteSearchFilter } from 'lib/types';
|
2022-12-13 04:45:38 +01:00
|
|
|
|
2023-07-30 07:03:34 +02:00
|
|
|
async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> {
|
2022-12-13 04:45:38 +01:00
|
|
|
return prisma.client.website.findUnique({
|
|
|
|
where,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-30 07:03:34 +02:00
|
|
|
export async function getWebsiteById(id: string) {
|
|
|
|
return getWebsite({ id });
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getWebsiteByShareId(shareId: string) {
|
|
|
|
return getWebsite({ shareId });
|
|
|
|
}
|
|
|
|
|
2023-08-10 22:26:33 +02:00
|
|
|
export async function getWebsites(
|
|
|
|
WebsiteSearchFilter: WebsiteSearchFilter,
|
|
|
|
options?: { include?: Prisma.WebsiteInclude },
|
|
|
|
): Promise<FilterResult<Website[]>> {
|
|
|
|
const {
|
|
|
|
userId,
|
|
|
|
teamId,
|
|
|
|
includeTeams,
|
2023-08-14 07:21:49 +02:00
|
|
|
onlyTeams,
|
2023-08-10 22:26:33 +02:00
|
|
|
filter,
|
|
|
|
filterType = WEBSITE_FILTER_TYPES.all,
|
|
|
|
} = WebsiteSearchFilter;
|
|
|
|
|
|
|
|
const where: Prisma.WebsiteWhereInput = {
|
|
|
|
...(teamId && {
|
|
|
|
teamWebsite: {
|
|
|
|
some: {
|
|
|
|
teamId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2023-08-11 20:37:01 +02:00
|
|
|
AND: [
|
|
|
|
{
|
|
|
|
OR: [
|
|
|
|
{
|
2023-08-14 07:21:49 +02:00
|
|
|
...(userId &&
|
|
|
|
!onlyTeams && {
|
|
|
|
userId,
|
|
|
|
}),
|
2023-08-11 20:37:01 +02:00
|
|
|
},
|
|
|
|
{
|
2023-08-14 07:21:49 +02:00
|
|
|
...((includeTeams || onlyTeams) && {
|
|
|
|
AND: [
|
|
|
|
{
|
|
|
|
teamWebsite: {
|
|
|
|
some: {
|
|
|
|
team: {
|
|
|
|
teamUser: {
|
|
|
|
some: {
|
|
|
|
userId,
|
|
|
|
},
|
|
|
|
},
|
2023-08-11 20:37:01 +02:00
|
|
|
},
|
2023-08-10 22:26:33 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-08-14 07:21:49 +02:00
|
|
|
{
|
|
|
|
userId: {
|
|
|
|
not: userId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
OR: [
|
|
|
|
{
|
|
|
|
...((filterType === WEBSITE_FILTER_TYPES.all ||
|
|
|
|
filterType === WEBSITE_FILTER_TYPES.name) && {
|
|
|
|
name: { startsWith: filter, mode: 'insensitive' },
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...((filterType === WEBSITE_FILTER_TYPES.all ||
|
|
|
|
filterType === WEBSITE_FILTER_TYPES.domain) && {
|
|
|
|
domain: { startsWith: filter, mode: 'insensitive' },
|
2023-08-11 20:37:01 +02:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2023-08-10 22:26:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const [pageFilters, getParameters] = prisma.getPageFilters({
|
|
|
|
orderBy: 'name',
|
|
|
|
...WebsiteSearchFilter,
|
|
|
|
});
|
|
|
|
|
|
|
|
const websites = await prisma.client.website.findMany({
|
|
|
|
where: {
|
|
|
|
...where,
|
|
|
|
deletedAt: null,
|
|
|
|
},
|
|
|
|
...pageFilters,
|
|
|
|
...(options?.include && { include: options.include }),
|
|
|
|
});
|
|
|
|
const count = await prisma.client.website.count({ where });
|
|
|
|
|
|
|
|
return { data: websites, count, ...getParameters };
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getWebsitesByUserId(
|
|
|
|
userId: string,
|
|
|
|
filter?: WebsiteSearchFilter,
|
|
|
|
): Promise<FilterResult<Website[]>> {
|
2023-08-14 07:21:49 +02:00
|
|
|
return getWebsites(
|
|
|
|
{ userId, ...filter },
|
|
|
|
{
|
|
|
|
include: {
|
|
|
|
teamWebsite: {
|
|
|
|
include: {
|
|
|
|
team: {
|
|
|
|
select: {
|
|
|
|
name: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
select: {
|
|
|
|
username: true,
|
2023-08-14 07:37:04 +02:00
|
|
|
id: true,
|
2023-08-14 07:21:49 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
2023-08-10 22:26:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getWebsitesByTeamId(
|
|
|
|
teamId: string,
|
|
|
|
filter?: WebsiteSearchFilter,
|
|
|
|
): Promise<FilterResult<Website[]>> {
|
|
|
|
return getWebsites(
|
|
|
|
{
|
|
|
|
teamId,
|
|
|
|
...filter,
|
|
|
|
includeTeams: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
include: {
|
|
|
|
teamWebsite: {
|
|
|
|
include: {
|
|
|
|
team: {
|
|
|
|
include: {
|
|
|
|
teamUser: {
|
|
|
|
where: { role: ROLES.teamOwner },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
username: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getUserWebsites(
|
|
|
|
userId: string,
|
|
|
|
options?: { includeTeams: boolean },
|
|
|
|
): Promise<Website[]> {
|
|
|
|
const { rawQuery } = prisma;
|
|
|
|
|
|
|
|
if (options?.includeTeams) {
|
|
|
|
const websites = await rawQuery(
|
|
|
|
`
|
|
|
|
select
|
|
|
|
website_id as "id",
|
|
|
|
name,
|
|
|
|
domain,
|
|
|
|
share_id as "shareId",
|
|
|
|
reset_at as "resetAt",
|
|
|
|
user_id as "userId",
|
|
|
|
created_at as "createdAt",
|
|
|
|
updated_at as "updatedAt",
|
|
|
|
deleted_at as "deletedAt",
|
|
|
|
null as "teamId",
|
|
|
|
null as "teamName"
|
|
|
|
from website
|
|
|
|
where user_id = {{userId::uuid}}
|
|
|
|
and deleted_at is null
|
|
|
|
union
|
|
|
|
select
|
|
|
|
w.website_id as "id",
|
|
|
|
w.name,
|
|
|
|
w.domain,
|
|
|
|
w.share_id as "shareId",
|
|
|
|
w.reset_at as "resetAt",
|
|
|
|
w.user_id as "userId",
|
|
|
|
w.created_at as "createdAt",
|
|
|
|
w.updated_at as "updatedAt",
|
|
|
|
w.deleted_at as "deletedAt",
|
|
|
|
t.team_id as "teamId",
|
|
|
|
t.name as "teamName"
|
|
|
|
from website w
|
|
|
|
inner join team_website tw
|
|
|
|
on tw.website_id = w.website_id
|
|
|
|
inner join team t
|
|
|
|
on t.team_id = tw.team_id
|
|
|
|
inner join team_user tu
|
|
|
|
on tu.team_id = tw.team_id
|
|
|
|
where tu.user_id = {{userId::uuid}}
|
|
|
|
and w.deleted_at is null
|
|
|
|
`,
|
|
|
|
{ userId },
|
|
|
|
);
|
|
|
|
|
|
|
|
return websites.reduce((arr, item) => {
|
|
|
|
if (!arr.find(({ id }) => id === item.id)) {
|
|
|
|
return arr.concat(item);
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
2022-12-13 04:45:38 +01:00
|
|
|
return prisma.client.website.findMany({
|
2023-08-10 22:26:33 +02:00
|
|
|
where: {
|
|
|
|
userId,
|
|
|
|
deletedAt: null,
|
2022-12-13 04:45:38 +01:00
|
|
|
},
|
2023-08-10 22:26:33 +02:00
|
|
|
orderBy: [
|
|
|
|
{
|
|
|
|
name: 'asc',
|
|
|
|
},
|
|
|
|
],
|
2022-12-13 04:45:38 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createWebsite(
|
|
|
|
data: Prisma.WebsiteCreateInput | Prisma.WebsiteUncheckedCreateInput,
|
|
|
|
): Promise<Website> {
|
|
|
|
return prisma.client.website
|
|
|
|
.create({
|
|
|
|
data,
|
|
|
|
})
|
|
|
|
.then(async data => {
|
|
|
|
if (cache.enabled) {
|
|
|
|
await cache.storeWebsite(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function updateWebsite(
|
|
|
|
websiteId,
|
|
|
|
data: Prisma.WebsiteUpdateInput | Prisma.WebsiteUncheckedUpdateInput,
|
|
|
|
): Promise<Website> {
|
|
|
|
return prisma.client.website.update({
|
|
|
|
where: {
|
|
|
|
id: websiteId,
|
|
|
|
},
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function resetWebsite(
|
|
|
|
websiteId,
|
|
|
|
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
|
|
|
|
const { client, transaction } = prisma;
|
|
|
|
|
|
|
|
return transaction([
|
2023-05-30 06:19:53 +02:00
|
|
|
client.eventData.deleteMany({
|
|
|
|
where: { websiteId },
|
|
|
|
}),
|
2022-12-13 04:45:38 +01:00
|
|
|
client.websiteEvent.deleteMany({
|
|
|
|
where: { websiteId },
|
|
|
|
}),
|
|
|
|
client.session.deleteMany({
|
|
|
|
where: { websiteId },
|
|
|
|
}),
|
2023-03-27 20:25:16 +02:00
|
|
|
client.website.update({
|
|
|
|
where: { id: websiteId },
|
|
|
|
data: {
|
|
|
|
resetAt: new Date(),
|
|
|
|
},
|
|
|
|
}),
|
2022-12-13 04:45:38 +01:00
|
|
|
]).then(async data => {
|
|
|
|
if (cache.enabled) {
|
|
|
|
await cache.storeWebsite(data[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-28 01:01:34 +01:00
|
|
|
export async function deleteWebsite(
|
2022-12-13 04:45:38 +01:00
|
|
|
websiteId,
|
|
|
|
): Promise<[Prisma.BatchPayload, Prisma.BatchPayload, Website]> {
|
|
|
|
const { client, transaction } = prisma;
|
2023-02-28 01:01:34 +01:00
|
|
|
const cloudMode = process.env.CLOUD_MODE;
|
2022-12-13 04:45:38 +01:00
|
|
|
|
|
|
|
return transaction([
|
2023-04-07 20:40:28 +02:00
|
|
|
client.eventData.deleteMany({
|
|
|
|
where: { websiteId },
|
|
|
|
}),
|
2022-12-13 04:45:38 +01:00
|
|
|
client.websiteEvent.deleteMany({
|
|
|
|
where: { websiteId },
|
|
|
|
}),
|
|
|
|
client.session.deleteMany({
|
|
|
|
where: { websiteId },
|
|
|
|
}),
|
2023-03-03 07:48:30 +01:00
|
|
|
client.teamWebsite.deleteMany({
|
|
|
|
where: {
|
|
|
|
websiteId,
|
|
|
|
},
|
|
|
|
}),
|
2023-05-29 06:37:34 +02:00
|
|
|
client.report.deleteMany({
|
2023-05-18 22:13:18 +02:00
|
|
|
where: {
|
|
|
|
websiteId,
|
|
|
|
},
|
|
|
|
}),
|
2023-02-28 01:01:34 +01:00
|
|
|
cloudMode
|
|
|
|
? prisma.client.website.update({
|
|
|
|
data: {
|
|
|
|
deletedAt: new Date(),
|
|
|
|
},
|
|
|
|
where: { id: websiteId },
|
|
|
|
})
|
|
|
|
: client.website.delete({
|
|
|
|
where: { id: websiteId },
|
|
|
|
}),
|
2022-12-13 04:45:38 +01:00
|
|
|
]).then(async data => {
|
|
|
|
if (cache.enabled) {
|
|
|
|
await cache.deleteWebsite(websiteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
});
|
|
|
|
}
|