summaryrefslogtreecommitdiff
path: root/src/app/api/product
diff options
context:
space:
mode:
Diffstat (limited to 'src/app/api/product')
-rw-r--r--src/app/api/product/import/route.tsx27
-rw-r--r--src/app/api/product/route.tsx48
2 files changed, 75 insertions, 0 deletions
diff --git a/src/app/api/product/import/route.tsx b/src/app/api/product/import/route.tsx
new file mode 100644
index 0000000..7358205
--- /dev/null
+++ b/src/app/api/product/import/route.tsx
@@ -0,0 +1,27 @@
+import { NextRequest, NextResponse } from "next/server";
+import { prisma } from "prisma/client";
+import * as XLSX from "xlsx";
+
+export async function POST(request: NextRequest) {
+ const body = await request.arrayBuffer();
+ const workbook = XLSX.read(body, { type: 'buffer' })
+ const worksheetName = workbook.SheetNames[0]
+ const worksheet = workbook.Sheets[worksheetName]
+ const fileData: any[] = XLSX.utils.sheet_to_json(worksheet, { header: 1 })
+ fileData.shift();
+
+ const newProducts = fileData.map(row => ({
+ id: undefined,
+ isDifferent: false,
+ name: row[0],
+ barcode: row[1],
+ itemCode: row[2],
+ onhandQty: row[3],
+ differenceQty: row[4],
+ companyId: row[5],
+ }));
+
+ await prisma.product.createMany({ data: newProducts })
+
+ return NextResponse.json(true)
+} \ No newline at end of file
diff --git a/src/app/api/product/route.tsx b/src/app/api/product/route.tsx
new file mode 100644
index 0000000..1161a4e
--- /dev/null
+++ b/src/app/api/product/route.tsx
@@ -0,0 +1,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 })
+} \ No newline at end of file