|
| 1 | +# Document Management System Configuration |
| 2 | + |
| 3 | +## Environment Variables |
| 4 | + |
| 5 | +Create a `.env` file in the backend root directory with the following configurations: |
| 6 | + |
| 7 | +```env |
| 8 | +# Database Configuration |
| 9 | +DB_HOST=localhost |
| 10 | +DB_PORT=5432 |
| 11 | +DB_USERNAME=postgres |
| 12 | +DB_PASSWORD=password |
| 13 | +DB_DATABASE=manage_assets |
| 14 | +DB_SYNCHRONIZE=true |
| 15 | +NODE_ENV=development |
| 16 | +
|
| 17 | +# Document Storage |
| 18 | +UPLOAD_DIR=./uploads/documents |
| 19 | +MAX_FILE_SIZE=524288000 |
| 20 | +
|
| 21 | +# JWT Configuration |
| 22 | +JWT_SECRET=your_jwt_secret_key |
| 23 | +JWT_EXPIRATION=24h |
| 24 | +
|
| 25 | +# Server Configuration |
| 26 | +PORT=3000 |
| 27 | +API_PREFIX=api |
| 28 | +
|
| 29 | +# CORS Configuration |
| 30 | +CORS_ORIGIN=http://localhost:3000,http://localhost:3001 |
| 31 | +
|
| 32 | +# File Upload Configuration |
| 33 | +ALLOWED_MIME_TYPES=application/pdf,image/jpeg,image/png,image/gif,text/plain,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
| 34 | +
|
| 35 | +# Audit Configuration |
| 36 | +ENABLE_AUDIT_LOGGING=true |
| 37 | +AUDIT_LOG_RETENTION_DAYS=365 |
| 38 | +
|
| 39 | +# Storage Configuration |
| 40 | +ENABLE_FILE_COMPRESSION=false |
| 41 | +ENABLE_FILE_ENCRYPTION=false |
| 42 | +ENCRYPTION_KEY=your_encryption_key |
| 43 | +
|
| 44 | +# Feature Flags |
| 45 | +ENABLE_DOCUMENT_PREVIEW=false |
| 46 | +ENABLE_OCR=false |
| 47 | +ENABLE_CLOUD_STORAGE=false |
| 48 | +``` |
| 49 | + |
| 50 | +## Database Setup |
| 51 | + |
| 52 | +### 1. Create Tables |
| 53 | + |
| 54 | +The tables are automatically created by TypeORM synchronization when `DB_SYNCHRONIZE=true`. |
| 55 | + |
| 56 | +Tables created: |
| 57 | +- `documents` |
| 58 | +- `document_versions` |
| 59 | +- `document_access_permissions` |
| 60 | +- `document_audit_logs` |
| 61 | + |
| 62 | +### 2. Create Indexes |
| 63 | + |
| 64 | +Indexes are automatically created during table synchronization. |
| 65 | + |
| 66 | +### 3. Initial Data |
| 67 | + |
| 68 | +No initial data is required. The system is ready to use once tables are created. |
| 69 | + |
| 70 | +## Application Setup |
| 71 | + |
| 72 | +### 1. Installation |
| 73 | + |
| 74 | +```bash |
| 75 | +cd backend |
| 76 | +npm install |
| 77 | +``` |
| 78 | + |
| 79 | +### 2. Database Migration |
| 80 | + |
| 81 | +```bash |
| 82 | +# For development with synchronize: true |
| 83 | +npm run start:dev |
| 84 | + |
| 85 | +# For production with migrations |
| 86 | +npm run migration:run |
| 87 | +``` |
| 88 | + |
| 89 | +### 3. Start Server |
| 90 | + |
| 91 | +```bash |
| 92 | +# Development with watch mode |
| 93 | +npm run start:dev |
| 94 | + |
| 95 | +# Production build and run |
| 96 | +npm run build |
| 97 | +npm run start:prod |
| 98 | +``` |
| 99 | + |
| 100 | +## Module Integration |
| 101 | + |
| 102 | +The DocumentsModule is already integrated into the AppModule. No additional setup is needed. |
| 103 | + |
| 104 | +To verify integration: |
| 105 | + |
| 106 | +1. Check [app.module.ts](app.module.ts) imports the DocumentsModule |
| 107 | +2. Verify all entities are included in TypeOrmModule.forRoot() |
| 108 | +3. Confirm DocumentsModule is exported from documents module |
| 109 | + |
| 110 | +## API Documentation |
| 111 | + |
| 112 | +Once the server is running, access the Swagger documentation at: |
| 113 | + |
| 114 | +``` |
| 115 | +http://localhost:3000/api/docs |
| 116 | +``` |
| 117 | + |
| 118 | +## Testing |
| 119 | + |
| 120 | +### Manual Testing with cURL |
| 121 | + |
| 122 | +```bash |
| 123 | +# 1. Upload a document |
| 124 | +curl -X POST http://localhost:3000/documents/upload \ |
| 125 | + -H "Authorization: Bearer YOUR_JWT_TOKEN" \ |
| 126 | + -F "file=@test-file.pdf" \ |
| 127 | + -F "assetId=550e8400-e29b-41d4-a716-446655440000" \ |
| 128 | + -F "documentType=invoice" \ |
| 129 | + -F "name=Test Invoice" |
| 130 | + |
| 131 | +# 2. List documents |
| 132 | +curl -X GET "http://localhost:3000/documents?limit=10" \ |
| 133 | + -H "Authorization: Bearer YOUR_JWT_TOKEN" |
| 134 | + |
| 135 | +# 3. Get document details |
| 136 | +curl -X GET http://localhost:3000/documents/DOC_ID \ |
| 137 | + -H "Authorization: Bearer YOUR_JWT_TOKEN" |
| 138 | + |
| 139 | +# 4. Download document |
| 140 | +curl -X GET http://localhost:3000/documents/DOC_ID/download \ |
| 141 | + -H "Authorization: Bearer YOUR_JWT_TOKEN" \ |
| 142 | + -o downloaded-file.pdf |
| 143 | + |
| 144 | +# 5. Grant access |
| 145 | +curl -X POST http://localhost:3000/documents/DOC_ID/permissions/grant \ |
| 146 | + -H "Authorization: Bearer YOUR_JWT_TOKEN" \ |
| 147 | + -H "Content-Type: application/json" \ |
| 148 | + -d '{ |
| 149 | + "userId": "user-uuid", |
| 150 | + "permissions": ["view", "download"], |
| 151 | + "expiresAt": "2025-12-31" |
| 152 | + }' |
| 153 | +``` |
| 154 | + |
| 155 | +## Troubleshooting |
| 156 | + |
| 157 | +### Issue: Upload Directory Not Found |
| 158 | + |
| 159 | +**Solution:** Ensure the `UPLOAD_DIR` exists or is accessible: |
| 160 | + |
| 161 | +```bash |
| 162 | +mkdir -p ./uploads/documents |
| 163 | +chmod 755 ./uploads/documents |
| 164 | +``` |
| 165 | + |
| 166 | +### Issue: File Size Limit Exceeded |
| 167 | + |
| 168 | +**Solution:** Increase `MAX_FILE_SIZE` in environment variables: |
| 169 | + |
| 170 | +```env |
| 171 | +MAX_FILE_SIZE=1073741824 # 1GB |
| 172 | +``` |
| 173 | + |
| 174 | +### Issue: Permission Denied on File Operations |
| 175 | + |
| 176 | +**Solution:** Ensure proper file permissions: |
| 177 | + |
| 178 | +```bash |
| 179 | +chmod -R 755 ./uploads |
| 180 | +``` |
| 181 | + |
| 182 | +### Issue: Database Connection Failed |
| 183 | + |
| 184 | +**Solution:** Verify database configuration: |
| 185 | + |
| 186 | +```bash |
| 187 | +# Test connection |
| 188 | +psql -h localhost -U postgres -d manage_assets |
| 189 | +``` |
| 190 | + |
| 191 | +### Issue: JWT Token Errors |
| 192 | + |
| 193 | +**Solution:** Ensure JWT_SECRET is configured and token is valid: |
| 194 | + |
| 195 | +```env |
| 196 | +JWT_SECRET=your_secure_secret_key |
| 197 | +JWT_EXPIRATION=24h |
| 198 | +``` |
| 199 | + |
| 200 | +## Performance Optimization |
| 201 | + |
| 202 | +### 1. File Storage |
| 203 | + |
| 204 | +- Use SSD for upload directory |
| 205 | +- Implement file cleanup policies |
| 206 | +- Consider cloud storage integration (S3, Azure Blob) |
| 207 | + |
| 208 | +### 2. Database |
| 209 | + |
| 210 | +- Add indexes for frequently searched columns |
| 211 | +- Implement partitioning for audit logs |
| 212 | +- Regular maintenance and vacuuming |
| 213 | + |
| 214 | +### 3. API |
| 215 | + |
| 216 | +- Implement caching for frequently accessed documents |
| 217 | +- Use pagination for list operations |
| 218 | +- Compress responses |
| 219 | + |
| 220 | +### 4. Monitoring |
| 221 | + |
| 222 | +- Monitor disk usage |
| 223 | +- Track API response times |
| 224 | +- Log error rates |
| 225 | + |
| 226 | +## Security Hardening |
| 227 | + |
| 228 | +### 1. File Upload |
| 229 | + |
| 230 | +```typescript |
| 231 | +// Implement in upload validation |
| 232 | +- Validate file type via magic bytes, not just extension |
| 233 | +- Scan uploaded files for malware |
| 234 | +- Implement rate limiting |
| 235 | +``` |
| 236 | + |
| 237 | +### 2. Access Control |
| 238 | + |
| 239 | +```typescript |
| 240 | +// Already implemented features: |
| 241 | +- Permission-based access control |
| 242 | +- User identity verification |
| 243 | +- Audit logging of all access |
| 244 | +- Permission expiration |
| 245 | +``` |
| 246 | + |
| 247 | +### 3. Data Protection |
| 248 | + |
| 249 | +```typescript |
| 250 | +// Recommended additions: |
| 251 | +- Encrypt sensitive files at rest |
| 252 | +- Use HTTPS for all communications |
| 253 | +- Implement TLS/SSL |
| 254 | +- Use secure headers |
| 255 | +``` |
| 256 | + |
| 257 | +## Backup and Recovery |
| 258 | + |
| 259 | +### 1. File Backup |
| 260 | + |
| 261 | +```bash |
| 262 | +# Daily backup script |
| 263 | +#!/bin/bash |
| 264 | +DATE=$(date +%Y%m%d) |
| 265 | +tar -czf /backups/documents_$DATE.tar.gz ./uploads/documents |
| 266 | +``` |
| 267 | + |
| 268 | +### 2. Database Backup |
| 269 | + |
| 270 | +```bash |
| 271 | +# PostgreSQL backup |
| 272 | +pg_dump -h localhost -U postgres manage_assets | gzip > backup_$(date +%Y%m%d).sql.gz |
| 273 | +``` |
| 274 | + |
| 275 | +### 3. Recovery Procedure |
| 276 | + |
| 277 | +```bash |
| 278 | +# Restore files |
| 279 | +tar -xzf /backups/documents_$DATE.tar.gz -C ./ |
| 280 | +
|
| 281 | +# Restore database |
| 282 | +gunzip < backup_$DATE.sql.gz | psql -h localhost -U postgres manage_assets |
| 283 | +``` |
| 284 | + |
| 285 | +## Monitoring and Logging |
| 286 | + |
| 287 | +### 1. Application Logs |
| 288 | + |
| 289 | +Logs are output to console in development and file in production. |
| 290 | + |
| 291 | +### 2. Audit Logs |
| 292 | + |
| 293 | +Query audit logs via API: |
| 294 | + |
| 295 | +```bash |
| 296 | +curl -X GET "http://localhost:3000/documents/DOC_ID/audit-logs" \ |
| 297 | + -H "Authorization: Bearer YOUR_JWT_TOKEN" |
| 298 | +``` |
| 299 | + |
| 300 | +### 3. Error Monitoring |
| 301 | + |
| 302 | +Configure error tracking service (Sentry, DataDog): |
| 303 | + |
| 304 | +```typescript |
| 305 | +import * as Sentry from "@sentry/node"; |
| 306 | +
|
| 307 | +// In main.ts |
| 308 | +Sentry.init({ |
| 309 | + dsn: process.env.SENTRY_DSN, |
| 310 | +}); |
| 311 | +``` |
| 312 | + |
| 313 | +## Migration from Other Systems |
| 314 | + |
| 315 | +### 1. Prepare Data |
| 316 | + |
| 317 | +```sql |
| 318 | +-- Create temporary table |
| 319 | +CREATE TABLE temp_documents ( |
| 320 | + asset_id UUID, |
| 321 | + file_path VARCHAR, |
| 322 | + document_type VARCHAR, |
| 323 | + name VARCHAR, |
| 324 | + description TEXT |
| 325 | +); |
| 326 | +
|
| 327 | +-- Import from CSV |
| 328 | +COPY temp_documents FROM 'documents.csv' WITH (FORMAT csv); |
| 329 | +``` |
| 330 | + |
| 331 | +### 2. Transform and Load |
| 332 | + |
| 333 | +```typescript |
| 334 | +// Use DocumentService to import |
| 335 | +// Handle file migration to new storage system |
| 336 | +``` |
| 337 | + |
| 338 | +### 3. Verification |
| 339 | + |
| 340 | +```bash |
| 341 | +# Verify document count |
| 342 | +curl -X GET "http://localhost:3000/documents?limit=1000" \ |
| 343 | + -H "Authorization: Bearer YOUR_JWT_TOKEN" \ |
| 344 | + | jq '.total' |
| 345 | +``` |
| 346 | + |
| 347 | +## Future Enhancements |
| 348 | + |
| 349 | +- [ ] Cloud storage integration (S3, Azure, GCS) |
| 350 | +- [ ] Full-text search with Elasticsearch |
| 351 | +- [ ] Document preview generation |
| 352 | +- [ ] OCR capability |
| 353 | +- [ ] E-signature integration |
| 354 | +- [ ] Workflow approvals |
| 355 | +- [ ] Advanced analytics |
| 356 | +- [ ] Mobile app sync |
| 357 | +- [ ] Real-time collaboration |
| 358 | +- [ ] Advanced version comparison |
| 359 | + |
| 360 | +## Support and Documentation |
| 361 | + |
| 362 | +- API Documentation: http://localhost:3000/api/docs |
| 363 | +- Module README: [./README.md](./README.md) |
| 364 | +- Source Code: [./src](./src) |
| 365 | +- Entity Definitions: [./entities](./entities) |
| 366 | +- DTOs: [./dto](./dto) |
| 367 | +- Services: [./services](./services) |
| 368 | +- Controllers: [./controllers](./controllers) |
0 commit comments