All files / src/views RolesPage.vue

96% Statements 72/75
79.68% Branches 51/64
81.25% Functions 13/16
95.83% Lines 69/72

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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309  26x       1x 49x                             49x     1x   1x   3x           1x   1x 1x 1x                 1x   1x         1x 1x                               3x                               1x 1x 1x               1x 1x     1x 3x                               1x                                               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.currentTenant" 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"
      :loading="formLoading"
      :error="formError || ''"
      @update:form-data="roleForm = $event"
      @close="cancelRoleForm"
      @submit="saveRole"
    />
 
    <div v-if="!authStore.currentTenant" 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.currentTenant" 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>
 
    <!-- Roles List -->
    <VirtualCardGrid
      v-if="authStore.currentTenant && !rolesStore.loading && rolesStore.roles.length > 0"
      :items="rolesStore.roles"
      :has-more="rolesStore.hasMore"
      :is-loading="rolesStore.loading"
      @load-more="loadMoreRolesSafe"
    >
      <template #card="{ item: role }">
        <div
          class="card h-100 position-relative shadow-sm"
          :class="{ 'card-clickable': !role.isSystemRole }"
          @click="!role.isSystemRole && editRole(role)"
        >
          <!-- Delete Icon (top-right) -->
          <button
            v-if="!role.isSystemRole"
            class="btn btn-sm btn-danger delete-icon"
            title="Delete"
            @click.stop="deleteRoleConfirm(role)"
          >
            <font-awesome-icon icon="fa-solid fa-trash" />
          </button>
 
          <div class="card-body">
            <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">
              {{ 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.createdBy }}</div>
            </div>
          </div>
        </div>
      </template>
    </VirtualCardGrid>
  </PageLayout>
</template>
 
<script setup lang="ts">
import PageLayout from '@/components/PageLayout.vue'
import RoleFormSidebar from '@/components/RoleFormSidebar.vue'
import VirtualCardGrid from '@/components/VirtualCardGrid.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 { storeToRefs } from 'pinia'
 
const rolesStore = useRolesStore()
const authStore = useAuthStore()
const { getTranslation, defaultLanguage, enabledLanguages } = useLanguage()
const { t } = useUILanguage()
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('')
 
const currentTenantId = computed(() => authStore.currentTenant || '')
 
onMounted(async () => {
  if (currentTenantId.value) {
    try {
      await rolesStore.fetchRoles(currentTenantId.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 (!currentTenantId.value) return
 
  formLoading.value = true
  formError.value = ''
 
  try {
    if (editingRole.value) {
      await rolesStore.updateRole(currentTenantId.value, editingRole.value.roleId, roleForm.value)
    } else {
      await rolesStore.createRole(currentTenantId.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(currentTenantId.value, role.roleId)
  } catch (error: any) {
    formError.value = error.response?.data?.error || 'Failed to delete role'
  } finally {
    formLoading.value = false
  }
}
 
function loadMoreRolesSafe() {
  Eif (currentTenantId.value) {
    rolesStore.loadMoreRoles(currentTenantId.value)
  }
}
</script>
 
<style scoped>
.card {
  overflow: hidden;
}
 
.card-clickable {
  cursor: pointer;
  transition:
    transform 0.2s,
    box-shadow 0.2s,
    background-color 0.2s;
}
 
.card-clickable::before {
  opacity: 0 !important;
}
 
.card-clickable:hover {
  transform: scale(1.02);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  background-color: #f8f9fa !important;
}
 
.card-clickable:hover::before {
  opacity: 0 !important;
}
 
.delete-icon {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 10;
  padding: 0.25rem 0.5rem;
  font-size: 0.875rem;
  border-radius: 4px;
  opacity: 0.9;
}
 
.delete-icon:hover {
  opacity: 1;
}
 
.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>