Skip to content

Commit 11e84d8

Browse files
committed
feat: Add employee seeding with branch assignments for role-based access control
1 parent 7b902d4 commit 11e84d8

File tree

3 files changed

+108
-13
lines changed

3 files changed

+108
-13
lines changed

seeds/employees.seed.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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;

seeds/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const userSeed = require('./users.seed');
2+
const employeeSeed = require('./employees.seed');
23
const roomSeed = require('./rooms.seed');
34
const guestSeed = require('./guests.seed');
45
const bookingSeed = require('./bookings.seed');
@@ -14,6 +15,7 @@ const seedAll = async () => {
1415

1516
// Run seeds in order
1617
await userSeed();
18+
await employeeSeed(); // Must run after userSeed
1719
await roomSeed();
1820
await guestSeed();
1921
await bookingSeed();

seeds/users.seed.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,34 @@ const seedUsers = async () => {
88
const users = [
99
{
1010
username: 'admin',
11-
email: 'admin@hotel.com',
1211
password: await hashPassword('admin123'),
13-
role: 'admin',
14-
is_active: true
12+
role: 'Admin' // Capitalized to match database schema
1513
},
1614
{
1715
username: 'manager',
18-
email: 'manager@hotel.com',
1916
password: await hashPassword('manager123'),
20-
role: 'manager',
21-
is_active: true
17+
role: 'Manager' // Capitalized to match database schema
2218
},
2319
{
2420
username: 'reception',
25-
email: 'reception@hotel.com',
2621
password: await hashPassword('reception123'),
27-
role: 'receptionist',
28-
is_active: true
22+
role: 'Receptionist' // Capitalized to match database schema
23+
},
24+
{
25+
username: 'accountant',
26+
password: await hashPassword('accountant123'),
27+
role: 'Accountant' // New accountant user
2928
},
3029
{
3130
username: 'staff1',
32-
email: 'staff1@hotel.com',
3331
password: await hashPassword('staff123'),
34-
role: 'receptionist',
35-
is_active: true
32+
role: 'Receptionist' // Capitalized to match database schema
3633
}
3734
];
3835

3936
for (const userData of users) {
4037
await User.findOrCreate({
41-
where: { email: userData.email },
38+
where: { username: userData.username },
4239
defaults: userData
4340
});
4441
}

0 commit comments

Comments
 (0)