[CLAUDE] Drastic refactor: flat workflow Phòng × Cấp + Migration 21 (Chunk A)
All checks were successful
Deploy SOLUTION_ERP / build-deploy (push) Successful in 3m18s

User chốt drastic refactor — bỏ phase enum hoàn toàn, dùng ChoDuyet=10
đơn nhất + currentStepIndex tracking. Workflow flat list (Phòng × Cấp ×
Approvers). Mỗi PE/HĐ pin WorkflowDefinitionId chạy hết quy trình đó.

Schema (Migration 21 `RefactorWorkflowToFlatModel`):
- Phase enum +ChoDuyet=10 (PE + Contract). Legacy 2-9 + 98 deprecated.
- WorkflowStep + DepartmentId Guid? (FK Restrict) + PositionLevel int?
  (PE + Contract — mirror).
- PE/Contract + CurrentWorkflowStepIndex int? + RejectedAtStepIndex int?
- DROP table PurchaseEvaluationWorkflowStepInnerSteps (Mig 18)
- DROP table WorkflowStepInnerSteps (Mig 20)
- DROP column ContractDeptApproval.InnerStepId (Mig 20)
- DROP column PEDeptApproval.InnerStepId (Mig 18)
- DROP filtered indexes (Mig 19/20) + restore simple unique
  (TargetId, Phase, Dept, Stage) non-filtered

Service rewrite (PE + Contract WorkflowService.TransitionAsync):
- Phase transitions: DangSoanThao → ChoDuyet (Drafter trình, init idx=0)
- ChoDuyet → ChoDuyet (advance idx per approve)
- ChoDuyet → DaDuyet/DaPhatHanh (idx >= steps.Count → terminal)
- ChoDuyet → DangSoanThao (Trả lại — save RejectedAtStepIndex)
- ChoDuyet → TuChoi (Từ chối — khoá vĩnh viễn)
- DangSoanThao + RejectedAtStepIndex → ChoDuyet jump-back to saved idx
- Approver match: actor.Dept == step.Dept AND actor.PositionLevel >=
  step.PositionLevel (OR-of-many cùng cấp/dept = pass) OR
  Approvers.Any(Kind=User AND id match) OR
  Approvers.Any(Kind=Role AND actorRoles contains)
- Admin role bypass policy. Last step done → gen mã HĐ (Contract only)

App CQRS:
- WorkflowStepDto + WorkflowStepInput drop InnerStep, add DepartmentId
  + PositionLevel fields. PE + Contract mirror.

Tests rewrite:
- DROP PeNStageApprovalTests.cs (6 test) + ContractNStageApprovalTests.cs
  (6 test) + PeTwoStageApprovalTests.cs (7 test) — legacy N-stage/2-stage
  no longer applicable
- UPDATE PeWorkflowAdminTests signature to new flat input
- 96 → 77 test pass (drop 19 legacy)

Reference Domain entities removed:
- WorkflowStepInnerStep (Contract)
- PurchaseEvaluationWorkflowStepInnerStep (PE)
- DTOs WorkflowStepInnerStepDto / CreateWorkflowStepInnerStepInput per module

Memory `feedback_drastic_refactor_scope.md` validated: drastic refactor
done in dedicated session với context fresh, scope ~5h actual (planned ~8-10h
with 2x buffer).

Verify:
- dotnet build SolutionErp.slnx 0 error
- dotnet ef database update Mig 21 LocalDB applied OK
- dotnet test 77 pass (54 Domain + 23 Infra)
- 3-file rule: Migration .cs + Designer.cs + Snapshot updated

Pending Chunk B: FE Designer flat UI (PeWorkflowsPage + WorkflowsPage).
Pending Chunk C: FE PeWorkflowPanel + workflow timeline display.
Pending Chunk D: Docs + Skill + Memory + session log.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
pqhuy1987
2026-05-08 12:04:51 +07:00
parent 38d10b7897
commit dbb0089e28
23 changed files with 4501 additions and 2123 deletions

View File

