umami/queries/admin/report.ts

178 lines
4.5 KiB
TypeScript
Raw Normal View History

import { Prisma, Report } from '@prisma/client';
2023-08-10 22:26:33 +02:00
import { REPORT_FILTER_TYPES } from 'lib/constants';
import prisma from 'lib/prisma';
2023-08-10 22:26:33 +02:00
import { FilterResult, ReportSearchFilter, ReportSearchFilterType, SearchFilter } from 'lib/types';
export async function createReport(data: Prisma.ReportUncheckedCreateInput): Promise<Report> {
return prisma.client.report.create({ data });
}
export async function getReportById(reportId: string): Promise<Report> {
return prisma.client.report.findUnique({
where: {
id: reportId,
},
});
}
export async function updateReport(
2023-07-30 07:03:34 +02:00
reportId: string,
data: Prisma.ReportUpdateInput,
): Promise<Report> {
2023-07-30 07:03:34 +02:00
return prisma.client.report.update({ where: { id: reportId }, data });
}
2023-07-30 07:03:34 +02:00
export async function deleteReport(reportId: string): Promise<Report> {
return prisma.client.report.delete({ where: { id: reportId } });
}
2023-08-10 22:26:33 +02:00
export async function getReports(
ReportSearchFilter: ReportSearchFilter,
2023-08-14 07:21:49 +02:00
options?: { include?: Prisma.ReportInclude },
2023-08-10 22:26:33 +02:00
): Promise<FilterResult<Report[]>> {
2023-08-14 07:21:49 +02:00
const {
userId,
websiteId,
includeTeams,
filter,
filterType = REPORT_FILTER_TYPES.all,
} = ReportSearchFilter;
2023-08-10 22:26:33 +02:00
const where: Prisma.ReportWhereInput = {
...(userId && { userId: userId }),
...(websiteId && { websiteId: websiteId }),
2023-08-14 07:21:49 +02:00
AND: [
{
OR: [
{
...(userId && { userId: userId }),
},
{
...(includeTeams && {
website: {
teamWebsite: {
some: {
team: {
teamUser: {
some: {
userId,
},
},
},
},
},
},
}),
},
],
},
{
2023-08-10 22:26:33 +02:00
OR: [
{
...((filterType === REPORT_FILTER_TYPES.all ||
filterType === REPORT_FILTER_TYPES.name) && {
name: {
startsWith: filter,
2023-08-14 01:42:11 +02:00
mode: 'insensitive',
2023-08-10 22:26:33 +02:00
},
}),
},
{
...((filterType === REPORT_FILTER_TYPES.all ||
filterType === REPORT_FILTER_TYPES.description) && {
description: {
startsWith: filter,
2023-08-14 01:42:11 +02:00
mode: 'insensitive',
2023-08-10 22:26:33 +02:00
},
}),
},
{
...((filterType === REPORT_FILTER_TYPES.all ||
filterType === REPORT_FILTER_TYPES.type) && {
type: {
startsWith: filter,
2023-08-14 01:42:11 +02:00
mode: 'insensitive',
2023-08-10 22:26:33 +02:00
},
}),
},
{
...((filterType === REPORT_FILTER_TYPES.all ||
filterType === REPORT_FILTER_TYPES['user:username']) && {
user: {
username: {
startsWith: filter,
2023-08-14 01:42:11 +02:00
mode: 'insensitive',
2023-08-10 22:26:33 +02:00
},
},
}),
},
{
...((filterType === REPORT_FILTER_TYPES.all ||
filterType === REPORT_FILTER_TYPES['website:name']) && {
website: {
name: {
startsWith: filter,
2023-08-14 01:42:11 +02:00
mode: 'insensitive',
2023-08-10 22:26:33 +02:00
},
},
}),
},
{
...((filterType === REPORT_FILTER_TYPES.all ||
filterType === REPORT_FILTER_TYPES['website:domain']) && {
website: {
domain: {
startsWith: filter,
2023-08-14 01:42:11 +02:00
mode: 'insensitive',
2023-08-10 22:26:33 +02:00
},
},
}),
},
],
},
2023-08-14 07:21:49 +02:00
],
2023-08-10 22:26:33 +02:00
};
const [pageFilters, getParameters] = prisma.getPageFilters(ReportSearchFilter);
const reports = await prisma.client.report.findMany({
where,
...pageFilters,
2023-08-14 07:21:49 +02:00
...(options?.include && { include: options.include }),
2023-08-10 22:26:33 +02:00
});
const count = await prisma.client.report.count({
where,
});
return {
data: reports,
count,
...getParameters,
};
}
export async function getReportsByUserId(
userId: string,
filter: SearchFilter<ReportSearchFilterType>,
): Promise<FilterResult<Report[]>> {
2023-08-14 07:21:49 +02:00
return getReports(
{ userId, ...filter },
{
include: {
website: {
select: {
domain: true,
},
},
},
},
);
2023-08-10 22:26:33 +02:00
}
export async function getReportsByWebsiteId(
websiteId: string,
filter: SearchFilter<ReportSearchFilterType>,
): Promise<FilterResult<Report[]>> {
return getReports({ websiteId, ...filter });
}