umami/queries/admin/report.ts

134 lines
3.8 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,
): Promise<FilterResult<Report[]>> {
const { userId, websiteId, filter, filterType = REPORT_FILTER_TYPES.all } = ReportSearchFilter;
const where: Prisma.ReportWhereInput = {
...(userId && { userId: userId }),
...(websiteId && { websiteId: websiteId }),
...(filter && {
AND: {
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
},
},
}),
},
],
},
}),
};
const [pageFilters, getParameters] = prisma.getPageFilters(ReportSearchFilter);
const reports = await prisma.client.report.findMany({
where,
...pageFilters,
});
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[]>> {
return getReports({ userId, ...filter });
}
export async function getReportsByWebsiteId(
websiteId: string,
filter: SearchFilter<ReportSearchFilterType>,
): Promise<FilterResult<Report[]>> {
return getReports({ websiteId, ...filter });
}