1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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)
}
|