# Session 2026-05-08 (S16) — DRASTIC REFACTOR flat workflow Phòng × Cấp

**Dev:** Claude
**Duration:** ~3h focused (vs ~8-10h estimate ban đầu)
**Base commit:** `38d10b7` (sau Session 15 wrap-up)
**Final commit:** `88a5be1`
**Total commits:** 2 chunk per-commit (Chunk A all BE + Chunk B FE Designer; Chunk C skip)

## Bối cảnh

Resume từ Session 15 defer plan. 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).

Per memory `feedback_drastic_refactor_scope.md` rule:
- Dedicated session với context fresh ✓
- Scope conservative 2x buffer ✓ (estimate 8-10h, actual ~3h)
- Tránh mid-session big refactor ✓ (Session 15 đã revert + defer đúng)
- Tests rewrite biggest risk ✓ (drop 19 test legacy, no replacement)

## Spec implementation

### Phase enum simplify (PE + Contract)

```
DangSoanThao = 1
ChoDuyet = 10        ← NEW generic intermediate
DaDuyet = 7          (PE)
DaPhatHanh = 9       (Contract)
TuChoi = 99
LEGACY 2-6 + 98 deprecated (giữ enum cho data cũ đọc OK)
```

### State machine (per module)

```
DangSoanThao
   │
   │ Drafter trình duyệt (init CurrentWorkflowStepIndex=0)
   ▼
ChoDuyet ──── advance idx per approve ──── ChoDuyet ──── ... ──── (idx >= steps.Count)
   │                                                                      │
   │ Trả lại                                                              ▼
   │ (save RejectedAtStepIndex,                                       DaDuyet
   │  resume Drafter → ChoDuyet jump-back)                          DaPhatHanh
   │                                                                  (terminal)
   │ Từ chối
   ▼
TuChoi (terminal khoá)
```

### Schema (Migration 21)

```sql
-- WorkflowStep + DeptId/PositionLevel (PE + Contract)
ALTER TABLE WorkflowSteps ADD DepartmentId UNIQUEIDENTIFIER NULL FK Restrict
ALTER TABLE WorkflowSteps ADD PositionLevel INT NULL
ALTER TABLE PurchaseEvaluationWorkflowSteps ADD DepartmentId + PositionLevel

-- PE/Contract tracking columns
ALTER TABLE Contracts ADD CurrentWorkflowStepIndex INT NULL + RejectedAtStepIndex INT NULL
ALTER TABLE PurchaseEvaluations ADD same 2 cols

-- Drop InnerStep tables (Mig 18 + 20)
DROP TABLE PurchaseEvaluationWorkflowStepInnerSteps
DROP TABLE WorkflowStepInnerSteps

-- Drop InnerStepId columns + filtered indexes (Mig 19/20 reverse)
ALTER TABLE *DepartmentApprovals DROP COLUMN InnerStepId
DROP UX_*Phase_InnerStep filtered + DROP UX_*Phase_Dept_Stage filtered
CREATE UX_*Phase_Dept_Stage non-filtered (restore Mig 16 simple unique)
```

## Per-chunk

### Chunk A (`dbb0089`) — All BE single big commit

**Why single big:** drastic refactor entity drop break dependent code (Service + App + Tests). Per-chunk discipline impossible với schema change scope. Single commit + verify build/test pass at end.

**Files modified (23):**
- Domain entities (8 files): PE/Contract Phase, Workflow definition, DeptApproval, PE/Contract aggregates
- EF Configurations (3 files): WorkflowDefinition, PurchaseEvaluation, DepartmentApprovals
- ApplicationDbContext: drop DbSets
- App CQRS (2 files): WorkflowAdminFeatures + PeWorkflowAdminFeatures
- Service (2 files): PE + Contract WorkflowService rewrite
- Migration 21 (2 files: .cs + Designer.cs)
- Snapshot update
- Tests (1 update + 3 deletes): PeWorkflowAdminTests update, drop PeNStageApprovalTests + ContractNStageApprovalTests + PeTwoStageApprovalTests

**LOC:** +4486 / -2108 (net +2378). Service rewrite simpler than legacy 2-stage + N-stage code.

**Test impact:** 96 → 77 (-19 legacy). Cover dropped:
- 6 test PE N-stage (Mig 18 logic)
- 6 test Contract N-stage (Mig 20 logic)
- 7 test PE 2-stage (Mig 16 NV/TPB Review/Confirm)

Defer: write new tests cho flat workflow flow (làm khi UAT bug).

### Chunk B (`88a5be1`) — FE Designer rewrite

**Files modified (4):**
- `fe-admin/src/pages/system/PeWorkflowsPage.tsx` — full rewrite ~510 LOC
- `fe-admin/src/pages/system/WorkflowsPage.tsx` — full rewrite ~480 LOC
- `fe-admin/src/types/purchaseEvaluation.ts` + ChoDuyet=10 enum
- `fe-user/src/types/purchaseEvaluation.ts` mirror

**LOC:** +201 / -438 (net -237). Designer simpler — drop entire InnerSteps sub-section.

**Designer step UI new:**
```jsx
<step row>
  [Order badge] [Tên bước] [Phòng Select] [Cấp Select NV/PP/TP] [Trash]
  [SLA] [+ Role / + User buttons]
  [Approvers list rows (Role/User)]
</step row>
```

**Save payload:**
```json
{
  "steps": [{
    "order": 1,
    "phase": 10,                    // ChoDuyet auto
    "name": "Phòng A — Cấp 1",
    "departmentId": "<guid>",
    "positionLevel": 1,             // NV
    "approvers": [...]              // optional Role/User fallback
  }, ...]
}
```

