summaryrefslogtreecommitdiff
path: root/src/app/api/stock-opname/location
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/stock-opname/location
parent5d5401ae36e7e0c8eb38ccd943c1aa44a9573d35 (diff)
Initial commit
Diffstat (limited to 'src/app/api/stock-opname/location')
-rw-r--r--src/app/api/stock-opname/location/route.tsx57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/app/api/stock-opname/location/route.tsx b/src/app/api/stock-opname/location/route.tsx
new file mode 100644
index 0000000..1009486
--- /dev/null
+++ b/src/app/api/stock-opname/location/route.tsx
@@ -0,0 +1,57 @@
+import { DetailTeam, StockOpnameLocationRes } from "@/common/types/stockOpname";
+import { Location, Team } from "@prisma/client";
+import { NextRequest, NextResponse } from "next/server";
+import { prisma } from "prisma/client";
+
+
+
+const getOpnameQuantity = async (where: { locationId: number, productId: number }) => {
+ const detailTeam: DetailTeam = {
+ COUNT1: { quantity: undefined, user: undefined },
+ COUNT2: { quantity: undefined, user: undefined },
+ VERIFICATION: { quantity: undefined, user: undefined },
+ }
+ for (const team of Object.keys(Team)) {
+ const opname = await prisma.stockOpname.findFirst({
+ where: { ...where, team: team as Team },
+ select: { quantity: true, user: true },
+ })
+ if (!opname) continue
+ detailTeam[team as Team]['quantity'] = opname?.quantity
+ detailTeam[team as Team]['user'] = opname?.user
+ }
+ return detailTeam
+}
+
+export async function GET(request: NextRequest) {
+ const searchParams = request.nextUrl.searchParams;
+ const productId = searchParams.get('productId') ?? '';
+ const companyId = searchParams.get('companyId');
+ const intProductId = parseInt(productId);
+
+ if (!companyId) {
+ return NextResponse.json({ error: 'Bad Request. Missing companyId' }, { status: 400 })
+ }
+
+ const intCompanyId = parseInt(companyId);
+
+ const opnameLocByProduct = await prisma.stockOpname.groupBy({
+ by: ['locationId'],
+ where: { productId: intProductId, companyId: intCompanyId },
+ })
+
+ const locationIds = opnameLocByProduct.map((opname) => opname.locationId)
+
+ const result: StockOpnameLocationRes[] = []
+
+ for (const locationId of locationIds) {
+ const detail = await getOpnameQuantity({ locationId, productId: intProductId })
+ const location = await prisma.location.findFirst({
+ where: { id: locationId, companyId: intCompanyId }
+ })
+ if (!location) continue
+ result.push({ ...location, ...detail })
+ }
+
+ return NextResponse.json(result)
+} \ No newline at end of file