# SVN Commit Notes - Customer Migration Environment Support & Rollback Fixes ## Summary Added --env flag support for production migrations, fixed critical rollback bugs (invoice field & entity restoration), and resolved /importingStatus route filtering issue. ## New Features ### Environment Configuration - Added --env flag to migrateCustomerData.js and rollbackMigration.js * Enables production migrations: `--env ../environment_prod.env` * Supports relative paths (from scripts/ dir) and absolute paths * Defaults to ../environment.env for development * Script prints loaded environment path for verification ### Migration Enhancements - Updated command-line argument parser to recognize --env flag - Added environment path validation (relative vs absolute) - Enhanced migration preview to show environment being used ## Bug Fixes ### Critical Rollback Fixes - **Invoice Restoration**: Fixed rollback updating wrong field * Was updating `invoice.customer` field (non-existent) * Now correctly updates `invoice.byPuid` field (matches migration) * Line 218 in rollbackMigration.js - **Entity Restoration**: Fixed E11000 duplicate key errors during rollback * Added existence check before restoring deleted products/crops * Prevents attempting to restore entities that were never deleted * Handles already-rolled-back state gracefully * Lines 120-130, 167-177 in rollbackMigration.js ### Application Route Fix - **importingStatus Route**: Fixed showing applications from wrong customer * Added `$match` stage after `$lookup` to filter out null job results * Filter: `{ "appjob._id": { $exists: true } }` * Prevents displaying applications whose jobs don't belong to current user * Line 1303 in controllers/job.js * Issue occurred after migration rollback when apps linked to migrated jobs ## Improvements ### Migration History Accuracy - Enhanced error handling with changesRolledBack array - Records rollback reason when transaction fails - Clears changes array on transaction failure to prevent incorrect history ### Documentation - Updated scripts/README_CUSTOMER_MIGRATION.md with --env examples - Updated ADMIN_CONVERSION_SUMMARY.md with production environment usage - Created scripts/MIGRATION_ENVIRONMENT_GUIDE.md (comprehensive env guide) * Safe production migration workflow * Best practices and common mistakes * Troubleshooting section with verification steps - Added rollback section with --env flag documentation - All examples now show both dev and production environment options ## Files Modified ### Migration Scripts - scripts/migrateCustomerData.js * Added --env argument parsing at startup (lines 48-58) * Added --env case to argument switch (lines 1284-1289) * Updated help message with --env option - scripts/rollbackMigration.js * Added --env argument parsing at startup (lines 25-32) * Fixed invoice restoration field from 'customer' to 'byPuid' (line 218) * Added entity existence checks before restoration (lines 120-130, 167-177) * Updated help documentation with --env option and usage ### Controllers - controllers/job.js * Fixed importingStatus_post route filtering (line 1303) * Added $match stage to filter applications by job ownership * Prevents showing apps from wrong customer after rollback ### Documentation - scripts/README_CUSTOMER_MIGRATION.md * Added --env option to command-line options table * Updated all usage examples with production environment syntax * Added "Rollback Migrations" section with --env examples * Added best practice about custom environment files - ADMIN_CONVERSION_SUMMARY.md * Updated all usage examples with --env flag for production * Added separate examples for dev and production environments * Updated rollback examples with environment flag - scripts/MIGRATION_ENVIRONMENT_GUIDE.md (NEW) * Comprehensive environment configuration guide * Quick reference for dev/prod usage * Safe production migration workflow examples * Best practices and common mistakes section * Troubleshooting guide ## Database Impact None - Bug fixes and configuration enhancements only ## Testing Completed ### Environment Configuration - ✅ Verified --env flag parsing with relative paths - ✅ Verified --env flag parsing with absolute paths - ✅ Verified environment file loading prints correct path - ✅ Tested default behavior (uses environment.env) ### Rollback Fixes - ✅ Tested invoice restoration now updates correct 'byPuid' field - ✅ Verified entity existence check prevents duplicate key errors - ✅ Confirmed rollback handles already-restored entities gracefully - ✅ Tested complete rollback cycle with entity reuse enabled ### Application Filtering - ✅ Verified /importingStatus returns only user's applications - ✅ Tested route after migration rollback (no wrong customer apps) - ✅ Confirmed $match filter excludes orphaned applications ## Deployment Notes ### Required Actions - None - Backwards compatible changes only ### Recommended Actions - Use --env flag for all production migrations going forward - Review existing migration_history.json for any inconsistencies - Test rollback capability in development before production use ### Verification Steps 1. Run migration with --preview and --env to verify correct database connection 2. Check script output shows correct environment file path 3. Test rollback on development environment first ## Usage Examples ```bash # Production preview node scripts/migrateCustomerData.js \ --env ../environment_prod.env \ --sources source@example.com \ --destination dest@example.com \ --preview # Production migration with entity reuse node scripts/migrateCustomerData.js \ --env ../environment_prod.env \ --sources source@example.com \ --destination dest@example.com \ --reuse-existing-entities # Production rollback node scripts/rollbackMigration.js --env ../environment_prod.env ``` ## Related Documentation - scripts/README_CUSTOMER_MIGRATION.md - Complete migration guide - scripts/MIGRATION_ENVIRONMENT_GUIDE.md - Environment configuration guide - ADMIN_CONVERSION_SUMMARY.md - Admin conversion implementation details - REUSE_ENTITIES_ROLLBACK.md - Entity reuse feature documentation ### Workers - workers/partner_sync_worker.js - Async queue operations - workers/partner_data_polling_worker.js - Better error handling - workers/job_worker.js - Enum constants instead of strings ### Documentation - docs/API_SPECIFICATION.md - New endpoint documentation - docs/PARTNER_INTEGRATION_IMPLEMENTATION.md - Queue improvements - docs/RECENT_UPDATES_SUMMARY.md - NEW: Comprehensive change summary ## Impact - Improved system reliability with enhanced queue error handling - Better partner management with new customer and auth endpoints - Enhanced aircraft tracking with tailNumber and assignment status - More maintainable code with service factory pattern and enum usage - Comprehensive documentation for easier maintenance and expansion ## Testing - All modified files validated for syntax errors - Queue operations tested for error scenarios - API endpoints verified for proper request/response handling - MongoDB aggregations validated for data consistency