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-19 15:40:45 +03:30
|
|
|
import { PosCacheInvalidationService } from '@/modules/pos/cache/pos-cache-invalidation.service'
|
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()
|
2026-04-27 10:45:39 +03:30
|
|
|
export class ConsumerBusinessActivityGoodsService {
|
2026-05-07 20:30:24 +03:30
|
|
|
constructor(
|
|
|
|
|
private readonly prisma: PrismaService,
|
|
|
|
|
private readonly uploaderService: UploaderService,
|
2026-05-19 15:40:45 +03:30
|
|
|
private readonly posCacheInvalidationService: PosCacheInvalidationService,
|
2026-05-07 20:30:24 +03:30
|
|
|
) {}
|
2026-03-16 20:59:13 +03:30
|
|
|
|
|
|
|
|
defaultSelect: GoodSelect = {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
barcode: true,
|
|
|
|
|
local_sku: true,
|
|
|
|
|
pricing_model: true,
|
2026-04-16 22:19:20 +03:30
|
|
|
image_url: true,
|
2026-05-01 19:43:59 +03:30
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 10:45:39 +03:30
|
|
|
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: {
|
2026-04-27 10:45:39 +03:30
|
|
|
business_activity: {
|
|
|
|
|
id: business_activity_id,
|
|
|
|
|
consumer_id,
|
2026-03-16 20:59:13 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
select: this.defaultSelect,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.list(goods)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 10:45:39 +03:30
|
|
|
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: {
|
2026-04-27 10:45:39 +03:30
|
|
|
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,
|
|
|
|
|
) {
|
2026-05-01 19:43:59 +03:30
|
|
|
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,
|
2026-04-27 10:45:39 +03:30
|
|
|
business_activity: {
|
2026-03-16 20:59:13 +03:30
|
|
|
connect: {
|
2026-04-27 10:45:39 +03:30
|
|
|
id: business_activity_id,
|
2026-03-16 20:59:13 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
category: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: category_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-05-01 19:43:59 +03:30
|
|
|
sku: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: sku_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
measure_unit: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: measure_unit_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-03-16 20:59:13 +03:30
|
|
|
},
|
|
|
|
|
})
|
2026-05-19 15:40:45 +03:30
|
|
|
await this.posCacheInvalidationService.invalidateGoodsListByBusinessActivity(
|
|
|
|
|
business_activity_id,
|
|
|
|
|
)
|
2026-03-16 20:59:13 +03:30
|
|
|
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,
|
|
|
|
|
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: {
|
2026-04-27 10:45:39 +03:30
|
|
|
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-01 19:43:59 +03:30
|
|
|
},
|
2026-05-07 20:30:24 +03:30
|
|
|
sku: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: sku_id,
|
|
|
|
|
},
|
2026-05-01 19:43:59 +03:30
|
|
|
},
|
2026-05-07 20:30:24 +03:30
|
|
|
measure_unit: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: measure_unit_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
...rest,
|
2026-05-01 19:43:59 +03:30
|
|
|
},
|
2026-05-07 20:30:24 +03:30
|
|
|
})
|
2026-03-16 20:59:13 +03:30
|
|
|
})
|
2026-05-19 15:40:45 +03:30
|
|
|
await this.posCacheInvalidationService.invalidateGoodsListByBusinessActivity(
|
|
|
|
|
business_activity_id,
|
|
|
|
|
)
|
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()
|
|
|
|
|
// }
|
|
|
|
|
}
|