PSA/ee/docs/plans/2025-08-05-email-system-provider-integration-refactor.md
Hermes 284313f908
Some checks are pending
Bidi Control Character Guard / bidi-control-guard (push) Waiting to run
Circular Dependency Check / Check for new circular dependencies (push) Waiting to run
Citus Migration Smoke / Combined migrations on single-node Citus (push) Waiting to run
E2E Fresh Install Tests / fresh-install-e2e (push) Waiting to run
ext-v2 guardrails / Run ext-v2 guard and ESLint (push) Waiting to run
Integration Tests / Check for relevant changes (push) Waiting to run
Integration Tests / ${{ (github.event_name == 'schedule' || github.event.inputs.suite == 'full') && 'Full integration suite' || 'Tier-1 integration subset' }} (push) Blocked by required conditions
Mobile checks / Mobile lint + typecheck (push) Waiting to run
Mobile checks / Mobile unit tests (push) Waiting to run
Mobile checks / Mobile dependency audit (report) (push) Waiting to run
Mobile checks / Mobile reproducibility checks (push) Waiting to run
Secrets guard (env backups) / Ensure no tracked env backup files (push) Waiting to run
Temporal Readiness / fast-readiness (push) Waiting to run
Temporal Readiness / docker-parity (push) Waiting to run
TypeScript Type Check / Nx affected typecheck (push) Waiting to run
Unit Tests / Skipped-test budget (push) Waiting to run
Unit Tests / Nx affected unit tests (push) Waiting to run
Unit Tests / Server unit coverage (informational) (push) Waiting to run
Validate Tenant Management Schema / Check for relevant changes (push) Waiting to run
Validate Tenant Management Schema / Validate Tenant Management Schema (push) Blocked by required conditions
EE Workflows Build Guard / ee-workflows-build-guard (push) Waiting to run
Initial import of AlgaPSA codebase from PSA server
Excluded: .git, node_modules, secrets/, compose.env, assemblyscript tgz

Source: /opt/alga-psa on psa.joliet.tech
2026-06-22 16:12:17 -05:00

20 KiB

Email System Provider Integration Refactor Plan

Intro / Rationale

Executive Summary

The current email system has evolved into a fragmented architecture with duplicate implementations and inconsistent provider usage. This refactoring will consolidate the email system to properly utilize the existing email provider infrastructure (ResendEmailProvider, SMTPEmailProvider, etc.) and eliminate duplication while establishing clear separation between system and tenant emails.

Business and Technical Drivers

  • Architectural Inconsistency: BaseEmailService uses nodemailer directly instead of leveraging existing provider infrastructure
  • Code Duplication: Two TenantEmailService implementations exist with overlapping functionality
  • Provider Underutilization: Sophisticated provider system (EmailProviderManager, IEmailProvider) not fully utilized
  • Maintenance Complexity: Multiple email service implementations require parallel maintenance
  • System/Tenant Confusion: No clear distinction between platform emails and business emails

Success Criteria

  • Single SystemEmailService using environment variables and provider system
  • Single TenantEmailService using database settings and provider system
  • BaseEmailService eliminated or refactored to use providers
  • All email services leverage existing provider infrastructure
  • Clear separation between system and tenant email purposes
  • Zero downtime during migration

Key Stakeholders

  • Development team implementing email features
  • Operations team managing email configurations
  • System administrators configuring email providers
  • End users receiving system and business emails

Phased Implementation Checklist

Phase 1: Provider Integration Assessment and Preparation

Goal: Understand current provider system and prepare integration strategy

  • Task 1.1: Analyze existing provider architecture
    • Document IEmailProvider interface capabilities and methods
    • Review EmailProviderManager initialization and usage patterns
    • Catalog available providers (Resend, SMTP, Gmail, Microsoft Graph)
    • Identify provider configuration patterns in database
  • Task 1.2: Create provider configuration for system emails
    • Define SystemEmailProviderConfig interface
    • Design environment variable mapping to provider configs
    • Create provider selection logic for system emails
    • Add validation for system email provider configuration
  • Task 1.3: Document current service usage patterns
    • Identify all files using BaseEmailService
    • Map all references to duplicate TenantEmailService files
    • Catalog email sending patterns throughout the application
    • Document template processing patterns

