|
| 1 | +const { Employee, User, Branch } = require('../models'); |
| 2 | + |
| 3 | +/** |
| 4 | + * Seed employee records for staff users |
| 5 | + * Links users to branches and provides employee information |
| 6 | + */ |
| 7 | +const seedEmployees = async () => { |
| 8 | + try { |
| 9 | + console.log('👔 Seeding employees...'); |
| 10 | + |
| 11 | + // First, get the branches and users |
| 12 | + const branches = await Branch.findAll(); |
| 13 | + if (branches.length === 0) { |
| 14 | + console.warn('⚠️ No branches found. Please seed branches first.'); |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + // Define employee data for existing users |
| 19 | + const employeesData = [ |
| 20 | + { |
| 21 | + username: 'manager', |
| 22 | + name: 'John Manager', |
| 23 | + email: 'manager@hotel.com', |
| 24 | + contact_no: '+94771234567', |
| 25 | + branch_id: branches[0].branch_id // Assign to first branch |
| 26 | + }, |
| 27 | + { |
| 28 | + username: 'reception', |
| 29 | + name: 'Sarah Receptionist', |
| 30 | + email: 'reception@hotel.com', |
| 31 | + contact_no: '+94771234568', |
| 32 | + branch_id: branches[0].branch_id // Assign to first branch |
| 33 | + }, |
| 34 | + { |
| 35 | + username: 'accountant', |
| 36 | + name: 'David Accountant', |
| 37 | + email: 'accountant@hotel.com', |
| 38 | + contact_no: '+94771234570', |
| 39 | + branch_id: branches[0].branch_id // Assign to first branch |
| 40 | + }, |
| 41 | + { |
| 42 | + username: 'staff1', |
| 43 | + name: 'Mike Staff', |
| 44 | + email: 'staff1@hotel.com', |
| 45 | + contact_no: '+94771234569', |
| 46 | + branch_id: branches.length > 1 ? branches[1].branch_id : branches[0].branch_id // Assign to second branch if exists |
| 47 | + } |
| 48 | + ]; |
| 49 | + |
| 50 | + let createdCount = 0; |
| 51 | + let skippedCount = 0; |
| 52 | + |
| 53 | + for (const empData of employeesData) { |
| 54 | + // Find the user by username |
| 55 | + const user = await User.findOne({ |
| 56 | + where: { username: empData.username } |
| 57 | + }); |
| 58 | + |
| 59 | + if (!user) { |
| 60 | + console.warn(`⚠️ User '${empData.username}' not found. Skipping employee creation.`); |
| 61 | + skippedCount++; |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + // Check if employee record already exists |
| 66 | + const existingEmployee = await Employee.findOne({ |
| 67 | + where: { user_id: user.user_id } |
| 68 | + }); |
| 69 | + |
| 70 | + if (existingEmployee) { |
| 71 | + console.log(`ℹ️ Employee record for '${empData.username}' already exists. Skipping.`); |
| 72 | + skippedCount++; |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + // Create employee record |
| 77 | + await Employee.create({ |
| 78 | + user_id: user.user_id, |
| 79 | + branch_id: empData.branch_id, |
| 80 | + name: empData.name, |
| 81 | + email: empData.email, |
| 82 | + contact_no: empData.contact_no |
| 83 | + }); |
| 84 | + |
| 85 | + console.log(`✅ Created employee record for '${empData.username}' (Branch ID: ${empData.branch_id})`); |
| 86 | + createdCount++; |
| 87 | + } |
| 88 | + |
| 89 | + console.log(`✅ Employee seeding complete: ${createdCount} created, ${skippedCount} skipped`); |
| 90 | + } catch (error) { |
| 91 | + console.error('❌ Error seeding employees:', error); |
| 92 | + throw error; |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +module.exports = seedEmployees; |
0 commit comments