Cài đặt tính năng quản lí người dùng cho admin
This commit is contained in:
Vendored
+79
-2
@@ -11,10 +11,11 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||
return function (target, key) { decorator(target, key, paramIndex); }
|
||||
};
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { Module, Controller, Get, Post, Body, Param, ParseIntPipe, NotFoundException, BadRequestException, UnauthorizedException } from '@nestjs/common';
|
||||
import { Module, Controller, Get, Post, Body, Param, Patch, Delete, ParseIntPipe, NotFoundException, BadRequestException, UnauthorizedException, UseGuards } from '@nestjs/common';
|
||||
import { PrismaService } from './prisma.service.js';
|
||||
import 'dotenv/config';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { AdminGuard } from './admin.guard.js';
|
||||
let AppController = class AppController {
|
||||
getHello() {
|
||||
return 'Travel Planning API is running!';
|
||||
@@ -44,6 +45,9 @@ let AuthController = class AuthController {
|
||||
if (!user || !(await bcrypt.compare(password, user.passwordHash))) {
|
||||
throw new UnauthorizedException('Email hoặc mật khẩu không chính xác');
|
||||
}
|
||||
if (user.isBlocked) {
|
||||
throw new UnauthorizedException('Tài khoản của bạn đã bị khóa. Vui lòng liên hệ quản trị viên.');
|
||||
}
|
||||
return {
|
||||
access_token: 'simulated-jwt-token',
|
||||
user: {
|
||||
@@ -147,11 +151,84 @@ TourController = __decorate([
|
||||
Controller('v1/tours'),
|
||||
__metadata("design:paramtypes", [PrismaService])
|
||||
], TourController);
|
||||
let UserController = class UserController {
|
||||
constructor(prisma) {
|
||||
this.prisma = prisma;
|
||||
}
|
||||
async getAllUsers() {
|
||||
return this.prisma.user.findMany({
|
||||
select: { id: true, email: true, name: true, isAdmin: true, isBlocked: true, createdAt: true }
|
||||
});
|
||||
}
|
||||
async updateUser(id, data) {
|
||||
if (data.password) {
|
||||
data.passwordHash = await bcrypt.hash(data.password, 10);
|
||||
delete data.password;
|
||||
}
|
||||
return this.prisma.user.update({
|
||||
where: { id },
|
||||
data,
|
||||
select: { id: true, email: true, name: true, isAdmin: true, isBlocked: true }
|
||||
});
|
||||
}
|
||||
async deleteUser(id) {
|
||||
const user = await this.prisma.user.findUnique({ where: { id } });
|
||||
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.user.delete({ where: { id } });
|
||||
return { success: true };
|
||||
}
|
||||
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({
|
||||
where: { id },
|
||||
data: { isBlocked: !user.isBlocked }
|
||||
});
|
||||
}
|
||||
};
|
||||
__decorate([
|
||||
Get(),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", []),
|
||||
__metadata("design:returntype", Promise)
|
||||
], UserController.prototype, "getAllUsers", null);
|
||||
__decorate([
|
||||
Patch(':id'),
|
||||
__param(0, Param('id', ParseIntPipe)),
|
||||
__param(1, Body()),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Number, Object]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], UserController.prototype, "updateUser", null);
|
||||
__decorate([
|
||||
Delete(':id'),
|
||||
__param(0, Param('id', ParseIntPipe)),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Number]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], UserController.prototype, "deleteUser", null);
|
||||
__decorate([
|
||||
Post('block/:id'),
|
||||
__param(0, Param('id', ParseIntPipe)),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Number]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], UserController.prototype, "toggleBlock", null);
|
||||
UserController = __decorate([
|
||||
Controller('v1/users'),
|
||||
UseGuards(AdminGuard),
|
||||
__metadata("design:paramtypes", [PrismaService])
|
||||
], UserController);
|
||||
let AppModule = class AppModule {
|
||||
};
|
||||
AppModule = __decorate([
|
||||
Module({
|
||||
controllers: [AppController, AuthController, TourController],
|
||||
controllers: [AppController, AuthController, TourController, UserController],
|
||||
providers: [PrismaService],
|
||||
exports: [PrismaService]
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user