Introduction
The Documents API provides comprehensive functionality for managing documents within the Happy Habitat platform. This includes uploading files, creating document metadata, downloading documents, and organizing documents by community.Key Features
Document Management
- Create, read, update, and delete document records
- Store document metadata including title, description, and dates
- Associate documents with specific communities
- Track document uploaders for access control
File Upload
- Upload files up to 20 MB
- Organized file storage by community, category, and resident
- Automatic path sanitization and validation
- Support for multiple file types
Access Control
- Role-based authorization (ADMIN_COMPANY, SYSTEM_ADMIN)
- Document download permissions based on uploader or community admin role
- Secure file path handling to prevent path traversal attacks
Document Organization
- Filter documents by community
- Categorize documents (e.g., general, contracts, reports)
- Associate documents with specific residents
- Track creation dates and metadata
Document Structure
Documents in the system contain the following key properties:| Property | Type | Description |
|---|---|---|
Id | Guid | Unique document identifier |
CommunityId | Guid? | Associated community (optional) |
Titulo | string | Document title |
Descripcion | string | Document description |
Fecha | DateTime | Document date |
UserCreated | string | Name or ID of the user who uploaded it |
NombreDocumento | string | Original file name |
UrlDoc | string | URL or relative path to the document file |
UserId | Guid? | User who uploaded the document (for authorization) |
File Storage Organization
Uploaded files are organized in the following directory structure:- communityId: The community’s unique identifier
- category: Document category (defaults to “general” if not provided)
- residentId: Resident’s unique identifier (defaults to “general” if not provided)
- fileName: Sanitized file name
Authorization
Most document endpoints require authentication with one of the following roles:- ADMIN_COMPANY: Company administrator with access to their community’s documents
- SYSTEM_ADMIN: System administrator with full access
- Company admins can download documents from their community
- Users can download documents they uploaded
- Other users are forbidden from downloading documents
Common Use Cases
Upload and Create Document
- Upload a file using
POST /api/Documents/upload - Receive the
RelativePathin the response - Create a document record using
POST /api/Documentswith the relative path asUrlDoc
Download Document
- Retrieve document metadata using
GET /api/Documents/{id} - Download the file using
GET /api/Documents/{id}/download - The system verifies permissions before serving the file
Community Documents
- Query documents for a specific community using
GET /api/Documents/community/{communityId} - Returns all documents associated with that community
Error Handling
The API returns standard HTTP status codes:- 200 OK: Successful request
- 201 Created: Document successfully created
- 204 No Content: Document successfully deleted
- 400 Bad Request: Invalid request data or missing required fields
- 401 Unauthorized: Authentication required
- 403 Forbidden: User lacks permission to access the resource
- 404 Not Found: Document not found
- 500 Internal Server Error: Server error during file operations
File Size Limits
- Maximum upload size: 20 MB
- Applies to both
/api/Documents/uploadand/api/File/uploadendpoints - Configured via
RequestSizeLimitandRequestFormLimitsattributes