跳至主要内容

Scenario 3: 重複電話檢測(新增時警告)

User Story: US-301: 客戶基本資料管理

Given: 系統初始狀態

已登入用戶

{
"userId": "staff-001",
"email": "staff@florist.com",
"role": "SALES_STAFF",
"tenantId": "ROLE_FLORIST-abc",
"tenantName": "ABC 花店",
"name": "王店員"
}

現有客戶資料

{
"id": "cust-001",
"tenantId": "ROLE_FLORIST-abc",
"customerNumber": "ABC-CUST-000001",
"type": "individual",
"name": "李大華",
"phone": "0912345678",
"status": "active",
"createdAt": "2025-10-15T09:00:00Z"
}

When: 執行操作

Step 1: 檢查電話是否重複

API 請求

GET /api/v1/customers/check-duplicate?phone=0912-345-678
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

響應

{
"isDuplicate": true,
"existingCustomer": {
"id": "cust-001",
"customerNumber": "ABC-CUST-000001",
"name": "李大華",
"phone": "0912345678"
}
}

Step 2: 用戶選擇「繼續創建」

API 請求

POST /api/v1/customers
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Content-Type: application/json

{
"type": "individual",
"name": "李小華",
"phone": "0912-345-678",
"ignoreDuplicateWarning": true
}

Then: 預期結果

1. HTTP 響應狀態碼

201 Created

2. 響應 Payload

{
"id": "cust-003",
"customerNumber": "ABC-CUST-000003",
"type": "individual",
"name": "李小華",
"phone": "0912345678",
"email": null,
"status": "active",
"tier": "regular",
"totalSpent": 0,
"totalOrders": 0,
"lastOrderDate": null,
"createdAt": "2025-11-04T10:10:00Z",
"updatedAt": "2025-11-04T10:10:00Z"
}

3. 資料庫狀態變更

customers 表新增記錄

{
"id": "cust-003",
"tenantId": "ROLE_FLORIST-abc",
"customerNumber": "ABC-CUST-000003",
"type": "individual",
"name": "李小華",
"phone": "0912345678",
"status": "active",
"createdAt": "2025-11-04T10:10:00Z",
"createdBy": "staff-001"
}

系統中現在有 2 筆客戶使用相同電話

[
{
"customerNumber": "ABC-CUST-000001",
"name": "李大華",
"phone": "0912345678"
},
{
"customerNumber": "ABC-CUST-000003",
"name": "李小華",
"phone": "0912345678"
}
]

4. 業務規則驗證

✅ 重複檢測邏輯

  • 檢查電話: 0912-345-678
  • 標準化後: 0912345678
  • 查詢結果: 找到現有客戶「李大華」

✅ 允許強制創建

  • 前端顯示警告: 「此電話已被客戶『李大華』使用」
  • 用戶選擇: 「繼續創建」
  • 系統行為: 允許創建(可能是家人共用電話)

✅ 電話格式統一

  • 輸入格式: 0912-345-678
  • 儲存格式: 0912345678
  • 查詢時不區分格式

5. 審計日誌

{
"action": "CREATE_CUSTOMER",
"tenantId": "ROLE_FLORIST-abc",
"userId": "staff-001",
"resourceType": "customer",
"resourceId": "cust-003",
"details": {
"customerNumber": "ABC-CUST-000003",
"name": "李小華",
"phone": "0912345678",
"duplicateWarningIgnored": true,
"existingCustomerId": "cust-001"
},
"timestamp": "2025-11-04T10:10:00Z"
}

驗證重點

  • 重複電話檢測 API 正確運作
  • 電話格式自動標準化(移除連字號)
  • 前端正確顯示警告訊息與現有客戶資訊
  • 允許用戶忽略警告並強制創建
  • 審計日誌記錄重複警告被忽略的情況

Alternative Scenario: 用戶選擇「取消創建」

When: 用戶選擇「取消」

前端行為

  • 顯示警告訊息後,用戶點擊「取消」按鈕
  • 不發送創建客戶的 API 請求
  • 停留在「新增客戶」頁面

Then: 系統狀態

✅ 資料庫無變更

  • 僅現有客戶「李大華」存在
  • 未創建新客戶「李小華」

✅ 用戶可檢視現有客戶

  • 警告訊息中顯示「查看現有客戶」連結
  • 點擊連結導航至客戶「李大華」的詳情頁

相關場景