All files / src/utils structurePermissions.ts

96.07% Statements 49/51
86.66% Branches 26/30
100% Functions 11/11
100% Lines 43/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125                              2x 49x     49x 232x 232x 116x   232x 232x       49x 122x 48x 41x 40x 40x       49x 116x   116x   35x 65x                       157x 167x   81x   29x                     29x 29x 34x 34x 34x 34x   34x 32x   2x 1x 1x     29x 56x                     52x 82x                         49x           2x 3x 1x   2x  
import type { Permission } from '../types'
 
export interface StructuredOption {
  label: string
  value: string
  isParent?: boolean
  indent?: number
  category?: string
  description?: string
}
 
/**
 * Structure permissions for hierarchical display in select
 * Groups permissions by category and creates parent-child relationships
 */
export function structurePermissions(permissions: Permission[]): StructuredOption[] {
  const structured: StructuredOption[] = []
  
  // Group permissions by category
  const grouped = permissions.reduce((acc, perm) => {
    const category = perm.category || 'Other'
    if (!acc[category]) {
      acc[category] = []
    }
    acc[category].push(perm)
    return acc
  }, {} as Record<string, Permission[]>)
 
  // Order categories: Special first, Other second, then everything else alphabetically
  const categoryOrder = Object.keys(grouped).sort((a, b) => {
    if (a === 'Special') return -1
    if (b === 'Special') return 1
    if (a === 'Other') return -1
    Iif (b === 'Other') return 1
    return a.localeCompare(b)
  })
 
  // Process each category
  categoryOrder.forEach(category => {
    const categoryPerms = grouped[category]
    
    if (category === 'Special') {
      // Special permissions are flat (no hierarchy)
      categoryPerms.forEach(perm => {
        structured.push({
          label: perm.label,
          value: perm.value,
          category,
          description: perm.description,
          isParent: false,
          indent: 0
        })
      })
    } else {
      // Collection permissions have hierarchy
      // Find the wildcard permission (parent)
      const wildcardPerm = categoryPerms.find(p => p.value.endsWith('.*'))
      const otherPerms = categoryPerms.filter(p => !p.value.endsWith('.*'))
      
      if (wildcardPerm) {
        // Add parent
        structured.push({
          label: wildcardPerm.label,
          value: wildcardPerm.value,
          category,
          description: wildcardPerm.description,
          isParent: true,
          indent: 0
        })
        
        // Add children with indentation
        // Order: manage, create, read, update, delete, others (CRUD order)
        const operationOrder = ['manage', 'create', 'read', 'update', 'delete']
        const sortedPerms = otherPerms.sort((a, b) => {
          const aOp = a.value.split('.').pop() || ''
          const bOp = b.value.split('.').pop() || ''
          const aIndex = operationOrder.indexOf(aOp)
          const bIndex = operationOrder.indexOf(bOp)
          
          if (aIndex !== -1 && bIndex !== -1) {
            return aIndex - bIndex
          }
          if (aIndex !== -1) return -1
          Iif (bIndex !== -1) return 1
          return aOp.localeCompare(bOp)
        })
        
        sortedPerms.forEach(perm => {
          structured.push({
            label: perm.label,
            value: perm.value,
            category,
            description: perm.description,
            isParent: false,
            indent: 1
          })
        })
      } else {
        // No wildcard, add all as flat
        otherPerms.forEach(perm => {
          structured.push({
            label: perm.label,
            value: perm.value,
            category,
            description: perm.description,
            isParent: false,
            indent: 0
          })
        })
      }
    }
  })
 
  return structured
}
 
/**
 * Get display label with visual hierarchy indicators
 */
export function getDisplayLabel(option: StructuredOption): string {
  if (option.indent === 1) {
    return `  └─ ${option.label}`
  }
  return option.label
}