2026-03-16 20:59:13 +03:30
|
|
|
import { GoodSelect } from '@/generated/prisma/models'
|
|
|
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
|
|
|
|
import { Injectable } from '@nestjs/common'
|
|
|
|
|
import { ResponseMapper } from 'common/response/response-mapper'
|
|
|
|
|
import { CreateGoodDto, UpdateGoodDto } from './dto/good.dto'
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class ConsumerComplexGoodsService {
|
|
|
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
|
|
|
|
|
|
defaultSelect: GoodSelect = {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
barcode: true,
|
|
|
|
|
sku: true,
|
|
|
|
|
local_sku: true,
|
|
|
|
|
pricing_model: true,
|
|
|
|
|
unit_type: true,
|
2026-04-16 22:19:20 +03:30
|
|
|
image_url: true,
|
2026-03-16 20:59:13 +03:30
|
|
|
category: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
created_at: true,
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 14:47:05 +03:30
|
|
|
async findAll(consumer_id: string, business_activity_id: string, complex_id: string) {
|
2026-03-16 20:59:13 +03:30
|
|
|
const goods = await this.prisma.good.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
complex_id,
|
|
|
|
|
complex: {
|
|
|
|
|
id: complex_id,
|
|
|
|
|
business_activity: {
|
|
|
|
|
id: business_activity_id,
|
2026-04-11 14:47:05 +03:30
|
|
|
consumer_id,
|
2026-03-16 20:59:13 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
select: this.defaultSelect,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.list(goods)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOne(
|
2026-04-11 14:47:05 +03:30
|
|
|
consumer_id: string,
|
2026-03-16 20:59:13 +03:30
|
|
|
business_activity_id: string,
|
|
|
|
|
complex_id: string,
|
|
|
|
|
id: string,
|
|
|
|
|
) {
|
|
|
|
|
const good = await this.prisma.good.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
complex: {
|
|
|
|
|
id: complex_id,
|
|
|
|
|
business_activity: {
|
|
|
|
|
id: business_activity_id,
|
2026-04-11 14:47:05 +03:30
|
|
|
consumer_id,
|
2026-03-16 20:59:13 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
id,
|
|
|
|
|
},
|
|
|
|
|
select: this.defaultSelect,
|
|
|
|
|
})
|
|
|
|
|
return ResponseMapper.single(good)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create(complex_id: string, data: CreateGoodDto) {
|
|
|
|
|
const { category_id, ...rest } = data
|
|
|
|
|
|
|
|
|
|
const good = await this.prisma.good.create({
|
|
|
|
|
data: {
|
|
|
|
|
...rest,
|
|
|
|
|
complex: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: complex_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
category: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: category_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return ResponseMapper.create(good)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async update(
|
2026-04-11 14:47:05 +03:30
|
|
|
consumer_id: string,
|
2026-03-16 20:59:13 +03:30
|
|
|
business_activity_id: string,
|
|
|
|
|
complex_id: string,
|
|
|
|
|
id: string,
|
|
|
|
|
data: UpdateGoodDto,
|
|
|
|
|
) {
|
|
|
|
|
const { category_id, ...rest } = data
|
|
|
|
|
const good = await this.prisma.good.update({
|
|
|
|
|
where: {
|
|
|
|
|
complex: {
|
|
|
|
|
id: complex_id,
|
|
|
|
|
business_activity: {
|
|
|
|
|
id: business_activity_id,
|
2026-04-11 14:47:05 +03:30
|
|
|
consumer_id,
|
2026-03-16 20:59:13 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
id,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
complex: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: complex_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
category: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: category_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
...rest,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return ResponseMapper.update(good)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// async delete(id: string) {
|
|
|
|
|
// await this.prisma.complex.delete({ where: { id } })
|
|
|
|
|
// return ResponseMapper.delete()
|
|
|
|
|
// }
|
|
|
|
|
}
|