summaryrefslogtreecommitdiff
path: root/src/app/api/location/route.tsx
blob: 1b5cb43261c6cad0f3256b85f070c8cb25baebf5 (plain)
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
import getServerCredential from "@/common/libs/getServerCredential";
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "prisma/client";

export async function GET(request: NextRequest) {
  const searchParams = request.nextUrl.searchParams;
  const search = searchParams.get('search');

  const credential = getServerCredential()

  if (!credential) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })

  const { companyId } = credential

  const locations = await prisma.location.findMany({
    where: {
      companyId,
      name: { mode: 'insensitive', contains: search ?? '' }
    },
    take: 20,
    orderBy: { name: 'asc' }
  })

  return NextResponse.json(locations)
}