All files / src/views RolesPage.vue

95.06% Statements 77/81
86.11% Branches 62/72
78.94% Functions 15/19
94.87% Lines 74/78

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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282  26x               1x 49x                                 49x   1x 1x   1x   1x       3x           1x   1x 1x 1x                 1x   1x         1x 1x                                   3x                       3x     1x 1x   3x           1x 1x 1x 3x                               1x                                                   26x 26x 26x 26x 26x 26x   26x 26x 26x                 26x 26x     26x   26x   26x 26x 23x 23x     1x           2x 2x         2x       3x 3x                 3x       1x 1x 1x               7x   6x 6x   6x 6x 2x   4x     2x   4x 4x   6x         3x   2x 2x 2x 2x   1x   2x         1x 1x                                        
<template>
  <PageLayout :title="t('navigation.roles')" class="roles-page">
    <template #action>
      <button
        v-if="authStore.currentFlow && hasPermission('roles:create')"
        class="btn btn-success"
        @click="createNewRole"
      >
        <font-awesome-icon icon="fa-solid fa-plus" class="me-md-2" />
        <span class="d-none d-md-inline">{{ t('role.createNew') }}</span>
      </button>
    </template>
 
    <!-- Role Form Sidebar -->
    <RoleFormSidebar
      :is-open="showRoleDialog"
      :mode="editingRole ? 'edit' : 'create'"
      :form-data="roleForm"
      :default-language="defaultLanguage"
      :enabled-languages="enabledLanguages"
      :current-language="currentFormLanguage"
      :loading="formLoading"
      :error="formError || ''"
      @update:form-data="roleForm = $event"
      @update:current-language="currentFormLanguage = $event"
      @close="cancelRoleForm"
      @submit="saveRole"
    />
 
    <div class="page-header-spacing">
      <div class="container-fluid">
        <div v-if="!authStore.currentFlow" class="row">
          <div class="col-12">
            <div class="alert alert-warning shadow-sm" role="alert">
              <h5 class="alert-heading">{{ t('flow.noActiveFlow') }}</h5>
              <p>{{ t('flow.selectFlowFirst') }}</p>
              <router-link to="/flows" class="btn btn-success">{{
                t('flow.goToFlows')
              }}</router-link>
            </div>
          </div>
        </div>
 
        <div v-if="authStore.currentFlow" class="page-header-spacing">
          <div class="container-fluid">
            <div v-if="error" class="row mb-4">
              <div class="col-12">
                <div class="alert alert-danger">
                  <h5 class="alert-heading">
                    <font-awesome-icon icon="fa-solid fa-exclamation-triangle" class="me-2" />
                    {{ t('common.accessDenied') }}
                  </h5>
                  <p>{{ error }}</p>
                </div>
              </div>
            </div>
            <div v-else-if="rolesStore.loading" class="row">
              <div class="col-12 text-center py-4">
                <div class="spinner-border text-primary" role="status">
                  <span class="visually-hidden">{{ t('common.loading') }}</span>
                </div>
              </div>
            </div>
            <div v-else-if="rolesStore.roles.length === 0" class="row">
              <div class="col-12 text-center py-4">
                <div class="alert alert-info">
                  <p>{{ t('role.noRoles') }}</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
 
    <!-- Roles List -->
    <VirtualCardGrid
      v-if="authStore.currentFlow && !rolesStore.loading && rolesStore.roles.length > 0"
      :items="rolesStore.roles"
      :has-more="rolesStore.hasMore"
      :is-loading="rolesStore.loading"
      @load-more="loadMoreRolesSafe"
    >
      <template #card="{ item: role }">
        <UnifiedContentCard
          :clickable="!role.isSystemRole && hasPermission('roles:update')"
          @click="editRole(role)"
        >
          <template v-if="!role.isSystemRole && hasPermission('roles:delete')" #actions>
            <button
              class="btn btn-sm btn-danger action-icon"
              title="Delete"
              @click.stop="deleteRoleConfirm(role)"
            >
              <font-awesome-icon icon="fa-solid fa-trash" />
            </button>
          </template>
 
          <div class="d-flex justify-content-between align-items-start mb-2">
            <h5 class="card-title mb-0">{{ getTranslation(role.name) }}</h5>
            <span v-if="role.isSystemRole" class="badge bg-warning text-dark">System</span>
          </div>
 
          <p v-if="role.description" class="card-text text-muted small">
            {{ getTranslation(role.description) }}
          </p>
 
          <div class="mb-3">
            <h6 class="small text-muted mb-1">Permissions ({{ role.permissions?.length || 0 }})</h6>
            <div class="d-flex flex-wrap gap-1">
              <code
                v-for="permission in role.permissions?.slice(0, 3)"
                :key="permission"
                class="small bg-light p-1 rounded"
              >
                {{ permission }}
              </code>
              <span
                v-if="(role.permissions?.length || 0) > 3"
                class="badge bg-light text-dark small"
              >
                +{{ (role.permissions?.length || 0) - 3 }} more
              </span>
            </div>
          </div>
 
          <div class="small text-muted mb-2">
            <div>
              Created: {{ role.createdAt ? new Date(role.createdAt).toLocaleDateString() : '' }}
            </div>
            <div>By: {{ role.createdByName || role.createdBy }}</div>
          </div>
        </UnifiedContentCard>
      </template>
    </VirtualCardGrid>
  </PageLayout>
