/** * Script: Sync Teacher Profiles * Mục đích: Tạo user_profile cho các teacher đã có sẵn trong hệ thống * * Usage: * node src/scripts/sync-teacher-profiles.js * node src/scripts/sync-teacher-profiles.js --teacher-id=uuid * node src/scripts/sync-teacher-profiles.js --dry-run */ require('dotenv').config(); const teacherProfileService = require('../services/teacherProfileService'); const { sequelize } = require('../config/database'); async function main() { console.log('🚀 Starting Teacher Profile Sync...\n'); try { // Parse command line arguments const args = process.argv.slice(2); const dryRun = args.includes('--dry-run'); const teacherIdArg = args.find(arg => arg.startsWith('--teacher-id=')); const teacherId = teacherIdArg ? teacherIdArg.split('=')[1] : null; if (dryRun) { console.log('⚠️ DRY RUN MODE - No changes will be made\n'); } if (teacherId) { console.log(`🎯 Syncing specific teacher: ${teacherId}\n`); } else { console.log('🔄 Syncing all teachers without profiles\n'); } // Test database connection await sequelize.authenticate(); console.log('✅ Database connection established\n'); if (dryRun) { console.log('Dry run completed. Use without --dry-run to apply changes.'); process.exit(0); } // Execute sync console.log('Processing...\n'); const results = await teacherProfileService.syncExistingTeachers(teacherId); // Display results console.log('\n' + '='.repeat(60)); console.log('📊 SYNC RESULTS'); console.log('='.repeat(60)); console.log(`\n✅ Success: ${results.success.length}`); if (results.success.length > 0) { results.success.forEach((item, index) => { console.log(` ${index + 1}. ${item.teacher_code} → Profile created (user_id: ${item.user_id})`); }); } console.log(`\n⏭️ Skipped: ${results.skipped.length}`); if (results.skipped.length > 0) { results.skipped.slice(0, 5).forEach((item, index) => { console.log(` ${index + 1}. ${item.teacher_code} - ${item.reason}`); }); if (results.skipped.length > 5) { console.log(` ... and ${results.skipped.length - 5} more`); } } console.log(`\n❌ Failed: ${results.failed.length}`); if (results.failed.length > 0) { results.failed.forEach((item, index) => { console.log(` ${index + 1}. ${item.teacher_code} - ${item.reason || item.error}`); }); } console.log('\n' + '='.repeat(60)); console.log('✅ Sync completed successfully!'); console.log('='.repeat(60) + '\n'); process.exit(0); } catch (error) { console.error('\n❌ Sync failed:', error.message); console.error(error.stack); process.exit(1); } } // Run main();