refactor(goods): streamline goods service and controller, remove favorites module
- Removed the PosGoodFavorite module, controller, and service to simplify the codebase. - Updated goods service to handle cache invalidation directly. - Refactored goods controller to remove commented-out code and improve clarity. - Introduced OwnedGoods module for better organization of owned goods functionality. - Updated DTOs to extend from existing structures for consistency. - Enhanced cache invalidation logic in goods service and owned goods service.
This commit is contained in:
@@ -1,19 +1,15 @@
|
||||
import { ResponseMapper } from '@/common/response/response-mapper'
|
||||
import { RedisKeyMaker } from '@/common/utils/redisKeyMaker'
|
||||
import { GoodSelect } from '@/generated/prisma/models'
|
||||
import { PosCacheInvalidationService } from '@/modules/pos/cache/pos-cache-invalidation.service'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { RedisService } from '@/redis/redis.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from '../../../common/response/response-mapper'
|
||||
import { PrismaService } from '../../../prisma/prisma.service'
|
||||
import { CreateGoodDto } from './dto/create-good.dto'
|
||||
import { UpdateGoodDto } from './dto/update-good.dto'
|
||||
|
||||
@Injectable()
|
||||
export class GoodsService {
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
private readonly redisService: RedisService,
|
||||
private readonly posCacheInvalidationService: PosCacheInvalidationService,
|
||||
) {}
|
||||
|
||||
private readonly listCacheTtlSeconds = 300
|
||||
@@ -56,44 +52,47 @@ export class GoodsService {
|
||||
guild_id: string,
|
||||
consumer_account_id: string,
|
||||
) {
|
||||
const cacheKey = RedisKeyMaker.posGoodsList(business_activity_id, guild_id)
|
||||
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.list(cached)
|
||||
}
|
||||
|
||||
const goods = await this.prisma.good.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
is_default_guild_good: true,
|
||||
category: {
|
||||
guild_id,
|
||||
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,
|
||||
},
|
||||
},
|
||||
{
|
||||
business_activity_id,
|
||||
},
|
||||
],
|
||||
},
|
||||
select: {
|
||||
...this.defaultSelect,
|
||||
consumer_account_good_favorites: {
|
||||
where: {
|
||||
consumer_account_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
const mappedGoods = goods.map(good => ({
|
||||
...good,
|
||||
is_favorite: good.consumer_account_good_favorites.length > 0,
|
||||
}))
|
||||
const mappedGoods = goods.map(good => ({
|
||||
...good,
|
||||
is_favorite: good.consumer_account_good_favorites.length > 0,
|
||||
}))
|
||||
|
||||
await this.redisService.setJson(cacheKey, mappedGoods, this.listCacheTtlSeconds)
|
||||
await this.redisService.setJson(cacheKey, mappedGoods, this.listCacheTtlSeconds)
|
||||
|
||||
return ResponseMapper.list(mappedGoods)
|
||||
return ResponseMapper.list(mappedGoods)
|
||||
}
|
||||
}
|
||||
|
||||
async findOne(good_id: string, business_activity_id: string, guild_id: string) {
|
||||
@@ -111,113 +110,99 @@ export class GoodsService {
|
||||
return ResponseMapper.single(good)
|
||||
}
|
||||
|
||||
async create(data: CreateGoodDto, business_activity_id: string) {
|
||||
const { category_id, sku_id, measure_unit_id, ...rest } = data
|
||||
// async create(data: CreateGoodDto, business_activity_id: string, guild_id: string) {
|
||||
// const { category_id, sku_id, measure_unit_id, ...rest } = data
|
||||
|
||||
const dataToCreate = {
|
||||
...rest,
|
||||
business_activity_id,
|
||||
connect: {
|
||||
category: {
|
||||
id: category_id,
|
||||
},
|
||||
},
|
||||
}
|
||||
// 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: {
|
||||
...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,
|
||||
},
|
||||
},
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
// const good = await this.prisma.good.create({
|
||||
// data: {
|
||||
// ...dataToCreate,
|
||||
// },
|
||||
// select: this.defaultSelect,
|
||||
// })
|
||||
|
||||
await this.posCacheInvalidationService.invalidateGoodsListByBusinessActivity(
|
||||
business_activity_id,
|
||||
)
|
||||
// await this.posCacheInvalidationService.invalidatePosGoodsList(
|
||||
// guild_id,
|
||||
// business_activity_id,
|
||||
// )
|
||||
|
||||
return ResponseMapper.create(good)
|
||||
}
|
||||
// return ResponseMapper.create(good)
|
||||
// }
|
||||
|
||||
async update(id: string, data: UpdateGoodDto, business_activity_id: string) {
|
||||
const { category_id, sku_id, measure_unit_id, ...rest } = data
|
||||
// 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,
|
||||
})
|
||||
// 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,
|
||||
)
|
||||
// await this.posCacheInvalidationService.invalidatePosGoodsList(
|
||||
// guild_id,
|
||||
// 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()
|
||||
}
|
||||
// return ResponseMapper.update(good)
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user