umami/src/queries/admin/report.ts

140 lines
2.9 KiB
TypeScript
Raw Normal View History

import { Prisma, Report } from '@prisma/client';
import prisma from 'lib/prisma';
2023-08-17 23:39:59 +00:00
import { FilterResult, ReportSearchFilter } from 'lib/types';
2024-01-30 08:10:25 +00:00
import ReportFindUniqueArgs = Prisma.ReportFindUniqueArgs;
import ReportFindManyArgs = Prisma.ReportFindManyArgs;
2024-01-30 08:10:25 +00:00
async function findReport(criteria: ReportFindUniqueArgs) {
return prisma.client.report.findUnique(criteria);
}
2024-01-30 08:10:25 +00:00
export async function getReport(reportId: string): Promise<Report> {
return findReport({
where: {
id: reportId,
},
});
}
2023-08-10 20:26:33 +00:00
export async function getReports(
2024-01-30 08:10:25 +00:00
criteria: ReportFindManyArgs,
filters: ReportSearchFilter = {},
2023-08-10 20:26:33 +00:00
): Promise<FilterResult<Report[]>> {
2024-01-14 10:21:39 +00:00
const mode = prisma.getQueryMode();
2024-01-30 08:10:25 +00:00
const { query, userId, websiteId } = filters;
2023-08-15 17:57:25 +00:00
2023-08-10 20:26:33 +00:00
const where: Prisma.ReportWhereInput = {
userId,
websiteId,
2024-01-30 10:10:23 +00:00
...criteria.where,
2023-08-14 05:21:49 +00:00
AND: [
{
OR: [
{
userId,
2023-08-14 05:21:49 +00:00
},
],
},
{
2023-08-10 20:26:33 +00:00
OR: [
{
name: {
contains: query,
2024-01-14 10:21:39 +00:00
mode,
},
2023-08-10 20:26:33 +00:00
},
{
description: {
contains: query,
2024-01-14 10:21:39 +00:00
mode,
},
2023-08-10 20:26:33 +00:00
},
{
type: {
contains: query,
2024-01-14 10:21:39 +00:00
mode,
},
2023-08-10 20:26:33 +00:00
},
{
user: {
username: {
contains: query,
2024-01-14 10:21:39 +00:00
mode,
2023-08-10 20:26:33 +00:00
},
},
2023-08-10 20:26:33 +00:00
},
{
website: {
name: {
contains: query,
2024-01-14 10:21:39 +00:00
mode,
2023-08-10 20:26:33 +00:00
},
},
2023-08-10 20:26:33 +00:00
},
{
website: {
domain: {
contains: query,
2024-01-14 10:21:39 +00:00
mode,
2023-08-10 20:26:33 +00:00
},
},
2023-08-10 20:26:33 +00:00
},
],
},
2023-08-14 05:21:49 +00:00
],
2023-08-10 20:26:33 +00:00
};
2024-02-01 06:08:48 +00:00
return prisma.pagedQuery('report', { ...criteria, where }, filters);
2023-08-10 20:26:33 +00:00
}
2024-01-30 08:10:25 +00:00
export async function getUserReports(
2023-08-10 20:26:33 +00:00
userId: string,
2024-01-30 08:10:25 +00:00
filters?: ReportSearchFilter,
2023-08-10 20:26:33 +00:00
): Promise<FilterResult<Report[]>> {
2023-08-14 05:21:49 +00:00
return getReports(
{
2024-01-30 08:10:25 +00:00
where: {
userId,
},
2023-08-14 05:21:49 +00:00
include: {
website: {
select: {
domain: true,
2023-08-14 05:32:25 +00:00
userId: true,
2023-08-14 05:21:49 +00:00
},
},
},
},
2024-01-30 08:10:25 +00:00
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 getWebsiteReports(
2023-08-10 20:26:33 +00:00
websiteId: string,
2024-01-30 08:10:25 +00:00
filters: ReportSearchFilter = {},
2023-08-10 20:26:33 +00:00
): Promise<FilterResult<Report[]>> {
2024-01-30 08:10:25 +00:00
return getReports(
{
where: {
websiteId,
},
},
filters,
);
}
export async function createReport(data: Prisma.ReportUncheckedCreateInput): Promise<Report> {
return prisma.client.report.create({ data });
}
export async function updateReport(
reportId: string,
data: Prisma.ReportUpdateInput,
): Promise<Report> {
return prisma.client.report.update({ where: { id: reportId }, data });
}
export async function deleteReport(reportId: string): Promise<Report> {
return prisma.client.report.delete({ where: { id: reportId } });
2023-08-10 20:26:33 +00:00
}