Dependencies: None
Completion Criteria: Complete understanding of provider system, provider configuration strategy defined

Phase 2: Refactor BaseEmailService to Use Providers

Goal: Transform BaseEmailService from nodemailer-direct to provider-based

  • Task 2.1: Create provider-based email service architecture
    • Design new BaseEmailService interface that uses EmailProviderManager
    • Create ProviderEmailService as new base class
    • Implement provider initialization logic in base service
    • Add provider health checking and fallback mechanisms
  • Task 2.2: Update BaseEmailService implementation
    • Replace nodemailer transporter with EmailProviderManager
    • Modify sendEmail method to use provider.sendEmail()
    • Update address normalization to use provider address formats
    • Preserve template processing functionality
  • Task 2.3: Test BaseEmailService provider integration
    • Create unit tests for provider-based email sending
    • Test email address format conversion
    • Test template processing with provider integration
    • Verify error handling and logging

Dependencies: Phase 1 completion
Completion Criteria: BaseEmailService successfully uses providers instead of nodemailer

Phase 3: Create Unified SystemEmailService

Goal: Build SystemEmailService using environment variables and provider system

  • Task 3.1: Implement SystemEmailService with provider support
    • Create SystemEmailService extending new provider-based BaseEmailService
    • Implement environment variable configuration loading
    • Map env vars to provider configuration format
    • Add system email provider initialization
  • Task 3.2: Create system email provider configuration
    • Design env var to provider config mapping
    • Support SMTP, Resend, and other providers via env vars
    • Add provider type selection based on env vars
    • Implement provider initialization with env var config
  • Task 3.3: Integrate system email templates
    • Move system templates to /src/lib/email/system/templates/
    • Create template processor for system emails
    • Implement static template loading for system use
    • Add template validation and error handling
  • Task 3.4: Create system email factory and utilities
    • Create getSystemEmailService() factory function
    • Add system email configuration validation
    • Implement health checking for system email service
    • Add logging and monitoring for system emails

Dependencies: Phase 2 completion
Completion Criteria: SystemEmailService works with providers and environment variables

Phase 4: Consolidate TenantEmailService Implementations

Goal: Remove duplicate TenantEmailService and enhance the remaining one

  • Task 4.1: Choose canonical TenantEmailService implementation
    • Compare /src/lib/services/TenantEmailService.ts vs /src/lib/email/tenant/TenantEmailService.ts
    • Identify best features from each implementation
    • Select primary implementation location (/src/lib/services/)
    • Document features to merge from secondary implementation
  • Task 4.2: Enhance chosen TenantEmailService
    • Merge best features from both implementations
    • Ensure full provider system integration via EmailProviderManager
    • Verify database configuration loading works correctly
    • Add comprehensive error handling and logging
  • Task 4.3: Update TenantEmailService to use new base service
    • Modify TenantEmailService to extend new provider-based BaseEmailService
    • Update database configuration loading to use provider configs
    • Ensure template processing works with provider system
    • Test multi-tenant provider configurations
  • Task 4.4: Remove duplicate TenantEmailService implementation
    • Update all import references to point to canonical implementation
    • Remove /src/lib/email/tenant/TenantEmailService.ts after verification
    • Update any exports or re-exports
    • Clean up unused template processors if any

Dependencies: Phase 3 completion
Completion Criteria: Single TenantEmailService using provider system with database configs

Phase 5: Update Email Function Integrations

