From 07819844d5ef7e323fd956eacfedecb2f4f4bb80 Mon Sep 17 00:00:00 2001
From: Rafi Zadanly <zadanlyr@gmail.com>
Date: Fri, 17 Nov 2023 15:16:23 +0700
Subject: Update result feature

---
 src/app/api/product/import/route.tsx | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

(limited to 'src/app/api/product/import/route.tsx')

diff --git a/src/app/api/product/import/route.tsx b/src/app/api/product/import/route.tsx
index 7358205..eb87f07 100644
--- a/src/app/api/product/import/route.tsx
+++ b/src/app/api/product/import/route.tsx
@@ -3,6 +3,12 @@ import { prisma } from "prisma/client";
 import * as XLSX from "xlsx";
 
 export async function POST(request: NextRequest) {
+  const searchParams = request.nextUrl.searchParams
+  const companyId = searchParams.get('companyId')
+
+  if (!companyId) return NextResponse.json({ error: 'Bad Request. Missing companyId' }, { status: 400 })
+  const intCompanyId = parseInt(companyId)
+
   const body = await request.arrayBuffer();
   const workbook = XLSX.read(body, { type: 'buffer' })
   const worksheetName = workbook.SheetNames[0]
@@ -13,14 +19,19 @@ export async function POST(request: NextRequest) {
   const newProducts = fileData.map(row => ({
     id: undefined,
     isDifferent: false,
-    name: row[0],
-    barcode: row[1],
-    itemCode: row[2],
+    name: row[0].toString(),
+    barcode: row[1].toString(),
+    itemCode: row[2].toString(),
     onhandQty: row[3],
     differenceQty: row[4],
     companyId: row[5],
   }));
 
+  const whereCompany = { companyId: intCompanyId }
+
+  await prisma.stockOpname.deleteMany({ where: whereCompany })
+  await prisma.product.deleteMany({ where: whereCompany })
+
   await prisma.product.createMany({ data: newProducts })
 
   return NextResponse.json(true)
-- 
cgit v1.2.3

