agregar modulo usuarios con AG Grid + tema shadcn + integracion KAPPA employees
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import { kappa } from '@/services/kappa-api'
|
||||
import type { KappaEmployee } from '@/types/kappa'
|
||||
|
||||
export interface AlphaUser {
|
||||
id: number
|
||||
email: string
|
||||
first_name: string
|
||||
last_name: string
|
||||
full_name: string
|
||||
is_active: boolean
|
||||
cell?: string
|
||||
role?: string
|
||||
seniority?: string
|
||||
projects: string[]
|
||||
projects_count: number
|
||||
employee_ref?: KappaEmployee
|
||||
}
|
||||
|
||||
export const useUsersStore = defineStore('users', () => {
|
||||
const users = ref<AlphaUser[]>([])
|
||||
const employees = ref<KappaEmployee[]>([])
|
||||
const loading = ref(false)
|
||||
const error = ref<string | null>(null)
|
||||
const totalPages = ref(1)
|
||||
const currentPage = ref(1)
|
||||
|
||||
const activeUsers = computed(() => users.value.filter(u => u.is_active))
|
||||
const devsByProject = computed(() => {
|
||||
const map = new Map<string, AlphaUser[]>()
|
||||
for (const u of users.value) {
|
||||
for (const p of u.projects) {
|
||||
if (!map.has(p)) map.set(p, [])
|
||||
map.get(p)!.push(u)
|
||||
}
|
||||
}
|
||||
return map
|
||||
})
|
||||
|
||||
async function fetchAll() {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const [rawUsers, page1] = await Promise.all([
|
||||
kappa.getUsers(),
|
||||
kappa.getEmployees(1),
|
||||
])
|
||||
|
||||
employees.value = page1.results
|
||||
|
||||
totalPages.value = Math.ceil(page1.count / 25)
|
||||
if (totalPages.value > 1) {
|
||||
const restPages = []
|
||||
for (let p = 2; p <= totalPages.value; p++) {
|
||||
restPages.push(kappa.getEmployees(p))
|
||||
}
|
||||
const rest = await Promise.all(restPages)
|
||||
for (const r of rest) {
|
||||
employees.value.push(...r.results)
|
||||
}
|
||||
}
|
||||
|
||||
users.value = buildUsers(rawUsers, employees.value)
|
||||
} catch (e: any) {
|
||||
error.value = e.message
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function buildUsers(raw: unknown[], emps: KappaEmployee[]): AlphaUser[] {
|
||||
return raw.map((u: any) => {
|
||||
const userEmps = emps.filter(e => e.user === u.id)
|
||||
|
||||
return {
|
||||
id: u.id,
|
||||
email: u.email || '',
|
||||
first_name: u.first_name || '',
|
||||
last_name: u.last_name || '',
|
||||
full_name: u.full_name || `${u.first_name || ''} ${u.last_name || ''}`.trim() || u.email,
|
||||
is_active: u.is_active !== undefined ? u.is_active : true,
|
||||
projects: userEmps
|
||||
.filter(e => e.initiative_name)
|
||||
.map(e => e.initiative_name!),
|
||||
projects_count: userEmps.filter(e => e.initiative).length,
|
||||
employee_ref: userEmps[0] || undefined,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function updateLocal(id: number, data: Partial<AlphaUser>) {
|
||||
const idx = users.value.findIndex(u => u.id === id)
|
||||
if (idx !== -1) {
|
||||
users.value[idx] = { ...users.value[idx], ...data }
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
users,
|
||||
employees,
|
||||
loading,
|
||||
error,
|
||||
totalPages,
|
||||
currentPage,
|
||||
activeUsers,
|
||||
devsByProject,
|
||||
fetchAll,
|
||||
updateLocal,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user