2026-05-14 02:59:54 +00:00
|
|
|
export const DEFAULT_APP_VIEW_ORDER = [
|
|
|
|
|
'overview',
|
|
|
|
|
'workbench',
|
|
|
|
|
'requests',
|
|
|
|
|
'approval',
|
|
|
|
|
'policies',
|
|
|
|
|
'audit',
|
2026-05-15 09:36:16 +00:00
|
|
|
'logs',
|
2026-05-14 02:59:54 +00:00
|
|
|
'employees',
|
|
|
|
|
'settings'
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
const ALWAYS_VISIBLE_VIEWS = new Set(['workbench', 'requests', 'policies'])
|
2026-05-20 14:21:56 +08:00
|
|
|
const VIEW_ROLE_RULES = {
|
|
|
|
|
overview: ['finance', 'executive'],
|
|
|
|
|
approval: ['approver', 'finance', 'executive'],
|
2026-05-18 02:51:25 +00:00
|
|
|
audit: ['auditor', 'finance'],
|
2026-05-15 09:36:16 +00:00
|
|
|
logs: ['manager'],
|
|
|
|
|
employees: ['manager'],
|
|
|
|
|
settings: ['manager']
|
|
|
|
|
}
|
2026-05-21 09:28:33 +08:00
|
|
|
const CLAIM_MANAGER_ROLE_CODES = new Set(['executive'])
|
2026-05-20 21:00:47 +08:00
|
|
|
const CLAIM_RETURN_ROLE_CODES = new Set(['finance', 'executive', 'manager', 'approver'])
|
2026-05-21 09:28:33 +08:00
|
|
|
const CLAIM_LEADER_APPROVAL_ROLE_CODES = new Set(['manager', 'approver'])
|
2026-05-09 05:59:46 +00:00
|
|
|
|
|
|
|
|
function normalizedRoleCodes(user) {
|
|
|
|
|
if (!user) {
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 14:21:56 +08:00
|
|
|
return Array.isArray(user.roleCodes)
|
|
|
|
|
? user.roleCodes.map((item) => String(item || '').trim().toLowerCase()).filter(Boolean)
|
|
|
|
|
: []
|
|
|
|
|
}
|
2026-05-09 05:59:46 +00:00
|
|
|
|
2026-05-18 02:51:25 +00:00
|
|
|
export function isManagerUser(user) {
|
|
|
|
|
return Boolean(user?.isAdmin) || normalizedRoleCodes(user).includes('manager')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isFinanceUser(user) {
|
|
|
|
|
return normalizedRoleCodes(user).includes('finance')
|
|
|
|
|
}
|
2026-05-20 14:21:56 +08:00
|
|
|
|
|
|
|
|
export function isExecutiveUser(user) {
|
|
|
|
|
return normalizedRoleCodes(user).includes('executive')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function canManageExpenseClaims(user) {
|
|
|
|
|
if (Boolean(user?.isAdmin)) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return normalizedRoleCodes(user).some((roleCode) => CLAIM_MANAGER_ROLE_CODES.has(roleCode))
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 21:00:47 +08:00
|
|
|
export function canReturnExpenseClaims(user) {
|
|
|
|
|
if (Boolean(user?.isAdmin)) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return normalizedRoleCodes(user).some((roleCode) => CLAIM_RETURN_ROLE_CODES.has(roleCode))
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-21 09:28:33 +08:00
|
|
|
export function canApproveLeaderExpenseClaims(user) {
|
|
|
|
|
if (Boolean(user?.isAdmin)) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return normalizedRoleCodes(user).some((roleCode) => CLAIM_LEADER_APPROVAL_ROLE_CODES.has(roleCode))
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 14:21:56 +08:00
|
|
|
export function canAccessAppView(user, viewId) {
|
2026-05-09 05:59:46 +00:00
|
|
|
if (!viewId || !user) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isManagerUser(user)) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ALWAYS_VISIBLE_VIEWS.has(viewId)) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const requiredRoles = VIEW_ROLE_RULES[viewId] || []
|
|
|
|
|
const roleCodes = normalizedRoleCodes(user)
|
|
|
|
|
return requiredRoles.some((roleCode) => roleCodes.includes(roleCode))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getAccessibleViewIds(user) {
|
|
|
|
|
return DEFAULT_APP_VIEW_ORDER.filter((viewId) => canAccessAppView(user, viewId))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function filterNavItemsByAccess(navItems, user) {
|
|
|
|
|
return navItems.filter((item) => canAccessAppView(user, item.id))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function resolveDefaultAuthorizedRoute(user) {
|
|
|
|
|
const firstVisibleView = getAccessibleViewIds(user)[0]
|
|
|
|
|
return { name: `app-${firstVisibleView || 'workbench'}` }
|
|
|
|
|
}
|