feat: 支持会员管理筛选

This commit is contained in:
richarjiang
2026-04-07 09:22:58 +08:00
parent 58c7588a96
commit 0ca93ec97e
6 changed files with 129 additions and 8 deletions

View File

@@ -1,8 +1,10 @@
import { Injectable, NotFoundException } from '@nestjs/common'
import { MembershipStatus, BookingStatus, UserRole } from '@mp-pilates/shared'
import { MembershipStatus, BookingStatus, UserRole, CardTypeCategory } from '@mp-pilates/shared'
import type { PaginatedData, UserProfileResponse, UserStatsResponse } from '@mp-pilates/shared'
import { PrismaService } from '../prisma/prisma.service'
const VALID_CARD_TYPES = new Set<string>(Object.values(CardTypeCategory))
@Injectable()
export class UserService {
constructor(private readonly prisma: PrismaService) {}
@@ -124,6 +126,7 @@ export class UserService {
page: number,
limit: number,
search?: string,
cardType?: string,
): Promise<PaginatedData<{
userId: string
openid: string
@@ -134,7 +137,16 @@ export class UserService {
completedBookings: number
cancelledBookings: number
}>> {
const where = search
const where: {
OR?: Array<{ [key: string]: unknown }>
memberships?: {
some: {
status: MembershipStatus
cardType?: { type: CardTypeCategory }
}
}
NOT?: { memberships?: { some: { status: MembershipStatus } } }
} = search
? {
OR: [
{ nickname: { contains: search, mode: 'insensitive' as const } },
@@ -144,6 +156,18 @@ export class UserService {
}
: {}
// cardType filter: NONE = no active membership, otherwise filter by card type category
if (cardType === 'NONE') {
where.NOT = { memberships: { some: { status: MembershipStatus.ACTIVE } } }
} else if (cardType && VALID_CARD_TYPES.has(cardType)) {
where.memberships = {
some: {
status: MembershipStatus.ACTIVE,
cardType: { type: cardType as CardTypeCategory },
},
}
}
const [users, total] = await Promise.all([
this.prisma.user.findMany({
where,