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 {
|
||||
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+5
-1
@@ -9,9 +9,13 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
||||
};
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { PrismaPg } from '@prisma/adapter-pg';
|
||||
import { Pool } from 'pg';
|
||||
let PrismaService = class PrismaService extends PrismaClient {
|
||||
constructor() {
|
||||
super();
|
||||
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
||||
const adapter = new PrismaPg(pool);
|
||||
super({ adapter });
|
||||
}
|
||||
async onModuleInit() { await this.$connect(); }
|
||||
async onModuleDestroy() { await this.$disconnect(); }
|
||||
|
||||
Vendored
+1
-1
@@ -1 +1 @@
|
||||
{"version":3,"file":"prisma.service.js","sourceRoot":"","sources":["../prisma.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,CASzB"}
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user