Goal: Update specific email functions to use correct consolidated services

  • Task 5.1: Update system email functions
    • Modify /src/lib/email/sendVerificationEmail.ts to use SystemEmailService
    • Update password reset functions to use SystemEmailService
    • Update any system notification functions to use SystemEmailService
    • Test all system email flows end-to-end
  • Task 5.2: Update tenant/business email functions
    • Verify /src/lib/email/sendPortalInvitationEmail.ts uses SystemEmailService (temporarily)
    • Update invoice email functions to use TenantEmailService
    • Update project/ticket notification functions to use TenantEmailService
    • Test all tenant email flows end-to-end
  • Task 5.3: Update application integration points
    • Update all API routes using email services
    • Update action files and server components
    • Update workflow email processing to use appropriate service
    • Update any background jobs or scheduled tasks
  • Task 5.4: Create email service factory
    • Create /src/lib/email/index.ts as main entry point
    • Export getSystemEmailService() and getTenantEmailService() functions
    • Re-export common types and interfaces
    • Add service selection utilities

Dependencies: Phase 4 completion
Completion Criteria: All email functions use appropriate consolidated services

Phase 6: Provider Configuration and Validation

Goal: Ensure robust provider configuration and validation

  • Task 6.1: Enhance provider configuration validation
    • Add comprehensive config validation for all provider types
    • Create provider configuration testing utilities
    • Add environment variable validation for system emails
    • Add database configuration validation for tenant emails
  • Task 6.2: Create provider configuration documentation
    • Document env var configuration for each provider type
    • Document database configuration for tenant providers
    • Create configuration examples and troubleshooting guides
    • Add provider selection decision matrix
  • Task 6.3: Add provider health monitoring
    • Enhance health checking for all provider types
    • Add configuration validation endpoints
    • Create provider status dashboard components
    • Add alerting for provider configuration issues

Dependencies: Phase 5 completion
Completion Criteria: Robust provider configuration with validation and monitoring

Phase 7: Testing and Documentation

Goal: Comprehensive testing and documentation of new email system

  • Task 7.1: Create comprehensive test suite
    • Unit tests for SystemEmailService with different providers
    • Unit tests for TenantEmailService with provider configurations
    • Integration tests for email sending flows
    • End-to-end tests for system and tenant email scenarios
  • Task 7.2: Create migration and setup documentation
    • Document environment variable configuration
    • Create tenant email provider setup guide
    • Document migration steps for existing installations
    • Create troubleshooting and debugging guide
  • Task 7.3: Create developer documentation
    • Document when to use SystemEmailService vs TenantEmailService
    • Create code examples for common email scenarios
    • Document provider configuration patterns
    • Create email service architecture diagrams

Dependencies: Phase 6 completion
Completion Criteria: Complete test coverage and comprehensive documentation

Background Details / Investigation / Implementation Advice

Current Architecture Analysis

Existing Email Providers Infrastructure

The system already has a sophisticated provider infrastructure:

IEmailProvider Interface:

  • Standardized methods: sendEmail(), sendBulkEmails(), healthCheck()
  • Provider capabilities: attachment support, templating, bulk sending
  • Domain management: createDomain(), verifyDomain(), listDomains()
  • Rate limiting and error handling built-in

Available Providers:

  • ResendEmailProvider: Full-featured with domain management, rate limiting
  • SMTPEmailProvider: Traditional SMTP with standard features
  • GmailAdapter: OAuth-based Gmail integration
  • MicrosoftGraphAdapter: OAuth-based Microsoft 365 integration

EmailProviderManager:

  • Manages provider initialization and lifecycle
  • Handles provider selection and failover
  • Supports multiple providers per tenant
  • Provides unified interface for email sending

Current Issues

BaseEmailService Problems:

  • Uses nodemailer directly instead of leveraging provider system
  • Bypasses sophisticated provider features like rate limiting
  • Duplicates functionality already available in providers
  • No access to provider-specific capabilities

TenantEmailService Duplication:

  • Two implementations: /src/lib/services/TenantEmailService.ts vs /src/lib/email/tenant/TenantEmailService.ts
  • Different interfaces and capabilities
  • Inconsistent provider integration
  • Maintenance burden and confusion

Provider Underutilization:

  • Sophisticated provider system exists but isn't fully leveraged
  • Provider capabilities like bulk sending not utilized
  • Domain management features unused
  • Health checking and monitoring capabilities ignored