</template>
 
<script setup lang="ts">
// @implements UC-RBAC-001.1, UC-RBAC-003.1, UC-RBAC-004.1, UC-RBAC-005.1, UC-RBAC-006.1
import PageLayout from '@/components/PageLayout.vue'
import RoleFormSidebar from '@/components/RoleFormSidebar.vue'
import VirtualCardGrid from '@/components/VirtualCardGrid.vue'
import UnifiedContentCard from '@/components/UnifiedContentCard.vue'
import { ref, onMounted, computed } from 'vue'
import { useRolesStore } from '@/stores/roles'
import { useAuthStore } from '@/stores/auth'
import { useLanguage } from '@/composables/useLanguage'
import { useUILanguage } from '@/composables/useUILanguage'
import { usePermissions } from '@/composables/usePermissions'
import { storeToRefs } from 'pinia'
 
const rolesStore = useRolesStore()
const authStore = useAuthStore()
const { getTranslation, defaultLanguage, enabledLanguages } = useLanguage()
const { t } = useUILanguage()
const { hasPermission } = usePermissions()
const { error } = storeToRefs(rolesStore)
 
const showRoleDialog = ref(false)
const editingRole = ref<any>(null)
const roleForm = ref<{
  name: string | Record<string, string>
  description: string | Record<string, string>
  permissions: string[]
}>({
  name: { en: '', fr: '' },
  description: { en: '', fr: '' },
  permissions: [] as string[]
})
const formLoading = ref(false)
const formError = ref('')
 
// Global language selector for multilingual fields
const currentFormLanguage = ref(defaultLanguage.value)
 
const currentFlowId = computed(() => authStore.currentFlow || '')
 
onMounted(async () => {
  if (currentFlowId.value) {
    try {
      await rolesStore.fetchRoles(currentFlowId.value)
    } catch (err) {
      // Error is already stored in the store
      console.error('Failed to fetch roles:', err)
    }
  }
})
 
function createNewRole() {
  editingRole.value = null
  roleForm.value = {
    name: { en: '', fr: '' },
    description: { en: '', fr: '' },
    permissions: []
  }
  showRoleDialog.value = true
}
 
function editRole(role: any) {
  editingRole.value = role
  roleForm.value = {
    name:
      typeof role.name === 'string' ? { en: role.name, fr: '' } : role.name || { en: '', fr: '' },
    description:
      typeof role.description === 'string'
        ? { en: role.description, fr: '' }
        : role.description || { en: '', fr: '' },
    permissions: [...role.permissions]
  }
  showRoleDialog.value = true
}
 
function cancelRoleForm() {
  showRoleDialog.value = false
  editingRole.value = null
  roleForm.value = {
    name: { en: '', fr: '' },
    description: { en: '', fr: '' },
    permissions: []
  }
}
 
async function saveRole() {
  if (!currentFlowId.value) return
 
  formLoading.value = true
  formError.value = ''
 
  try {
    if (editingRole.value) {
      await rolesStore.updateRole(currentFlowId.value, editingRole.value.roleId, roleForm.value)
    } else {
      await rolesStore.createRole(currentFlowId.value, roleForm.value)
    }
 
    showRoleDialog.value = false
  } catch (error: any) {
    console.error('Error saving role:', error)
    formError.value = error.response?.data?.error || error.message || 'Failed to save role'
  } finally {
    formLoading.value = false
  }
}
 
async function deleteRoleConfirm(role: any) {
  if (!confirm(t('common.confirmDeleteRole').replace('{name}', getTranslation(role.name)))) return
 
  formLoading.value = true
  formError.value = ''
  try {
    await rolesStore.deleteRole(currentFlowId.value, role.roleId)
  } catch (error: any) {
    formError.value = error.response?.data?.error || 'Failed to delete role'
  } finally {
    formLoading.value = false
  }
}
 
function loadMoreRolesSafe() {
  Eif (currentFlowId.value) {
    rolesStore.loadMoreRoles(currentFlowId.value)
  }
}
</script>
 
<style scoped>
.roles-page :deep(.page-content) {
  padding: 0 !important;
}
 
.page-header-spacing {
  padding: 0 2rem;
}
 
@media (max-width: 767px) {
  .page-header-spacing {
    padding: 0 1rem;
  }
}
</style>