This commit is contained in:
@@ -277,7 +277,7 @@ export class CorrespondenceService {
|
||||
'correspondence',
|
||||
savedCorr.id.toString(),
|
||||
{
|
||||
projectId: createDto.projectId,
|
||||
projectId: resolvedProjectId,
|
||||
originatorId: userOrgId,
|
||||
disciplineId: createDto.disciplineId,
|
||||
initiatorId: user.user_id,
|
||||
@@ -297,7 +297,7 @@ export class CorrespondenceService {
|
||||
title: createDto.subject,
|
||||
description: createDto.description,
|
||||
status: 'DRAFT',
|
||||
projectId: createDto.projectId,
|
||||
projectId: resolvedProjectId,
|
||||
createdAt: new Date(),
|
||||
});
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@ export class DocumentNumberAudit {
|
||||
'CANCEL',
|
||||
'MANUAL_OVERRIDE',
|
||||
'VOID',
|
||||
'VOID_REPLACE',
|
||||
'GENERATE',
|
||||
],
|
||||
default: 'CONFIRM',
|
||||
|
||||
@@ -26,6 +26,7 @@ export class DocumentNumberError {
|
||||
'SEQUENCE_EXHAUSTED',
|
||||
'RESERVATION_EXPIRED',
|
||||
'DUPLICATE_NUMBER',
|
||||
'GENERATE_ERROR',
|
||||
],
|
||||
})
|
||||
errorType!: string;
|
||||
|
||||
@@ -475,17 +475,35 @@ export class DocumentNumberingService {
|
||||
return (await this.auditRepo.save(audit)) as unknown as DocumentNumberAudit;
|
||||
}
|
||||
|
||||
private async logError(error: any, ctx: any, operation: string) {
|
||||
private mapErrorType(error: Error): string {
|
||||
const name = error.name || '';
|
||||
const msg = error.message || '';
|
||||
if (name === 'ConflictException' || msg.includes('version'))
|
||||
return 'VERSION_CONFLICT';
|
||||
if (msg.includes('lock') || msg.includes('timeout')) return 'LOCK_TIMEOUT';
|
||||
if (msg.includes('Redis') || msg.includes('redis')) return 'REDIS_ERROR';
|
||||
if (msg.includes('duplicate') || msg.includes('Duplicate'))
|
||||
return 'DUPLICATE_NUMBER';
|
||||
if (msg.includes('validation') || msg.includes('Validation'))
|
||||
return 'VALIDATION_ERROR';
|
||||
if (msg.includes('exhausted') || msg.includes('maximum'))
|
||||
return 'SEQUENCE_EXHAUSTED';
|
||||
if (msg.includes('expired') || msg.includes('reservation'))
|
||||
return 'RESERVATION_EXPIRED';
|
||||
if (msg.includes('database') || msg.includes('query')) return 'DB_ERROR';
|
||||
return 'GENERATE_ERROR';
|
||||
}
|
||||
|
||||
private async logError(error: unknown, ctx: unknown, operation: string) {
|
||||
try {
|
||||
const err = error instanceof Error ? error : new Error(String(error));
|
||||
const errEntity = this.errorRepo.create({
|
||||
errorMessage: error.message || 'Unknown Error',
|
||||
errorType: error.name || 'GENERATE_ERROR', // Simple mapping
|
||||
errorMessage: err.message || 'Unknown Error',
|
||||
errorType: this.mapErrorType(err),
|
||||
contextData: {
|
||||
// Mapped from context
|
||||
...ctx,
|
||||
...(typeof ctx === 'object' && ctx !== null ? ctx : {}),
|
||||
operation,
|
||||
inputPayload: JSON.stringify(ctx),
|
||||
},
|
||||
} as Record<string, unknown>,
|
||||
});
|
||||
await this.errorRepo.save(errEntity);
|
||||
} catch (e) {
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import { IsInt, IsOptional, IsString, IsUUID } from 'class-validator';
|
||||
import { IsInt, IsOptional, IsString } from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
|
||||
export class SearchRfaDto {
|
||||
@IsUUID('all')
|
||||
projectUuid!: string; // ADR-019: Public UUID of the project
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
projectId?: number | string; // ADR-019: Accept INT or UUID string
|
||||
|
||||
/** @internal Resolved INT ID — set by controller, do NOT expose in API */
|
||||
projectId?: number;
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
@Type(() => Number)
|
||||
statusId?: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
|
||||
@@ -54,7 +54,10 @@ export class RfaController {
|
||||
|
||||
@Post(':uuid/submit')
|
||||
@ApiOperation({ summary: 'Submit RFA to Workflow' })
|
||||
@ApiParam({ name: 'uuid', description: 'RFA UUID (from correspondences.uuid)' })
|
||||
@ApiParam({
|
||||
name: 'uuid',
|
||||
description: 'RFA UUID (from correspondences.uuid)',
|
||||
})
|
||||
@ApiBody({ type: SubmitRfaDto })
|
||||
@ApiResponse({ status: 200, description: 'RFA submitted successfully' })
|
||||
@RequirePermission('rfa.create')
|
||||
@@ -71,7 +74,10 @@ export class RfaController {
|
||||
|
||||
@Post(':uuid/action')
|
||||
@ApiOperation({ summary: 'Process Workflow Action (Approve/Reject)' })
|
||||
@ApiParam({ name: 'uuid', description: 'RFA UUID (from correspondences.uuid)' })
|
||||
@ApiParam({
|
||||
name: 'uuid',
|
||||
description: 'RFA UUID (from correspondences.uuid)',
|
||||
})
|
||||
@ApiBody({ type: WorkflowActionDto })
|
||||
@ApiResponse({
|
||||
status: 200,
|
||||
@@ -94,15 +100,24 @@ export class RfaController {
|
||||
@ApiResponse({ status: 200, description: 'List of RFAs' })
|
||||
@RequirePermission('document.view')
|
||||
async findAll(@Query() query: SearchRfaDto) {
|
||||
// ADR-019: resolve projectUuid → internal INT projectId
|
||||
const project = await this.projectService.findOneByUuid(query.projectUuid);
|
||||
query.projectId = project.id;
|
||||
// ADR-019: resolve projectId UUID→INT if provided
|
||||
if (query.projectId) {
|
||||
const pid = query.projectId;
|
||||
const num = Number(pid);
|
||||
if (typeof pid === 'string' && isNaN(num)) {
|
||||
const project = await this.projectService.findOneByUuid(pid);
|
||||
query.projectId = project.id;
|
||||
}
|
||||
}
|
||||
return this.rfaService.findAll(query);
|
||||
}
|
||||
|
||||
@Get(':uuid')
|
||||
@ApiOperation({ summary: 'Get RFA details with revisions and items' })
|
||||
@ApiParam({ name: 'uuid', description: 'RFA UUID (from correspondences.uuid)' })
|
||||
@ApiParam({
|
||||
name: 'uuid',
|
||||
description: 'RFA UUID (from correspondences.uuid)',
|
||||
})
|
||||
@ApiResponse({ status: 200, description: 'RFA details' })
|
||||
@RequirePermission('document.view')
|
||||
findOne(@Param('uuid', ParseUuidPipe) uuid: string) {
|
||||
|
||||
@@ -38,10 +38,9 @@ export class TransmittalItemDto {
|
||||
}
|
||||
|
||||
export class CreateTransmittalDto {
|
||||
@ApiProperty({ description: 'ID ของโครงการ', example: 1 })
|
||||
@IsInt()
|
||||
@ApiProperty({ description: 'ID หรือ UUID ของโครงการ (ADR-019)' })
|
||||
@IsNotEmpty()
|
||||
projectId!: number;
|
||||
projectId!: number | string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'เรื่อง',
|
||||
@@ -51,10 +50,16 @@ export class CreateTransmittalDto {
|
||||
@IsNotEmpty()
|
||||
subject!: string;
|
||||
|
||||
@ApiProperty({ description: 'ผู้รับ (Organization ID)', example: 2 })
|
||||
@IsInt()
|
||||
@ApiProperty({ description: 'ผู้รับ Organization ID หรือ UUID (ADR-019)' })
|
||||
@IsNotEmpty()
|
||||
recipientOrganizationId!: number;
|
||||
recipientOrganizationId!: number | string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'Correspondence ID หรือ UUID (ADR-019)',
|
||||
required: false,
|
||||
})
|
||||
@IsOptional()
|
||||
correspondenceId?: number | string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'วัตถุประสงค์',
|
||||
@@ -65,6 +70,11 @@ export class CreateTransmittalDto {
|
||||
@IsOptional()
|
||||
purpose?: TransmittalPurpose;
|
||||
|
||||
@ApiProperty({ description: 'หมายเหตุ', required: false })
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
remarks?: string;
|
||||
|
||||
@ApiProperty({ description: 'รายการที่แนบ', type: [TransmittalItemDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
|
||||
@@ -21,6 +21,8 @@ import { CorrespondenceRevision } from '../correspondence/entities/correspondenc
|
||||
import { CorrespondenceType } from '../correspondence/entities/correspondence-type.entity';
|
||||
import { CorrespondenceStatus } from '../correspondence/entities/correspondence-status.entity';
|
||||
import { Project } from '../project/entities/project.entity';
|
||||
import { Organization } from '../organization/entities/organization.entity';
|
||||
import { CorrespondenceRecipient } from '../correspondence/entities/correspondence-recipient.entity';
|
||||
|
||||
@Injectable()
|
||||
export class TransmittalService {
|
||||
@@ -55,6 +57,22 @@ export class TransmittalService {
|
||||
return project.id;
|
||||
}
|
||||
|
||||
/**
|
||||
* ADR-019: Resolve organizationId (INT or UUID string) to internal INT ID
|
||||
*/
|
||||
private async resolveOrganizationId(orgId: number | string): Promise<number> {
|
||||
if (typeof orgId === 'number') return orgId;
|
||||
const num = Number(orgId);
|
||||
if (!isNaN(num)) return num;
|
||||
const org = await this.dataSource.manager.findOne(Organization, {
|
||||
where: { uuid: orgId },
|
||||
select: ['id'],
|
||||
});
|
||||
if (!org)
|
||||
throw new NotFoundException(`Organization with UUID ${orgId} not found`);
|
||||
return org.id;
|
||||
}
|
||||
|
||||
async create(createDto: CreateTransmittalDto, user: User) {
|
||||
// 1. Get Transmittal Type (Assuming Code '901' or 'TRN')
|
||||
const type = await this.typeRepo.findOne({
|
||||
@@ -119,11 +137,22 @@ export class TransmittalService {
|
||||
});
|
||||
await queryRunner.manager.save(revision);
|
||||
|
||||
// ADR-019: Resolve recipientOrganizationId UUID→INT and create recipient record
|
||||
const internalRecipientOrgId = await this.resolveOrganizationId(
|
||||
createDto.recipientOrganizationId
|
||||
);
|
||||
const recipient = queryRunner.manager.create(CorrespondenceRecipient, {
|
||||
correspondenceId: savedCorr.id,
|
||||
recipientOrganizationId: internalRecipientOrgId,
|
||||
recipientType: 'TO',
|
||||
});
|
||||
await queryRunner.manager.save(recipient);
|
||||
|
||||
// 5. Create Transmittal
|
||||
const transmittal = queryRunner.manager.create(Transmittal, {
|
||||
correspondenceId: savedCorr.id,
|
||||
purpose: 'FOR_REVIEW', // Default or from DTO
|
||||
// remarks: createDto.remarks, // Add if in DTO
|
||||
purpose: createDto.purpose || 'FOR_REVIEW',
|
||||
remarks: createDto.remarks,
|
||||
});
|
||||
const savedTransmittal = await queryRunner.manager.save(transmittal);
|
||||
|
||||
@@ -169,7 +198,6 @@ export class TransmittalService {
|
||||
if (!correspondence) {
|
||||
throw new NotFoundException(`Transmittal with UUID ${uuid} not found`);
|
||||
}
|
||||
return this.findOne(correspondence.id);
|
||||
}
|
||||
|
||||
async findOne(id: number) {
|
||||
|
||||
@@ -91,6 +91,7 @@ export class UserService {
|
||||
.leftJoinAndSelect('assignments.role', 'role')
|
||||
.select([
|
||||
'user.user_id',
|
||||
'user.uuid',
|
||||
'user.username',
|
||||
'user.email',
|
||||
'user.firstName',
|
||||
|
||||
Reference in New Issue
Block a user