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,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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user