实现用户注销和删除系统

This commit is contained in:
2025-05-18 22:25:05 +08:00
parent 4d9245aabb
commit 1baff0712a
9 changed files with 87 additions and 30 deletions

View File

@@ -1,7 +1,6 @@
import { Module } from '@nestjs/common';
import { AdminController } from './admin.controller';
import { AdminUserController } from './controller/admin-user.controller';
import { AdminUserService } from './service/admin-user.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from 'src/user/entities/user.entity';
import { UserModule } from 'src/user/user.module';
@@ -35,8 +34,5 @@ import { BlogModule } from 'src/blog/blog.module';
AdminWebResourceController,
AdminWebBlogController,
],
providers: [
AdminUserService,
]
})
export class AdminModule { }

View File

@@ -4,6 +4,7 @@ import { CreateDto } from "../dto/admin-user/create.dto";
import { UserService } from "src/user/user.service";
import { UpdateDto } from "../dto/admin-user/update.dto";
import { UpdatePasswordDto } from "../dto/admin-user/update-password.dto";
import { RemoveUserDto } from "../dto/admin-user/remove.dto";
@Controller('admin/user')
export class AdminUserController {
@@ -53,8 +54,9 @@ export class AdminUserController {
@Delete(':userId')
async delete(
@Param('userId', new ParseUUIDPipe({ version: '4' })) userId: string,
@Query() dto: RemoveUserDto,
) {
return this.userService.delete(userId);
return this.userService.delete(userId, dto.soft);
}
@Post(':userId/password')

View File

@@ -0,0 +1,8 @@
import { Transform } from "class-transformer";
import { IsBoolean } from "class-validator";
export class RemoveUserDto {
@Transform(({ value }) => value === 'true')
@IsBoolean({ message: '需指定删除类型' })
soft: boolean;
}

View File

@@ -51,12 +51,23 @@ export class UserService {
}
}
async delete(userId: string): Promise<void> {
const existingUser = await this.userRepository.findOne({ where: { userId } });
async delete(userId: string, soft: boolean) {
const existingUser = await this.userRepository.findOne({ where: { userId }, withDeleted: true });
if (!existingUser) {
throw new BadRequestException('User not found');
throw new BadRequestException('用户不存在');
}
await this.userRepository.softDelete(existingUser.userId);
if (existingUser.deletedAt && soft) {
throw new BadRequestException('账户已注销,不得重复操作')
}
if (!existingUser.deletedAt && !soft) {
throw new BadRequestException('账号未注销,请先注销再执行删除操作')
}
return soft
? await this.userRepository.softDelete(existingUser.userId)
: await this.userRepository.delete(existingUser.userId)
}
hashPassword(password: string, salt: string): string {