Files
psp_api/src/modules/consumer/business-activities/goods/goods.service.ts
T

185 lines
4.2 KiB
TypeScript
Raw Normal View History

2026-05-07 20:30:24 +03:30
import { UploadedFileTypes } from '@/common/enums/enums'
2026-03-16 20:59:13 +03:30
import { GoodSelect } from '@/generated/prisma/models'
2026-05-07 20:30:24 +03:30
import { UploaderService } from '@/modules/uploader/uploader.service'
2026-03-16 20:59:13 +03:30
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 ConsumerBusinessActivityGoodsService {
2026-05-07 20:30:24 +03:30
constructor(
private readonly prisma: PrismaService,
private readonly uploaderService: UploaderService,
) {}
2026-03-16 20:59:13 +03:30
defaultSelect: GoodSelect = {
id: true,
name: true,
barcode: true,
local_sku: true,
pricing_model: true,
image_url: true,
sku: {
select: {
id: true,
name: true,
},
},
measure_unit: {
select: {
id: true,
name: true,
},
},
2026-03-16 20:59:13 +03:30
category: {
select: {
id: true,
name: true,
},
},
created_at: true,
}
async findAll(consumer_id: string, business_activity_id: string) {
2026-03-16 20:59:13 +03:30
const goods = await this.prisma.good.findMany({
where: {
business_activity: {
id: business_activity_id,
consumer_id,
2026-03-16 20:59:13 +03:30
},
},
select: this.defaultSelect,
})
return ResponseMapper.list(goods)
}
async findOne(consumer_id: string, business_activity_id: string, id: string) {
2026-03-16 20:59:13 +03:30
const good = await this.prisma.good.findUnique({
where: {
business_activity: {
id: business_activity_id,
consumer_id,
2026-03-16 20:59:13 +03:30
},
id,
},
select: this.defaultSelect,
})
return ResponseMapper.single(good)
}
2026-05-07 20:30:24 +03:30
async create(
business_activity_id: string,
data: CreateGoodDto,
file: Express.Multer.File,
) {
const { category_id, sku_id, measure_unit_id, ...rest } = data
2026-05-07 20:30:24 +03:30
let image_url = ''
if (file) {
const uploadedUrl = await this.uploaderService.uploadFile(
file,
UploadedFileTypes.GOOD,
)
image_url = uploadedUrl || ''
}
2026-03-16 20:59:13 +03:30
const good = await this.prisma.good.create({
data: {
...rest,
2026-05-07 20:30:24 +03:30
image_url,
business_activity: {
2026-03-16 20:59:13 +03:30
connect: {
id: business_activity_id,
2026-03-16 20:59:13 +03:30
},
},
category: {
connect: {
id: category_id,
},
},
sku: {
connect: {
id: sku_id,
},
},
measure_unit: {
connect: {
id: measure_unit_id,
},
},
2026-03-16 20:59:13 +03:30
},
})
return ResponseMapper.create(good)
}
async update(
consumer_id: string,
2026-03-16 20:59:13 +03:30
business_activity_id: string,
id: string,
data: UpdateGoodDto,
2026-05-07 20:30:24 +03:30
file: Express.Multer.File,
2026-03-16 20:59:13 +03:30
) {
2026-05-07 20:30:24 +03:30
const good = await this.prisma.$transaction(async tx => {
const { category_id, sku_id, measure_unit_id, ...rest } = data
let image_url = ''
if (file) {
const uploadedUrl = await this.uploaderService.uploadFile(
file,
UploadedFileTypes.GOOD,
)
image_url = uploadedUrl || ''
}
const prevImage = await tx.good.findUnique({
where: { id },
select: { image_url: true },
})
if (image_url && prevImage?.image_url) {
this.uploaderService.deleteFile(prevImage.image_url, UploadedFileTypes.GOOD)
}
return await tx.good.update({
where: {
business_activity: {
id: business_activity_id,
2026-05-07 20:30:24 +03:30
consumer_id,
2026-03-16 20:59:13 +03:30
},
2026-05-07 20:30:24 +03:30
id,
2026-03-16 20:59:13 +03:30
},
2026-05-07 20:30:24 +03:30
data: {
image_url,
business_activity: {
connect: {
id: business_activity_id,
},
2026-03-16 20:59:13 +03:30
},
2026-05-07 20:30:24 +03:30
category: {
connect: {
id: category_id,
},
},
2026-05-07 20:30:24 +03:30
sku: {
connect: {
id: sku_id,
},
},
2026-05-07 20:30:24 +03:30
measure_unit: {
connect: {
id: measure_unit_id,
},
},
...rest,
},
2026-05-07 20:30:24 +03:30
})
2026-03-16 20:59:13 +03:30
})
return ResponseMapper.update(good)
}
// async delete(id: string) {
// await this.prisma.complex.delete({ where: { id } })
// return ResponseMapper.delete()
// }
}