Files

43 lines
1.9 KiB
Markdown
Raw Permalink Normal View History

# Task: Create AlertService class
## Goal
Create `src/backend/services/alert_service.py` with the `AlertService` class. This is Phase 1 of the alert refactor — only create the service, don't touch the router yet.
## Requirements
### Create `src/backend/services/alert_service.py`
Implement an `AlertService` class with these methods:
1. `get_user_alerts(watchlist_id, status, page, page_size, user_id)` — returns list of alert dicts + total count
2. `create_alert(watchlist_id, type, trigger_type, message, severity, ticker, metadata, user_id)` — inserts alert, returns alert dict
3. `get_alert(alert_id, user_id)` — returns alert dict or None
4. `update_alert(alert_id, updates_dict, user_id)` — updates fields, returns alert dict
5. `resolve_alert(alert_id, user_id)` — sets status='resolved', returns alert dict
6. `dismiss_alert(alert_id, user_id)` — deletes alert, returns None
7. `_require_watchlist_owner(watchlist_id, user_id)` — raises HTTPException if not owner
### Implementation Details
- Import `execute_query`, `execute_command`, `execute_one` from `database`
- Import `HTTPException` from `fastapi`
- All methods should be `async`
- Use parameterized queries (no f-strings for SQL)
- Handle the `watchlist_id IN (...)` pattern for listing alerts across multiple watchlists
### Service Pattern
Follow the existing `SentimentService` or `RotationService` pattern for class structure.
## Acceptance Criteria
1. File `src/backend/services/alert_service.py` exists
2. `AlertService` class has all 7 methods listed above
3. File is under 200 lines (per RULES.md)
4. Can be imported without errors: `from services.alert_service import AlertService`
## Files to Create
- `src/backend/services/alert_service.py`
## Files to Read First
- `src/backend/routers/alerts.py` — extract logic from each endpoint
- `src/backend/services/sentiment_service.py` — follow as pattern reference
- `src/backend/services/rotation_service.py` — follow as pattern reference