@ -30,11 +30,15 @@ public class Contract : AuditableEntity
public string? BudgetManualName { get; set; } // Tên tham chiếu
public decimal? BudgetManualAmount { get; set; } // Tổng số tiền nhập tay (đ)
// Smart reject (Phase 9 — Migration 16): Phase nguồn khi reject. Drafter
// sửa lại + trình lại → quay về RejectedFromPhase thay vì DangSoanThao
// tuần tự lại từ đầu. Null khi chưa từng reject hoặc đã trình lại xong.
// Smart reject (Phase 9 — Migration 16): Phase nguồn khi reject.
public ContractPhase? RejectedFromPhase { get; set; }
// Flat workflow tracking (Session 16 — Migration 21):
// - CurrentWorkflowStepIndex: 0-based pointer step đang chờ approver
// - RejectedAtStepIndex: snapshot khi Trả lại, restore khi resume
public int? CurrentWorkflowStepIndex { get; set; }
public int? RejectedAtStepIndex { get; set; }
public List<ContractApproval> Approvals { get; set; } = new();
public List<ContractComment> Comments { get; set; } = new();
public List<ContractAttachment> Attachments { get; set; } = new();

View File

@ -23,9 +23,5 @@ public class ContractDepartmentApproval : AuditableEntity
public DateTime ApprovedAt { get; set; }
public bool IsBypassed { get; set; } // true nếu NV bypass (User.CanBypassReview=true)
// N-stage inner step link (Mig 20) — null cho data legacy 2-stage Review/Confirm.
// Có giá trị khi step cha có InnerSteps configured. Mirror PE Mig 18 pattern.
public Guid? InnerStepId { get; set; }
public Contract? Contract { get; set; }
}

View File

@ -1,16 +1,25 @@
namespace SolutionErp.Domain.Contracts;
// 9 phase state machine — xem docs/workflow-contract.md
// State machine HĐ — Session 16 drastic refactor (Mig 21):
// DangSoanThao → ChoDuyet (Drafter trình, init CurrentWorkflowStepIndex=0)
// ChoDuyet → ChoDuyet (advance step pointer per approve)
// ChoDuyet → DaPhatHanh (last step done — terminal)
// ChoDuyet → DangSoanThao (Trả lại — save RejectedAtStepIndex, Drafter sửa)
// ChoDuyet → TuChoi (Từ chối — terminal khoá)
//
// LEGACY values (DangChon, DangGopY, DangDamPhan, DangInKy, DangKiemTraCCM,
// DangTrinhKy, DangDongDau) deprecated post-Mig 21 — giữ enum cho data cũ.
public enum ContractPhase
{
DangChon = 1,
DangChon = 1, // [LEGACY]
DangSoanThao = 2,
DangGopY = 3,
DangDamPhan = 4,
DangInKy = 5,
DangKiemTraCCM = 6,
DangTrinhKy = 7,
DangDongDau = 8,
DaPhatHanh = 9,
TuChoi = 99,
DangGopY = 3, // [LEGACY]
DangDamPhan = 4, // [LEGACY]
DangInKy = 5, // [LEGACY]
DangKiemTraCCM = 6, // [LEGACY]
DangTrinhKy = 7, // [LEGACY]
DangDongDau = 8, // [LEGACY]
DaPhatHanh = 9, // terminal thành công (= DaDuyet cho HĐ)
ChoDuyet = 10, // [Mig 21] generic intermediate, dùng CurrentWorkflowStepIndex tracking
TuChoi = 99, // terminal khoá
}

View File