Technical Implementation Guidelines

Provider Integration Strategy

  1. Replace Direct Transport: Replace nodemailer transporters with EmailProviderManager
  2. Configuration Mapping: Map environment variables to provider configurations
  3. Provider Selection: Implement provider type selection based on configuration
  4. Capability Utilization: Leverage provider-specific capabilities (bulk, domains, health)

SystemEmailService Design

interface SystemEmailConfig {
  providerType: 'smtp' | 'resend' | 'gmail' | 'microsoft';
  providerConfig: Record<string, any>;
  fromAddress: string;
  fromName?: string;
}

// Environment variable mapping
EMAIL_PROVIDER_TYPE=resend
RESEND_API_KEY=re_abc123...
EMAIL_FROM_ADDRESS=system@company.com
EMAIL_FROM_NAME=Company System

TenantEmailService Enhancement

// Use existing EmailProviderManager with database configs
const emailProviderManager = new EmailProviderManager();
await emailProviderManager.initialize(tenantEmailSettings);

Provider Configuration Examples

Resend via Environment Variables:

EMAIL_PROVIDER_TYPE=resend
RESEND_API_KEY=re_abc123...
EMAIL_FROM=noreply@company.com

SMTP via Environment Variables:

EMAIL_PROVIDER_TYPE=smtp
EMAIL_HOST=smtp.company.com
EMAIL_PORT=587
EMAIL_USERNAME=system@company.com
EMAIL_PASSWORD=password123

Database Configuration (Tenant):

{
  "providerType": "resend",
  "config": {
    "apiKey": "re_tenant_key...",
    "defaultFromDomain": "client.com"
  },
  "isEnabled": true
}

Migration Strategy

Zero-Downtime Approach

  1. Parallel Implementation: Keep existing services running while building new ones
  2. Feature Flags: Use feature flags to control which email service is used
  3. Gradual Migration: Move email functions one at a time
  4. Rollback Plan: Maintain ability to revert to old implementation

Testing Strategy

  1. Provider Mocking: Create mock providers for testing
  2. Configuration Testing: Test all provider configuration combinations
  3. End-to-End Testing: Test complete email flows with real providers
  4. Load Testing: Test provider performance under load

Error Handling Strategy

  1. Provider Fallback: Implement provider fallback mechanisms
  2. Configuration Validation: Validate configurations before initialization
  3. Graceful Degradation: Handle provider failures gracefully
  4. Comprehensive Logging: Log all email operations for debugging

Implementation Best Practices

Provider Configuration

  • Validation: Validate all provider configurations before use
  • Security: Store sensitive credentials securely (environment variables, database encryption)
  • Flexibility: Support multiple provider types with consistent interface
  • Monitoring: Monitor provider health and performance

Service Design

  • Single Responsibility: Clear separation between system and tenant emails
  • Dependency Injection: Inject providers rather than hard-coding
  • Interface Consistency: Maintain consistent interfaces across services
  • Error Handling: Comprehensive error handling with meaningful messages

Testing Requirements

  • Unit Tests: Test each service with mocked providers
  • Integration Tests: Test provider integration and configuration
  • End-to-End Tests: Test complete email flows in realistic scenarios
  • Performance Tests: Test email sending performance and rate limits

Potential Pitfalls and Solutions

Pitfall: Provider initialization failures breaking email service
Solution: Implement graceful degradation and provider health monitoring

Pitfall: Configuration conflicts between environment and database settings
Solution: Clear precedence rules and validation of configuration sources

Pitfall: Breaking existing email functionality during migration
Solution: Feature flags and parallel implementation during transition

Pitfall: Provider-specific features not working across different providers
Solution: Provider capability checking and feature abstraction

Pitfall: Performance degradation from provider abstraction overhead
Solution: Optimize provider selection and caching, performance testing

Resources and References

