blob: 1161a4e425c693a6b9324d7e06b1eb3487fe4c39 (
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "prisma/client";
import { Credential } from "@/common/types/auth"
export async function GET(request: NextRequest) {
const PAGE_SIZE = 30;
const searchParams = request.nextUrl.searchParams;
const type = searchParams.get('type')
const search = searchParams.get('search');
const page = searchParams.get('page');
const intPage: number = page ? parseInt(page) : 1;
const credentialStr = request.cookies.get('credential')?.value
const credential: Credential | null = credentialStr ? JSON.parse(credentialStr) : null
if (!credential) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const { companyId } = credential
const where = {
AND: {
OR: [
{ name: { contains: search ?? '' } },
{ barcode: { contains: search ?? '' } },
{ itemCode: { contains: search ?? '' } },
],
companyId: type == 'all' ? undefined : companyId
}
}
const products = await prisma.product.findMany({
where,
include: { company: true },
take: PAGE_SIZE,
skip: (intPage - 1) * PAGE_SIZE
})
const count = await prisma.product.count({ where })
const pagination = {
page: intPage,
totalPage: Math.ceil(count / PAGE_SIZE),
}
return NextResponse.json({ products, ...pagination })
}
|