# HANDOFF — Brief 5 phút cho session tiếp theo **Last updated:** 2026-05-08 (Session 16 — **🎯 DRASTIC REFACTOR DONE: flat workflow Phòng × Cấp + Mig 21 + Service rewrite + FE Designer rewrite. 2 commit Chunk A+B. 96 → 77 test pass (drop 19 legacy). Backward compat: legacy phase 2-9+98 enum giữ cho data cũ.**) ## TL;DR Session 16 (08/05 — Drastic refactor flat workflow EXECUTE) Resume từ Session 15 defer plan. Per memory `feedback_drastic_refactor_scope.md`: dedicated session, fresh context, conservative buffer. **Spec:** Workflow flat list (Phòng × Cấp × Approvers). Mỗi step = 1 (Phòng × Cấp). Service iterate steps OrderBy Order, advance pointer. Phase enum simplify ChoDuyet=10. Pin WorkflowDefinitionId. **2 chunk per-commit (5-6 chunk plan rút gọn vì BE tightly coupled):** ### Chunk A (`dbb0089`) — All BE: Domain + Mig 21 + Service + Tests **Domain entities:** - Phase enum (PE + Contract): + ChoDuyet=10 generic intermediate. Legacy 2-6 + 98 deprecated (giữ enum cho data cũ). - WorkflowStep + DepartmentId Guid? FK Restrict + PositionLevel int? - PurchaseEvaluation/Contract + CurrentWorkflowStepIndex int? + RejectedAtStepIndex int? - DROP class WorkflowStepInnerStep + nav (PE + Contract) - DROP *DepartmentApproval.InnerStepId column **EF Configurations:** - DROP InnerStep config (PE + Contract) → table dropped - WorkflowStep config + DeptId/PositionLevel + FK Restrict - DepartmentApprovals: restore simple unique non-filtered (Mig 19/20 filtered split reverse) **ApplicationDbContext:** DROP DbSet<*WorkflowStepInnerStep> × 2 **Migration 21** `RefactorWorkflowToFlatModel` GỘP: - 4 ALTER (PE/Contract +CurrentStepIndex +RejectedAtStepIndex) - 2 ALTER (WorkflowStep +DepartmentId +PositionLevel) PE + Contract - DROP TABLE × 2 (PEWorkflowStepInnerSteps + WorkflowStepInnerSteps Mig 18+20) - DROP COLUMN × 2 (*DeptApproval.InnerStepId) - DROP filtered indexes Mig 19/20 - RESTORE simple UNIQUE (TargetId, Phase, Dept, Stage) non-filtered × 2 **Service rewrite (PE + Contract WorkflowService.TransitionAsync):** - DangSoanThao → ChoDuyet (Drafter trình, init idx=0) - ChoDuyet → ChoDuyet (advance idx per approve) - ChoDuyet → DaDuyet/DaPhatHanh (idx ≥ steps.Count → terminal, gen mã HĐ Contract) - ChoDuyet → DangSoanThao (Trả lại — save RejectedAtStepIndex) - ChoDuyet → TuChoi (Từ chối — khoá vĩnh viễn) - Resume Drafter (DangSoanThao + RejectedAtStepIndex≠null) → ChoDuyet jump-back - Match approver: actor.Dept == step.Dept AND actor.PositionLevel >= step.PositionLevel (OR-of-many cùng cấp/dept) OR Approvers.Kind=User|Role match - Admin role bypass policy **App CQRS:** WorkflowStepDto + WorkflowStepInput drop InnerStep, add DepartmentId/DepartmentName/PositionLevel (PE + Contract mirror). **Tests:** - DROP `PeNStageApprovalTests.cs` (6) + `ContractNStageApprovalTests.cs` (6) + `PeTwoStageApprovalTests.cs` (7) — legacy - UPDATE `PeWorkflowAdminTests` signature for new flat input - **96 → 77 test pass** (-19 legacy) **3-file rule** Mig 21 commit đủ (.cs + Designer + Snapshot). ### Chunk B (`88a5be1`) — FE Designer + types **PeWorkflowsPage + WorkflowsPage rewrite (~210 LOC each):** - Drop InnerStepDto + EditInnerStep types - Drop PHASE_OPTIONS (auto-assign ChoDuyet=10 behind scenes) - StepDto + EditStep + departmentId, departmentName, positionLevel - Designer step UI rewrite: Tên + Phòng Select + Cấp Select + SLA + Approvers (Role/User optional fallback). Drop InnerSteps sub-section. - DefinitionCard view: badge Phòng emerald + Cấp NV/PP/TP violet - Save payload: phase=10 (ChoDuyet) - Hint amber: "User cùng Phòng + Cấp ≥ step → duyệt được (OR-of-many)" **types/purchaseEvaluation.ts (fe-admin + fe-user mirror):** + ChoDuyet=10 enum + label "Đang duyệt" + color amber. Legacy 2-6 + 98 keep. **Chunk C (FE PeWorkflowPanel) SKIPPED** — existing UI compatible (workflow.nextPhases BE-driven, 3-button Trả lại/Từ chối Session 14 reuse với target=DangSoanThao/TuChoi pattern). ### Verify - ✅ dotnet build SolutionErp.slnx 0 error - ✅ dotnet ef database update Mig 21 LocalDB applied OK - ✅ dotnet test 77 pass (54 Domain + 23 Infra) - ✅ npm build fe-admin + fe-user pass ### Cumulative sau Session 16 | | Trước S16 | Sau S16 | |---|---:|---:| | BE LOC | ~15800 | ~15500 (-300 service simplified) | | Migrations | 20 | **21** | | DB tables | 57 | **55** (-2 InnerStep tables) | | Tests | 96 | **77** (-19 legacy N-stage/2-stage) | | FE pages | 32 | 32 (rewrite existing 2 designer) | ## ⚠️ CẢNH BÁO Session 17+ 1. **UAT live test** — workflow flat ready. Tạo new workflow definition qua `/system/pe-workflows/:typeCode` với 3 phòng × N cấp setup. Verify Drafter trình → cấp 1 phòng A → cấp 2 phòng A → cấp 1 phòng B → ... → DaDuyet flow. 2. **Old PE/HĐ pinned legacy workflow definitions** (phase=ChoPurchasing/ChoCCM/etc) — service rewrite chỉ handle ChoDuyet=10 + DangSoanThao/DaDuyet. Old data ở phase 2-6 sẽ stuck (admin manual transition required). Recommend: data migration script convert old workflow → new flat model (defer). 3. **Approver explicit (Role/User Approvers list)** — fallback nếu user không match Dept+PositionLevel của step. Cho phép user external (không thuộc dept) duyệt qua Role match (vd Admin) hoặc User explicit. 4. **Bypass cấp dưới cùng dept** — User TP với CanBypassReview=true cùng dept và PositionLevel cao hơn step.PositionLevel → duyệt qua. KHÔNG batch upsert NV+PP rows như Mig 18 N-stage trước (đơn giản hóa: 1 step approve = 1 row). 5. **N-stage tests dropped** — 19 test legacy (Mig 18, 20 N-stage + Mig 16 2-stage). Có thể viết test mới cho flat workflow flow nếu UAT phát sinh bug. Defer. 6. **Sample data N-stage seed** task vẫn pending (Session 14). Block trên DesignTime vs Runtime DB gotcha + DbInitializer seed flow. 7. **Budget N-stage** vẫn defer (cần versioned WF migration trước). 8. **schema-diagram §17-21 update** defer cron audit 2026-06-01. ## TL;DR Session 15 (07/05 — Tooltip diagnose + drastic refactor DEFER) User UAT live screenshot báo button "Lưu & Gửi Duyệt" KHÔNG hoạt động + suy đoán "trùng ID" giữa các phiếu. **Diagnose (commit `835cc7f`):** - Root cause: button silent disabled khi `evaluation.workflow.nextPhases` không có forward phase (chỉ TuChoi/TraLai). Cause khả năng: workflow definition pinned thiếu adjacent step → `policy.NextPhasesFrom(DangSoanThao)` return empty. - Improvement: tooltip + dialog hiển thị reason rõ ràng: - `submitDisabledReason` text: "Phiếu đã ở phase X — chỉ Bản nháp/Trả lại mới sửa+gửi" / "Workflow không có phase tiếp theo từ X. Liên hệ admin kiểm tra cấu hình" - Button title attribute → hover show reason hoặc forward phase label - Dialog confirm show forward phase explicit ("Sẽ chuyển sang Chờ Purchasing") - Mirror fe-admin + fe-user. Build pass cả 2. KHÔNG đụng BE — chỉ FE diagnostic UX. - "Trùng ID" KHÔNG phải bug FE — `PurchaseEvaluationWorkspacePage` URL state đúng, mỗi PE row unique GUID + MaPhieu. Suy đoán user do button silent. **Plan drastic refactor → DEFER:** User confirm "bỏ phase enum hoàn toàn, dùng ChoDuyet=10 đơn nhất + currentStepIndex tracking" — refactor workflow từ phase-based + InnerStep nested model sang flat WorkflowStep model (mỗi step = Phòng × Cấp + Approvers users). Edit working tree 12 files (Domain entities + EF Configurations + DbContext): - Phase enum +ChoDuyet=10, legacy values 2-6 deprecated - WorkflowStep +DepartmentId, +PositionLevel - Drop class WorkflowStepInnerStep + nav (PE + Contract) - PE/Contract +CurrentWorkflowStepIndex int?, +RejectedAtStepIndex int? - *DepartmentApproval drop InnerStepId column - EF Configurations: drop InnerStep config + nav, restore simple unique non-filtered - DbContext: drop DbSet × 2 Reality check scope realistic ~8-10h: 1. Domain + EF + DbContext (~50min) ✓ done in working tree 2. PolicyRegistry rewrite PE+Contract (~45min) 3. App CQRS DTOs rewrite (~45min) 4. Service rewrite PE+Contract (~2-3h) 5. Tests rewrite — drop 12 N-stage tests + update remaining (~1.5h) 6. Migration 21 + LocalDB apply + verify (~30min) 7. FE Designer rewrite (~1.5h) 8. FE PeWorkflowPanel + workflow timeline (~1h) 9. Docs/Skill update (~45min) Vượt session boundary + risk session deep ~30 commits → **REVERT working tree** về `835cc7f` clean state. Test 96 pass intact. **Decision memorized:** add memory `feedback_drastic_refactor_scope.md` — drastic refactor cần dedicated session, scope estimation conservative (2x buffer), tránh mid-session big refactor. ## ⚠️ CẢNH BÁO Session 16+ 1. **Drastic refactor flat workflow chưa làm — DEFER** với plan chi tiết. Khi resume: - Plan kỹ 6 chunk per-commit - Buffer 2x estimate (~16h thực tế) - Tests rewrite biggest risk - Hoặc fall back Approach Y (FE Designer flat UI giới hạn 5 phòng) ROI 1-2h nếu user OK trade-off 2. **Task 2 sample data seed N-stage** vẫn pending (block trên DesignTime vs Runtime DB gotcha + DbInitializer seed flow) 3. **schema-diagram §17-19 Mig 18-20** vẫn defer cron audit 2026-06-01 4. **Hard blockers Ops** giữ nguyên 6 task ## TL;DR Session 14 (07/05 — PE 3-button approval workflow) User chỉ thị thay 2-button approval (Duyệt + Reject mơ hồ) bằng **3 hành động rõ ràng** cho approver: - **Duyệt** = forward phase tiếp theo (decision=Approve) - **Trả lại** = về DangSoanThao + Drafter sửa (decision=Reject + target=DangSoanThao). Smart reject pattern Mig 16 + clear N-stage rows + Drafter resume jump-back tới phase đã reject. - **Từ chối** = phase=TuChoi (decision=Reject + target=TuChoi). Phiếu khoá vĩnh viễn (17 handler Mig 16 lock edit). Drafter phải tạo phiếu mới. **1 commit (`0d77698`):** - **Domain `PurchaseEvaluationPolicy.cs`**: NccOnly + NccWithPlan thêm `(X → TuChoi)` transition cho mọi phase trung gian (ChoPurchasing/ChoDuAn/ChoCCM/ChoCEODuyetPA/ChoCEODuyetNCC) với roles của phase đó. FromDefinition expand: mỗi step (trừ DangSoanThao) thêm (step.Phase → TuChoi) với roles step. - **Service** `PurchaseEvaluationWorkflowService.TransitionAsync` — Reject branch tách 2 case: ``` if (decision == Reject) { if (target != TuChoi) { // Trả lại RejectedFromPhase = fromPhase target = DangSoanThao // force clear N-stage rows tại fromPhase } // else target=TuChoi: giữ nguyên, KHÔNG set RejectedFromPhase, KHÔNG clear } ``` - **FE PeWorkflowPanel (admin + user mirror)**: render 3 button rõ: - "✓ Duyệt →