Skip to content

Commit fb25943

Browse files
committed
fix: Improve branch locking logic to handle string and number branch_id values
1 parent a52932d commit fb25943

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

src/context/BranchContext.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,25 @@ export const BranchProvider = ({ children }) => {
2424
console.log('🔍 BranchContext - User object:', user);
2525
console.log('🔍 BranchContext - User role:', user.role);
2626
console.log('🔍 BranchContext - User branch_id:', user.branch_id);
27+
console.log('🔍 BranchContext - branch_id type:', typeof user.branch_id);
28+
29+
// Check if user has a branch_id (could be string or number)
30+
const hasBranchId = user.branch_id !== null && user.branch_id !== undefined && user.branch_id !== '';
2731

2832
// If user is not Admin and has a branch_id, lock them to their branch
29-
if (user.role !== 'Admin' && user.branch_id) {
33+
if (user.role !== 'Admin' && hasBranchId) {
3034
console.log(`🔒 User ${user.name || user.username} locked to branch ${user.branch_id}`);
31-
setSelectedBranchId(user.branch_id);
35+
// Convert to number if it's a string
36+
const branchId = typeof user.branch_id === 'string' ? parseInt(user.branch_id, 10) : user.branch_id;
37+
setSelectedBranchId(branchId);
3238
} else if (user.role === 'Admin') {
3339
console.log('👑 Admin user - can access all branches');
3440
setSelectedBranchId('All');
3541
} else {
3642
console.warn('⚠️ Non-admin user but no branch_id found!', {
3743
role: user.role,
3844
branch_id: user.branch_id,
45+
hasBranchId: hasBranchId,
3946
user: user
4047
});
4148
}
@@ -67,20 +74,26 @@ export const BranchProvider = ({ children }) => {
6774

6875
// Prevent non-admin users from changing branch
6976
const handleSetSelectedBranchId = (branchId) => {
70-
if (user && user.role !== 'Admin' && user.branch_id) {
77+
const hasBranchId = user?.branch_id !== null && user?.branch_id !== undefined && user?.branch_id !== '';
78+
79+
if (user && user.role !== 'Admin' && hasBranchId) {
7180
console.warn('⚠️ Non-admin users cannot change branch');
81+
console.warn('⚠️ Attempted to change to:', branchId, 'but locked to:', user.branch_id);
7282
return; // Silently ignore attempt to change branch
7383
}
7484
setSelectedBranchId(branchId);
7585
};
7686

87+
const hasBranchId = user?.branch_id !== null && user?.branch_id !== undefined && user?.branch_id !== '';
88+
const isLocked = user && user.role !== 'Admin' && hasBranchId;
89+
7790
const value = {
7891
selectedBranchId,
7992
setSelectedBranchId: handleSetSelectedBranchId,
8093
branches,
8194
loading,
8295
selectedBranch: Array.isArray(branches) ? branches.find(b => b.branch_id === selectedBranchId) || null : null,
83-
isLocked: user && user.role !== 'Admin' && user.branch_id ? true : false
96+
isLocked: isLocked
8497
};
8598

8699
return (

0 commit comments

Comments
 (0)