Add SQL query for average cost retrieval and generate TriggerLog model
This commit is contained in:
@@ -30,8 +30,9 @@ export class ProductsService {
|
||||
|
||||
async findAll() {
|
||||
const items = await this.prisma.product.findMany({
|
||||
include: { brand: true, category: true },
|
||||
include: { brand: true, category: true, stockBalances: true },
|
||||
})
|
||||
|
||||
const mapped = items.map(p => {
|
||||
const { brandId, categoryId, ...rest } = p as any
|
||||
const brand: ShortEntity | null = p.brand
|
||||
@@ -40,7 +41,12 @@ export class ProductsService {
|
||||
const category: ShortEntity | null = p.category
|
||||
? { id: p.category.id, name: p.category.name }
|
||||
: null
|
||||
return { ...rest, brand, category }
|
||||
const stock =
|
||||
p.stockBalances && p.stockBalances.length > 0
|
||||
? p.stockBalances.reduce((acc, sb) => acc + Number(sb.quantity), 0)
|
||||
: 0
|
||||
|
||||
return { ...rest, brand, category, stock }
|
||||
})
|
||||
return ResponseMapper.list(mapped)
|
||||
}
|
||||
@@ -48,7 +54,7 @@ export class ProductsService {
|
||||
async findOne(id: number) {
|
||||
const p = await this.prisma.product.findUnique({
|
||||
where: { id },
|
||||
include: { brand: true, category: true, productCharges: true },
|
||||
include: { brand: true, category: true, stockBalances: true },
|
||||
})
|
||||
if (!p) return null
|
||||
|
||||
@@ -63,7 +69,15 @@ export class ProductsService {
|
||||
...rest,
|
||||
brand,
|
||||
category,
|
||||
count: p.productCharges.reduce((acc, charge) => acc + Number(charge.count), 0.0),
|
||||
stock:
|
||||
p.stockBalances && p.stockBalances.length > 0
|
||||
? p.stockBalances.reduce((acc, sb) => acc + Number(sb.quantity), 0)
|
||||
: 0,
|
||||
avgCost:
|
||||
p.stockBalances && p.stockBalances.length > 0
|
||||
? p.stockBalances.reduce((acc, sb) => acc + Number(sb.avgCost), 0) /
|
||||
p.stockBalances.length
|
||||
: 0,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user