This commit is contained in:
@@ -25,7 +25,7 @@ export class CirculationWorkflowService {
|
||||
private readonly circulationRepo: Repository<Circulation>,
|
||||
@InjectRepository(CirculationStatusCode)
|
||||
private readonly statusRepo: Repository<CirculationStatusCode>,
|
||||
private readonly dataSource: DataSource,
|
||||
private readonly dataSource: DataSource
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -44,7 +44,7 @@ export class CirculationWorkflowService {
|
||||
|
||||
if (!circulation) {
|
||||
throw new NotFoundException(
|
||||
`Circulation ID ${circulationId} not found`,
|
||||
`Circulation ID ${circulationId} not found`
|
||||
);
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ export class CirculationWorkflowService {
|
||||
this.WORKFLOW_CODE,
|
||||
'circulation',
|
||||
circulation.id.toString(),
|
||||
context,
|
||||
context
|
||||
);
|
||||
|
||||
// Auto start (OPEN -> IN_REVIEW)
|
||||
@@ -68,14 +68,14 @@ export class CirculationWorkflowService {
|
||||
'START',
|
||||
userId,
|
||||
'Start Circulation Process',
|
||||
{},
|
||||
{}
|
||||
);
|
||||
|
||||
// Sync Status
|
||||
await this.syncStatus(
|
||||
circulation,
|
||||
transitionResult.nextState,
|
||||
queryRunner,
|
||||
queryRunner
|
||||
);
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
@@ -99,7 +99,7 @@ export class CirculationWorkflowService {
|
||||
async processAction(
|
||||
instanceId: string,
|
||||
userId: number,
|
||||
dto: WorkflowTransitionDto,
|
||||
dto: WorkflowTransitionDto
|
||||
) {
|
||||
// ส่งให้ Engine
|
||||
const result = await this.workflowEngine.processTransition(
|
||||
@@ -107,7 +107,7 @@ export class CirculationWorkflowService {
|
||||
dto.action,
|
||||
userId,
|
||||
dto.comment,
|
||||
dto.payload,
|
||||
dto.payload
|
||||
);
|
||||
|
||||
// Sync Status กลับ
|
||||
@@ -130,7 +130,7 @@ export class CirculationWorkflowService {
|
||||
private async syncStatus(
|
||||
circulation: Circulation,
|
||||
workflowState: string,
|
||||
queryRunner?: any,
|
||||
queryRunner?: import('typeorm').QueryRunner
|
||||
) {
|
||||
const statusMap: Record<string, string> = {
|
||||
DRAFT: 'OPEN',
|
||||
@@ -158,7 +158,7 @@ export class CirculationWorkflowService {
|
||||
await manager.save(circulation);
|
||||
|
||||
this.logger.log(
|
||||
`Synced Circulation #${circulation.id}: State=${workflowState} -> Status=${targetCode}`,
|
||||
`Synced Circulation #${circulation.id}: State=${workflowState} -> Status=${targetCode}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,8 +14,7 @@ import { CreateCirculationDto } from './dto/create-circulation.dto';
|
||||
import { UpdateCirculationRoutingDto } from './dto/update-circulation-routing.dto';
|
||||
import { SearchCirculationDto } from './dto/search-circulation.dto';
|
||||
import { DocumentNumberingService } from '../document-numbering/services/document-numbering.service';
|
||||
import { Project } from '../project/entities/project.entity';
|
||||
import { Correspondence } from '../correspondence/entities/correspondence.entity';
|
||||
import { UuidResolverService } from '../../common/services/uuid-resolver.service';
|
||||
|
||||
@Injectable()
|
||||
export class CirculationService {
|
||||
@@ -25,61 +24,10 @@ export class CirculationService {
|
||||
@InjectRepository(CirculationRouting)
|
||||
private routingRepo: Repository<CirculationRouting>,
|
||||
private numberingService: DocumentNumberingService,
|
||||
private dataSource: DataSource
|
||||
private dataSource: DataSource,
|
||||
private uuidResolver: UuidResolverService
|
||||
) {}
|
||||
|
||||
/**
|
||||
* ADR-019: Resolve projectId (INT or UUID string) to internal INT ID
|
||||
*/
|
||||
private async resolveProjectId(projectId: number | string): Promise<number> {
|
||||
if (typeof projectId === 'number') return projectId;
|
||||
const num = Number(projectId);
|
||||
if (!isNaN(num)) return num;
|
||||
const project = await this.dataSource.manager.findOne(Project, {
|
||||
where: { uuid: projectId },
|
||||
select: ['id'],
|
||||
});
|
||||
if (!project)
|
||||
throw new NotFoundException(`Project with UUID ${projectId} not found`);
|
||||
return project.id;
|
||||
}
|
||||
|
||||
/**
|
||||
* ADR-019: Resolve correspondenceId (INT or UUID string) to internal INT ID
|
||||
*/
|
||||
private async resolveCorrespondenceId(
|
||||
corrId: number | string
|
||||
): Promise<number> {
|
||||
if (typeof corrId === 'number') return corrId;
|
||||
const num = Number(corrId);
|
||||
if (!isNaN(num)) return num;
|
||||
const corr = await this.dataSource.manager.findOne(Correspondence, {
|
||||
where: { uuid: corrId },
|
||||
select: ['id'],
|
||||
});
|
||||
if (!corr)
|
||||
throw new NotFoundException(
|
||||
`Correspondence with UUID ${corrId} not found`
|
||||
);
|
||||
return corr.id;
|
||||
}
|
||||
|
||||
/**
|
||||
* ADR-019: Resolve userId (INT or UUID string) to internal user_id
|
||||
*/
|
||||
private async resolveUserId(userId: number | string): Promise<number> {
|
||||
if (typeof userId === 'number') return userId;
|
||||
const num = Number(userId);
|
||||
if (!isNaN(num)) return num;
|
||||
const user = await this.dataSource.manager.findOne(User, {
|
||||
where: { uuid: userId },
|
||||
select: ['user_id'],
|
||||
});
|
||||
if (!user)
|
||||
throw new NotFoundException(`User with UUID ${userId} not found`);
|
||||
return user.user_id;
|
||||
}
|
||||
|
||||
async create(createDto: CreateCirculationDto, user: User) {
|
||||
if (!user.primaryOrganizationId) {
|
||||
throw new BadRequestException('User must belong to an organization');
|
||||
@@ -92,13 +40,13 @@ export class CirculationService {
|
||||
try {
|
||||
// ADR-019: Resolve UUID references to internal INT IDs
|
||||
const resolvedProjectId = createDto.projectId
|
||||
? await this.resolveProjectId(createDto.projectId)
|
||||
? await this.uuidResolver.resolveProjectId(createDto.projectId)
|
||||
: 0;
|
||||
const resolvedCorrId = await this.resolveCorrespondenceId(
|
||||
const resolvedCorrId = await this.uuidResolver.resolveCorrespondenceId(
|
||||
createDto.correspondenceId
|
||||
);
|
||||
const resolvedAssigneeIds = await Promise.all(
|
||||
createDto.assigneeIds.map((id) => this.resolveUserId(id))
|
||||
createDto.assigneeIds.map((id) => this.uuidResolver.resolveUserId(id))
|
||||
);
|
||||
|
||||
// Generate No. using DocumentNumberingService (Type 900 - Circulation)
|
||||
|
||||
Reference in New Issue
Block a user