130 lines
2.8 KiB
TypeScript
130 lines
2.8 KiB
TypeScript
|
|
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,
|
||
|
|
category: {
|
||
|
|
select: {
|
||
|
|
id: true,
|
||
|
|
name: true,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
created_at: true,
|
||
|
|
}
|
||
|
|
|
||
|
|
async findAll(user_id: string, business_activity_id: string, complex_id: string) {
|
||
|
|
const goods = await this.prisma.good.findMany({
|
||
|
|
where: {
|
||
|
|
complex_id,
|
||
|
|
complex: {
|
||
|
|
id: complex_id,
|
||
|
|
business_activity: {
|
||
|
|
id: business_activity_id,
|
||
|
|
user_id,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
select: this.defaultSelect,
|
||
|
|
})
|
||
|
|
|
||
|
|
return ResponseMapper.list(goods)
|
||
|
|
}
|
||
|
|
|
||
|
|
async findOne(
|
||
|
|
user_id: string,
|
||
|
|
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,
|
||
|
|
user_id,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
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(
|
||
|
|
user_id: string,
|
||
|
|
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,
|
||
|
|
user_id,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
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()
|
||
|
|
// }
|
||
|
|
}
|