2026-04-18 12:14:19 +03:30
|
|
|
import { UploadedFileTypes } from '@/common/enums/enums'
|
2026-05-19 15:40:45 +03:30
|
|
|
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
|
2026-04-18 12:14:19 +03:30
|
|
|
import { GoodSelect, GoodWhereInput } from '@/generated/prisma/models'
|
2026-05-19 15:40:45 +03:30
|
|
|
import { AdminGuildCacheInvalidationService } from '@/modules/admin/guilds/cache/admin-guild-cache-invalidation.service'
|
2026-04-18 12:14:19 +03:30
|
|
|
import { UploaderService } from '@/modules/uploader/uploader.service'
|
2026-03-07 11:25:11 +03:30
|
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
2026-05-19 15:40:45 +03:30
|
|
|
import { RedisService } from '@/redis/redis.service'
|
2026-03-07 11:25:11 +03:30
|
|
|
import { Injectable } from '@nestjs/common'
|
|
|
|
|
import { ResponseMapper } from 'common/response/response-mapper'
|
2026-04-18 12:14:19 +03:30
|
|
|
import { CreateGuildGoodDto, UpdateGuildGoodDto } from './dto/create-good.dto'
|
2026-03-07 11:25:11 +03:30
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class GoodsService {
|
2026-04-18 12:14:19 +03:30
|
|
|
constructor(
|
|
|
|
|
private prisma: PrismaService,
|
|
|
|
|
private readonly uploaderService: UploaderService,
|
2026-05-19 15:40:45 +03:30
|
|
|
private readonly redisService: RedisService,
|
|
|
|
|
private readonly cacheInvalidationService: AdminGuildCacheInvalidationService,
|
2026-04-18 12:14:19 +03:30
|
|
|
) {}
|
2026-03-07 11:25:11 +03:30
|
|
|
|
2026-05-19 15:40:45 +03:30
|
|
|
private readonly listCacheTtlSeconds = 300
|
|
|
|
|
|
2026-04-18 12:14:19 +03:30
|
|
|
private readonly defaultSelect: GoodSelect = {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
pricing_model: true,
|
|
|
|
|
image_url: 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-01 19:43:59 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
measure_unit: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
2026-05-16 14:49:23 +03:30
|
|
|
code: true,
|
2026-05-01 19:43:59 +03:30
|
|
|
},
|
|
|
|
|
},
|
2026-04-18 12:14:19 +03:30
|
|
|
category: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly defaultWhere = (guild_id: string): GoodWhereInput => ({
|
2026-03-07 11:25:11 +03:30
|
|
|
is_default_guild_good: true,
|
2026-04-18 12:14:19 +03:30
|
|
|
category: {
|
|
|
|
|
guild_id,
|
|
|
|
|
},
|
|
|
|
|
})
|
2026-03-07 11:25:11 +03:30
|
|
|
|
|
|
|
|
async findAll(guildId: string) {
|
2026-05-19 15:40:45 +03:30
|
|
|
const cacheKey = RedisKeyMaker.guildGoodsList(guildId)
|
|
|
|
|
const cached = await this.redisService.getJson<{ goods: unknown[]; total: number }>(
|
|
|
|
|
cacheKey,
|
|
|
|
|
)
|
|
|
|
|
if (cached) {
|
|
|
|
|
return ResponseMapper.paginate(cached.goods, { total: cached.total })
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-26 09:54:48 +03:30
|
|
|
const [goods, total] = await this.prisma.$transaction([
|
2026-03-07 11:25:11 +03:30
|
|
|
this.prisma.good.findMany({
|
2026-04-18 12:14:19 +03:30
|
|
|
where: this.defaultWhere(guildId),
|
|
|
|
|
select: this.defaultSelect,
|
2026-03-07 11:25:11 +03:30
|
|
|
}),
|
|
|
|
|
this.prisma.good.count(),
|
|
|
|
|
])
|
2026-05-19 15:40:45 +03:30
|
|
|
|
|
|
|
|
await this.redisService.setJson(cacheKey, { goods, total }, this.listCacheTtlSeconds)
|
|
|
|
|
|
2026-03-07 11:25:11 +03:30
|
|
|
return ResponseMapper.paginate(goods, {
|
2026-04-26 09:54:48 +03:30
|
|
|
total,
|
2026-03-07 11:25:11 +03:30
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOne(guild_id: string, id: string) {
|
|
|
|
|
const good = await this.prisma.good.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id,
|
2026-04-18 12:14:19 +03:30
|
|
|
...(this.defaultWhere(guild_id) as any),
|
2026-03-07 11:25:11 +03:30
|
|
|
},
|
2026-04-18 12:14:19 +03:30
|
|
|
select: this.defaultSelect,
|
|
|
|
|
})
|
2026-03-07 11:25:11 +03:30
|
|
|
|
2026-04-18 12:14:19 +03:30
|
|
|
return ResponseMapper.single(good)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create(data: CreateGuildGoodDto, file: Express.Multer.File) {
|
2026-05-01 19:43:59 +03:30
|
|
|
const { category_id, measure_unit_id, sku_id, ...rest } = data
|
2026-05-19 15:40:45 +03:30
|
|
|
const category = await this.prisma.goodCategory.findUnique({
|
|
|
|
|
where: { id: category_id },
|
|
|
|
|
select: { guild_id: true },
|
|
|
|
|
})
|
2026-04-18 12:14:19 +03:30
|
|
|
|
|
|
|
|
const good = await this.prisma.$transaction(async tx => {
|
|
|
|
|
let image_url = ''
|
|
|
|
|
if (file) {
|
|
|
|
|
const uploadedUrl = await this.uploaderService.uploadFile(
|
|
|
|
|
file,
|
|
|
|
|
UploadedFileTypes.GOOD,
|
|
|
|
|
)
|
|
|
|
|
image_url = uploadedUrl || ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await tx.good.create({
|
|
|
|
|
data: {
|
|
|
|
|
...rest,
|
|
|
|
|
is_default_guild_good: true,
|
|
|
|
|
image_url,
|
2026-05-01 19:43:59 +03:30
|
|
|
sku: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: sku_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
measure_unit: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: measure_unit_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-04-18 12:14:19 +03:30
|
|
|
category: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: category_id,
|
|
|
|
|
},
|
2026-03-11 20:42:34 +03:30
|
|
|
},
|
|
|
|
|
},
|
2026-04-18 12:14:19 +03:30
|
|
|
select: this.defaultSelect,
|
|
|
|
|
})
|
2026-03-07 11:25:11 +03:30
|
|
|
})
|
|
|
|
|
|
2026-05-19 15:40:45 +03:30
|
|
|
if (category?.guild_id) {
|
|
|
|
|
await this.cacheInvalidationService.invalidateGoodsList(category.guild_id)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-18 12:14:19 +03:30
|
|
|
return ResponseMapper.create(good)
|
2026-03-07 11:25:11 +03:30
|
|
|
}
|
|
|
|
|
|
2026-04-18 12:14:19 +03:30
|
|
|
async update(id: string, data: UpdateGuildGoodDto, file: Express.Multer.File) {
|
2026-05-01 19:43:59 +03:30
|
|
|
const { category_id, measure_unit_id, sku_id, ...rest } = data
|
2026-05-19 15:40:45 +03:30
|
|
|
const prevGood = await this.prisma.good.findUnique({
|
|
|
|
|
where: { id },
|
|
|
|
|
select: { category: { select: { guild_id: true } } },
|
|
|
|
|
})
|
|
|
|
|
const nextCategory = category_id
|
|
|
|
|
? await this.prisma.goodCategory.findUnique({
|
|
|
|
|
where: { id: category_id },
|
|
|
|
|
select: { guild_id: true },
|
|
|
|
|
})
|
|
|
|
|
: null
|
2026-03-07 11:25:11 +03:30
|
|
|
|
2026-04-18 12:14:19 +03:30
|
|
|
const good = await this.prisma.$transaction(async tx => {
|
|
|
|
|
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: { id },
|
|
|
|
|
data: {
|
|
|
|
|
...rest,
|
|
|
|
|
is_default_guild_good: true,
|
|
|
|
|
image_url,
|
2026-05-01 19:43:59 +03:30
|
|
|
sku: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: sku_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
measure_unit: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: measure_unit_id,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-04-18 12:14:19 +03:30
|
|
|
category: {
|
|
|
|
|
connect: {
|
|
|
|
|
id: category_id,
|
|
|
|
|
},
|
2026-03-07 11:25:11 +03:30
|
|
|
},
|
|
|
|
|
},
|
2026-04-18 12:14:19 +03:30
|
|
|
select: this.defaultSelect,
|
|
|
|
|
})
|
2026-03-07 11:25:11 +03:30
|
|
|
})
|
|
|
|
|
|
2026-05-19 15:40:45 +03:30
|
|
|
const cacheGuildIds = new Set<string>()
|
|
|
|
|
if (prevGood?.category?.guild_id) {
|
|
|
|
|
cacheGuildIds.add(prevGood.category.guild_id)
|
|
|
|
|
}
|
|
|
|
|
if (nextCategory?.guild_id) {
|
|
|
|
|
cacheGuildIds.add(nextCategory.guild_id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const guildId of cacheGuildIds) {
|
|
|
|
|
await this.cacheInvalidationService.invalidateGoodsList(guildId)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-07 11:25:11 +03:30
|
|
|
return ResponseMapper.create(good)
|
|
|
|
|
}
|
|
|
|
|
}
|