2025-03-20 12:32:06 +08:00
---
2025-03-20 13:33:54 +08:00
title: Add Dashboard to Collections REST Endpoint
2025-03-20 12:32:06 +08:00
author: Cascade
date: 2025-03-19
2025-03-20 13:50:55 +08:00
status: Complete
2025-03-20 12:32:06 +08:00
---
2025-03-20 13:33:54 +08:00
# Add Dashboard to Collections REST Endpoint
2025-03-20 12:32:06 +08:00
## Problem Statement
2025-03-20 13:33:54 +08:00
Users need the ability to programmatically add a dashboard to multiple collections via a REST API. Currently, this functionality is not available, limiting the ability to manage collections through the API.
2025-03-20 12:32:06 +08:00
## Goals
2025-03-20 13:33:54 +08:00
1. ✅ Create a REST endpoint to add a dashboard to multiple collections
2025-03-20 13:26:31 +08:00
2. ✅ Implement proper permission validation
3. ✅ Ensure data integrity with proper error handling
4. ✅ Follow established patterns for REST endpoints and handlers
2025-03-20 12:32:06 +08:00
## Non-Goals
1. Modifying the existing collections functionality
2. Creating UI components for this endpoint
## Technical Design
### REST Endpoint
2025-03-20 13:33:54 +08:00
**Endpoint:** `POST /dashboards/:id/collections`
2025-03-20 12:32:06 +08:00
**Request Body:**
```json
{
2025-03-20 13:33:54 +08:00
"collection_ids": ["uuid1", "uuid2", "uuid3"]
2025-03-20 12:32:06 +08:00
}
```
**Response:**
- `200 OK` - Success
```json
{
2025-03-20 13:33:54 +08:00
"message": "Dashboard added to collections successfully"
2025-03-20 12:32:06 +08:00
}
```
- `400 Bad Request` - Invalid input
- `403 Forbidden` - Insufficient permissions
2025-03-20 13:33:54 +08:00
- `404 Not Found` - Dashboard not found
2025-03-20 12:32:06 +08:00
- `500 Internal Server Error` - Server error
### Handler Implementation
The handler will:
2025-03-20 13:33:54 +08:00
1. ✅ Validate that the dashboard exists
2025-03-20 13:26:31 +08:00
2. ✅ Check if the user has appropriate permissions (Owner, FullAccess, or CanEdit)
2025-03-20 13:33:54 +08:00
3. ✅ Validate that the collections exist and the user has access to them
4. ✅ Add the dashboard to the collections by creating records in the `collections_to_assets` table
5. ✅ Handle the case where a dashboard was previously in a collection but was deleted (upsert)
2025-03-20 12:32:06 +08:00
### File Changes
#### New Files
2025-03-20 13:33:54 +08:00
1. ✅ `libs/handlers/src/dashboards/add_dashboard_to_collections_handler.rs`
2. ✅ `src/routes/rest/routes/dashboards/add_dashboard_to_collections.rs`
3. ✅ Update `libs/handlers/src/dashboards/mod.rs` to include the new handler
4. ✅ Update `src/routes/rest/routes/dashboards/mod.rs` to include the new endpoint
2025-03-20 12:32:06 +08:00
### Database Operations
2025-03-20 13:26:31 +08:00
The implementation uses the `collections_to_assets` table with the following operations:
1. ✅ SELECT to check if records exist
2. ✅ INSERT for new records
3. ✅ UPDATE for records that were previously deleted
2025-03-20 12:32:06 +08:00
## Testing Strategy
### Unit Tests
2025-03-20 13:26:31 +08:00
1. ✅ Test the handler with mocked database connections
2025-03-20 13:33:54 +08:00
- ✅ Test adding a dashboard to collections
- ✅ Test error cases (dashboard not found, collection not found, insufficient permissions)
- ✅ Test adding a dashboard that was previously in a collection but deleted
2025-03-20 12:32:06 +08:00
2025-03-20 13:26:31 +08:00
2. ✅ Test the REST endpoint
- ✅ Test successful request
- ✅ Test error responses for various scenarios
2025-03-20 12:32:06 +08:00
### Integration Tests
2025-03-20 13:26:31 +08:00
1. ✅ Test skeleton created for the endpoint with a test database
2025-03-20 12:32:06 +08:00
## Security Considerations
2025-03-20 13:26:31 +08:00
- ✅ The endpoint requires authentication
- ✅ Permission checks ensure users can only modify collections they have access to
- ✅ Input validation prevents malicious data
2025-03-20 12:32:06 +08:00
## Monitoring and Logging
2025-03-20 13:26:31 +08:00
- ✅ All operations are logged with appropriate context
- ✅ Errors are logged with detailed information
2025-03-20 12:32:06 +08:00
## Dependencies
- `libs/sharing` - For permission checking
- `libs/database` - For database operations
## Rollout Plan
2025-03-20 13:26:31 +08:00
1. ✅ Implement the handler and endpoint
2. ✅ Write tests
3. ✅ Code review
2025-03-20 12:32:06 +08:00
4. Deploy to staging
5. Test in staging
6. Deploy to production
7. Monitor for issues