Current Implementation Files

  • Provider System: /src/services/email/providers/
  • Provider Manager: /src/services/email/EmailProviderManager.ts
  • Email Types: /src/types/email.types.ts
  • Existing Services: /src/lib/services/TenantEmailService.ts, /src/lib/email/

Provider Documentation

  • ResendEmailProvider: Feature-complete with domain management
  • SMTPEmailProvider: Standard SMTP with authentication
  • EmailProviderManager: Provider lifecycle and selection management
  • IEmailProvider Interface: Standardized provider contract

Implementer's Scratch Pad

Progress Tracking

  • Phase 1 started: 2025-08-05
  • Phase 1 completed: 2025-08-05
  • Phase 2 started: 2025-08-05
  • Phase 2 completed: 2025-08-05
  • Phase 3 started: 2025-08-05
  • Phase 3 completed: 2025-08-05
  • Phase 4 started: 2025-08-05
  • Phase 4 completed: 2025-08-05
  • Phase 5 started: 2025-08-05
  • Phase 5 completed: ___________
  • Phase 6 started: ___________
  • Phase 6 completed: ___________
  • Phase 7 started: ___________
  • Phase 7 completed: ___________

Implementation Notes

Phase 1 Notes:
- Provider architecture analysis status: COMPLETE
- Environment variable mapping design: COMPLETE - SystemEmailProviderFactory created
- Current service usage documentation: COMPLETE

Phase 2 Notes:
- BaseEmailService refactoring status: COMPLETE - Now uses IEmailProvider instead of nodemailer
- Provider integration testing: COMPLETE - Uses provider.sendEmail()
- Nodemailer replacement verification: COMPLETE - All nodemailer imports removed

Phase 3 Notes:
- SystemEmailService implementation: COMPLETE - Extends BaseEmailService with provider support
- Environment variable configuration: COMPLETE - SystemEmailProviderFactory handles env var mapping
- System template integration: COMPLETE - Template methods maintained

Phase 4 Notes:
- TenantEmailService consolidation: COMPLETE - Kept /src/lib/services/TenantEmailService.ts
- Duplicate removal status: COMPLETE - Removed /src/lib/email/tenant/TenantEmailService.ts
- Database configuration integration: COMPLETE - Uses EmailProviderManager

Phase 5 Notes:
- Email function migration status: IN PROGRESS
- Integration point updates: PARTIAL - sendPortalInvitationEmail updated
- End-to-end testing results: PENDING

Phase 6 Notes:
- Provider configuration validation:
- Health monitoring implementation:
- Documentation completion:

Phase 7 Notes:
- Test suite completion:
- Documentation review:
- Migration guide validation:

Issues Encountered and Resolutions

Issue 1: [Description]
Resolution: [How resolved]
Impact: [Impact on timeline/scope]

Issue 2: [Description]
Resolution: [How resolved]
Impact: [Impact on timeline/scope]

Deviations From Original Plan

Deviation 1: [What changed]
Reason: [Why it changed]
Approval: [Who approved change]

Deviation 2: [What changed]
Reason: [Why it changed]
Approval: [Who approved change]

Performance Metrics and Test Results

Provider Integration Tests:
- Resend provider integration: [PASS/FAIL]
- SMTP provider integration: [PASS/FAIL]
- Provider manager initialization: [PASS/FAIL]

System Email Tests:
- Environment variable configuration: [PASS/FAIL]
- System email template processing: [PASS/FAIL]
- System email sending: [PASS/FAIL]

Tenant Email Tests:
- Database configuration loading: [PASS/FAIL]
- Tenant provider initialization: [PASS/FAIL]
- Multi-tenant email sending: [PASS/FAIL]

Migration Tests:
- Service consolidation: [PASS/FAIL]
- Import path updates: [PASS/FAIL]
- End-to-end email flows: [PASS/FAIL]

Questions for Review

1. Should we maintain backward compatibility interfaces during migration?
2. What is the preferred approach for environment variable validation?
3. Should we implement provider failover mechanisms in Phase 1 or later?
4. How should we handle provider rate limits in the consolidated services?