All files / src/stores roles.ts

97.59% Statements 81/83
88.37% Branches 38/43
100% Functions 13/13
97.5% Lines 78/80

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                                                              8x 139x 139x 139x 139x 139x 139x 139x 139x 139x     9x 2x     7x 7x   7x 4x 4x 4x     7x 7x 7x 3x     7x 6x     6x 1x 1x       1x   5x 2x   3x   5x 5x     1x 1x 1x   1x   7x         1x                     2x 2x 2x                       2x 2x 2x 1x   2x       1x 3x       1x 1x       9x 5x 5x 5x     9x 2x     7x 7x 7x 7x 3x     7x 6x     6x 1x 1x       1x   5x 2x   3x   5x 5x     6x         1x       1x 2x     139x                                          
import { defineStore } from 'pinia'
import { ref } from 'vue'
import api from '@/services/api'
 
export interface Role {
  PK: string
  SK: string
  tenantId: string
  roleId: string
  name: string | Record<string, string>
  description: string | Record<string, string>
  permissions: string[]
  isSystemRole: boolean
  createdBy: string
  createdAt: string
  updatedAt: string
}
 
export interface Invitation {
  PK: string
  SK: string
  userId: string
  tenantId: string
  roleIds: string[]
  status: string
  invitedBy: string
  joinedAt: string
  tenantName?: string
  tenantSlug?: string
}
 
export const useRolesStore = defineStore('roles', () => {
  const roles = ref<Role[]>([])
  const invitations = ref<Invitation[]>([])
  const loading = ref(false)
  const error = ref<string | null>(null)
  const nextToken = ref<string | null>(null)
  const hasMore = ref(true)
  const invitationsNextToken = ref<string | null>(null)
  const invitationsHasMore = ref(true)
  const invitationsLoading = ref(false)
 
  async function fetchRoles(tenantId: string, reset = true) {
    if (loading.value || (!reset && !hasMore.value)) {
      return
    }
 
    loading.value = true
    error.value = null
 
    if (reset) {
      roles.value = []
      nextToken.value = null
      hasMore.value = true
    }
 
    try {
      const params: any = { limit: 30 }
      if (!reset && nextToken.value) {
        params.nextToken = nextToken.value
      }
 
      const response = await api.get(`/v1/tenants/${tenantId}/roles`, { params })
      const data = response.data
 
      // Handle both old format (array) and new format (object with items)
      if (Array.isArray(data)) {
        if (reset) {
          roles.value = data
        } else E{
          roles.value = [...roles.value, ...data]
        }
        hasMore.value = false
      } else {
        if (reset) {
          roles.value = data.items
        } else {
          roles.value = [...roles.value, ...data.items]
        }
        nextToken.value = data.nextToken
        hasMore.value = data.hasMore
      }
    } catch (err: any) {
      error.value = err.response?.data?.error || err.message || 'Failed to fetch roles'
      Eif (reset) {
        roles.value = []
      }
      throw err
    } finally {
      loading.value = false
    }
  }
 
  async function loadMoreRoles(tenantId: string) {
    await fetchRoles(tenantId, false)
  }
 
  async function createRole(
    tenantId: string,
    data: {
      name: string | Record<string, string>
      description?: string | Record<string, string>
      permissions: string[]
    }
  ) {
    const response = await api.post(`/v1/tenants/${tenantId}/roles`, data)
    roles.value.push(response.data)
    return response.data
  }
 
  async function updateRole(
    tenantId: string,
    roleId: string,
    data: {
      name?: string | Record<string, string>
      description?: string | Record<string, string>
      permissions?: string[]
    }
  ) {
    const response = await api.put(`/v1/tenants/${tenantId}/roles/${roleId}`, data)
    const index = roles.value.findIndex((r) => r.roleId === roleId)
    if (index !== -1) {
      roles.value[index] = response.data
    }
    return response.data
  }
 
  async function deleteRole(tenantId: string, roleId: string) {
    await api.delete(`/v1/tenants/${tenantId}/roles/${roleId}`)
    roles.value = roles.value.filter((r) => r.roleId !== roleId)
  }
 
  async function assignRoles(tenantId: string, userId: string, roleIds: string[]) {
    const response = await api.post(`/v1/tenants/${tenantId}/members/${userId}/roles`, { roleIds })
    return response.data
  }
 
  async function fetchInvitations(reset = true) {
    if (reset) {
      invitations.value = []
      invitationsNextToken.value = null
      invitationsHasMore.value = true
    }
 
    if (invitationsLoading.value || (!reset && !invitationsHasMore.value)) {
      return
    }
 
    invitationsLoading.value = true
    try {
      const params: any = { limit: 30 }
      if (!reset && invitationsNextToken.value) {
        params.nextToken = invitationsNextToken.value
      }
 
      const response = await api.get('/v1/invitations', { params })
      const data = response.data
 
      // Handle both old format (array) and new format (object with items)
      if (Array.isArray(data)) {
        if (reset) {
          invitations.value = data
        } else E{
          invitations.value = [...invitations.value, ...data]
        }
        invitationsHasMore.value = false
      } else {
        if (reset) {
          invitations.value = data.items
        } else {
          invitations.value = [...invitations.value, ...data.items]
        }
        invitationsNextToken.value = data.nextToken
        invitationsHasMore.value = data.hasMore
      }
    } finally {
      invitationsLoading.value = false
    }
  }
 
  async function loadMoreInvitations() {
    await fetchInvitations(false)
  }
 
  async function declineInvitation(tenantId: string) {
    await api.delete(`/v1/invitations/${tenantId}`)
    invitations.value = invitations.value.filter((inv) => inv.tenantId !== tenantId)
  }
 
  return {
    roles,
    invitations,
    loading,
    error,
    nextToken,
    hasMore,
    invitationsNextToken,
    invitationsHasMore,
    invitationsLoading,
    fetchRoles,
    loadMoreRoles,
    createRole,
    updateRole,
    deleteRole,
    assignRoles,
    fetchInvitations,
    loadMoreInvitations,
    declineInvitation
  }
})