690513:0920 Refactor Workflow module: Lint error #01
This commit is contained in:
@@ -1,5 +1,13 @@
|
||||
// File: src/modules/delegation/delegation.controller.ts
|
||||
import { Controller, Get, Post, Delete, Body, Param, UseGuards } from '@nestjs/common';
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Delete,
|
||||
Body,
|
||||
Param,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
|
||||
import { CurrentUser } from '../../common/decorators/current-user.decorator';
|
||||
import { User } from '../user/entities/user.entity';
|
||||
@@ -25,7 +33,7 @@ export class DelegationController {
|
||||
* สร้าง Delegation ใหม่ (FR-011)
|
||||
*/
|
||||
@Post()
|
||||
create(@CurrentUser() user: User, @Body() dto: CreateDelegationDto) { // eslint-disable-line @typescript-eslint/no-unused-vars
|
||||
create(@CurrentUser() user: User, @Body() dto: CreateDelegationDto) {
|
||||
return this.delegationService.create(user.publicId, dto);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,10 +9,7 @@ import { CircularDetectionService } from './services/circular-detection.service'
|
||||
import { UserModule } from '../user/user.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([Delegation, User]),
|
||||
UserModule,
|
||||
],
|
||||
imports: [TypeOrmModule.forFeature([Delegation, User]), UserModule],
|
||||
providers: [DelegationService, CircularDetectionService],
|
||||
controllers: [DelegationController],
|
||||
exports: [DelegationService],
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
// File: src/modules/delegation/delegation.service.ts
|
||||
import { Injectable, Logger, BadRequestException, NotFoundException } from '@nestjs/common';
|
||||
import {
|
||||
Injectable,
|
||||
Logger,
|
||||
BadRequestException,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Delegation } from './entities/delegation.entity';
|
||||
@@ -16,18 +21,29 @@ export class DelegationService {
|
||||
private readonly delegationRepo: Repository<Delegation>,
|
||||
@InjectRepository(User)
|
||||
private readonly userRepo: Repository<User>,
|
||||
private readonly circularDetectionService: CircularDetectionService,
|
||||
private readonly circularDetectionService: CircularDetectionService
|
||||
) {}
|
||||
|
||||
/**
|
||||
* สร้าง Delegation ใหม่ พร้อมตรวจสอบ Circular (FR-011, FR-012)
|
||||
*/
|
||||
async create(delegatorPublicId: string, dto: CreateDelegationDto): Promise<Delegation> {
|
||||
const delegator = await this.userRepo.findOne({ where: { publicId: delegatorPublicId } });
|
||||
if (!delegator) throw new NotFoundException(`User not found: ${delegatorPublicId}`);
|
||||
async create(
|
||||
delegatorPublicId: string,
|
||||
dto: CreateDelegationDto
|
||||
): Promise<Delegation> {
|
||||
const delegator = await this.userRepo.findOne({
|
||||
where: { publicId: delegatorPublicId },
|
||||
});
|
||||
if (!delegator)
|
||||
throw new NotFoundException(`User not found: ${delegatorPublicId}`);
|
||||
|
||||
const delegate = await this.userRepo.findOne({ where: { publicId: dto.delegateUserPublicId } });
|
||||
if (!delegate) throw new NotFoundException(`Delegate user not found: ${dto.delegateUserPublicId}`);
|
||||
const delegate = await this.userRepo.findOne({
|
||||
where: { publicId: dto.delegateUserPublicId },
|
||||
});
|
||||
if (!delegate)
|
||||
throw new NotFoundException(
|
||||
`Delegate user not found: ${dto.delegateUserPublicId}`
|
||||
);
|
||||
|
||||
// ตรวจสอบ date range
|
||||
if (dto.startDate >= dto.endDate) {
|
||||
@@ -38,12 +54,12 @@ export class DelegationService {
|
||||
const isCircular = await this.circularDetectionService.wouldCreateCircle(
|
||||
delegator.user_id,
|
||||
delegate.user_id,
|
||||
dto.startDate,
|
||||
dto.startDate
|
||||
);
|
||||
|
||||
if (isCircular) {
|
||||
throw new BadRequestException(
|
||||
'Circular delegation detected — this would create a delegation loop',
|
||||
'Circular delegation detected — this would create a delegation loop'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -64,7 +80,9 @@ export class DelegationService {
|
||||
* ดึง Delegations ของ User ทั้งหมด (ในฐานะผู้มอบหมาย)
|
||||
*/
|
||||
async findByDelegator(delegatorPublicId: string): Promise<Delegation[]> {
|
||||
const user = await this.userRepo.findOne({ where: { publicId: delegatorPublicId } });
|
||||
const user = await this.userRepo.findOne({
|
||||
where: { publicId: delegatorPublicId },
|
||||
});
|
||||
if (!user) throw new NotFoundException(delegatorPublicId);
|
||||
|
||||
return this.delegationRepo.find({
|
||||
@@ -78,7 +96,10 @@ export class DelegationService {
|
||||
* ดึง Active Delegations สำหรับ User ณ วันที่กำหนด (FR-013)
|
||||
* ใช้ใน ReviewTaskService ก่อน assign task
|
||||
*/
|
||||
async findActiveDelegate(userId: number, date: Date = new Date()): Promise<User | null> {
|
||||
async findActiveDelegate(
|
||||
userId: number,
|
||||
date: Date = new Date()
|
||||
): Promise<User | null> {
|
||||
const delegation = await this.delegationRepo
|
||||
.createQueryBuilder('d')
|
||||
.innerJoinAndSelect('d.delegate', 'delegate')
|
||||
@@ -100,10 +121,13 @@ export class DelegationService {
|
||||
where: { publicId },
|
||||
});
|
||||
|
||||
if (!delegation) throw new NotFoundException(`Delegation not found: ${publicId}`);
|
||||
if (!delegation)
|
||||
throw new NotFoundException(`Delegation not found: ${publicId}`);
|
||||
|
||||
// ตรวจสอบ ownership
|
||||
const delegator = await this.userRepo.findOne({ where: { publicId: delegatorPublicId } });
|
||||
const delegator = await this.userRepo.findOne({
|
||||
where: { publicId: delegatorPublicId },
|
||||
});
|
||||
if (!delegator || delegation.delegatorUserId !== delegator.user_id) {
|
||||
throw new BadRequestException('You can only revoke your own delegations');
|
||||
}
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
// File: src/modules/delegation/dto/create-delegation.dto.ts
|
||||
import { IsDate, IsEnum, IsOptional, IsString, IsUUID, MaxLength } from 'class-validator';
|
||||
import {
|
||||
IsDate,
|
||||
IsEnum,
|
||||
IsOptional,
|
||||
IsString,
|
||||
IsUUID,
|
||||
MaxLength,
|
||||
} from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
import { DelegationScope } from '../../common/enums/review.enums';
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import { Delegation } from '../entities/delegation.entity';
|
||||
export class CircularDetectionService {
|
||||
constructor(
|
||||
@InjectRepository(Delegation)
|
||||
private readonly delegationRepo: Repository<Delegation>,
|
||||
private readonly delegationRepo: Repository<Delegation>
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -24,7 +24,7 @@ export class CircularDetectionService {
|
||||
async wouldCreateCircle(
|
||||
proposedFrom: number,
|
||||
proposedTo: number,
|
||||
today: Date = new Date(),
|
||||
today: Date = new Date()
|
||||
): Promise<boolean> {
|
||||
// ถ้า A→B และ proposedFrom=B, proposedTo=A → circular ชัดเจน
|
||||
if (proposedFrom === proposedTo) return true;
|
||||
@@ -57,7 +57,7 @@ export class CircularDetectionService {
|
||||
current: number,
|
||||
target: number,
|
||||
graph: Map<number, number[]>,
|
||||
visited: Set<number>,
|
||||
visited: Set<number>
|
||||
): boolean {
|
||||
if (current === target) return true;
|
||||
if (visited.has(current)) return false;
|
||||
|
||||
Reference in New Issue
Block a user