feat: implement automation module

- Event-based triggers (Phase 1):
  - Trigger/TriggerLog models with field_change type
  - TriggerService for condition evaluation and action execution
  - Trigger CRUD API endpoints
  - Task integration (status, assignee, priority changes)
  - Frontend: TriggerList, TriggerForm components

- Weekly reports (Phase 2):
  - ScheduledReport/ReportHistory models
  - ReportService for stats generation
  - APScheduler for Friday 16:00 job
  - Report preview/generate/history API
  - Frontend: WeeklyReportPreview, ReportHistory components

- Tests: 23 new tests (14 triggers + 9 reports)
- OpenSpec: add-automation change archived

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
beabigegg
2025-12-29 22:59:00 +08:00
parent 3108fe1dff
commit 95c281d8e1
32 changed files with 3163 additions and 3 deletions

View File

@@ -0,0 +1,105 @@
## MODIFIED Requirements
### Requirement: Trigger-Based Automation
系統 SHALL 支援觸發器 (Triggers),當特定條件滿足時自動執行動作。
#### Scenario: 狀態變更觸發通知
- **GIVEN** 專案設定了「當任務狀態變更為待測試時,通知指派者」的觸發器
- **WHEN** 任務狀態變更為「待測試」
- **THEN** 系統自動發送通知給任務指派者
- **AND** 觸發器執行記錄至 TriggerLog
#### Scenario: 建立觸發器
- **GIVEN** 專案管理者需要建立自動化規則
- **WHEN** 管理者透過 API 設定觸發條件與動作
- **THEN** 系統儲存觸發器規則
- **AND** 規則立即生效
### Requirement: Trigger Conditions
系統 SHALL 支援欄位變更觸發條件。
#### Scenario: 欄位變更條件
- **GIVEN** 觸發器設定為「當 status_id 欄位變更為特定值」
- **WHEN** 任務的 status_id 欄位變更為該值
- **THEN** 觸發器被觸發
- **AND** 支援運算子: equals, not_equals, changed_to, changed_from
### Requirement: Trigger Actions
系統 SHALL 支援發送通知動作。
#### Scenario: 發送通知動作
- **GIVEN** 觸發器動作設定為 notify
- **WHEN** 觸發器被觸發
- **THEN** 系統使用 NotificationService 發送通知
- **AND** 通知目標支援: assignee, creator, project_owner, user:<id>
- **AND** 通知內容可使用變數模板
### Requirement: Automated Weekly Report
系統 SHALL 每週五下午 4:00 自動彙整本週任務狀態發送給主管。
#### Scenario: 週報自動生成
- **GIVEN** APScheduler 排程設定為每週五 16:00
- **WHEN** 到達排程時間
- **THEN** ReportService 彙整使用者所屬專案的任務狀態
- **AND** 生成週報並透過 NotificationService 發送
#### Scenario: 週報內容
- **GIVEN** 週報生成中
- **WHEN** 系統彙整資料
- **THEN** 週報 JSON 包含:
- completed_count: 本週已完成任務數
- in_progress_count: 進行中任務數
- overdue_count: 逾期任務數
- tasks: 詳細任務清單
## MODIFIED Data Model
```
pjctrl_triggers
├── id: UUID (PK)
├── project_id: UUID (FK -> projects)
├── name: VARCHAR(200)
├── description: TEXT
├── trigger_type: ENUM('field_change', 'schedule')
├── conditions: JSON
│ └── { "field": "status_id", "operator": "changed_to", "value": "uuid" }
├── actions: JSON
│ └── [{ "type": "notify", "target": "assignee", "template": "..." }]
├── is_active: BOOLEAN DEFAULT true
├── created_by: UUID (FK -> users)
├── created_at: TIMESTAMP
└── updated_at: TIMESTAMP
pjctrl_trigger_logs
├── id: UUID (PK)
├── trigger_id: UUID (FK -> triggers)
├── task_id: UUID (FK -> tasks, nullable)
├── executed_at: TIMESTAMP
├── status: ENUM('success', 'failed')
├── details: JSON
└── error_message: TEXT
pjctrl_scheduled_reports
├── id: UUID (PK)
├── report_type: ENUM('weekly')
├── recipient_id: UUID (FK -> users)
├── is_active: BOOLEAN DEFAULT true
├── last_sent_at: TIMESTAMP
└── created_at: TIMESTAMP
pjctrl_report_history
├── id: UUID (PK)
├── report_id: UUID (FK -> scheduled_reports)
├── generated_at: TIMESTAMP
├── content: JSON
├── status: ENUM('sent', 'failed')
└── error_message: TEXT
```
## MODIFIED Technical Notes
- 使用 APScheduler (AsyncIOScheduler) 處理排程任務,嵌入 FastAPI 應用內運行
- 觸發器評估採用同步處理,任務更新時直接執行,避免複雜的異步處理
- 所有觸發器執行都記錄至 TriggerLog 供追蹤
- Phase 1 僅支援 notify 動作
- 條件運算子: equals, not_equals, changed_to, changed_from