feat(pos): add favorite functionality for goods
- Implemented PosGoodFavoriteController to handle attaching and detaching favorites for goods. - Created PosGoodFavoriteService to manage the business logic for favorites, including database operations and cache invalidation. - Added PosGoodFavoriteModule to encapsulate the controller and service.
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
import { PosInfo } from '@/common/decorators/posInfo.decorator'
|
||||
import type { IPosPayload } from '@/common/models/posPayload.model'
|
||||
import { Controller, Get, Param } from '@nestjs/common'
|
||||
import { GoodsService } from './goods.service'
|
||||
import type { IPosPayload } from '@/common/models/posPayload.model'
|
||||
|
||||
@Controller('pos/goods')
|
||||
export class GoodsController {
|
||||
constructor(private readonly goodsService: GoodsService) {}
|
||||
|
||||
@Get()
|
||||
findAll(@PosInfo() { business_id, guild_id }: IPosPayload) {
|
||||
return this.goodsService.findAll(business_id, guild_id)
|
||||
findAll(@PosInfo() { business_id, guild_id, consumer_account_id }: IPosPayload) {
|
||||
return this.goodsService.findAll(business_id, guild_id, consumer_account_id)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { PosGoodFavoriteModule } from '../favorite/favorite.module'
|
||||
import { GoodsController } from './goods.controller'
|
||||
import { GoodsService } from './goods.service'
|
||||
|
||||
@Module({
|
||||
imports: [PosGoodFavoriteModule],
|
||||
controllers: [GoodsController],
|
||||
providers: [GoodsService],
|
||||
})
|
||||
|
||||
@@ -51,7 +51,11 @@ export class GoodsService {
|
||||
},
|
||||
}
|
||||
|
||||
async findAll(business_activity_id: string, guild_id: string) {
|
||||
async findAll(
|
||||
business_activity_id: string,
|
||||
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) {
|
||||
@@ -72,12 +76,24 @@ export class GoodsService {
|
||||
},
|
||||
],
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
select: {
|
||||
...this.defaultSelect,
|
||||
consumer_account_good_favorites: {
|
||||
where: {
|
||||
consumer_account_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
await this.redisService.setJson(cacheKey, goods, this.listCacheTtlSeconds)
|
||||
const mappedGoods = goods.map(good => ({
|
||||
...good,
|
||||
is_favorite: good.consumer_account_good_favorites.length > 0,
|
||||
}))
|
||||
|
||||
return ResponseMapper.list(goods)
|
||||
await this.redisService.setJson(cacheKey, mappedGoods, this.listCacheTtlSeconds)
|
||||
|
||||
return ResponseMapper.list(mappedGoods)
|
||||
}
|
||||
|
||||
async findOne(good_id: string, business_activity_id: string, guild_id: string) {
|
||||
|
||||
Reference in New Issue
Block a user