diff options
Diffstat (limited to 'src/app/api/product')
| -rw-r--r-- | src/app/api/product/import/route.tsx | 17 | ||||
| -rw-r--r-- | src/app/api/product/route.tsx | 16 |
2 files changed, 20 insertions, 13 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) diff --git a/src/app/api/product/route.tsx b/src/app/api/product/route.tsx index 1161a4e..07ba9c7 100644 --- a/src/app/api/product/route.tsx +++ b/src/app/api/product/route.tsx @@ -1,24 +1,20 @@ +import getServerCredential from "@/common/libs/getServerCredential"; 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 paramCompanyId = searchParams.get('companyId') + const companyId = paramCompanyId ? parseInt(paramCompanyId) : null - const credentialStr = request.cookies.get('credential')?.value - const credential: Credential | null = credentialStr ? JSON.parse(credentialStr) : null + const credential = getServerCredential() - if (!credential) { - return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) - } - - const { companyId } = credential + if (!credential) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) const where = { AND: { @@ -27,7 +23,7 @@ export async function GET(request: NextRequest) { { barcode: { contains: search ?? '' } }, { itemCode: { contains: search ?? '' } }, ], - companyId: type == 'all' ? undefined : companyId + companyId: companyId ?? credential.companyId } } |
