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:
@@ -0,0 +1,25 @@
|
||||
import { PosInfo } from '@/common/decorators/posInfo.decorator'
|
||||
import type { IPosPayload } from '@/common/models/posPayload.model'
|
||||
import { Controller, Delete, Param, Post } from '@nestjs/common'
|
||||
import { PosGoodFavoriteService } from './favorite.service'
|
||||
|
||||
@Controller('pos/goods/:good_id/favorite')
|
||||
export class PosGoodFavoriteController {
|
||||
constructor(private readonly service: PosGoodFavoriteService) {}
|
||||
|
||||
@Post()
|
||||
attach(
|
||||
@Param('good_id') good_id: string,
|
||||
@PosInfo() { business_id, guild_id, consumer_account_id }: IPosPayload,
|
||||
) {
|
||||
return this.service.attach(good_id, business_id, guild_id, consumer_account_id)
|
||||
}
|
||||
|
||||
@Delete()
|
||||
detach(
|
||||
@Param('good_id') good_id: string,
|
||||
@PosInfo() { business_id, guild_id, consumer_account_id }: IPosPayload,
|
||||
) {
|
||||
return this.service.detach(good_id, business_id, guild_id, consumer_account_id)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { PosGoodFavoriteController } from './favorite.controller'
|
||||
import { PosGoodFavoriteService } from './favorite.service'
|
||||
|
||||
@Module({
|
||||
controllers: [PosGoodFavoriteController],
|
||||
providers: [PosGoodFavoriteService],
|
||||
})
|
||||
export class PosGoodFavoriteModule {}
|
||||
@@ -0,0 +1,88 @@
|
||||
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 { PosCacheInvalidationService } from '../cache/pos-cache-invalidation.service'
|
||||
|
||||
@Injectable()
|
||||
export class PosGoodFavoriteService {
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
|
||||
private readonly redisService: RedisService,
|
||||
private readonly posCacheInvalidationService: PosCacheInvalidationService,
|
||||
) {}
|
||||
|
||||
async attach(
|
||||
good_id: string,
|
||||
business_id: string,
|
||||
guild_id: string,
|
||||
consumer_account_id: string,
|
||||
) {
|
||||
const favorite = await this.prisma.consumerAccountGoodFavorite.upsert({
|
||||
where: {
|
||||
consumer_account_id_good_id: {
|
||||
consumer_account_id,
|
||||
good_id,
|
||||
},
|
||||
good: {
|
||||
OR: [
|
||||
{
|
||||
business_activity_id: business_id,
|
||||
},
|
||||
{
|
||||
sku: {
|
||||
guild_id,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
update: {},
|
||||
create: {
|
||||
consumer_account_id,
|
||||
good_id,
|
||||
},
|
||||
})
|
||||
|
||||
await this.posCacheInvalidationService.invalidateGoodsListByBusinessActivity(
|
||||
business_id,
|
||||
)
|
||||
|
||||
return ResponseMapper.create(favorite)
|
||||
}
|
||||
|
||||
async detach(
|
||||
good_id: string,
|
||||
business_id: string,
|
||||
guild_id: string,
|
||||
consumer_account_id: string,
|
||||
) {
|
||||
const favorite = await this.prisma.consumerAccountGoodFavorite.delete({
|
||||
where: {
|
||||
consumer_account_id_good_id: {
|
||||
consumer_account_id,
|
||||
good_id,
|
||||
},
|
||||
good: {
|
||||
OR: [
|
||||
{
|
||||
business_activity_id: business_id,
|
||||
},
|
||||
{
|
||||
sku: {
|
||||
guild_id,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
await this.posCacheInvalidationService.invalidateGoodsListByBusinessActivity(
|
||||
business_id,
|
||||
)
|
||||
|
||||
return ResponseMapper.delete()
|
||||
}
|
||||
}
|
||||
@@ -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