Sửa lỗi hiện danh sách trong bảng thêm thành viên
This commit is contained in:
@@ -598,13 +598,13 @@ class RoutingController {
|
||||
}
|
||||
|
||||
@Controller('v1/users')
|
||||
@UseGuards(JwtAuthGuard, AdminGuard)
|
||||
class UserController {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
@Get()
|
||||
async getAllUsers(@Query('q') q?: string) {
|
||||
return this.prisma.user.findMany({
|
||||
async getAllUsers(@Req() req: any, @Query('q') q?: string) {
|
||||
const currentUserId = req.user?.sub;
|
||||
const users = await this.prisma.user.findMany({
|
||||
where: q
|
||||
? {
|
||||
OR: [
|
||||
@@ -615,11 +615,11 @@ class UserController {
|
||||
: undefined,
|
||||
select: { id: true, email: true, name: true, isAdmin: true, isBlocked: true, createdAt: true, phone: true, address: true }
|
||||
});
|
||||
return users.filter((u: any) => u.id !== currentUserId);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
async updateUser(@Param('id', ParseUUIDPipe) id: string, @Body() data: any) {
|
||||
// Nếu đổi mật khẩu thì cần hash
|
||||
if (data.password) {
|
||||
data.passwordHash = await bcrypt.hash(data.password, 10);
|
||||
delete data.password;
|
||||
@@ -633,28 +633,31 @@ class UserController {
|
||||
|
||||
@Delete(':id')
|
||||
async deleteUser(@Param('id', ParseUUIDPipe) id: string) {
|
||||
// Không cho phép tự xóa chính mình hoặc xóa admin cuối cùng (logic đơn giản)
|
||||
const user = await this.prisma.user.findUnique({ where: { id } });
|
||||
if (user?.isAdmin) {
|
||||
if (!user) throw new NotFoundException('Không tìm thấy người dùng');
|
||||
if (user.isAdmin) {
|
||||
const adminCount = await this.prisma.user.count({ where: { isAdmin: true } });
|
||||
if (adminCount <= 1) throw new BadRequestException('Không thể xóa Quản trị viên cuối cùng');
|
||||
}
|
||||
await this.prisma.tourParticipant.deleteMany({ where: { userId: id } });
|
||||
await this.prisma.user.delete({ where: { id } });
|
||||
return { success: true };
|
||||
return { message: 'Đã xóa người dùng' };
|
||||
}
|
||||
|
||||
@Post('block/:id')
|
||||
async toggleBlock(@Param('id', ParseUUIDPipe) id: string) {
|
||||
const user = await this.prisma.user.findUnique({ where: { id } });
|
||||
if (!user) throw new NotFoundException('Người dùng không tồn tại');
|
||||
return this.prisma.user.update({
|
||||
const updated = await this.prisma.user.update({
|
||||
where: { id },
|
||||
data: { isBlocked: !user.isBlocked }
|
||||
data: { isBlocked: !user.isBlocked },
|
||||
select: { id: true, email: true, name: true, isBlocked: true }
|
||||
});
|
||||
return updated;
|
||||
}
|
||||
}
|
||||
|
||||
@Module({
|
||||
@Module({
|
||||
imports: [
|
||||
JwtModule.register({
|
||||
secret: process.env.JWT_SECRET || 'super-secret',
|
||||
|
||||
Reference in New Issue
Block a user