📕 Files
📖 Overview
The Files service manages document storage and retrieval for project-related files, including deliverables, resources, and client-specific documents. It provides secure file management with access control based on account relationships and user scopes.
Source Files:
- Service:
internal/api/v1/projects/services/files.service.js - Controller:
internal/api/v1/projects/controllers/files.controller.js
Key Capabilities:
- Store file metadata after S3 upload
- List files with pagination and search
- Update file metadata
- Delete single or multiple files
- Scope-based access control
- Client-specific file organization
🗄️ Collections Used
projects-files
- Operations: Create, Read, Update, Delete
- Model:
shared/models/projects-file.js - Usage Context: Primary storage for file metadata
Key Fields:
{
_id: ObjectId,
accountId: ObjectId, // Agency account
clientId: ObjectId, // Client account
userId: ObjectId, // Uploader
key: String, // S3 key
fileName: String,
bucketName: String,
fileType: String,
fileSize: Number,
created_by: ObjectId,
createdAt: Date,
updatedAt: Date
}
_users
- Operations: Read (enrichment)
- Usage Context: Provides uploader information
🔧 Business Logic & Functions
Service Layer Functions
storeFiles(options)
Purpose: Stores file metadata after S3 upload completion.
Parameters:
accountId(ObjectId) - Agency account IDuserId(ObjectId) - User who uploadedreqBody(Object) - File details from S3 uploadclientId,key,fileName,bucketName,fileType,fileSize
Returns: Created file document
Business Logic:
- Creates file record with metadata
- Associates with both agency and client accounts
- Tracks uploader for audit trail
getAllFiles(options)
Purpose: Retrieves paginated file list with search and filtering.
Parameters:
clientId(ObjectId, optional) - Filter by clientsearch(String, optional) - Filename search termskip(Number) - Pagination offsetlimit(Number) - Max files to returnaccountId(ObjectId) - Agency account IDdashclicks(Object) - User permissionsdashboardPreferences(Object) - Client dashboard settings
Returns: { data: [], count: Number }
Business Logic:
-
Access Scoping:
- Agency users: Scoped to
accountId - Client dashboard: Scoped to
dashboardPreferences.parent_account - Admins with PROJECT_ROLES: No account filter (global access)
- Agency users: Scoped to
-
Search Implementation: Case-insensitive regex on
fileName -
User Enrichment: Joins uploader information
-
Sorting: Newest files first (
createdAt: -1)
updateFile(options)
Purpose: Updates file metadata.
Parameters:
data(Object) - Fields to updatefileId(ObjectId) - Target file IDaccountId(ObjectId) - Account validation
Returns: Updated file document
Business Logic:
- Validates file belongs to account
- Uses
findOneAndUpdatefor atomic update - Returns updated document
deleteFile(options)
Purpose: Deletes a single file (soft or hard delete based on implementation).
Parameters:
fileId(ObjectId) - Target file IDclientId(ObjectId, optional) - Client filterscope(Array) - User scopesaccountId(ObjectId) - Account validation
Returns: Delete result
Business Logic:
- Authorization Check: Requires
filesorfiles.deletescope - Dual Query: Matches by
accountIdOR by_idalone (admin bypass) - Physical deletion from database
deleteMultipleFiles(options)
Purpose: Batch deletes multiple files.
Parameters:
ids(Array) - Array of file IDsclientId,scope,accountId- Same asdeleteFile
Returns: { message: "X files successfully deleted" }
Business Logic:
- Same authorization as single delete
- Uses
$inoperator for batch operation - Returns count of deleted files
Controller Layer
All controllers enforce main account restriction:
if (!req.auth.account.main) {
throw badRequest('You are not allowed to perform this action');
}
Only main agency accounts (not sub-accounts) can manage files.
🔀 Integration Points
Internal Dependencies
PROJECT_ROLES- Admin role validationcatchAsync()- Error handlinggeneratePagination()- Pagination helper
External Services
- AWS S3 - Actual file storage (external to this module)
⚠️ Important Notes
- 🔒 Main Account Only: Only main agency accounts can create/update/delete files
- 📁 S3 Integration: This module only handles metadata; actual uploads happen via separate S3 service
- 🗑️ Hard Deletes: Files are physically removed from database (no soft delete)
- 🔍 Search: Simple regex search (consider full-text indexing for scale)
- 📊 Scope Required: Delete operations require explicit
filesorfiles.deletescope
🔗 Related Documentation
- Projects Module Overview - Parent module
- Activity - File uploads may be logged
Last Updated: 2025-10-08
Service Files:services/files.service.js,controllers/files.controller.js
Primary Functions: 5 service functions, 5 controller endpoints