@ -23,21 +23,28 @@ public class WorkflowDefinition : BaseEntity
public List<WorkflowStep> Steps { get; set; } = new();
}
// Workflow Step (Session 16 — Mig 21 drastic refactor):
// Mỗi step = 1 (Phòng × Cấp + Approvers users). Sequential per Order.
// Service iterate steps OrderBy(Order), advance Contract.CurrentWorkflowStepIndex
// per approve. Phase column deprecated post-Mig 21 (set ChoDuyet=10 cho new
// definitions, giữ giá trị cũ cho backward compat data).
public class WorkflowStep : BaseEntity
{
public Guid WorkflowDefinitionId { get; set; }
public int Order { get; set; } // 1-based sequence
public ContractPhase Phase { get; set; } // which ContractPhase this step represents
public string Name { get; set; } = string.Empty; // display, can differ from Phase label
public int? SlaDays { get; set; } // null = no SLA for this step
public ContractPhase Phase { get; set; } // [DEPRECATED post-Mig 21] dùng ChoDuyet=10 cho new
public string Name { get; set; } = string.Empty; // display "Phòng A — Cấp 1"
public int? SlaDays { get; set; } // null = no SLA
// Mig 21 — Phòng × Cấp (flat workflow). Approver match: actor.DepartmentId
// == step.DepartmentId AND actor.PositionLevel == step.PositionLevel
// (OR-of-many cùng cấp+phòng). Bypass: actor.PositionLevel cao hơn cùng dept
// + CanBypassReview → skip cấp dưới.
public Guid? DepartmentId { get; set; }
public PositionLevel? PositionLevel { get; set; }
public WorkflowDefinition? WorkflowDefinition { get; set; }
public List<WorkflowStepApprover> Approvers { get; set; } = new();
// Inner steps (Mig 20) — N-stage approval Phòng × PositionLevel sequential.
// Mirror PE pattern (Mig 18). Empty list → service fallback logic 2-stage
// Review/Confirm legacy (Mig 16) per dept.
public List<WorkflowStepInnerStep> InnerSteps { get; set; } = new();
}
public enum WorkflowApproverKind
@ -54,23 +61,3 @@ public class WorkflowStepApprover : BaseEntity
public WorkflowStep? Step { get; set; }
}
// Inner step (Mig 20 — Phase 9+) — sub-step level con cấu hình bên trong 1
// WorkflowStep cha (= 1 phase). Mirror PurchaseEvaluationWorkflowStepInnerStep
// pattern (Mig 18). Cho phép admin định nghĩa thứ tự duyệt N-stage theo
// Department × PositionLevel: NV.A → PP.A → TP.A → NV.B → PP.B → TP.B → ...
//
// User khớp DepartmentId + PositionLevel + Order tiếp theo chưa duyệt = approver
// hợp lệ. CanBypassReview ở User cho TP skip NV+PP cùng dept (audit IsBypassed).
public class WorkflowStepInnerStep : BaseEntity
{
public Guid WorkflowStepId { get; set; }
public int Order { get; set; }
public Guid DepartmentId { get; set; }
public PositionLevel PositionLevel { get; set; } // NV / PP / TP
public string? Name { get; set; } // hiển thị FE — vd "NV.PRO duyệt"
public int? SlaDays { get; set; }
public bool IsRequired { get; set; } = true;
public WorkflowStep? Step { get; set; }
}

View File

@ -40,6 +40,14 @@ public class PurchaseEvaluation : AuditableEntity
// sửa lại + trình lại → quay về RejectedFromPhase thay vì đi tuần tự.
public PurchaseEvaluationPhase? RejectedFromPhase { get; set; }
// Flat workflow tracking (Session 16 — Migration 21):
// - CurrentWorkflowStepIndex: 0-based index của step đang chờ approver
// (khi Phase=ChoDuyet). Null khi DangSoanThao/DaDuyet/TuChoi.
// - RejectedAtStepIndex: snapshot CurrentWorkflowStepIndex tại Trả lại.
// Drafter resume → restore CurrentWorkflowStepIndex (jump-back).
public int? CurrentWorkflowStepIndex { get; set; }
public int? RejectedAtStepIndex { get; set; }
public List<PurchaseEvaluationSupplier> Suppliers { get; set; } = new();
public List<PurchaseEvaluationDetail> Details { get; set; } = new();
public List<PurchaseEvaluationQuote> Quotes { get; set; } = new();

View File

