umami/src/queries/admin/report.ts

101 lines
2.1 KiB
TypeScript
Raw Normal View History

import { Prisma, Report } from '@prisma/client';
import prisma from 'lib/prisma';
import { PageResult, PageParams } from 'lib/types';
2024-01-30 08:10:25 +00:00
import ReportFindManyArgs = Prisma.ReportFindManyArgs;
2024-02-07 05:50:48 +00:00
async function findReport(criteria: Prisma.ReportFindUniqueArgs): Promise<Report> {
2024-01-30 08:10:25 +00:00
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: PageParams = {},
): Promise<PageResult<Report[]>> {
2024-02-04 07:19:29 +00:00
const { query } = filters;
2023-08-15 17:57:25 +00:00
2023-08-10 20:26:33 +00:00
const where: Prisma.ReportWhereInput = {
2024-01-30 10:10:23 +00:00
...criteria.where,
2024-02-04 07:19:29 +00:00
...prisma.getSearchParameters(query, [
{ name: 'contains' },
{ description: 'contains' },
{ type: 'contains' },
2023-08-14 05:21:49 +00:00
{
2024-02-04 07:19:29 +00:00
user: {
username: 'contains',
},
2023-08-14 05:21:49 +00:00
},
{
2024-02-04 07:19:29 +00:00
website: {
name: 'contains',
},
2023-08-10 20:26:33 +00:00
},
2024-02-04 07:19:29 +00:00
{
website: {
domain: 'contains',
},
},
]),
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,
filters?: PageParams,
): Promise<PageResult<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,
filters: PageParams = {},
): Promise<PageResult<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
}