aboutsummaryrefslogtreecommitdiff
path: root/src/app/api/admin/users/route.ts
blob: 2e5226157a40564998ad61dbc6a800df1bf3f7e7 (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
import { z } from 'zod';
import { parseRequest } from '@/lib/request';
import { json, unauthorized } from '@/lib/response';
import { pagingParams, searchParams } from '@/lib/schema';
import { canViewUsers } from '@/permissions';
import { getUsers } from '@/queries/prisma/user';

export async function GET(request: Request) {
  const schema = z.object({
    ...pagingParams,
    ...searchParams,
  });

  const { auth, query, error } = await parseRequest(request, schema);

  if (error) {
    return error();
  }

  if (!(await canViewUsers(auth))) {
    return unauthorized();
  }

  const users = await getUsers(
    {
      include: {
        _count: {
          select: {
            websites: {
              where: { deletedAt: null },
            },
          },
        },
      },
      omit: {
        password: true,
      },
      orderBy: {
        createdAt: 'desc',
      },
    },
    query,
  );

  return json(users);
}