Skip to content

Commit 5ec2698

Browse files
committed
fix: Correct employee table name from 'employees' to 'employee' in Sequelize model
1 parent 11e84d8 commit 5ec2698

File tree

5 files changed

+194
-3
lines changed

5 files changed

+194
-3
lines changed

fix-employees.js

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

models/employee.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const Employee = sequelize.define('Employee', {
4040
allowNull: false
4141
}
4242
}, {
43-
tableName: 'employees',
43+
tableName: 'employee', // Fixed: Table name is singular in database
4444
timestamps: true
4545
});
4646

seeds/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const userSeed = require('./users.seed');
22
const employeeSeed = require('./employees.seed');
33
const roomSeed = require('./rooms.seed');
44
const guestSeed = require('./guests.seed');
5-
const bookingSeed = require('./bookings.seed');
5+
const bookingSeed = require('./booking.seed'); // Fixed: booking.seed not bookings.seed
66
const { sequelize } = require('../models');
77

88
const seedAll = async () => {

seeds/users.seed.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { User } = require('../models');
2-
const { hashPassword } = require('../utils/helpers');
2+
const { hashPassword } = require('../utils/helper'); // Fixed: helper.js not helpers.js
33

44
const seedUsers = async () => {
55
try {

test-accountant-login.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const { pool } = require('./config/database');
2+
const bcrypt = require('bcryptjs');
3+
4+
async function testAccountantLogin() {
5+
const client = await pool.connect();
6+
try {
7+
// Simulate login for accountant_colombo
8+
const username = 'accountant_colombo';
9+
10+
const query = `
11+
SELECT
12+
u.user_id,
13+
u.username,
14+
u.password_hash,
15+
u.role,
16+
e.employee_id,
17+
e.branch_id,
18+
e.name as employee_name,
19+
b.branch_name,
20+
u.guest_id,
21+
g.email AS guest_email
22+
FROM public.user_account u
23+
LEFT JOIN public.employee e ON u.user_id = e.user_id
24+
LEFT JOIN public.branch b ON e.branch_id = b.branch_id
25+
LEFT JOIN public.guest g ON u.guest_id = g.guest_id
26+
WHERE
27+
u.username = $1
28+
OR e.email = $1
29+
OR g.email = $1
30+
`;
31+
32+
const result = await client.query(query, [username]);
33+
const user = result.rows[0];
34+
35+
if (!user) {
36+
console.log(`❌ User '${username}' not found`);
37+
return;
38+
}
39+
40+
console.log('\n✅ User Found:');
41+
console.log('--------------------------------------------------');
42+
console.log('Username:', user.username);
43+
console.log('Role:', user.role);
44+
console.log('Employee ID:', user.employee_id);
45+
console.log('Branch ID:', user.branch_id);
46+
console.log('Branch Name:', user.branch_name);
47+
console.log('Employee Name:', user.employee_name);
48+
console.log('--------------------------------------------------');
49+
50+
// Check what would be in the JWT payload
51+
const payload = {
52+
userId: user.user_id,
53+
role: user.role,
54+
employeeId: user.employee_id || null,
55+
branchId: user.branch_id || null
56+
};
57+
58+
console.log('\n🔐 JWT Payload would be:');
59+
console.log(JSON.stringify(payload, null, 2));
60+
61+
// Check what frontend expects
62+
console.log('\n📋 Frontend BranchContext would see:');
63+
console.log(`- user.role: "${user.role}"`);
64+
console.log(`- user.branch_id: ${user.branch_id}`);
65+
console.log(`- isLocked calculation: user.role !== 'Admin' && user.branch_id = ${user.role !== 'Admin' && user.branch_id ? 'true' : 'false'}`);
66+
67+
if (user.role !== 'Admin' && user.branch_id) {
68+
console.log(`\n✅ User SHOULD be locked to branch ${user.branch_id} (${user.branch_name})`);
69+
console.log('✅ Navbar should show BADGE, not dropdown');
70+
} else {
71+
console.log(`\n❌ User would NOT be locked (role: ${user.role}, branch_id: ${user.branch_id})`);
72+
console.log('❌ Navbar would show dropdown');
73+
}
74+
75+
} catch (error) {
76+
console.error('❌ Error:', error.message);
77+
} finally {
78+
client.release();
79+
process.exit(0);
80+
}
81+
}
82+
83+
testAccountantLogin();

0 commit comments

Comments
 (0)