Sửa lỗi hiện danh sách trong bảng thêm thành viên
This commit is contained in:
Vendored
+16
-10
@@ -16,7 +16,6 @@ import { Module, Controller, Get, Post, Body, Param, Patch, Delete, ParseUUIDPip
|
||||
import { PrismaService } from './prisma.service.js';
|
||||
import 'dotenv/config';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { AdminGuard } from './admin.guard.js';
|
||||
import { JwtModule, JwtService } from '@nestjs/jwt';
|
||||
import { JwtAuthGuard } from './jwt-auth.guard.js';
|
||||
import { JwtStrategy } from './jwt.strategy.js';
|
||||
@@ -671,8 +670,9 @@ let UserController = class UserController {
|
||||
constructor(prisma) {
|
||||
this.prisma = prisma;
|
||||
}
|
||||
async getAllUsers(q) {
|
||||
return this.prisma.user.findMany({
|
||||
async getAllUsers(req, q) {
|
||||
const currentUserId = req.user?.sub;
|
||||
const users = await this.prisma.user.findMany({
|
||||
where: q
|
||||
? {
|
||||
OR: [
|
||||
@@ -683,6 +683,7 @@ let UserController = class UserController {
|
||||
: undefined,
|
||||
select: { id: true, email: true, name: true, isAdmin: true, isBlocked: true, createdAt: true, phone: true, address: true }
|
||||
});
|
||||
return users.filter((u) => u.id !== currentUserId);
|
||||
}
|
||||
async updateUser(id, data) {
|
||||
if (data.password) {
|
||||
@@ -697,29 +698,35 @@ let UserController = class UserController {
|
||||
}
|
||||
async deleteUser(id) {
|
||||
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' };
|
||||
}
|
||||
async toggleBlock(id) {
|
||||
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;
|
||||
}
|
||||
};
|
||||
__decorate([
|
||||
Get(),
|
||||
__param(0, Query('q')),
|
||||
__param(0, Req()),
|
||||
__param(1, Query('q')),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [String]),
|
||||
__metadata("design:paramtypes", [Object, String]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], UserController.prototype, "getAllUsers", null);
|
||||
__decorate([
|
||||
@@ -746,7 +753,6 @@ __decorate([
|
||||
], UserController.prototype, "toggleBlock", null);
|
||||
UserController = __decorate([
|
||||
Controller('v1/users'),
|
||||
UseGuards(JwtAuthGuard, AdminGuard),
|
||||
__metadata("design:paramtypes", [PrismaService])
|
||||
], UserController);
|
||||
let AppModule = class AppModule {
|
||||
|
||||
Reference in New Issue
Block a user