2026-05-19 15:40:45 +03:30
|
|
|
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
|
2026-03-29 18:06:41 +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'
|
|
|
|
|
import { RedisService } from '@/redis/redis.service'
|
2026-02-12 20:31:04 +03:30
|
|
|
import { Injectable } from '@nestjs/common'
|
2026-03-07 11:25:11 +03:30
|
|
|
import { ResponseMapper } from '../../../common/response/response-mapper'
|
|
|
|
|
import { PrismaService } from '../../../prisma/prisma.service'
|
2026-02-12 20:31:04 +03:30
|
|
|
import { CreateGoodDto } from './dto/create-good.dto'
|
2026-05-19 15:40:45 +03:30
|
|
|
import { UpdateGoodDto } from './dto/update-good.dto'
|
2026-02-12 20:31:04 +03:30
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class GoodsService {
|
2026-05-19 15:40:45 +03:30
|
|
|
constructor(
|
|
|
|
|
private prisma: PrismaService,
|
|
|
|
|
private readonly redisService: RedisService,
|
|
|
|
|
private readonly posCacheInvalidationService: PosCacheInvalidationService,
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
private readonly listCacheTtlSeconds = 300
|
2026-02-12 20:31:04 +03:30
|
|
|
|
2026-03-29 18:06:41 +03:30
|
|
|
private readonly defaultSelect: GoodSelect = {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
barcode: true,
|
|
|
|
|
created_at: true,
|
|
|
|
|
description: true,
|
2026-05-01 19:43:59 +03:30
|
|
|
sku: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
2026-05-16 14:49:23 +03:30
|
|
|
code: true,
|
2026-05-17 12:04:30 +03:30
|
|
|
VAT: true,
|
2026-05-01 19:43:59 +03:30
|
|
|
},
|
|
|
|
|
},
|
2026-03-29 18:06:41 +03:30
|
|
|
local_sku: true,
|
|
|
|
|
is_default_guild_good: true,
|
|
|
|
|
pricing_model: true,
|
2026-05-01 19:43:59 +03:30
|
|
|
measure_unit: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-04-18 12:14:19 +03:30
|
|
|
image_url: true,
|
2026-03-29 18:06:41 +03:30
|
|
|
category: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
image_url: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 11:42:59 +03:30
|
|
|
async findAll(
|
|
|
|
|
business_activity_id: string,
|
|
|
|
|
guild_id: string,
|
|
|
|
|
consumer_account_id: string,
|
|
|
|
|
) {
|
2026-05-19 15:40:45 +03:30
|
|
|
const cacheKey = RedisKeyMaker.posGoodsList(business_activity_id, guild_id)
|
|
|
|
|
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
|
|
|
|
|
if (cached) {
|
|
|
|
|
return ResponseMapper.list(cached)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 20:31:04 +03:30
|
|
|
const goods = await this.prisma.good.findMany({
|
|
|
|
|
where: {
|
2026-03-29 18:06:41 +03:30
|
|
|
OR: [
|
|
|
|
|
{
|
|
|
|
|
is_default_guild_good: true,
|
|
|
|
|
category: {
|
|
|
|
|
guild_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2026-04-27 10:45:39 +03:30
|
|
|
business_activity_id,
|
2026-03-29 18:06:41 +03:30
|
|
|
},
|
|
|
|
|
],
|
2026-02-12 20:31:04 +03:30
|
|
|
},
|
2026-05-20 11:42:59 +03:30
|
|
|
select: {
|
|
|
|
|
...this.defaultSelect,
|
|
|
|
|
consumer_account_good_favorites: {
|
|
|
|
|
where: {
|
|
|
|
|
consumer_account_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-02-12 20:31:04 +03:30
|
|
|
})
|
|
|
|
|
|
2026-05-20 11:42:59 +03:30
|
|
|
const mappedGoods = goods.map(good => ({
|
|
|
|
|
...good,
|
|
|
|
|
is_favorite: good.consumer_account_good_favorites.length > 0,
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
await this.redisService.setJson(cacheKey, mappedGoods, this.listCacheTtlSeconds)
|
2026-05-19 15:40:45 +03:30
|
|
|
|
2026-05-20 11:42:59 +03:30
|
|
|
return ResponseMapper.list(mappedGoods)
|
2026-02-12 20:31:04 +03:30
|
|
|
}
|
|
|
|
|
|
2026-04-27 10:45:39 +03:30
|
|
|
async findOne(good_id: string, business_activity_id: string, guild_id: string) {
|
2026-02-12 20:31:04 +03:30
|
|
|
const good = await this.prisma.good.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: good_id,
|
2026-04-27 10:45:39 +03:30
|
|
|
business_activity: {
|
|
|
|
|
id: business_activity_id,
|
|
|
|
|
guild_id,
|
2026-03-29 18:06:41 +03:30
|
|
|
},
|
2026-02-12 20:31:04 +03:30
|
|
|
},
|
2026-03-29 18:06:41 +03:30
|
|
|
select: this.defaultSelect,
|
2026-02-12 20:31:04 +03:30
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.single(good)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 10:45:39 +03:30
|
|
|
async create(data: CreateGoodDto, business_activity_id: string) {
|
2026-05-01 19:43:59 +03:30
|
|
|
const { category_id, sku_id, measure_unit_id, ...rest } = data
|
2026-02-12 20:31:04 +03:30
|
|
|
|
|
|
|
|
const dataToCreate = {
|
|
|
|
|
...rest,
|
2026-04-27 10:45:39 +03:30
|
|
|
business_activity_id,
|
2026-03-07 11:25:11 +03:30
|
|
|
connect: {
|
|
|
|
|
category: {
|
2026-02-12 20:31:04 +03:30
|
|
|
id: category_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const good = await this.prisma.good.create({
|
2026-03-07 11:25:11 +03:30
|
|
|
data: {
|
|
|
|
|
...rest,
|
2026-05-01 19:43:59 +03:30
|
|
|
measure_unit: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: measure_unit_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
sku: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: sku_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-03-07 11:25:11 +03:30
|
|
|
category: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: category_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-04-27 10:45:39 +03:30
|
|
|
business_activity: {
|
2026-03-07 11:25:11 +03:30
|
|
|
connect: {
|
2026-04-27 10:45:39 +03:30
|
|
|
id: business_activity_id,
|
2026-03-07 11:25:11 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-03-29 18:06:41 +03:30
|
|
|
select: this.defaultSelect,
|
2026-02-12 20:31:04 +03:30
|
|
|
})
|
2026-02-16 20:51:34 +03:30
|
|
|
|
2026-05-19 15:40:45 +03:30
|
|
|
await this.posCacheInvalidationService.invalidateGoodsListByBusinessActivity(
|
|
|
|
|
business_activity_id,
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-12 20:31:04 +03:30
|
|
|
return ResponseMapper.create(good)
|
|
|
|
|
}
|
2026-05-19 15:40:45 +03:30
|
|
|
|
|
|
|
|
async update(id: string, data: UpdateGoodDto, business_activity_id: string) {
|
|
|
|
|
const { category_id, sku_id, measure_unit_id, ...rest } = data
|
|
|
|
|
|
|
|
|
|
const good = await this.prisma.good.update({
|
|
|
|
|
where: {
|
|
|
|
|
id,
|
|
|
|
|
business_activity_id,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
...rest,
|
|
|
|
|
...(measure_unit_id
|
|
|
|
|
? {
|
|
|
|
|
measure_unit: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: measure_unit_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
: {}),
|
|
|
|
|
...(sku_id
|
|
|
|
|
? {
|
|
|
|
|
sku: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: sku_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
: {}),
|
|
|
|
|
...(category_id
|
|
|
|
|
? {
|
|
|
|
|
category: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: category_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
: {}),
|
|
|
|
|
},
|
|
|
|
|
select: this.defaultSelect,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await this.posCacheInvalidationService.invalidateGoodsListByBusinessActivity(
|
|
|
|
|
business_activity_id,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.update(good)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async delete(id: string, business_activity_id: string) {
|
|
|
|
|
await this.prisma.good.delete({
|
|
|
|
|
where: {
|
|
|
|
|
id,
|
|
|
|
|
business_activity_id,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await this.posCacheInvalidationService.invalidateGoodsListByBusinessActivity(
|
|
|
|
|
business_activity_id,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.delete()
|
|
|
|
|
}
|
2026-02-12 20:31:04 +03:30
|
|
|
}
|