@@ -22,18 +22,24 @@ export const BranchProvider = ({ children }) => {
2222 useEffect ( ( ) => {
2323 if ( user ) {
2424 console . log ( '🔍 BranchContext - User logged in:' , user . username ) ;
25+ console . log ( '🔍 BranchContext - User role:' , user . role , '(type:' , typeof user . role , ')' ) ;
26+ console . log ( '🔍 BranchContext - User branch_id:' , user . branch_id , '(type:' , typeof user . branch_id , ')' ) ;
27+ console . log ( '🔍 BranchContext - Is Admin check:' , user . role === 'Admin' , user . role === 'admin' ) ;
2528
2629 // Check if user has a branch_id (could be string or number)
2730 const hasBranchId = user . branch_id !== null && user . branch_id !== undefined && user . branch_id !== '' ;
2831
29- // If user is not Admin and has a branch_id, lock them to their branch
32+ // CRITICAL: Admin should NEVER be locked, even if they have a branch_id
3033 if ( user . role !== 'Admin' && hasBranchId ) {
3134 console . log ( `🔒 User locked to branch ${ user . branch_id } ` ) ;
3235 // Convert to number if it's a string
3336 const branchId = typeof user . branch_id === 'string' ? parseInt ( user . branch_id , 10 ) : user . branch_id ;
3437 setSelectedBranchId ( branchId ) ;
3538 } else if ( user . role === 'Admin' ) {
36- console . log ( '👑 Admin user - can access all branches' ) ;
39+ console . log ( '👑 Admin user - can access all branches (branch_id ignored)' ) ;
40+ setSelectedBranchId ( 'All' ) ;
41+ } else {
42+ console . log ( '🌐 User has no branch restriction - showing all branches' ) ;
3743 setSelectedBranchId ( 'All' ) ;
3844 }
3945 } else {
@@ -70,6 +76,13 @@ export const BranchProvider = ({ children }) => {
7076
7177 // Prevent non-admin users from changing branch
7278 const handleSetSelectedBranchId = ( branchId ) => {
79+ // CRITICAL FIX: Admin should ALWAYS be able to change branch
80+ if ( user && user . role === 'Admin' ) {
81+ console . log ( '👑 Admin changing branch to:' , branchId ) ;
82+ setSelectedBranchId ( branchId ) ;
83+ return ;
84+ }
85+
7386 const hasBranchId = user ?. branch_id !== null && user ?. branch_id !== undefined && user ?. branch_id !== '' ;
7487
7588 if ( user && user . role !== 'Admin' && hasBranchId ) {
@@ -81,8 +94,9 @@ export const BranchProvider = ({ children }) => {
8194 } ;
8295
8396 // Calculate isLocked based on current user state
97+ // CRITICAL FIX: Admin should NEVER be locked, regardless of branch_id
8498 const hasBranchId = user ?. branch_id !== null && user ?. branch_id !== undefined && user ?. branch_id !== '' ;
85- const isLocked = user && user . role !== 'Admin' && hasBranchId ;
99+ const isLocked = user && user . role !== 'Admin' && hasBranchId ; // Admin is never locked
86100 const isNotAdmin = user ? user . role !== 'Admin' : false ;
87101
88102 // Find the selected branch with proper type comparison
0 commit comments