summaryrefslogtreecommitdiff
path: root/src/app/api/product/import/route.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'src/app/api/product/import/route.tsx')
-rw-r--r--src/app/api/product/import/route.tsx17
1 files changed, 14 insertions, 3 deletions
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)