aboutsummaryrefslogtreecommitdiff
path: root/src/queries/prisma/segment.ts
blob: 3a17d2735a32b10c1d774e3e927f1c61eea2c824 (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
import type { Prisma } from '@/generated/prisma/client';
import prisma from '@/lib/prisma';
import type { QueryFilters } from '@/lib/types';

async function findSegment(criteria: Prisma.SegmentFindUniqueArgs) {
  return prisma.client.segment.findUnique(criteria);
}

export async function getSegment(segmentId: string) {
  return findSegment({
    where: {
      id: segmentId,
    },
  });
}

export async function getSegments(criteria: Prisma.SegmentFindManyArgs, filters: QueryFilters) {
  const { search } = filters;
  const { getSearchParameters, pagedQuery } = prisma;

  const where: Prisma.SegmentWhereInput = {
    ...criteria.where,
    ...getSearchParameters(search, [
      {
        name: 'contains',
      },
    ]),
  };

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

export async function getWebsiteSegment(websiteId: string, segmentId: string) {
  return prisma.client.segment.findFirst({
    where: { id: segmentId, websiteId },
  });
}

export async function getWebsiteSegments(websiteId: string, type: string, filters?: QueryFilters) {
  return getSegments(
    {
      where: {
        websiteId,
        type,
      },
    },
    filters,
  );
}

export async function createSegment(data: Prisma.SegmentUncheckedCreateInput) {
  return prisma.client.segment.create({ data });
}

export async function updateSegment(SegmentId: string, data: Prisma.SegmentUpdateInput) {
  return prisma.client.segment.update({ where: { id: SegmentId }, data });
}

export async function deleteSegment(SegmentId: string) {
  return prisma.client.segment.delete({ where: { id: SegmentId } });
}