Files
psp_api/src/modules/pos/goods/goods.service.ts
T

209 lines
4.8 KiB
TypeScript
Raw Normal View History

import { ResponseMapper } from '@/common/response/response-mapper'
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
2026-03-29 18:06:41 +03:30
import { GoodSelect } from '@/generated/prisma/models'
import { PrismaService } from '@/prisma/prisma.service'
import { RedisService } from '@/redis/redis.service'
import { Injectable } from '@nestjs/common'
@Injectable()
export class GoodsService {
constructor(
private prisma: PrismaService,
private readonly redisService: RedisService,
) {}
private readonly listCacheTtlSeconds = 300
2026-03-29 18:06:41 +03:30
private readonly defaultSelect: GoodSelect = {
id: true,
name: true,
barcode: true,
created_at: true,
description: true,
sku: {
select: {
id: true,
name: true,
code: true,
2026-05-17 12:04:30 +03:30
VAT: true,
},
},
2026-03-29 18:06:41 +03:30
local_sku: true,
is_default_guild_good: true,
pricing_model: true,
measure_unit: {
select: {
id: true,
name: true,
},
},
image_url: true,
2026-03-29 18:06:41 +03:30
category: {
select: {
id: true,
name: true,
image_url: true,
},
},
}
async findAll(
business_activity_id: string,
guild_id: string,
consumer_account_id: string,
) {
const cacheKey = RedisKeyMaker.posGoodsList(guild_id, business_activity_id)
try {
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
if (cached) {
return ResponseMapper.list(cached)
}
throw new Error('Cache miss')
} catch (error) {
const goods = await this.prisma.good.findMany({
where: {
OR: [
{
is_default_guild_good: true,
category: {
guild_id,
},
},
{
business_activity_id,
},
],
},
select: {
...this.defaultSelect,
consumer_account_good_favorites: {
where: {
consumer_account_id,
2026-03-29 18:06:41 +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)
return ResponseMapper.list(mappedGoods)
}
}
async findOne(good_id: string, business_activity_id: string, guild_id: string) {
const good = await this.prisma.good.findUnique({
where: {
id: good_id,
business_activity: {
id: business_activity_id,
guild_id,
2026-03-29 18:06:41 +03:30
},
},
2026-03-29 18:06:41 +03:30
select: this.defaultSelect,
})
return ResponseMapper.single(good)
}
// async create(data: CreateGoodDto, business_activity_id: string, guild_id: string) {
// const { category_id, sku_id, measure_unit_id, ...rest } = data
// const dataToCreate = {
// ...rest,
// measure_unit: {
// connect: {
// id: measure_unit_id,
// },
// },
// sku: {
// connect: {
// id: sku_id,
// },
// },
// category: {
// connect: {
// id: category_id,
// },
// },
// business_activity: {
// connect: {
// id: business_activity_id,
// },
// },
// }
// const good = await this.prisma.good.create({
// data: {
// ...dataToCreate,
// },
// select: this.defaultSelect,
// })
// await this.posCacheInvalidationService.invalidatePosGoodsList(
// guild_id,
// business_activity_id,
// )
// return ResponseMapper.create(good)
// }
// async update(
// id: string,
// data: UpdateGoodDto,
// business_activity_id: string,
// guild_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.invalidatePosGoodsList(
// guild_id,
// business_activity_id,
// )
// return ResponseMapper.update(good)
// }
}