This commit is contained in:
@@ -16,10 +16,13 @@ import {
|
||||
import { Organization } from '../../organization/entities/organization.entity'; // Adjust path as needed
|
||||
import { UserAssignment } from './user-assignment.entity';
|
||||
import { UserPreference } from './user-preference.entity';
|
||||
import { UuidBaseEntity } from '../../../common/entities/uuid-base.entity';
|
||||
import { Exclude } from 'class-transformer';
|
||||
|
||||
@Entity('users')
|
||||
export class User {
|
||||
export class User extends UuidBaseEntity {
|
||||
@PrimaryGeneratedColumn({ name: 'user_id' })
|
||||
@Exclude()
|
||||
user_id!: number;
|
||||
|
||||
@Column({ unique: true, length: 50 })
|
||||
|
||||
@@ -33,6 +33,7 @@ import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
|
||||
import { RbacGuard } from '../../common/guards/rbac.guard';
|
||||
import { RequirePermission } from '../../common/decorators/require-permission.decorator';
|
||||
import { CurrentUser } from '../../common/decorators/current-user.decorator';
|
||||
import { ParseUuidPipe } from '../../common/pipes/parse-uuid.pipe';
|
||||
import { User } from './entities/user.entity';
|
||||
|
||||
@ApiTags('Users')
|
||||
@@ -123,35 +124,35 @@ export class UserController {
|
||||
return this.userService.findAll(query);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@Get(':uuid')
|
||||
@ApiOperation({ summary: 'Get user details' })
|
||||
@ApiParam({ name: 'id', description: 'User ID' })
|
||||
@ApiParam({ name: 'uuid', description: 'User UUID' })
|
||||
@ApiResponse({ status: 200, description: 'User details' })
|
||||
@RequirePermission('user.view')
|
||||
findOne(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.userService.findOne(id);
|
||||
findOne(@Param('uuid', ParseUuidPipe) uuid: string) {
|
||||
return this.userService.findOneByUuid(uuid);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@Patch(':uuid')
|
||||
@ApiOperation({ summary: 'Update user' })
|
||||
@ApiParam({ name: 'id', description: 'User ID' })
|
||||
@ApiParam({ name: 'uuid', description: 'User UUID' })
|
||||
@ApiBody({ type: UpdateUserDto })
|
||||
@ApiResponse({ status: 200, description: 'User updated' })
|
||||
@RequirePermission('user.edit')
|
||||
update(
|
||||
@Param('id', ParseIntPipe) id: number,
|
||||
@Param('uuid', ParseUuidPipe) uuid: string,
|
||||
@Body() updateUserDto: UpdateUserDto
|
||||
) {
|
||||
return this.userService.update(id, updateUserDto);
|
||||
return this.userService.update(uuid, updateUserDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@Delete(':uuid')
|
||||
@ApiOperation({ summary: 'Delete user (Soft delete)' })
|
||||
@ApiParam({ name: 'id', description: 'User ID' })
|
||||
@ApiParam({ name: 'uuid', description: 'User UUID' })
|
||||
@ApiResponse({ status: 200, description: 'User deleted' })
|
||||
@RequirePermission('user.delete')
|
||||
remove(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.userService.remove(id);
|
||||
remove(@Param('uuid', ParseUuidPipe) uuid: string) {
|
||||
return this.userService.remove(uuid);
|
||||
}
|
||||
|
||||
// --- Role Assignment ---
|
||||
|
||||
@@ -133,13 +133,31 @@ export class UserService {
|
||||
return user;
|
||||
}
|
||||
|
||||
async findOneByUuid(uuid: string): Promise<User> {
|
||||
const user = await this.usersRepository.findOne({
|
||||
where: { uuid },
|
||||
relations: [
|
||||
'preference',
|
||||
'assignments',
|
||||
'assignments.role',
|
||||
'assignments.role.permissions',
|
||||
],
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new NotFoundException(`User with UUID ${uuid} not found`);
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
async findOneByUsername(username: string): Promise<User | null> {
|
||||
return this.usersRepository.findOne({ where: { username } });
|
||||
}
|
||||
|
||||
// 4. แก้ไขข้อมูล
|
||||
async update(id: number, updateUserDto: UpdateUserDto): Promise<User> {
|
||||
const user = await this.findOne(id);
|
||||
async update(uuid: string, updateUserDto: UpdateUserDto): Promise<User> {
|
||||
const user = await this.findOneByUuid(uuid);
|
||||
|
||||
if (updateUserDto.password) {
|
||||
const salt = await bcrypt.genSalt();
|
||||
@@ -150,20 +168,21 @@ export class UserService {
|
||||
const savedUser = await this.usersRepository.save(updatedUser);
|
||||
|
||||
// ⚠️ สำคัญ: เมื่อมีการแก้ไขข้อมูล User ต้องเคลียร์ Cache สิทธิ์เสมอ
|
||||
await this.clearUserCache(id);
|
||||
await this.clearUserCache(user.user_id);
|
||||
|
||||
return savedUser;
|
||||
}
|
||||
|
||||
// 5. ลบผู้ใช้ (Soft Delete)
|
||||
async remove(id: number): Promise<void> {
|
||||
const result = await this.usersRepository.softDelete(id);
|
||||
async remove(uuid: string): Promise<void> {
|
||||
const user = await this.findOneByUuid(uuid);
|
||||
const result = await this.usersRepository.softDelete(user.user_id);
|
||||
|
||||
if (result.affected === 0) {
|
||||
throw new NotFoundException(`User with ID ${id} not found`);
|
||||
throw new NotFoundException(`User with UUID ${uuid} not found`);
|
||||
}
|
||||
// เคลียร์ Cache เมื่อลบ
|
||||
await this.clearUserCache(id);
|
||||
await this.clearUserCache(user.user_id);
|
||||
}
|
||||
|
||||
async findDocControlIdByOrg(organizationId: number): Promise<number | null> {
|
||||
|
||||
Reference in New Issue
Block a user