aboutsummaryrefslogtreecommitdiff
path: root/src/queries/prisma/report.ts
blob: 4a5b755c88bc6f756b26dde024bca0b5a5460c61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { Prisma } from '@/generated/prisma/client';
import prisma from '@/lib/prisma';
import type { QueryFilters } from '@/lib/types';

import ReportFindManyArgs = Prisma.ReportFindManyArgs;

async function findReport(criteria: Prisma.ReportFindUniqueArgs) {
  return prisma.client.report.findUnique(criteria);
}

export async function getReport(reportId: string) {
  return findReport({
    where: {
      id: reportId,
    },
  });
}

export async function getReports(criteria: ReportFindManyArgs, filters: QueryFilters = {}) {
  const { search } = filters;

  const where: Prisma.ReportWhereInput = {
    ...criteria.where,
    ...prisma.getSearchParameters(search, [
      { name: 'contains' },
      { description: 'contains' },
      { type: 'contains' },
      {
        user: {
          username: 'contains',
        },
      },
      {
        website: {
          name: 'contains',
        },
      },
      {
        website: {
          domain: 'contains',
        },
      },
    ]),
  };

  return prisma.pagedQuery('report', { ...criteria, where }, filters);
}

export async function getUserReports(userId: string, filters?: QueryFilters) {
  return getReports(
    {
      where: {
        userId,
      },
      include: {
        website: {
          select: {
            domain: true,
            userId: true,
          },
        },
      },
    },
    filters,
  );
}

export async function getWebsiteReports(websiteId: string, filters: QueryFilters = {}) {
  return getReports(
    {
      where: {
        websiteId,
      },
    },
    filters,
  );
}

export async function createReport(data: Prisma.ReportUncheckedCreateInput) {
  return prisma.client.report.create({ data });
}

export async function updateReport(reportId: string, data: any) {
  return prisma.client.report.update({ where: { id: reportId }, data });
}

export async function deleteReport(reportId: string) {
  return prisma.client.report.delete({ where: { id: reportId } });
}