251206:1710 specs: frontend plan P1,P3 wait Verification
This commit is contained in:
@@ -7,37 +7,49 @@ import {
|
||||
IsBoolean,
|
||||
IsInt,
|
||||
} from 'class-validator';
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
|
||||
export class CreateUserDto {
|
||||
@ApiProperty({ description: 'Username', example: 'john_doe' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
username!: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Password (min 6 chars)',
|
||||
example: 'password123',
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@MinLength(6, { message: 'Password must be at least 6 characters' })
|
||||
password!: string;
|
||||
|
||||
@ApiProperty({ description: 'Email address', example: 'john.d@example.com' })
|
||||
@IsEmail()
|
||||
@IsNotEmpty()
|
||||
email!: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'First name', example: 'John' })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
firstName?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'Last name', example: 'Doe' })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
lastName?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'Line ID', example: 'john.line' })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
lineId?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'Primary Organization ID', example: 1 })
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
primaryOrganizationId?: number; // รับเป็น ID ของ Organization
|
||||
|
||||
@ApiPropertyOptional({ description: 'Is user active?', default: true })
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
isActive?: boolean;
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
|
||||
import {
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
Column,
|
||||
ManyToMany,
|
||||
JoinTable,
|
||||
} from 'typeorm';
|
||||
import { Permission } from './permission.entity';
|
||||
|
||||
export enum RoleScope {
|
||||
GLOBAL = 'Global',
|
||||
@@ -26,4 +33,15 @@ export class Role {
|
||||
|
||||
@Column({ name: 'is_system', default: false })
|
||||
isSystem!: boolean;
|
||||
|
||||
@ManyToMany(() => Permission)
|
||||
@JoinTable({
|
||||
name: 'role_permissions',
|
||||
joinColumn: { name: 'role_id', referencedColumnName: 'roleId' },
|
||||
inverseJoinColumn: {
|
||||
name: 'permission_id',
|
||||
referencedColumnName: 'permissionId',
|
||||
},
|
||||
})
|
||||
permissions?: Permission[];
|
||||
}
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
// File: src/modules/user/user.controller.ts
|
||||
// บันทึกการแก้ไข: เพิ่ม Endpoints สำหรับ User Preferences (T1.3)
|
||||
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
@@ -12,18 +9,25 @@ import {
|
||||
UseGuards,
|
||||
ParseIntPipe,
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import {
|
||||
ApiTags,
|
||||
ApiOperation,
|
||||
ApiBearerAuth,
|
||||
ApiResponse,
|
||||
ApiBody,
|
||||
ApiParam,
|
||||
} from '@nestjs/swagger';
|
||||
|
||||
import { UserService } from './user.service';
|
||||
import { UserAssignmentService } from './user-assignment.service';
|
||||
import { UserPreferenceService } from './user-preference.service'; // ✅ เพิ่ม
|
||||
import { UserPreferenceService } from './user-preference.service';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { UpdateUserDto } from './dto/update-user.dto';
|
||||
import { AssignRoleDto } from './dto/assign-role.dto';
|
||||
import { UpdatePreferenceDto } from './dto/update-preference.dto'; // ✅ เพิ่ม DTO
|
||||
import { UpdatePreferenceDto } from './dto/update-preference.dto';
|
||||
|
||||
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
|
||||
import { RbacGuard } from '../../common/guards/rbac.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 { User } from './entities/user.entity';
|
||||
@@ -31,36 +35,39 @@ import { User } from './entities/user.entity';
|
||||
@ApiTags('Users')
|
||||
@ApiBearerAuth()
|
||||
@Controller('users')
|
||||
@UseGuards(JwtAuthGuard, RbacGuard) // RbacGuard จะเช็ค permission
|
||||
@UseGuards(JwtAuthGuard, RbacGuard)
|
||||
export class UserController {
|
||||
constructor(
|
||||
private readonly userService: UserService,
|
||||
private readonly assignmentService: UserAssignmentService,
|
||||
private readonly preferenceService: UserPreferenceService, // ✅ Inject Service
|
||||
private readonly preferenceService: UserPreferenceService
|
||||
) {}
|
||||
|
||||
// --- User Preferences (Me) ---
|
||||
// ต้องวางไว้ก่อน :id เพื่อไม่ให้ route ชนกัน
|
||||
|
||||
@Get('me/preferences')
|
||||
@ApiOperation({ summary: 'Get my preferences' })
|
||||
@UseGuards(JwtAuthGuard) // Bypass RBAC check for self
|
||||
@ApiResponse({ status: 200, description: 'User preferences' })
|
||||
@UseGuards(JwtAuthGuard)
|
||||
getMyPreferences(@CurrentUser() user: User) {
|
||||
return this.preferenceService.findByUser(user.user_id);
|
||||
}
|
||||
|
||||
@Patch('me/preferences')
|
||||
@ApiOperation({ summary: 'Update my preferences' })
|
||||
@UseGuards(JwtAuthGuard) // Bypass RBAC check for self
|
||||
@ApiBody({ type: UpdatePreferenceDto })
|
||||
@ApiResponse({ status: 200, description: 'Preferences updated' })
|
||||
@UseGuards(JwtAuthGuard)
|
||||
updateMyPreferences(
|
||||
@CurrentUser() user: User,
|
||||
@Body() dto: UpdatePreferenceDto,
|
||||
@Body() dto: UpdatePreferenceDto
|
||||
) {
|
||||
return this.preferenceService.update(user.user_id, dto);
|
||||
}
|
||||
|
||||
@Get('me/permissions')
|
||||
@ApiOperation({ summary: 'Get my permissions' })
|
||||
@ApiResponse({ status: 200, description: 'User permissions' })
|
||||
@UseGuards(JwtAuthGuard)
|
||||
getMyPermissions(@CurrentUser() user: User) {
|
||||
return this.userService.getUserPermissions(user.user_id);
|
||||
@@ -70,6 +77,8 @@ export class UserController {
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Create new user' })
|
||||
@ApiBody({ type: CreateUserDto })
|
||||
@ApiResponse({ status: 201, description: 'User created' })
|
||||
@RequirePermission('user.create')
|
||||
create(@Body() createUserDto: CreateUserDto) {
|
||||
return this.userService.create(createUserDto);
|
||||
@@ -77,6 +86,7 @@ export class UserController {
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'List all users' })
|
||||
@ApiResponse({ status: 200, description: 'List of users' })
|
||||
@RequirePermission('user.view')
|
||||
findAll() {
|
||||
return this.userService.findAll();
|
||||
@@ -84,6 +94,8 @@ export class UserController {
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'Get user details' })
|
||||
@ApiParam({ name: 'id', description: 'User ID' })
|
||||
@ApiResponse({ status: 200, description: 'User details' })
|
||||
@RequirePermission('user.view')
|
||||
findOne(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.userService.findOne(id);
|
||||
@@ -91,16 +103,21 @@ export class UserController {
|
||||
|
||||
@Patch(':id')
|
||||
@ApiOperation({ summary: 'Update user' })
|
||||
@ApiParam({ name: 'id', description: 'User ID' })
|
||||
@ApiBody({ type: UpdateUserDto })
|
||||
@ApiResponse({ status: 200, description: 'User updated' })
|
||||
@RequirePermission('user.edit')
|
||||
update(
|
||||
@Param('id', ParseIntPipe) id: number,
|
||||
@Body() updateUserDto: UpdateUserDto,
|
||||
@Body() updateUserDto: UpdateUserDto
|
||||
) {
|
||||
return this.userService.update(id, updateUserDto);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@ApiOperation({ summary: 'Delete user (Soft delete)' })
|
||||
@ApiParam({ name: 'id', description: 'User ID' })
|
||||
@ApiResponse({ status: 200, description: 'User deleted' })
|
||||
@RequirePermission('user.delete')
|
||||
remove(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.userService.remove(id);
|
||||
@@ -110,6 +127,8 @@ export class UserController {
|
||||
|
||||
@Post('assign-role')
|
||||
@ApiOperation({ summary: 'Assign role to user' })
|
||||
@ApiBody({ type: AssignRoleDto })
|
||||
@ApiResponse({ status: 201, description: 'Role assigned' })
|
||||
@RequirePermission('permission.assign')
|
||||
assignRole(@Body() dto: AssignRoleDto, @CurrentUser() user: User) {
|
||||
return this.assignmentService.assignRole(dto, user);
|
||||
|
||||
Reference in New Issue
Block a user