Default Alerts Feature Implementation
Overview​
The Default Alerts feature automatically creates two specific, non-deletable default alerts for every new client when they are first created in the system. These alerts are immediately visible in the client's alert settings, where they can be toggled on/off but never permanently deleted.
Implementation Details​
1. Database Schema​
The is_default column already exists in the app.alerts table:
is_default BOOLEAN DEFAULT false
2. Default Alert Configurations​
Alert 1: High CPLPV​
- Name:
[Default Alert] CPLPV >= 2x account average for 7d - Description: Trigger when Cost per landing page view is doubled the account average for 7 days
- Type:
cplpv - Data Level:
account - Data Source:
app.daily_metrics - Rule: Dynamic threshold - CPLPV > 2x account average over 7 days
- Action: Assign task to Media Buyer
Alert 2: Low eROAS​
- Name:
[Default Alert] eROAS < 1 over 1 day - Description: Identity any ad has an eROAS < 1 over 1 day
- Type:
eroas - Data Level:
ad - Data Source:
dm_dashboard.ad_daily_dashboard - Rule: Absolute threshold - projected_roas < 1 over 1 day
- Action: Assign task to Media Buyer
3. Backend Implementation​
Files Modified:​
-
services/default_alerts.py(New file)create_default_alerts_for_client(): Creates default alerts for new clientsget_default_alerts_for_client(): Retrieves default alerts for a clientis_default_alert(): Checks if an alert is a default alertcalculate_next_check(): Calculates next check time based on frequency
-
services/client.py- Updated
create_client()to call default alerts creation after client creation
- Updated
-
crud/crud_user.py- Updated
create_client()to call default alerts creation after client creation
- Updated
-
services/alerts.py- Updated
delete_alert_service()to return 403 Forbidden for default alerts
- Updated
4. API Changes​
DELETE /api/v1/alerts/{alert_id}​
- Before: Could delete any alert
- After: Returns 403 Forbidden with message "Cannot delete a default alert." for default alerts
5. Frontend Implementation​
Files Already Implemented:​
pages/alerts.tsx- ✅ Default alert badges: Shows
[Default]badge for default alerts - ✅ Disabled delete buttons: Delete button is hidden for default alerts (
!alert.is_default) - ✅ Toggle functionality: Enable/disable switch remains functional
- ✅ Default alert badges: Shows
6. Integration Points​
Client Creation Flow:​
-
Super Admin Client Creation (
/api/v1/clientPOST)- Creates client via
services/client.py - Calls
create_default_alerts_for_client() - Creates client alert settings
- Creates client via
-
User Registration with Client (
/api/v1/auth/signupPOST)- Creates client via
crud/crud_user.py - Calls
create_default_alerts_for_client() - Creates client alert settings
- Creates client via
7. Error Handling​
- Default alerts creation failure does not prevent client creation
- Errors are logged but do not fail the entire process
- Graceful fallback if default alerts service is unavailable
8. Testing​
Test Script: test_default_alerts.py​
The test script verifies:
- ✅ Client creation
- ✅ Default alerts creation
- ✅ Client alert settings creation
- ✅ Default alert identification
- ✅ Deletion prevention
- ✅ Cleanup
Manual Testing Steps:​
-
Create New Client:
curl -X POST /api/v1/client \
-H "Authorization: Bearer <token>" \
-d '{"name": "Test Client"}' -
Verify Default Alerts:
curl -X GET /api/v1/alerts/client/{client_id}/settings \
-H "Authorization: Bearer <token>" -
Test Deletion Prevention:
curl -X DELETE /api/v1/alerts/{default_alert_id}?client_id={client_id} \
-H "Authorization: Bearer <token>"
# Should return 403 Forbidden
9. Configuration​
Default Alert Settings:​
- Frequency: Daily at 5:00 AM (America/Los_Angeles timezone)
- Priority: Medium
- Assignee: Media Buyer role
- Task Creation: Enabled
- Initial State: Enabled
Customization:​
Default alerts can be customized by:
- Toggling on/off in client alert settings
- Modifying thresholds through custom_config
- Changing assignees through action_config
10. Monitoring​
Logging:​
- Default alerts creation:
INFOlevel - Creation failures:
ERRORlevel - Deletion attempts:
DEBUGlevel
Metrics to Monitor:​
- Default alerts creation success rate
- Default alerts deletion attempts
- Client creation with/without default alerts
Security Considerations​
- Permission Checks: Only super admins can create clients
- Default Alert Protection: Cannot be deleted via API
- Client Isolation: Default alerts are client-specific
- Audit Trail: All actions are logged
Future Enhancements​
- Custom Default Alerts: Allow per-client default alert templates
- Bulk Operations: Enable/disable all default alerts at once
- Analytics: Track default alert effectiveness
- Migration: Tool to add default alerts to existing clients
Troubleshooting​
Common Issues:​
-
Default alerts not created:
- Check database connection
- Verify client creation succeeded
- Check logs for errors
-
Cannot delete alert:
- Verify alert is marked as
is_default = true - Check API response for 403 status
- Verify alert is marked as
-
Frontend not showing badges:
- Verify
is_defaultfield in API response - Check frontend component rendering logic
- Verify
Debug Commands:​
-- Check default alerts for a client
SELECT * FROM app.alerts WHERE client_id = 'client-uuid' AND is_default = true;
-- Check client alert settings
SELECT * FROM app.client_alert_settings WHERE client_id = 'client-uuid';
-- Verify alert protection
SELECT id, name, is_default FROM app.alerts WHERE is_default = true;