[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; }
}