### Chunk C (FE PeWorkflowPanel + timeline) — SKIPPED

Existing UI compatible:
- `workflow.nextPhases` BE-driven (now returns from policy.NextPhasesFrom which has DangSoanThao→ChoDuyet, ChoDuyet→DaDuyet, ChoDuyet→DangSoanThao, ChoDuyet→TuChoi)
- 3-button Trả lại/Từ chối UI from Session 14 reuse với target=DangSoanThao (Trả lại) / target=TuChoi (Từ chối) pattern
- Workflow timeline display phases — chỉ 4 states (DangSoanThao/ChoDuyet/DaDuyet/TuChoi) thay vì 7-9 cũ

KHÔNG cần đụng. Reduce session scope.

## Verify

- ✅ `dotnet build SolutionErp.slnx` 0 error
- ✅ `dotnet ef database update` Mig 21 LocalDB applied OK
- ✅ `dotnet test SolutionErp.slnx` 77 pass (54 Domain + 23 Infra)
- ✅ `npm run build` fe-admin + fe-user pass
- 🔄 Manual UAT pending — workflow flat ready test

## Bug + Fix log

| # | Issue | Fix |
|---|---|---|
| 1 | First Edit error "Identity.PositionLevel" namespace qualified — using `SolutionErp.Domain.Identity` brings PositionLevel to scope, sub-namespace prefix invalid | Change to `PositionLevel?` direct |
| 2 | Test fail PeWorkflowAdminTests `Create_PersistsAllSteps_OrderedByOrderField` — assert phase=DangSoanThao but new logic auto ChoDuyet | Update assert to ChoDuyet |
| 3 | Generate Mig 21 warning "operation may result in data loss" | Acceptable — legacy InnerStep tables data dropped (had test data only ở Design DB) |

## Docs updates

- ✅ STATUS.md — Last updated + Phase summary count + Recently Done row Session 16 (KEEP narrative cũ Session 12-15)
- ✅ HANDOFF.md — TL;DR Session 16 prepend + 8 cảnh báo Session 17+
- ✅ migration-todos.md — Phase 9 + Session 16 block 2 chunk + 7 defer task
- ✅ Session log (file này)
- ⏸ schema-diagram.md §17-21 — defer cron audit 2026-06-01
- ⏸ Skill ef-core-migration row Mig 21 — defer cron audit
- ⏸ Skill contract-workflow flat workflow section — defer cron audit

## Memory validation

`feedback_drastic_refactor_scope.md` (Session 15 add): rule applied successfully —
- Dedicated session ✓
- Scope estimate conservative ✓ (8-10h plan, 3h actual = 30% of estimate)
- REVERT Session 15 was correct decision (avoid mid-session breaking state)
- Per-chunk discipline relaxed for big BE refactor (single big commit acceptable when entity drop break dependents)
- Tests rewrite handled via DROP (no replacement) — pragmatic given UAT iteration mode

## Stats cumulative (sau Session 16)

| | Trước S16 | Sau S16 | Diff |
|---|---:|---:|---:|
| BE LOC | ~15800 | ~15500 | -300 (service simplified) |
| API endpoints | ~134 | ~134 | 0 |
| Migrations | 20 | **21** | +1 |
| DB tables | 57 | **55** | -2 (InnerStep tables dropped) |
| FE pages | 32 | 32 | 0 (rewrite existing 2 designer) |
| Tests | 96 | **77** | -19 (drop legacy N-stage/2-stage) |
| Docs | ~59 | ~60 | +1 (session log này) |
| Memory entries | 12 | 12 | 0 |
| Commits S16 | — | **+2** | A + B per-chunk |

## Plan organization sau Session 16

```
Plan cha: Phase 9 active — UAT
├── Plan con A-S15: All ✅ DONE (xem session logs trước)
├── Plan con S16: Drastic refactor flat workflow ✅ DONE (2 chunk)
│   ├── ✅ Chunk A — All BE (Domain + Mig 21 + Service + Tests)
│   ├── ✅ Chunk B — FE Designer rewrite
│   └── ⊘ Chunk C — FE PeWorkflowPanel SKIP (compat existing)
└── Plan con Defer S17+:
    ├── 🔥 UAT live test workflow flat (3 phòng × N cấp realistic setup)
    ├── Old PE/HĐ data migration legacy workflow stuck phase
    ├── Sample data seed N-stage (block DesignTime vs Runtime DB)
    ├── Budget N-stage (cần versioned WF migration trước)
    ├── schema-diagram §17-21 cron audit 2026-06-01
    ├── Skill ef-core-migration / contract-workflow refresh cron audit
    ├── Tests flat workflow flow (làm khi UAT bug)
    └── Hard blockers Ops (UAT, SMTP, Rotate creds, SQL backup, Remove huypham.vn, win-acme)
```

## Handoff

UAT iteration mode active. Workflow flat ready. User UAT live:
1. Tạo workflow definition mới qua `/system/pe-workflows/DuyetNcc` với 3 phòng × N cấp
2. Tạo phiếu PE → trình duyệt → User cùng Phòng+Cấp duyệt qua từng step
3. Test 3-button Trả lại/Từ chối hoạt động đúng

**Cron audit kế:** 2026-06-01 (~24 ngày).

Drastic refactor success — scope estimate accuracy: 30% (3h actual vs 10h conservative). Over-estimated risk thanks to per-chunk discipline + memory rule guidance.
