251129:1700 update to 1.4.5

This commit is contained in:
admin
2025-11-29 16:50:34 +07:00
parent 6cafa6a2b9
commit 138b09d0c8
55 changed files with 14641 additions and 2090 deletions

View File

@@ -0,0 +1,67 @@
import { config } from 'dotenv';
import { DataSource } from 'typeorm';
import { seedWorkflowDefinitions } from '../seeds/workflow-definitions.seed'; // Import ฟังก์ชัน Seed ที่คุณมี
// Import Entities ที่เกี่ยวข้อง
import { WorkflowDefinition } from '../../modules/workflow-engine/entities/workflow-definition.entity';
import { WorkflowHistory } from '../../modules/workflow-engine/entities/workflow-history.entity';
import { WorkflowInstance } from '../../modules/workflow-engine/entities/workflow-instance.entity';
// โหลด Environment Variables (.env)
config();
const runSeed = async () => {
// ตั้งค่าการเชื่อมต่อฐานข้อมูล (ควรตรงกับ docker-compose หรือ .env ของคุณ)
const dataSource = new DataSource({
type: 'mariadb',
host: process.env.DB_HOST || 'localhost',
port: parseInt(process.env.DB_PORT || '3306'),
username: process.env.DB_USERNAME || 'root',
password: process.env.DB_PASSWORD || 'Center#2025',
database: process.env.DB_DATABASE || 'lcbp3_dev',
// สำคัญ: ต้องใส่ Entities ที่เกี่ยวข้องทั้งหมดเพื่อให้ TypeORM รู้จัก
entities: [
WorkflowDefinition,
WorkflowInstance,
WorkflowHistory,
// ใส่ Entity อื่นๆ ถ้าจำเป็น หรือใช้ path pattern: __dirname + '/../../modules/**/*.entity{.ts,.js}'
],
synchronize: false, // ห้ามใช้ true บน Production
});
try {
console.log('🔌 Connecting to database...');
await dataSource.initialize();
console.log('✅ Database connected.');
console.log('🌱 Running Seeds...');
await seedWorkflowDefinitions(dataSource);
console.log('✅ Seeding completed successfully.');
} catch (error) {
console.error('❌ Error during seeding:', error);
} finally {
if (dataSource.isInitialized) {
await dataSource.destroy();
console.log('🔌 Database connection closed.');
}
}
};
runSeed();
/*
npx ts-node -r tsconfig-paths/register src/database/run-seed.ts
**หรือเพิ่มใน `package.json` (แนะนำ):**
คุณสามารถเพิ่ม script ใน `package.json` เพื่อให้เรียกใช้ได้ง่ายขึ้นในอนาคต:
"scripts": {
"seed": "ts-node -r tsconfig-paths/register src/database/seeds/run-seed.ts"
}
http://googleusercontent.com/immersive_entry_chip/1
### 💡 ข้อควรระวัง
1. **Environment Variables:** ตรวจสอบให้แน่ใจว่าค่า Config (Host, User, Password) ในไฟล์ `run-seed.ts` หรือ `.env` นั้นถูกต้องและตรงกับ Docker Container ที่กำลังรันอยู่
2. **Entities:** หากฟังก์ชัน Seed มีการเรียกใช้ Entity อื่นนอกเหนือจาก `WorkflowDefinition` ต้องนำมาใส่ใน `entities: [...]` ของ `DataSource` ให้ครบ ไม่อย่างนั้นจะเจอ Error `RepositoryNotFoundError`
*/

View File

