|
| 1 | +const { pool } = require('./config/database'); |
| 2 | + |
| 3 | +async function checkAndFixEmployees() { |
| 4 | + const client = await pool.connect(); |
| 5 | + try { |
| 6 | + // 1. Check existing branches |
| 7 | + console.log('📍 Checking branches...'); |
| 8 | + const branchesResult = await client.query('SELECT * FROM branch ORDER BY branch_id'); |
| 9 | + console.log(`Found ${branchesResult.rows.length} branches:`); |
| 10 | + branchesResult.rows.forEach(b => console.log(` - ID: ${b.branch_id}, Name: ${b.branch_name}`)); |
| 11 | + |
| 12 | + if (branchesResult.rows.length === 0) { |
| 13 | + console.error('❌ No branches found! Please create branches first.'); |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + const firstBranchId = branchesResult.rows[0].branch_id; |
| 18 | + const secondBranchId = branchesResult.rows.length > 1 ? branchesResult.rows[1].branch_id : firstBranchId; |
| 19 | + |
| 20 | + // 2. Check users and their employee records |
| 21 | + console.log('\n👥 Checking users...'); |
| 22 | + const usersResult = await client.query(` |
| 23 | + SELECT u.user_id, u.username, u.role, e.employee_id, e.branch_id, e.name |
| 24 | + FROM user_account u |
| 25 | + LEFT JOIN employee e ON u.user_id = e.user_id |
| 26 | + ORDER BY u.role, u.username |
| 27 | + `); |
| 28 | + |
| 29 | + console.log(`\nFound ${usersResult.rows.length} users:`); |
| 30 | + usersResult.rows.forEach(u => { |
| 31 | + const empInfo = u.employee_id ? `Employee ID: ${u.employee_id}, Branch: ${u.branch_id}` : 'No employee record'; |
| 32 | + console.log(` - ${u.username} (${u.role}): ${empInfo}`); |
| 33 | + }); |
| 34 | + |
| 35 | + // 3. Insert missing employee records |
| 36 | + console.log('\n🔧 Fixing missing employee records...'); |
| 37 | + |
| 38 | + const employeeData = [ |
| 39 | + { username: 'accountant', name: 'David Accountant', email: 'accountant@hotel.com', contact: '+94771234570', branch_id: firstBranchId }, |
| 40 | + { username: 'manager', name: 'John Manager', email: 'manager@hotel.com', contact: '+94771234567', branch_id: firstBranchId }, |
| 41 | + { username: 'reception', name: 'Sarah Receptionist', email: 'reception@hotel.com', contact: '+94771234568', branch_id: firstBranchId }, |
| 42 | + { username: 'staff1', name: 'Mike Staff', email: 'staff1@hotel.com', contact: '+94771234569', branch_id: secondBranchId } |
| 43 | + ]; |
| 44 | + |
| 45 | + for (const emp of employeeData) { |
| 46 | + // Check if user exists |
| 47 | + const userCheck = await client.query( |
| 48 | + 'SELECT user_id FROM user_account WHERE username = $1', |
| 49 | + [emp.username] |
| 50 | + ); |
| 51 | + |
| 52 | + if (userCheck.rows.length === 0) { |
| 53 | + console.log(` ⚠️ User '${emp.username}' not found. Skipping.`); |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + const userId = userCheck.rows[0].user_id; |
| 58 | + |
| 59 | + // Check if employee record exists |
| 60 | + const empCheck = await client.query( |
| 61 | + 'SELECT employee_id FROM employee WHERE user_id = $1', |
| 62 | + [userId] |
| 63 | + ); |
| 64 | + |
| 65 | + if (empCheck.rows.length > 0) { |
| 66 | + console.log(` ℹ️ Employee record for '${emp.username}' already exists.`); |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + // Insert employee record |
| 71 | + await client.query( |
| 72 | + `INSERT INTO employee (user_id, branch_id, name, email, contact_no) |
| 73 | + VALUES ($1, $2, $3, $4, $5)`, |
| 74 | + [userId, emp.branch_id, emp.name, emp.email, emp.contact] |
| 75 | + ); |
| 76 | + |
| 77 | + console.log(` ✅ Created employee record for '${emp.username}' (Branch ID: ${emp.branch_id})`); |
| 78 | + } |
| 79 | + |
| 80 | + // 4. Verify final state |
| 81 | + console.log('\n📊 Final verification...'); |
| 82 | + const finalCheck = await client.query(` |
| 83 | + SELECT u.username, u.role, e.branch_id, b.branch_name |
| 84 | + FROM user_account u |
| 85 | + LEFT JOIN employee e ON u.user_id = e.user_id |
| 86 | + LEFT JOIN branch b ON e.branch_id = b.branch_id |
| 87 | + WHERE u.role != 'Admin' |
| 88 | + ORDER BY u.username |
| 89 | + `); |
| 90 | + |
| 91 | + console.log('\nNon-admin users and their branches:'); |
| 92 | + finalCheck.rows.forEach(u => { |
| 93 | + const branchInfo = u.branch_id ? `${u.branch_name} (ID: ${u.branch_id})` : '❌ NO BRANCH ASSIGNED'; |
| 94 | + console.log(` - ${u.username} (${u.role}): ${branchInfo}`); |
| 95 | + }); |
| 96 | + |
| 97 | + console.log('\n✅ Employee setup complete!'); |
| 98 | + |
| 99 | + } catch (error) { |
| 100 | + console.error('❌ Error:', error.message); |
| 101 | + console.error(error); |
| 102 | + } finally { |
| 103 | + client.release(); |
| 104 | + process.exit(0); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +checkAndFixEmployees(); |
0 commit comments