Business Flows
Detailed business logic and workflow processes for MS Word Integration
Business Flows
This document outlines the detailed business logic and workflow processes for the MS Word Integration feature, covering session management, file processing, and error handling.
Primary User Workflow
1. Session Initiation
Trigger: User clicks "Edit in Microsoft Word" button in Operations Waiting List
Business Logic:
- Validate user permissions for the lab report
- Check if an active session already exists
- If existing session found:
- If user is the author: Allow continuation
- If different author: Offer "Take Authorship" option
- If session expired: Create new session
- Retrieve selected template configuration
- Generate unique session token
- Create
ExternalReportEditsrecord withis_active = 1 - Fetch patient and test data for placeholder replacement
- Process template with placeholder substitution
- Generate presigned S3 URL for template download
- Return session data to frontend
Edge Cases:
- Template not found: Return error with available templates
- Concurrent session conflict: Handle authorship transfer
- Network timeout: Retry with exponential backoff
2. External Editing Session
Trigger: Microsoft Word opens with the processed template
Business Logic:
- Word application launches via
winword:protocol - User edits document with full Word functionality
- On first save, Word uploads files to the configured endpoint
- Backend validates uploaded files
- Store files in S3 with proper naming convention
- Update
ExternalReportEditsrecord with file paths - Mark lab report as partially completed (
isPartialFill = 1) - Send real-time notifications to waiting list
Session Management:
- Timeout: 2 hours of inactivity
- Auto-save: Files uploaded on each save operation
- Conflict Resolution: Last save wins, with timestamp tracking
3. Session Completion
Trigger: User explicitly ends session or timeout occurs
Business Logic:
- Set
is_active = 0onExternalReportEditsrecord - Update
updated_attimestamp - Clean up temporary files if any
- Log session completion for audit trail
- Notify other users that session is available
Template Processing Flow
Placeholder Replacement Logic
Data Sources:
- Patient Data: From
userDetailstable - Test Data: From
allTestsandreport_resultstables - Lab Data: From
labstable - Dynamic Data: Current timestamp, calculated fields
Replacement Rules:
- Use regex pattern:
{{\s*(\w+)\s*}} - Case-sensitive matching
- Empty string for missing data
- No HTML escaping (Word document context)
Template Validation
Pre-processing Checks:
- File exists in S3
- File is valid DOCX format
- File size within limits
- Contains at least one placeholder (optional)
Post-processing Validation:
- Output file is valid DOCX
- All placeholders processed
- File size reasonable
- No corruption during processing
File Upload and Storage Flow
Upload Processing
File Naming Convention:
{patient_name}_{timestamp}_{file_type}.{extension}S3 Path Structure:
ExternalReportEdit/{patient_id}/{filename}PDF Conversion
Trigger: Word document uploaded successfully
Process:
- Download Word document from S3
- Use Word automation or third-party service to convert to PDF
- Upload PDF to S3
- Update database with PDF path
- Clean up temporary files
Fallback: If conversion fails, store Word document as primary artifact
Error Handling and Recovery
Session Recovery
Scenario: User loses connection during editing
Recovery Logic:
- Session remains active for 2 hours
- User can reconnect using same session token
- Files are preserved in S3
- Authorship maintained
File Upload Failures
Scenario: Upload fails due to network issues
Recovery Logic:
- Retry upload with exponential backoff
- Store partial uploads for manual recovery
- Notify user of failure with retry option
- Maintain session state for continuation
Data Consistency
Scenario: Database update fails after file upload
Recovery Logic:
- Use database transactions for atomicity
- Implement cleanup jobs for orphaned files
- Audit trail for manual reconciliation
- Alert system administrators for critical failures
Security and Compliance
Authentication Flow
Audit Trail
Tracked Events:
- Session creation (user, timestamp, template)
- File uploads (filename, size, timestamp)
- Session termination (reason, duration)
- Authorship changes (old user, new user, timestamp)
- Error events (type, details, user impact)
Data Privacy
- Patient data encrypted in transit and at rest
- File access restricted to authorized lab users
- Automatic cleanup of temporary files
- Compliance with HIPAA/PHI regulations
Performance Considerations
Optimization Strategies
- Caching: Template metadata cached for 1 hour
- Streaming: Large file processing uses streaming to avoid memory issues
- Async Processing: File uploads processed asynchronously
- CDN: Use CloudFront for faster file downloads
Monitoring Metrics
- Session creation success rate
- Average session duration
- File upload success rate
- Template processing time
- Storage utilization
Integration Points
With Lab Report System
- Status Updates: Mark reports as partially completed
- Result Integration: Pull data from
report_resultstable - Workflow Integration: Seamless operation from waiting list
With Template Management
- Template Selection: Integration with template library
- Version Control: Track template versions used
- Usage Analytics: Report on template popularity
With User Management
- Permission Checks: Validate user access to specific reports
- Role-based Access: Different permissions for different user types
- Audit Logging: Track all user actions for compliance