Private Docs

Flows — Runtime Model + แคตตาล็อก

2 มุมของคำว่า flow: (1) วิธีที่ engine รัน flow ใดๆ (runtime model + state machine) และ (2) FlowDefinition ทั้งหมดที่เก็บในระบบ พร้อม sequence diagram

อัปเดต: 2026-06-04

คำว่า “flow” ใน Lego มี 2 ความหมาย — เอกสารนี้แยกให้ชัด:

  1. Flow การทำงานของ systemengine รัน flow ใดๆ อย่างไร (โมเดล runtime ที่เป็น generic)
  2. Flow Definition ที่เก็บในระบบแคตตาล็อก flow จริงทั้งหมด ที่ admin ตั้งค่าไว้

ส่วนที่ 1 — Flow การทำงานของ system (Runtime Model)

โมเดลหลัก: Definition → Snapshot → Instance

FlowDefinitiontemplate + FlowSteps
Snapshotแช่แข็งใน instance
FlowInstanceruntime + state
StepDataoutput ต่อ step

engine เป็น flow-agnostic — ไม่รู้จักชื่อ flow ใดเป็นพิเศษ มันอ่าน Snapshot แล้วเดินตามลำดับ step

Step มี 2 โหมดทำงาน

โหมดใครขับengine ทำอะไร
Interactiveรอ user action (Next/Back/Submit)หยุดรอที่ step นี้ แล้วคืน nav ให้ FE render
Automaticไม่รอใครengine รัน handler เองทันที แล้วเดินต่อ (chain) จนเจอ Interactive หรือจบ

หลัง user กด Next ที่ Interactive step → engine เลื่อนไป step ถัดไป แล้ว AdvanceAutomaticAsync รัน Automatic ที่คั่นทั้งหมดจน finalize หรือเจอ Interactive ถัดไป

sequenceDiagram
  autonumber
  participant U as User (FE)
  participant E as FlowEngine
  participant H as Handler
  U->>E: action Next (Interactive step)
  E->>H: ExecuteAsync (step ปัจจุบัน)
  H-->>E: Ok(output)
  E->>E: AdvanceTo(next interactive)
  loop AdvanceAutomaticAsync
    E->>H: รัน Automatic step ที่คั่น
    H-->>E: Ok / Fail(MarkStuck)
  end
  E-->>U: nav (Interactive ถัดไป) หรือ FINALIZED

State machine ของ FlowInstance

stateDiagram-v2
  [*] --> Draft
  Draft --> Submitted: submit
  Draft --> Finalized: no-approve
  Submitted --> Approved: 4eye OK
  Submitted --> Rejected: 4eye no
  Submitted --> CorrectionRequested: fix
  CorrectionRequested --> Draft: reopen
  Approved --> Finalized: auto
  Draft --> Abandoned: stale
  Rejected --> [*]
  Finalized --> [*]
  Abandoned --> [*]

ส่วนที่ 2 — Flow Definition ที่เก็บในระบบ (แคตตาล็อก)

flow จริงในระบบ (กลุ่ม customer + requestor):

STANDARD_CUSTOMER_ONBOARDING

customer ผู้ใช้ใหม่สมัครเอง — ยืนยันตัวตนผ่าน OTP แล้วสร้าง platform user (ไม่ต้องรอ approve)

OtpVerificationStep ConsentStep NdidStep PersonalInfoStep SubmitRegistrationStep CreatePlatformUser ActivateEntraUser Finalized

CUSTOMER_FIXED_ONBOARDING

customer ที่ถูก เชิญผ่าน invite token (ผูกบริษัทอยู่แล้ว) — ไม่มี NDID, มี automatic เพิ่มเพื่อผูกบริษัท + provision สิทธิ์

OtpVerificationStep ConsentStep PersonalInfoStep SubmitRegistrationStep CreatePlatformUser ActivateEntraUser CreateUserCompanyMapping ProvisionCustomerAccess Finalized

NEW_REQUESTOR

ตัวแทนนิติบุคคล (requestor) รายใหม่ ขอใช้บริการ FX — มี 4-eye approval แล้ว automatic สร้าง CompanyApplication + ส่งคำเชิญ customer

OtpVerificationStep JuristicBindingStep CompanyInfoStep UserModeStep DesignateCustomersStep ServiceTcConsentStep SignatoryFormStep SubmitApplicationStep CreateCompanyApplication GenerateCustomerInvitations SendInvitationEmails Finalized

RETURNING_REQUESTOR

requestor ที่มีบัญชี+ตัวตนแล้ว — เหมือน NEW แต่ ไม่มี OtpVerificationStep (login ด้วย JWT)

JuristicBindingStep CompanyInfoStep UserModeStep DesignateCustomersStep ServiceTcConsentStep SignatoryFormStep SubmitApplicationStep CreateCompanyApplication GenerateCustomerInvitations SendInvitationEmails Finalized

เปรียบเทียบสรุป

FlowOTPApproval (4-eye)Automatic chain
STANDARD_CUSTOMER_ONBOARDINGไม่มีCreatePlatformUser → ActivateEntra
CUSTOMER_FIXED_ONBOARDINGไม่มี+ CreateUserCompanyMapping, ProvisionCustomerAccess
NEW_REQUESTORCreateCompanyApplication → Invitations → Emails
RETURNING_REQUESTOR❌ (JWT)เหมือน NEW