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,47 +1,6 @@
|
||||
import { GoodPricingModel } from '@/generated/prisma/enums'
|
||||
import { ApiProperty, PartialType } from '@nestjs/swagger'
|
||||
import { IsEnum, IsNumber, IsOptional, IsString } from 'class-validator'
|
||||
import { CreateGuildGoodDto } from '@/modules/admin/guilds/goods/dto/create-good.dto'
|
||||
import { PartialType } from '@nestjs/swagger'
|
||||
|
||||
export class CreateGoodDto {
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
name: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@ApiProperty({ required: false })
|
||||
description?: string
|
||||
|
||||
@IsString()
|
||||
@ApiProperty({ required: true })
|
||||
sku_id: string
|
||||
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
local_sku: string
|
||||
|
||||
@IsEnum(GoodPricingModel)
|
||||
@ApiProperty({ required: true, enum: GoodPricingModel })
|
||||
pricing_model: GoodPricingModel
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@ApiProperty({ required: false })
|
||||
barcode?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@ApiProperty({ required: false })
|
||||
category_id?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
@ApiProperty({ required: false })
|
||||
base_sale_price?: number
|
||||
|
||||
@IsString()
|
||||
@ApiProperty({ required: true })
|
||||
measure_unit_id: string
|
||||
}
|
||||
export class CreateGoodDto extends CreateGuildGoodDto {}
|
||||
|
||||
export class UpdateGoodDto extends PartialType(CreateGoodDto) {}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import type { GoodsService } from '../goods.service'
|
||||
|
||||
export type GoodsServiceCreateResponseDto = Awaited<ReturnType<GoodsService['create']>>
|
||||
export type GoodsServiceFindAllResponseDto = Awaited<ReturnType<GoodsService['findAll']>>
|
||||
export type GoodsServiceFindOneResponseDto = Awaited<ReturnType<GoodsService['findOne']>>
|
||||
|
||||
@@ -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,84 @@
|
||||
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.invalidatePosGoodsList(guild_id, 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.invalidatePosGoodsList(guild_id, business_id)
|
||||
|
||||
return ResponseMapper.delete()
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ export class GoodsController {
|
||||
}
|
||||
|
||||
// @Post()
|
||||
// create(@Body() data: CreateGoodDto, @PosInfo() { complex_id, guild_id }: IPosPayload) {
|
||||
// return this.goodsService.create(data, complex_id, guild_id)
|
||||
// create(@Body() data: CreateGoodDto, @PosInfo('complex_id') complex_id: string) {
|
||||
// return this.goodsService.create(data, complex_id)
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { PosGoodFavoriteModule } from '../favorite/favorite.module'
|
||||
import { PosGoodFavoriteModule } from './favorite/favorite.module'
|
||||
import { GoodsController } from './goods.controller'
|
||||
import { GoodsService } from './goods.service'
|
||||
|
||||
|
||||
@@ -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