feat: Implement user role filtering for findAll endpoint and dynamically select user/agent profiles.
This commit is contained in:
@@ -32,6 +32,7 @@ export class UsersController {
|
|||||||
@ApiOperation({ summary: 'Get all users (Admin only)' })
|
@ApiOperation({ summary: 'Get all users (Admin only)' })
|
||||||
@ApiQuery({ name: 'page', required: false, type: Number })
|
@ApiQuery({ name: 'page', required: false, type: Number })
|
||||||
@ApiQuery({ name: 'limit', required: false, type: Number })
|
@ApiQuery({ name: 'limit', required: false, type: Number })
|
||||||
|
@ApiQuery({ name: 'role', required: false, enum: UserRole, description: 'Filter by user role' })
|
||||||
@ApiResponse({
|
@ApiResponse({
|
||||||
status: 200,
|
status: 200,
|
||||||
description: 'List of users',
|
description: 'List of users',
|
||||||
@@ -73,11 +74,12 @@ export class UsersController {
|
|||||||
async findAll(
|
async findAll(
|
||||||
@Query('page') page?: string,
|
@Query('page') page?: string,
|
||||||
@Query('limit') limit?: string,
|
@Query('limit') limit?: string,
|
||||||
|
@Query('role') role?: UserRole,
|
||||||
) {
|
) {
|
||||||
const pageNum = page ? parseInt(page, 10) : 1;
|
const pageNum = page ? parseInt(page, 10) : 1;
|
||||||
const limitNum = limit ? parseInt(limit, 10) : 10;
|
const limitNum = limit ? parseInt(limit, 10) : 10;
|
||||||
|
|
||||||
return this.usersService.findAll(pageNum, limitNum);
|
return this.usersService.findAll(pageNum, limitNum, role);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get(':id')
|
@Get(':id')
|
||||||
|
|||||||
@@ -1,52 +1,62 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { UserStatus, Prisma } from '@prisma/client';
|
import { UserStatus, UserRole, Prisma } from '@prisma/client';
|
||||||
import { PrismaService } from '../prisma/prisma.service';
|
import { PrismaService } from '../prisma/prisma.service';
|
||||||
|
|
||||||
type UserWithProfile = Prisma.UserGetPayload<{
|
type UserWithProfiles = Prisma.UserGetPayload<{
|
||||||
include: { userProfile: true };
|
include: { userProfile: true; agentProfile: true };
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UsersService {
|
export class UsersService {
|
||||||
constructor(private readonly prisma: PrismaService) {}
|
constructor(private readonly prisma: PrismaService) {}
|
||||||
|
|
||||||
async findAll(page = 1, limit = 10) {
|
async findAll(page = 1, limit = 10, role?: UserRole) {
|
||||||
const skip = (page - 1) * limit;
|
const skip = (page - 1) * limit;
|
||||||
|
|
||||||
|
// Build where clause for optional role filter
|
||||||
|
const where: Prisma.UserWhereInput = role ? { role } : {};
|
||||||
|
|
||||||
const [users, total] = await Promise.all([
|
const [users, total] = await Promise.all([
|
||||||
this.prisma.user.findMany({
|
this.prisma.user.findMany({
|
||||||
|
where,
|
||||||
skip,
|
skip,
|
||||||
take: limit,
|
take: limit,
|
||||||
orderBy: { createdAt: 'desc' },
|
orderBy: { createdAt: 'desc' },
|
||||||
include: {
|
include: {
|
||||||
userProfile: true,
|
userProfile: true,
|
||||||
|
agentProfile: true,
|
||||||
},
|
},
|
||||||
}) as Promise<UserWithProfile[]>,
|
}) as Promise<UserWithProfiles[]>,
|
||||||
this.prisma.user.count(),
|
this.prisma.user.count({ where }),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
users: users.map((user) => ({
|
users: users.map((user) => {
|
||||||
id: user.id,
|
// Get the appropriate profile based on role
|
||||||
email: user.email,
|
const profile = user.role === UserRole.AGENT ? user.agentProfile : user.userProfile;
|
||||||
role: user.role,
|
|
||||||
status: user.status,
|
return {
|
||||||
emailVerified: user.emailVerified,
|
id: user.id,
|
||||||
authProvider: user.authProvider,
|
email: user.email,
|
||||||
createdAt: user.createdAt,
|
role: user.role,
|
||||||
lastLoginAt: user.lastLoginAt,
|
status: user.status,
|
||||||
profile: user.userProfile
|
emailVerified: user.emailVerified,
|
||||||
? {
|
authProvider: user.authProvider,
|
||||||
firstName: user.userProfile.firstName,
|
createdAt: user.createdAt,
|
||||||
lastName: user.userProfile.lastName,
|
lastLoginAt: user.lastLoginAt,
|
||||||
avatar: user.userProfile.avatar,
|
profile: profile
|
||||||
phone: user.userProfile.phone,
|
? {
|
||||||
city: user.userProfile.city,
|
firstName: profile.firstName,
|
||||||
state: user.userProfile.state,
|
lastName: profile.lastName,
|
||||||
country: user.userProfile.country,
|
avatar: profile.avatar,
|
||||||
}
|
phone: profile.phone,
|
||||||
: null,
|
city: profile.city,
|
||||||
})),
|
state: profile.state,
|
||||||
|
country: profile.country,
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
};
|
||||||
|
}),
|
||||||
total,
|
total,
|
||||||
page,
|
page,
|
||||||
limit,
|
limit,
|
||||||
@@ -59,13 +69,17 @@ export class UsersService {
|
|||||||
where: { id },
|
where: { id },
|
||||||
include: {
|
include: {
|
||||||
userProfile: true,
|
userProfile: true,
|
||||||
|
agentProfile: true,
|
||||||
},
|
},
|
||||||
})) as UserWithProfile | null;
|
})) as UserWithProfiles | null;
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the appropriate profile based on role
|
||||||
|
const profile = user.role === UserRole.AGENT ? user.agentProfile : user.userProfile;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: user.id,
|
id: user.id,
|
||||||
email: user.email,
|
email: user.email,
|
||||||
@@ -75,7 +89,17 @@ export class UsersService {
|
|||||||
authProvider: user.authProvider,
|
authProvider: user.authProvider,
|
||||||
createdAt: user.createdAt,
|
createdAt: user.createdAt,
|
||||||
lastLoginAt: user.lastLoginAt,
|
lastLoginAt: user.lastLoginAt,
|
||||||
profile: user.userProfile,
|
profile: profile
|
||||||
|
? {
|
||||||
|
firstName: profile.firstName,
|
||||||
|
lastName: profile.lastName,
|
||||||
|
avatar: profile.avatar,
|
||||||
|
phone: profile.phone,
|
||||||
|
city: profile.city,
|
||||||
|
state: profile.state,
|
||||||
|
country: profile.country,
|
||||||
|
}
|
||||||
|
: null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user