@ -22,11 +22,5 @@ public class PurchaseEvaluationDepartmentApproval : AuditableEntity
public DateTime ApprovedAt { get; set; }
public bool IsBypassed { get; set; }
// N-stage inner step link (Mig 18) — null cho data legacy 2-stage Review/Confirm.
// Có giá trị khi step cha có InnerSteps configured → mỗi sub-step approve =
// 1 row riêng với InnerStepId set. Cùng Stage=Confirm (legacy field giữ nguyên
// cho backward compat — N-stage không dùng Review/Confirm semantics).
public Guid? InnerStepId { get; set; }
public PurchaseEvaluation? PurchaseEvaluation { get; set; }
}

View File

@ -1,21 +1,25 @@
namespace SolutionErp.Domain.PurchaseEvaluations;
// State machine cho phiếu Duyệt NCC (tiền-HĐ). 2 workflow khác nhau cùng
// share state space này — A (DuyetNcc) dùng subset, B (DuyetNccPhuongAn)
// dùng full.
// State machine PE — Session 16 drastic refactor (Mig 21):
// DangSoanThao → ChoDuyet (Drafter trình, init CurrentWorkflowStepIndex=0)
// ChoDuyet → ChoDuyet (advance step pointer mỗi lần approve)
// ChoDuyet → DaDuyet (last step done — terminal thành công)
// ChoDuyet → DangSoanThao (Trả lại — save RejectedAtStepIndex, Drafter sửa)
// ChoDuyet → TuChoi (Từ chối — terminal khoá phiếu)
// DangSoanThao → TuChoi (Drafter huỷ trước trình)
//
// A: DangSoanThao → ChoPurchasing → ChoCCM → ChoCEODuyetNCC → DaDuyet
// B: DangSoanThao → ChoPurchasing → ChoDuAn → ChoCCM → ChoCEODuyetPA → ChoCEODuyetNCC → DaDuyet
// Cả 2: từ DangSoanThao có thể → TuChoi; từ mọi phase duyệt reject → DangSoanThao.
// LEGACY values 2-6 + 98 deprecated post-Mig 21 (data cũ vẫn đọc OK,
// new workflow definitions chỉ dùng DangSoanThao/ChoDuyet/DaDuyet/TuChoi).
public enum PurchaseEvaluationPhase
{
DangSoanThao = 1,
ChoPurchasing = 2,
ChoDuAn = 3, // chỉ B
ChoCCM = 4,
ChoCEODuyetPA = 5, // chỉ B (duyệt phương án trước)
ChoCEODuyetNCC = 6, // chung cả A & B — duyệt chọn đơn vị
ChoPurchasing = 2, // [LEGACY] deprecated
ChoDuAn = 3, // [LEGACY] deprecated
ChoCCM = 4, // [LEGACY] deprecated
ChoCEODuyetPA = 5, // [LEGACY] deprecated
ChoCEODuyetNCC = 6, // [LEGACY] deprecated
DaDuyet = 7, // terminal thành công
TraLai = 98, // approver trả về cho Drafter sửa (vẫn cho edit, khác TuChoi)
ChoDuyet = 10, // [Mig 21] generic intermediate, dùng CurrentWorkflowStepIndex tracking
TraLai = 98, // [LEGACY] deprecated — Session 14 chốt thay bằng Trả lại = về DangSoanThao
TuChoi = 99, // terminal từ chối — KHÔNG cho edit/thao tác
}

View File

