feat(ECR-033): AskOperations HandoffCase 只读并 Closed

HandoffCase catalog (000034) · Loop continuous.
This commit is contained in:
jackyu66git
2026-08-08 03:15:45 +08:00
parent 01063208a1
commit 487bee78d1
25 changed files with 457 additions and 1 deletions
+1
View File
@@ -42,6 +42,7 @@
| [ops-moderation-case.md](ops-moderation-case.md) | ContentSafety ModerationCase | §7 | `GET /admin/content-safety/cases*` | Ops-D · **ECR-030 Closed** |
| [ops-crisis-event.md](ops-crisis-event.md) | CrisisCare CrisisEvent | §7 | `GET /admin/crisis/events*` | Ops-D · **ECR-031 Closed** |
| [ops-intervention-outcome.md](ops-intervention-outcome.md) | CrisisCare InterventionOutcome | §7 | `GET /admin/crisis/interventions*` | Ops-D · **ECR-032 Closed** |
| [ops-handoff-case.md](ops-handoff-case.md) | AskOperations HandoffCase | §7 | `GET /admin/ask/handoffs*` | Ops-D · **ECR-033 Closed** |
新功能:复制 `_TEMPLATE.md` → 填满 → 在本表登记 → 再编码。
@@ -0,0 +1,41 @@
# Feature Spec: AskOperations · HandoffCaseOps · ECR-033
> Status: `Active`Loop continuous · **ECR-033 Closed**
> Parent: WAVE0-FROZEN · Predecessor: ECR-032 Closed
> Capability: `AskOperations` · BC: `Ask_Ops`
> 授权:`docs/WAVE0/LOOP_AUTHORIZATION.md`
## Non-goals
转人工写流 · 顾问执业 · UGC · 真支付
## L2 Domain
| 概念 | 语义 |
|------|------|
| `HandoffCase` | 本切片只读目录;code 唯一(若适用) |
## L3 API
| Method | Path | 权限 | 语义 |
|--------|------|------|------|
| GET | `/admin/ask/handoffs` | `admin.ask.read` | 只读 |
| GET | `/admin/ask/handoffs/{id}` | `admin.ask.read` | 只读 |
## Migration
`000034`:表 + 种子(若有)(权限复用)
## L4 AC
| ID | Then |
|----|------|
| AC-F-01 | list 含种子或空列表合法 |
| AC-F-02 | 已知 id get 200 |
| AC-F-03 | 未知 id → 404 |
| AC-S-01 | 无 Admin → 401 |
| AC-S-02 | 无权限 → 403 |
| AC-P-01 | list < 500ms |
| AC-O-01 | N/A 只读 |
contract_diff: `docs/CONTRACT_DIFF/ECR-033.yaml`
+4
View File
@@ -461,6 +461,10 @@ export const adminApi = {
request<{ items: Array<Record<string, unknown>> }>('GET', '/crisis/interventions'),
intervention: (id: string) =>
request<Record<string, unknown>>('GET', `/crisis/interventions/${id}`),
handoffs: () =>
request<{ items: Array<Record<string, unknown>> }>('GET', '/ask/handoffs'),
handoff: (id: string) =>
request<Record<string, unknown>>('GET', `/ask/handoffs/${id}`),
orders: () =>
request<{
items: Array<{
+1
View File
@@ -61,6 +61,7 @@ func (h *AdminHandler) Register(api *gin.RouterGroup) {
h.registerModerationCases(authed)
h.registerCrisisEvents(authed)
h.registerInterventionOutcomes(authed)
h.registerHandoffCases(authed)
}
func (h *AdminHandler) Login(c *gin.Context) {
@@ -0,0 +1,46 @@
package handler
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/yuxingu/digital-psychology/apps/api/internal/middleware"
"github.com/yuxingu/digital-psychology/apps/api/internal/service/admin"
"github.com/yuxingu/digital-psychology/apps/api/pkg/response"
)
func (h *AdminHandler) registerHandoffCases(authed *gin.RouterGroup) {
g := authed.Group("/ask")
g.GET("/handoffs", middleware.RequireAdminPermission(h.Svc, admin.PermAskRead), h.ListHandoffCases)
g.GET("/handoffs/:id", middleware.RequireAdminPermission(h.Svc, admin.PermAskRead), h.GetHandoffCase)
}
func (h *AdminHandler) ListHandoffCases(c *gin.Context) {
items, err := h.Svc.ListHandoffCases(c.Request.Context())
if err != nil {
response.Fail(c, http.StatusInternalServerError, 50050, "list handoff-case failed")
return
}
response.OK(c, gin.H{"items": items})
}
func (h *AdminHandler) GetHandoffCase(c *gin.Context) {
id, err := uuid.Parse(c.Param("id"))
if err != nil {
response.Fail(c, http.StatusBadRequest, 40002, "invalid id")
return
}
row, err := h.Svc.GetHandoffCase(c.Request.Context(), id)
if errors.Is(err, admin.ErrHandoffCaseNotFound) {
response.Fail(c, http.StatusNotFound, 40420, "handoff-case not found")
return
}
if err != nil {
response.Fail(c, http.StatusInternalServerError, 50051, "get handoff-case failed")
return
}
response.OK(c, row)
}
@@ -0,0 +1,88 @@
package integration_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"time"
"github.com/google/uuid"
"golang.org/x/crypto/bcrypt"
)
func TestAskOpsHandoffs(t *testing.T) {
r, pool := setupAPIPool(t)
ctx := context.Background()
tok := adminLogin(t, r, "admin", "change-me")
_, code := doAdminJSON(t, r, http.MethodGet, "/api/v1/admin/ask/handoffs", nil, "")
if code != http.StatusUnauthorized {
t.Fatalf("expected 401, got %d", code)
}
limitedRoleID := uuid.New()
_, err := pool.Exec(ctx, `INSERT INTO admin_roles(id, name, system) VALUES ($1,$2,false)`,
limitedRoleID, "lim_"+limitedRoleID.String()[:8])
if err != nil {
t.Fatal(err)
}
_, err = pool.Exec(ctx, `INSERT INTO admin_role_permissions(role_id, code) VALUES ($1,'admin.users.read')`, limitedRoleID)
if err != nil {
t.Fatal(err)
}
hash, err := bcrypt.GenerateFromPassword([]byte("limited-pass"), bcrypt.DefaultCost)
if err != nil {
t.Fatal(err)
}
limUser := fmt.Sprintf("lim_%d", time.Now().UnixNano())
_, err = pool.Exec(ctx, `INSERT INTO admin_accounts(username, password_hash, role_id) VALUES ($1,$2,$3)`,
limUser, string(hash), limitedRoleID)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_, _ = pool.Exec(ctx, `DELETE FROM admin_accounts WHERE username=$1`, limUser)
_, _ = pool.Exec(ctx, `DELETE FROM admin_roles WHERE id=$1`, limitedRoleID)
})
limTok := adminLogin(t, r, limUser, "limited-pass")
_, code = doAdminJSON(t, r, http.MethodGet, "/api/v1/admin/ask/handoffs", nil, limTok)
if code != http.StatusForbidden {
t.Fatalf("expected 403, got %d", code)
}
start := time.Now()
env, code := doAdminJSON(t, r, http.MethodGet, "/api/v1/admin/ask/handoffs", nil, tok)
if code != 200 || env.Code != 0 {
t.Fatalf("list http=%d msg=%s", code, env.Message)
}
if time.Since(start) > 500*time.Millisecond {
t.Fatalf("list too slow %v", time.Since(start))
}
var list struct {
Items []struct {
ID string `json:"id"`
Code string `json:"code"`
} `json:"items"`
}
_ = json.Unmarshal(env.Data, &list)
var id string
for _, it := range list.Items {
if it.Code == "demo_handoff" {
id = it.ID
break
}
}
if id == "" {
t.Fatalf("missing demo_handoff: %#v", list.Items)
}
env, code = doAdminJSON(t, r, http.MethodGet, "/api/v1/admin/ask/handoffs/"+id, nil, tok)
if code != 200 {
t.Fatalf("get %d", code)
}
_, code = doAdminJSON(t, r, http.MethodGet, "/api/v1/admin/ask/handoffs/"+fakeUUID(), nil, tok)
if code != http.StatusNotFound {
t.Fatalf("expected 404, got %d", code)
}
}
@@ -0,0 +1,58 @@
package repository
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
)
// HandoffCaseRow is HandoffCase catalog row.
type HandoffCaseRow struct {
ID uuid.UUID `json:"id"`
Code string `json:"code"`
Title string `json:"title"`
Status string `json:"status"`
Active bool `json:"active"`
System bool `json:"system"`
UpdatedAt time.Time `json:"updated_at"`
}
// ListHandoffCases returns HandoffCase catalog.
func (r *AdminRepo) ListHandoffCases(ctx context.Context) ([]HandoffCaseRow, error) {
rows, err := r.Pool.Query(ctx, `
SELECT id, code, title, status, active, system, updated_at
FROM ask_handoff_cases
ORDER BY active DESC, code ASC`)
if err != nil {
return nil, err
}
defer rows.Close()
var out []HandoffCaseRow
for rows.Next() {
var row HandoffCaseRow
if err := rows.Scan(&row.ID, &row.Code, &row.Title, &row.Status, &row.Active, &row.System, &row.UpdatedAt); err != nil {
return nil, err
}
out = append(out, row)
}
return out, rows.Err()
}
// GetHandoffCase loads one by id.
func (r *AdminRepo) GetHandoffCase(ctx context.Context, id uuid.UUID) (*HandoffCaseRow, error) {
var row HandoffCaseRow
err := r.Pool.QueryRow(ctx, `
SELECT id, code, title, status, active, system, updated_at
FROM ask_handoff_cases WHERE id=$1`, id,
).Scan(&row.ID, &row.Code, &row.Title, &row.Status, &row.Active, &row.System, &row.UpdatedAt)
if errors.Is(err, pgx.ErrNoRows) {
return nil, err
}
if err != nil {
return nil, err
}
return &row, nil
}
@@ -0,0 +1,34 @@
package admin
import (
"context"
"errors"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/yuxingu/digital-psychology/apps/api/internal/repository"
)
var ErrHandoffCaseNotFound = errString("handoff case not found")
// ListHandoffCases returns catalog.
func (s *Service) ListHandoffCases(ctx context.Context) ([]repository.HandoffCaseRow, error) {
items, err := s.Repo.ListHandoffCases(ctx)
if err != nil {
return nil, err
}
if items == nil {
items = []repository.HandoffCaseRow{}
}
return items, nil
}
// GetHandoffCase loads one.
func (s *Service) GetHandoffCase(ctx context.Context, id uuid.UUID) (*repository.HandoffCaseRow, error) {
row, err := s.Repo.GetHandoffCase(ctx, id)
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrHandoffCaseNotFound
}
return row, err
}
@@ -0,0 +1 @@
DROP TABLE IF EXISTS ask_handoff_cases;
@@ -0,0 +1,18 @@
-- ECR-033 HandoffCase (read catalog)
CREATE TABLE IF NOT EXISTS ask_handoff_cases (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
code varchar(64) NOT NULL UNIQUE,
title varchar(128) NOT NULL,
status varchar(32) NOT NULL CHECK (status IN ('open','closed')),
active boolean NOT NULL DEFAULT true,
system boolean NOT NULL DEFAULT false,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_ask_handoff_cases_active ON ask_handoff_cases(active);
INSERT INTO ask_handoff_cases(code, title, status, active, system)
VALUES ('demo_handoff', '示例转接案', 'closed', true, true)
ON CONFLICT (code) DO NOTHING;
@@ -0,0 +1,23 @@
# Backend Design: ECR-033 HandoffCase
| ID | BD-2026-033 |
| Status | Approved |
| Coding | Loop authorized |
| Level | L2 |
| Migration | YES 000034 |
## Backend Change Boundary
```text
Domain: HandoffCase (read)
App: AdminHandler → admin.Service → AdminRepo
API: GET /admin/ask/handoffs; GET /admin/ask/handoffs/{id}
Permission: admin.ask.read
Migration: 000034
```
## Out of boundary
转人工写流 · 顾问执业 · UGC · 真支付
Rollback: down migration + remove routes/UI
+1
View File
@@ -2,6 +2,7 @@
## 2026-08-08
- **ECR-033 Closed**AskOperations HandoffCasemigration 000034 · admin client · /ask/handoffs · 只读)
- **ECR-032 Closed**CrisisCare InterventionOutcomemigration 000033 · admin client · /crisis/interventions · 只读)
- **ECR-031 Closed**CrisisCare CrisisEventmigration 000032 · admin client · /crisis/events · 只读)
- **ECR-030 Closed**ContentSafety ModerationCasemigration 000031 · admin client · /content-safety/cases · 只读)
+8
View File
@@ -0,0 +1,8 @@
# CODE_REVIEW — ECR-033
**Verdict:** Approve → Closed
Date: 2026-08-08 · Loop continuous
- HandoffCase 只读;无 UGC/真支付
- Integration AC mapped · OpenAPI updated
+22
View File
@@ -0,0 +1,22 @@
ecr: ECR-033
capability: AskOperations
bounded_context: Ask_Ops
parent: WAVE0-FROZEN
predecessor: ECR-032
change:
type: additive
breaking_change: false
migration_required: true
compatibility_notes: >
Adds HandoffCase read catalog. Forbidden: UGC / real payment.
apis:
- method: GET
path: /api/v1/admin/ask/handoffs
change: added
- method: GET
path: /api/v1/admin/ask/handoffs/{id}
change: added
perms:
- code: admin.ask.read
change: unchanged
+15
View File
@@ -0,0 +1,15 @@
# ECR-033
**Title:** AskOperations · HandoffCase(只读薄切片)
**Status:** **Closed**
**Closed:** 2026-08-08Loop continuous
**Parent:** WAVE0-FROZEN · **Predecessor:** ECR-032 Closed
**Change Level:** L2
## Change
HandoffCase 只读 · migration 000034 · admin client · /ask/handoffs
## Linked
Spec `ops-handoff-case.md` · BD-2026-033 · CONTRACT_DIFF/ECR-033.yaml · TEST_REPORT/ECR-033.md
@@ -0,0 +1,6 @@
# ENGINEERING_SPEC — ECR-033
1. migration 000034
2. AdminRepo/Service/Handler
3. OpenAPI + admin-h5
4. Integration · Closed
@@ -0,0 +1,3 @@
# HANDOFF — ECR-033 Architect → Engineer
Loop continuous · Approved + Coding. Migration 000034. Forbidden: UGC/真支付.
@@ -0,0 +1,3 @@
# HANDOFF — ECR-033 Engineer → Reviewer
TestAskOpsHandoffs PASS · Ready for Closed.
@@ -0,0 +1,3 @@
# PRODUCT_SPEC — ECR-033
对齐 ops-handoff-case.md · Approved · Loop · L2 · HandoffCase 只读
+6
View File
@@ -0,0 +1,6 @@
# STATE — ECR-033
| Status | **Closed** |
| Phase | closed |
| Spec | ops-handoff-case.md |
| Updated | 2026-08-08 |
+12
View File
@@ -0,0 +1,12 @@
id: TASK-033-ECR033
ecr: ECR-033
title: AskOperations · HandoffCase(只读薄切片)
role: engineer
status: closed
change_level: L2
parent: WAVE0-FROZEN
predecessor: ECR-032
acceptance:
- Spec AC mapped
- HandoffCase read only
- No UGC / payment
+33
View File
@@ -0,0 +1,33 @@
# TEST_REPORT — ECR-033 HandoffCase
Date: 2026-08-08 · Loop continuous · commit: `PENDING`
## Commands
```bash
cd apps/api && go test ./internal/integration/ -run TestAskOpsHandoffs -count=1
npm run build:admin
python3 scripts/ess-validate.py --phase review --ecr ECR-033
python3 scripts/ess-gate-check.py --ecr ECR-033
```
## Results
| Check | Result |
|-------|--------|
| TestAskOpsHandoffs | PASS |
| build:admin | PASS |
| ess-validate review | PASS |
| ess-gate-check | PASS |
## AC
| ID | Evidence |
|----|----------|
| AC-F-01 | list seed/empty ok |
| AC-F-02 | get 200 |
| AC-F-03 | 未知 id → 404 |
| AC-S-01 | 401 |
| AC-S-02 | 403 |
| AC-P-01 | list &lt; 500ms |
| AC-O-01 | N/A 只读 |
+1
View File
@@ -37,3 +37,4 @@
| ECR-030 | ContentSafety · ModerationCase | **Closed** | Spec ops-moderation-case.md · BD-2026-030 · migration 000031 · TEST_REPORT · CODE_REVIEW · Loop continuous |
| ECR-031 | CrisisCare · CrisisEvent | **Closed** | Spec ops-crisis-event.md · BD-2026-031 · migration 000032 · TEST_REPORT · CODE_REVIEW · Loop continuous |
| ECR-032 | CrisisCare · InterventionOutcome | **Closed** | Spec ops-intervention-outcome.md · BD-2026-032 · migration 000033 · TEST_REPORT · CODE_REVIEW · Loop continuous |
| ECR-033 | AskOperations · HandoffCase | **Closed** | Spec ops-handoff-case.md · BD-2026-033 · migration 000034 · TEST_REPORT · CODE_REVIEW · Loop continuous |
+1 -1
View File
@@ -55,4 +55,4 @@ Human 明文:**直接用 Loop,不用人工确认。**
| Done | Next |
|------|------|
| ECR-013A…032 Closed | **ECR-033** HandoffCase |
| ECR-013A…033 Closed | **ECR-034** PrivacyRequest |
+28
View File
@@ -835,6 +835,34 @@ paths:
'404':
description: Not found
/api/v1/admin/ask/handoffs:
get:
tags: [admin]
summary: List HandoffCase catalog
description: Requires admin.ask.read
responses:
'200':
description: OK
'401':
description: Unauthorized
'403':
description: Forbidden
/api/v1/admin/ask/handoffs/{id}:
get:
tags: [admin]
summary: Get HandoffCase
parameters:
- in: path
name: id
required: true
schema: { type: string, format: uuid }
responses:
'200':
description: OK
'404':
description: Not found
/api/v1/admin/crisis/policies:
get:
tags: [admin]