feat: stats relation

This commit is contained in:
Methapon2001 2024-06-28 18:00:07 +07:00
parent f753f8dec2
commit fb47a11712
2 changed files with 36 additions and 1 deletions

View file

@ -66,6 +66,13 @@ export class ProductGroup extends Controller {
const [result, total] = await prisma.$transaction([ const [result, total] = await prisma.$transaction([
prisma.productGroup.findMany({ prisma.productGroup.findMany({
include: {
_count: {
select: {
type: true,
},
},
},
orderBy: [{ statusOrder: "asc" }, { createdAt: "asc" }], orderBy: [{ statusOrder: "asc" }, { createdAt: "asc" }],
where, where,
take: pageSize, take: pageSize,
@ -74,7 +81,30 @@ export class ProductGroup extends Controller {
prisma.productGroup.count({ where }), prisma.productGroup.count({ where }),
]); ]);
return { result, page, pageSize, total }; const statsProduct = await prisma.productType.findMany({
include: {
_count: { select: { product: true } },
},
where: {
productGroupId: { in: result.map((v) => v.id) },
},
});
return {
result: result.map((v) => ({
...v,
_count: {
...v._count,
product: statsProduct.reduce(
(a, c) => (c.productGroupId === v.id ? a + c._count.product : a),
0,
),
},
})),
page,
pageSize,
total,
};
} }
@Get("{groupId}") @Get("{groupId}")

View file

@ -68,6 +68,11 @@ export class ProductType extends Controller {
} satisfies Prisma.ProductTypeWhereInput; } satisfies Prisma.ProductTypeWhereInput;
const [result, total] = await prisma.$transaction([ const [result, total] = await prisma.$transaction([
prisma.productType.findMany({ prisma.productType.findMany({
include: {
_count: {
select: { product: true },
},
},
orderBy: [{ statusOrder: "asc" }, { createdAt: "asc" }], orderBy: [{ statusOrder: "asc" }, { createdAt: "asc" }],
where, where,
take: pageSize, take: pageSize,