2025-12-04 21:05:57 +03:30
|
|
|
import { Injectable } from '@nestjs/common'
|
2025-12-09 13:59:07 +03:30
|
|
|
import { ShortEntity } from '../common/interfaces/response-models'
|
2025-12-06 17:48:10 +03:30
|
|
|
import { ResponseMapper } from '../common/response/response-mapper'
|
2025-12-04 21:05:57 +03:30
|
|
|
import { PrismaService } from '../prisma/prisma.service'
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class InventoriesService {
|
|
|
|
|
constructor(private prisma: PrismaService) {}
|
2025-12-06 17:48:10 +03:30
|
|
|
async create(data: any) {
|
|
|
|
|
const item = await this.prisma.inventory.create({ data })
|
|
|
|
|
return ResponseMapper.create(item)
|
2025-12-04 21:05:57 +03:30
|
|
|
}
|
|
|
|
|
|
2025-12-06 17:48:10 +03:30
|
|
|
async findAll() {
|
2025-12-09 13:59:07 +03:30
|
|
|
const items = await this.prisma.inventory.findMany({
|
|
|
|
|
include: { productCharges: { include: { product: true } } },
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const mapped = items.map(i => {
|
|
|
|
|
const productsMap: Record<number, ShortEntity> = {}
|
|
|
|
|
for (const pc of i.productCharges ?? []) {
|
|
|
|
|
if (pc.product) {
|
|
|
|
|
productsMap[pc.product.id] = { id: pc.product.id, name: pc.product.name }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const groupedProducts = Object.values(productsMap)
|
|
|
|
|
const { productCharges, ...rest } = i as any
|
|
|
|
|
return { ...rest, groupedProducts }
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.list(mapped)
|
2025-12-04 21:05:57 +03:30
|
|
|
}
|
|
|
|
|
|
2025-12-06 17:48:10 +03:30
|
|
|
async findOne(id: number) {
|
2025-12-09 13:59:07 +03:30
|
|
|
const item = await this.prisma.inventory.findUnique({
|
|
|
|
|
where: { id },
|
|
|
|
|
include: { productCharges: { include: { product: true } } },
|
|
|
|
|
})
|
2025-12-06 17:48:10 +03:30
|
|
|
if (!item) return null
|
2025-12-09 13:59:07 +03:30
|
|
|
const productsMap: Record<number, { id: number; name: string; count: number }> = {}
|
|
|
|
|
for (const pc of item.productCharges ?? []) {
|
|
|
|
|
const pid = pc.productId
|
|
|
|
|
const pname = pc.product?.name ?? `#${pid}`
|
|
|
|
|
if (!productsMap[pid]) productsMap[pid] = { id: pid, name: pname, count: 0 }
|
|
|
|
|
productsMap[pid].count += Number(pc.count)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const productsWithCount = Object.values(productsMap)
|
|
|
|
|
const groupedProducts: ShortEntity[] = productsWithCount.map(p => ({
|
|
|
|
|
id: p.id,
|
|
|
|
|
name: p.name,
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
const { productCharges, ...rest } = item as any
|
|
|
|
|
const itemWithCounts = {
|
|
|
|
|
...rest,
|
|
|
|
|
products: productsWithCount,
|
|
|
|
|
groupedProducts,
|
|
|
|
|
}
|
|
|
|
|
return ResponseMapper.single(itemWithCounts)
|
2025-12-04 21:05:57 +03:30
|
|
|
}
|
|
|
|
|
|
2025-12-06 17:48:10 +03:30
|
|
|
async update(id: number, data: any) {
|
|
|
|
|
const item = await this.prisma.inventory.update({ where: { id }, data })
|
|
|
|
|
return ResponseMapper.update(item)
|
2025-12-04 21:05:57 +03:30
|
|
|
}
|
|
|
|
|
|
2025-12-06 17:48:10 +03:30
|
|
|
async remove(id: number) {
|
|
|
|
|
const item = await this.prisma.inventory.delete({ where: { id } })
|
|
|
|
|
return ResponseMapper.single(item)
|
2025-12-04 21:05:57 +03:30
|
|
|
}
|
|
|
|
|
}
|