@@ -10,26 +10,41 @@ export const seedWorkflowDefinitions = async (dataSource: DataSource) => {
// 1. RFA Workflow (Standard)
const rfaDsl = {
workflow: 'RFA',
workflow: 'RFA_FLOW_V1', // [FIX] เปลี่ยนชื่อให้ตรงกับค่าใน RfaWorkflowService
version: 1,
description: 'Standard RFA Approval Workflow',
states: [
{
name: 'DRAFT',
initial: true,
on: { SUBMIT: { to: 'IN_REVIEW', requirements: [{ role: 'Editor' }] } },
on: {
SUBMIT: {
to: 'IN_REVIEW',
require: { role: 'Editor' }, // [FIX] แก้ไข Syntax เป็น Object
},
},
},
{
name: 'IN_REVIEW',
on: {
APPROVE: {
to: 'APPROVED',
requirements: [{ role: 'Contract Admin' }],
APPROVE_1: {
to: 'APPROVED', // [FIX] ชี้ไปที่ State ที่มีอยู่จริง
require: { role: 'Contract Admin' },
condition: "context.priority === 'HIGH'",
},
APPROVE_2: {
to: 'APPROVED', // [FIX] ชี้ไปที่ State ที่มีอยู่จริง
require: { role: 'Contract Admin' },
condition: "context.priority === 'NORMAL'",
},
REJECT: {
to: 'REJECTED',
requirements: [{ role: 'Contract Admin' }],
require: { role: 'Contract Admin' },
},
COMMENT: { to: 'DRAFT', requirements: [{ role: 'Contract Admin' }] }, // ส่งกลับแก้ไข
COMMENT: {
to: 'DRAFT',
require: { role: 'Contract Admin' },
}, // ส่งกลับแก้ไข
},
},
{ name: 'APPROVED', terminal: true },
@@ -39,18 +54,27 @@ export const seedWorkflowDefinitions = async (dataSource: DataSource) => {
// 2. Circulation Workflow
const circulationDsl = {
workflow: 'CIRCULATION',
workflow: 'CIRCULATION_INTERNAL_V1', // [FIX] เปลี่ยนชื่อให้ตรงกับค่าใน CirculationWorkflowService
version: 1,
description: 'Internal Document Circulation',
states: [
{
name: 'OPEN',
initial: true,
on: { SEND: { to: 'IN_REVIEW' } },
on: {
START: {
// [FIX] เปลี่ยนชื่อ Action ให้ตรงกับที่ Service เรียกใช้ ('START')
to: 'IN_REVIEW',
},
},
},
{
name: 'IN_REVIEW',
on: {
COMPLETE: { to: 'COMPLETED' }, // เมื่อทุกคนตอบครบ
COMPLETE_TASK: {
// [FIX] เปลี่ยนให้สอดคล้องกับ Action ที่ใช้จริง
to: 'COMPLETED',
},
CANCEL: { to: 'CANCELLED' },
},
},
@@ -59,24 +83,60 @@ export const seedWorkflowDefinitions = async (dataSource: DataSource) => {
],
};
const workflows = [rfaDsl, circulationDsl];
// 3. Correspondence Workflow (Optional - ถ้ามี)
const correspondenceDsl = {
workflow: 'CORRESPONDENCE_FLOW_V1',
version: 1,
description: 'Standard Correspondence Routing',
states: [
{
name: 'DRAFT',
initial: true,
on: { SUBMIT: { to: 'IN_REVIEW' } },
},
{
name: 'IN_REVIEW',
on: {
APPROVE: { to: 'APPROVED' },
REJECT: { to: 'REJECTED' },
},
},
{ name: 'APPROVED', terminal: true },
{ name: 'REJECTED', terminal: true },
],
};
const workflows = [rfaDsl, circulationDsl, correspondenceDsl];
for (const dsl of workflows) {
const exists = await repo.findOne({
where: { workflow_code: dsl.workflow, version: dsl.version },
});
if (!exists) {
const compiled = dslService.compile(dsl);
await repo.save(
repo.create({
workflow_code: dsl.workflow,
version: dsl.version,
dsl: dsl,
compiled: compiled,
is_active: true,
}),
try {
// Compile เพื่อ Validate และ Normalize ก่อนบันทึก
// cast as any เพื่อ bypass type checking ตอน seed raw data
const compiled = dslService.compile(dsl as any);
await repo.save(
repo.create({
workflow_code: dsl.workflow,
version: dsl.version,
description: dsl.description,
dsl: dsl,
compiled: compiled,
is_active: true,
}),
);
console.log(`✅ Seeded Workflow: ${dsl.workflow} v${dsl.version}`);
} catch (error) {
console.error(`❌ Failed to seed workflow ${dsl.workflow}:`, error);
}
} else {
console.log(
`⏭️ Workflow already exists: ${dsl.workflow} v${dsl.version}`,
);
console.log(`✅ Seeded Workflow: ${dsl.workflow} v${dsl.version}`);
}
}
};