summaryrefslogtreecommitdiff
path: root/src/app/api/product/import
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-11-09 15:40:16 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-11-09 15:40:16 +0700
commitbe0f537dc4fe384eef09436833c6407e6482c16d (patch)
tree194b1ad3f34396cb8149075bbbd38b854aedf361 /src/app/api/product/import
parent5d5401ae36e7e0c8eb38ccd943c1aa44a9573d35 (diff)
Initial commit
Diffstat (limited to 'src/app/api/product/import')
-rw-r--r--src/app/api/product/import/route.tsx27
1 files changed, 27 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