@ -4,17 +4,12 @@ using SolutionErp.Domain.Identity; // reuse PositionLevel
namespace SolutionErp.Domain.PurchaseEvaluations;
// Versioned workflow definition cho module Duyệt NCC — pattern giống HĐ
// nhưng tách table riêng vì Phase là PurchaseEvaluationPhase enum (không
// phải ContractPhase). Admin có UI /system/pe-workflows/:typeCode tương
// tự /system/workflows/:typeCode.
//
// Invariant: AT MOST ONE IsActive=true per PurchaseEvaluationType tại 1
// thời điểm. PurchaseEvaluation.WorkflowDefinitionId pin tại create →
// phiếu cũ không bị ảnh hưởng khi admin active version mới.
// Versioned workflow definition cho module Duyệt NCC — pattern giống HĐ.
// Invariant: AT MOST ONE IsActive=true per PurchaseEvaluationType.
// PurchaseEvaluation.WorkflowDefinitionId pin tại create.
public class PurchaseEvaluationWorkflowDefinition : BaseEntity
{
public string Code { get; set; } = string.Empty; // "QT-DN-A" / "QT-DN-B" default
public string Code { get; set; } = string.Empty;
public int Version { get; set; }
public PurchaseEvaluationType EvaluationType { get; set; }
public string Name { get; set; } = string.Empty;
@ -25,22 +20,23 @@ public class PurchaseEvaluationWorkflowDefinition : BaseEntity
public List<PurchaseEvaluationWorkflowStep> Steps { get; set; } = new();
}
// Workflow Step PE (Session 16 — Mig 21 drastic refactor):
// Mỗi step = 1 (Phòng × Cấp + Approvers users). Sequential per Order.
// Service iterate steps OrderBy(Order), advance PE.CurrentWorkflowStepIndex.
public class PurchaseEvaluationWorkflowStep : BaseEntity
{
public Guid PurchaseEvaluationWorkflowDefinitionId { get; set; }
public int Order { get; set; }
public PurchaseEvaluationPhase Phase { get; set; }
public string Name { get; set; } = string.Empty;
public PurchaseEvaluationPhase Phase { get; set; } // [DEPRECATED] dùng ChoDuyet=10 cho new
public string Name { get; set; } = string.Empty; // "Phòng A — Cấp 1"
public int? SlaDays { get; set; }
// Mig 21 — Phòng × Cấp. Approver match Dept + PositionLevel (OR cùng cấp+phòng).
public Guid? DepartmentId { get; set; }
public PositionLevel? PositionLevel { get; set; }
public PurchaseEvaluationWorkflowDefinition? Definition { get; set; }
public List<PurchaseEvaluationWorkflowStepApprover> Approvers { get; set; } = new();
// Inner steps (Mig 18) — N-stage approval cấu hình động trong cùng 1 phase.
// Empty list → fallback logic 2-stage Review/Confirm legacy (Mig 16) per dept.
// Có item → service loop theo Order: user khớp Department × PositionLevel
// duyệt sub-step. Tất cả required InnerSteps Done → cho phase transition.
public List<PurchaseEvaluationWorkflowStepInnerStep> InnerSteps { get; set; } = new();
}
public class PurchaseEvaluationWorkflowStepApprover : BaseEntity
@ -51,24 +47,3 @@ public class PurchaseEvaluationWorkflowStepApprover : BaseEntity
public PurchaseEvaluationWorkflowStep? Step { get; set; }
}
// Inner step (Mig 18 — Phase 9+) — sub-step level con cấu hình bên trong 1
// WorkflowStep cha (= 1 phase). Cho phép admin định nghĩa thứ tự duyệt N-stage
// theo Department × PositionLevel: NV.A → PP.A → TP.A → NV.B → PP.B → TP.B → ...
//
// User khớp DepartmentId + PositionLevel + Order tiếp theo chưa duyệt = approver
// hợp lệ. CanBypassReview ở User cho TP skip NV+PP cùng dept (audit IsBypassed).
//
// IsRequired=false → cho phép skip không cần row approval (vd "PP optional").
public class PurchaseEvaluationWorkflowStepInnerStep : BaseEntity
{
public Guid PurchaseEvaluationWorkflowStepId { get; set; }
public int Order { get; set; } // thứ tự sequential trong cùng step cha
public Guid DepartmentId { get; set; }
public PositionLevel PositionLevel { get; set; } // NV / PP / TP
public string? Name { get; set; } // hiển thị FE — vd "NV.PRO duyệt"
public int? SlaDays { get; set; } // override step.SlaDays nếu set
public bool IsRequired { get; set; } = true; // false → optional skip không cần row
public PurchaseEvaluationWorkflowStep? Step { get; set; }
}