251206:1710 specs: frontend plan P1,P3 wait Verification
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
// File: src/modules/correspondence/dto/create-correspondence.dto.ts
|
||||
import {
|
||||
IsInt,
|
||||
IsString,
|
||||
@@ -7,42 +6,64 @@ import {
|
||||
IsBoolean,
|
||||
IsObject,
|
||||
} from 'class-validator';
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
|
||||
export class CreateCorrespondenceDto {
|
||||
@ApiProperty({ description: 'Project ID', example: 1 })
|
||||
@IsInt()
|
||||
@IsNotEmpty()
|
||||
projectId!: number;
|
||||
|
||||
@ApiProperty({ description: 'Document Type ID', example: 1 })
|
||||
@IsInt()
|
||||
@IsNotEmpty()
|
||||
typeId!: number; // ID ของประเภทเอกสาร (เช่น RFA, LETTER)
|
||||
|
||||
@ApiPropertyOptional({ description: 'Discipline ID', example: 2 })
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
disciplineId?: number; // [Req 6B] สาขางาน (เช่น GEN, STR)
|
||||
|
||||
@ApiPropertyOptional({ description: 'Sub Type ID', example: 3 })
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
subTypeId?: number; // [Req 6B] ประเภทย่อย (เช่น MAT, SHP สำหรับ Transmittal/RFA)
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Correspondence Title',
|
||||
example: 'Monthly Progress Report',
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
title!: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'Correspondence Description',
|
||||
example: 'Detailed report...',
|
||||
})
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
description?: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'Additional details (JSON)',
|
||||
example: { key: 'value' },
|
||||
})
|
||||
@IsObject()
|
||||
@IsOptional()
|
||||
details?: Record<string, any>; // ข้อมูล JSON (เช่น RFI question)
|
||||
|
||||
@ApiPropertyOptional({ description: 'Is internal document?', default: false })
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
isInternal?: boolean;
|
||||
|
||||
// ✅ เพิ่ม Field สำหรับ Impersonation (เลือกองค์กรผู้ส่ง)
|
||||
@ApiPropertyOptional({
|
||||
description: 'Originator Organization ID (for impersonation)',
|
||||
example: 1,
|
||||
})
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
originatorId?: number;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Entity, Column, PrimaryGeneratedColumn, OneToMany } from 'typeorm';
|
||||
import { BaseEntity } from '../../../common/entities/base.entity.js'; // ถ้าไม่ได้ใช้ BaseEntity ก็ลบออกแล้วใส่ createdAt เอง
|
||||
import { RoutingTemplateStep } from './routing-template-step.entity.js'; // เดี๋ยวสร้าง
|
||||
import { BaseEntity } from '../../../common/entities/base.entity'; // ถ้าไม่ได้ใช้ BaseEntity ก็ลบออกแล้วใส่ createdAt เอง
|
||||
import { RoutingTemplateStep } from './routing-template-step.entity'; // เดี๋ยวสร้าง
|
||||
|
||||
@Entity('correspondence_routing_templates')
|
||||
export class RoutingTemplate {
|
||||
|
||||
@@ -182,7 +182,7 @@ export class DocumentNumberingService implements OnModuleInit, OnModuleDestroy {
|
||||
throw new InternalServerErrorException(
|
||||
'Failed to generate document number after retries.'
|
||||
);
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
this.logger.error(`Error generating number for ${resourceKey}`, error);
|
||||
|
||||
// [P0-4] Log error
|
||||
|
||||
@@ -8,7 +8,6 @@ import {
|
||||
MemoryHealthIndicator,
|
||||
DiskHealthIndicator,
|
||||
} from '@nestjs/terminus';
|
||||
import { MetricsService } from '../services/metrics.service';
|
||||
|
||||
@Controller()
|
||||
export class HealthController {
|
||||
@@ -17,8 +16,7 @@ export class HealthController {
|
||||
private http: HttpHealthIndicator,
|
||||
private db: TypeOrmHealthIndicator,
|
||||
private memory: MemoryHealthIndicator,
|
||||
private disk: DiskHealthIndicator,
|
||||
private metricsService: MetricsService,
|
||||
private disk: DiskHealthIndicator
|
||||
) {}
|
||||
|
||||
@Get('health')
|
||||
@@ -37,9 +35,4 @@ export class HealthController {
|
||||
this.disk.checkStorage('storage', { path: '/', thresholdPercent: 0.9 }),
|
||||
]);
|
||||
}
|
||||
|
||||
@Get('metrics')
|
||||
async getMetrics() {
|
||||
return await this.metricsService.getMetrics();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,11 @@ import { Global, Module } from '@nestjs/common';
|
||||
import { TerminusModule } from '@nestjs/terminus';
|
||||
import { HttpModule } from '@nestjs/axios';
|
||||
import { APP_INTERCEPTOR } from '@nestjs/core';
|
||||
import {
|
||||
PrometheusModule,
|
||||
makeCounterProvider,
|
||||
makeHistogramProvider,
|
||||
} from '@willsoto/nestjs-prometheus';
|
||||
|
||||
// Existing Components
|
||||
import { HealthController } from './controllers/health.controller';
|
||||
@@ -14,21 +19,39 @@ import { PerformanceInterceptor } from '../../common/interceptors/performance.in
|
||||
import { MonitoringController } from './monitoring.controller';
|
||||
import { MonitoringService } from './monitoring.service';
|
||||
|
||||
@Global() // Module นี้เป็น Global (ดีแล้วครับ)
|
||||
@Global()
|
||||
@Module({
|
||||
imports: [TerminusModule, HttpModule],
|
||||
controllers: [
|
||||
HealthController, // ✅ ของเดิม: /health
|
||||
MonitoringController, // ✅ ของใหม่: /monitoring/maintenance
|
||||
imports: [
|
||||
TerminusModule,
|
||||
HttpModule,
|
||||
PrometheusModule.register({
|
||||
path: '/metrics',
|
||||
defaultMetrics: {
|
||||
enabled: true,
|
||||
},
|
||||
}),
|
||||
],
|
||||
controllers: [HealthController, MonitoringController],
|
||||
providers: [
|
||||
MetricsService, // ✅ ของเดิม
|
||||
MonitoringService, // ✅ ของใหม่ (Logic เปิด/ปิด Maintenance)
|
||||
MetricsService,
|
||||
MonitoringService,
|
||||
{
|
||||
provide: APP_INTERCEPTOR,
|
||||
useClass: PerformanceInterceptor, // ✅ ของเดิม (จับเวลา Response Time)
|
||||
useClass: PerformanceInterceptor,
|
||||
},
|
||||
// Metrics Providers
|
||||
makeCounterProvider({
|
||||
name: 'http_requests_total',
|
||||
help: 'Total number of HTTP requests',
|
||||
labelNames: ['method', 'route', 'status_code'],
|
||||
}),
|
||||
makeHistogramProvider({
|
||||
name: 'http_request_duration_seconds',
|
||||
help: 'Duration of HTTP requests in seconds',
|
||||
labelNames: ['method', 'route', 'status_code'],
|
||||
buckets: [0.1, 0.2, 0.5, 1.0, 1.5, 2.0, 5.0],
|
||||
}),
|
||||
],
|
||||
exports: [MetricsService, MonitoringService],
|
||||
exports: [MetricsService, MonitoringService, PrometheusModule],
|
||||
})
|
||||
export class MonitoringModule {}
|
||||
|
||||
@@ -1,54 +1,16 @@
|
||||
// File: src/modules/monitoring/services/metrics.service.ts
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Registry, Counter, Histogram, Gauge } from 'prom-client';
|
||||
import { Counter, Histogram } from 'prom-client';
|
||||
import { InjectMetric } from '@willsoto/nestjs-prometheus';
|
||||
|
||||
@Injectable()
|
||||
export class MetricsService {
|
||||
private readonly registry: Registry;
|
||||
public readonly httpRequestsTotal: Counter<string>;
|
||||
public readonly httpRequestDuration: Histogram<string>;
|
||||
public readonly systemMemoryUsage: Gauge<string>;
|
||||
constructor(
|
||||
@InjectMetric('http_requests_total')
|
||||
public readonly httpRequestsTotal: Counter<string>,
|
||||
@InjectMetric('http_request_duration_seconds')
|
||||
public readonly httpRequestDuration: Histogram<string>
|
||||
) {}
|
||||
|
||||
constructor() {
|
||||
this.registry = new Registry();
|
||||
this.registry.setDefaultLabels({ app: 'lcbp3-backend' });
|
||||
|
||||
// นับจำนวน HTTP Request ทั้งหมด แยกตาม method, route, status_code
|
||||
this.httpRequestsTotal = new Counter({
|
||||
name: 'http_requests_total',
|
||||
help: 'Total number of HTTP requests',
|
||||
labelNames: ['method', 'route', 'status_code'],
|
||||
registers: [this.registry],
|
||||
});
|
||||
|
||||
// วัดระยะเวลา Response Time (Histogram)
|
||||
this.httpRequestDuration = new Histogram({
|
||||
name: 'http_request_duration_seconds',
|
||||
help: 'Duration of HTTP requests in seconds',
|
||||
labelNames: ['method', 'route', 'status_code'],
|
||||
buckets: [0.1, 0.2, 0.5, 1.0, 1.5, 2.0, 5.0], // Buckets สำหรับวัด Latency
|
||||
registers: [this.registry],
|
||||
});
|
||||
|
||||
// วัดการใช้ Memory (Gauge)
|
||||
this.systemMemoryUsage = new Gauge({
|
||||
name: 'system_memory_usage_bytes',
|
||||
help: 'Heap memory usage in bytes',
|
||||
registers: [this.registry],
|
||||
});
|
||||
|
||||
// เริ่มเก็บ Metrics พื้นฐานของ Node.js (Optional)
|
||||
// client.collectDefaultMetrics({ register: this.registry });
|
||||
}
|
||||
|
||||
/**
|
||||
* ดึงข้อมูล Metrics ทั้งหมดในรูปแบบ Text สำหรับ Prometheus Scrape
|
||||
*/
|
||||
async getMetrics(): Promise<string> {
|
||||
// อัปเดต Memory Usage ก่อน Return
|
||||
const memoryUsage = process.memoryUsage();
|
||||
this.systemMemoryUsage.set(memoryUsage.heapUsed);
|
||||
|
||||
return this.registry.metrics();
|
||||
}
|
||||
// Removed manual getMetrics() as PrometheusModule handles /metrics
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ import {
|
||||
ManyToOne,
|
||||
JoinColumn,
|
||||
} from 'typeorm';
|
||||
import { BaseEntity } from '../../../common/entities/base.entity.js';
|
||||
import { Project } from './project.entity.js';
|
||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||
import { Project } from './project.entity';
|
||||
|
||||
@Entity('contracts')
|
||||
export class Contract extends BaseEntity {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
|
||||
import { BaseEntity } from '../../../common/entities/base.entity.js';
|
||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||
|
||||
@Entity('organizations')
|
||||
export class Organization extends BaseEntity {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
|
||||
import { BaseEntity } from '../../../common/entities/base.entity.js';
|
||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||
|
||||
@Entity('projects')
|
||||
export class Project extends BaseEntity {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// File: src/modules/rfa/dto/create-rfa-revision.dto.ts
|
||||
import {
|
||||
IsString,
|
||||
IsNotEmpty,
|
||||
@@ -8,44 +7,76 @@ import {
|
||||
IsObject,
|
||||
IsArray,
|
||||
} from 'class-validator';
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
|
||||
export class CreateRfaRevisionDto {
|
||||
@ApiProperty({ description: 'RFA Title', example: 'RFA for Building A' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
title!: string;
|
||||
|
||||
@ApiProperty({ description: 'RFA Status Code ID', example: 1 })
|
||||
@IsInt()
|
||||
@IsNotEmpty()
|
||||
rfaStatusCodeId!: number;
|
||||
|
||||
@ApiPropertyOptional({ description: 'RFA Approve Code ID', example: 1 })
|
||||
@IsInt()
|
||||
@IsOptional()
|
||||
rfaApproveCodeId?: number;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'Document Date',
|
||||
example: '2025-12-06T00:00:00Z',
|
||||
})
|
||||
@IsDateString()
|
||||
@IsOptional()
|
||||
documentDate?: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'Issued Date',
|
||||
example: '2025-12-06T00:00:00Z',
|
||||
})
|
||||
@IsDateString()
|
||||
@IsOptional()
|
||||
issuedDate?: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'Received Date',
|
||||
example: '2025-12-06T00:00:00Z',
|
||||
})
|
||||
@IsDateString()
|
||||
@IsOptional()
|
||||
receivedDate?: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'Approved Date',
|
||||
example: '2025-12-06T00:00:00Z',
|
||||
})
|
||||
@IsDateString()
|
||||
@IsOptional()
|
||||
approvedDate?: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'Description',
|
||||
example: 'Details about the RFA...',
|
||||
})
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
description?: string;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'Additional Details (JSON)',
|
||||
example: { key: 'value' },
|
||||
})
|
||||
@IsObject()
|
||||
@IsOptional()
|
||||
details?: Record<string, any>;
|
||||
|
||||
@ApiPropertyOptional({
|
||||
description: 'Linked Shop Drawing Revision IDs',
|
||||
example: [1, 2],
|
||||
})
|
||||
@IsArray()
|
||||
@IsOptional()
|
||||
shopDrawingRevisionIds?: number[]; // IDs of linked Shop Drawings
|
||||
|
||||
@@ -8,12 +8,19 @@ import {
|
||||
Post,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import {
|
||||
ApiTags,
|
||||
ApiOperation,
|
||||
ApiResponse,
|
||||
ApiBearerAuth,
|
||||
ApiParam,
|
||||
ApiBody,
|
||||
} from '@nestjs/swagger';
|
||||
|
||||
import { WorkflowActionDto } from '../correspondence/dto/workflow-action.dto';
|
||||
import { User } from '../user/entities/user.entity';
|
||||
import { CreateRfaDto } from './dto/create-rfa.dto';
|
||||
import { SubmitRfaDto } from './dto/submit-rfa.dto'; // ✅ Import DTO ใหม่
|
||||
import { SubmitRfaDto } from './dto/submit-rfa.dto';
|
||||
import { RfaService } from './rfa.service';
|
||||
|
||||
import { Audit } from '../../common/decorators/audit.decorator';
|
||||
@@ -31,6 +38,8 @@ export class RfaController {
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: 'Create new RFA (Draft)' })
|
||||
@ApiBody({ type: CreateRfaDto })
|
||||
@ApiResponse({ status: 201, description: 'RFA created successfully' })
|
||||
@RequirePermission('rfa.create')
|
||||
@Audit('rfa.create', 'rfa')
|
||||
create(@Body() createDto: CreateRfaDto, @CurrentUser() user: User) {
|
||||
@@ -39,30 +48,41 @@ export class RfaController {
|
||||
|
||||
@Post(':id/submit')
|
||||
@ApiOperation({ summary: 'Submit RFA to Workflow' })
|
||||
@ApiParam({ name: 'id', description: 'RFA ID' })
|
||||
@ApiBody({ type: SubmitRfaDto })
|
||||
@ApiResponse({ status: 200, description: 'RFA submitted successfully' })
|
||||
@RequirePermission('rfa.create')
|
||||
@Audit('rfa.submit', 'rfa')
|
||||
submit(
|
||||
@Param('id', ParseIntPipe) id: number,
|
||||
@Body() submitDto: SubmitRfaDto, // ✅ ใช้ DTO
|
||||
@CurrentUser() user: User,
|
||||
@Body() submitDto: SubmitRfaDto,
|
||||
@CurrentUser() user: User
|
||||
) {
|
||||
return this.rfaService.submit(id, submitDto.templateId, user);
|
||||
}
|
||||
|
||||
@Post(':id/action')
|
||||
@ApiOperation({ summary: 'Process Workflow Action (Approve/Reject)' })
|
||||
@ApiParam({ name: 'id', description: 'RFA ID' })
|
||||
@ApiBody({ type: WorkflowActionDto })
|
||||
@ApiResponse({
|
||||
status: 200,
|
||||
description: 'Workflow action processed successfully',
|
||||
})
|
||||
@RequirePermission('workflow.action_review')
|
||||
@Audit('rfa.action', 'rfa')
|
||||
processAction(
|
||||
@Param('id', ParseIntPipe) id: number,
|
||||
@Body() actionDto: WorkflowActionDto,
|
||||
@CurrentUser() user: User,
|
||||
@CurrentUser() user: User
|
||||
) {
|
||||
return this.rfaService.processAction(id, actionDto, user);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: 'Get RFA details with revisions and items' })
|
||||
@ApiParam({ name: 'id', description: 'RFA ID' })
|
||||
@ApiResponse({ status: 200, description: 'RFA details' })
|
||||
@RequirePermission('document.view')
|
||||
findOne(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.rfaService.findOne(id);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -34,7 +34,7 @@ export class WorkflowDslParser {
|
||||
|
||||
// Step 5: Save to database
|
||||
return await this.workflowDefRepo.save(definition);
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
if (error instanceof SyntaxError) {
|
||||
throw new BadRequestException(`Invalid JSON: ${error.message}`);
|
||||
}
|
||||
@@ -132,11 +132,14 @@ export class WorkflowDslParser {
|
||||
*/
|
||||
private buildDefinition(dsl: WorkflowDsl): WorkflowDefinition {
|
||||
const definition = new WorkflowDefinition();
|
||||
definition.name = dsl.name;
|
||||
definition.version = dsl.version;
|
||||
definition.workflow_code = dsl.name;
|
||||
// Map Semver (1.0.0) to version int (1)
|
||||
const majorVersion = parseInt(dsl.version.split('.')[0], 10);
|
||||
definition.version = isNaN(majorVersion) ? 1 : majorVersion;
|
||||
definition.description = dsl.description;
|
||||
definition.dslContent = JSON.stringify(dsl, null, 2); // Pretty print for readability
|
||||
definition.isActive = true;
|
||||
definition.dsl = dsl;
|
||||
definition.compiled = dsl;
|
||||
definition.is_active = true;
|
||||
|
||||
return definition;
|
||||
}
|
||||
@@ -144,7 +147,7 @@ export class WorkflowDslParser {
|
||||
/**
|
||||
* Get parsed DSL from WorkflowDefinition
|
||||
*/
|
||||
async getParsedDsl(definitionId: number): Promise<WorkflowDsl> {
|
||||
async getParsedDsl(definitionId: string): Promise<WorkflowDsl> {
|
||||
const definition = await this.workflowDefRepo.findOne({
|
||||
where: { id: definitionId },
|
||||
});
|
||||
@@ -156,14 +159,14 @@ export class WorkflowDslParser {
|
||||
}
|
||||
|
||||
try {
|
||||
const dsl = JSON.parse(definition.dslContent);
|
||||
const dsl = definition.dsl;
|
||||
return WorkflowDslSchema.parse(dsl);
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
this.logger.error(
|
||||
`Failed to parse stored DSL for definition ${definitionId}`,
|
||||
error
|
||||
);
|
||||
throw new BadRequestException(`Invalid stored DSL: ${error.message}`);
|
||||
throw new BadRequestException(`Invalid stored DSL: ${error?.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,10 +179,10 @@ export class WorkflowDslParser {
|
||||
const dsl = WorkflowDslSchema.parse(rawDsl);
|
||||
this.validateStateMachine(dsl);
|
||||
return { valid: true };
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
return {
|
||||
valid: false,
|
||||
errors: [error.message],
|
||||
errors: [error?.message || 'Unknown validation error'],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ export const RFA_WORKFLOW_EXAMPLE: WorkflowDsl = {
|
||||
config: { status: 'APPROVED' },
|
||||
},
|
||||
{
|
||||
type: 'send_notification',
|
||||
type: 'create_notification',
|
||||
config: {
|
||||
message: 'RFA has been approved',
|
||||
type: 'success',
|
||||
|
||||
Reference in New Issue
Block a user