refactor: enhance role management UI and integrate profile management features

- Introduced a modal for managing user profiles, allowing for the creation and editing of profiles with improved state management.
- Updated the role filtering logic to enhance type safety and readability.
- Refactored UI components for better user experience, including improved button states and loading indicators.
- Removed outdated code related to permissions and streamlined the overall structure for maintainability.
This commit is contained in:
2025-11-03 15:14:33 -03:00
parent c1d9958c9f
commit 0d011b8f42
38 changed files with 2664 additions and 4919 deletions

View File

@@ -2,7 +2,82 @@
import { useConvexClient, useQuery } from "convex-svelte";
import { api } from "@sgse-app/backend/convex/_generated/api";
import { authStore } from "$lib/stores/auth.svelte";
import ProtectedRoute from "$lib/components/ProtectedRoute.svelte";
import { goto } from "$app/navigation";
import type { Id, Doc } from "@sgse-app/backend/convex/_generated/dataModel";
// Tipos baseados nos retornos das queries do backend
type Usuario = {
_id: Id<"usuarios">;
matricula: string;
nome: string;
email: string;
ativo: boolean;
bloqueado?: boolean;
motivoBloqueio?: string;
primeiroAcesso: boolean;
ultimoAcesso?: number;
criadoEm: number;
role: {
_id: Id<"roles">;
_creationTime?: number;
criadoPor?: Id<"usuarios">;
customizado?: boolean;
descricao: string;
editavel?: boolean;
nome: string;
nivel: number;
setor?: string;
erro?: boolean;
};
funcionario?: {
_id: Id<"funcionarios">;
nome: string;
matricula?: string;
descricaoCargo?: string;
simboloTipo: "cargo_comissionado" | "funcao_gratificada";
};
avisos?: Array<{
tipo: "erro" | "aviso" | "info";
mensagem: string;
}>;
};
type Funcionario = {
_id: Id<"funcionarios">;
nome: string;
matricula?: string;
cpf?: string;
rg?: string;
nascimento?: string;
email?: string;
telefone?: string;
endereco?: string;
cep?: string;
cidade?: string;
uf?: string;
simboloId: Id<"simbolos">;
simboloTipo: "cargo_comissionado" | "funcao_gratificada";
admissaoData?: string;
desligamentoData?: string;
descricaoCargo?: string;
};
type Gestor = Doc<"usuarios"> | null;
type TimeComDetalhes = Doc<"times"> & {
gestor: Gestor;
totalMembros: number;
};
type MembroTime = Doc<"timesMembros"> & {
funcionario: Doc<"funcionarios"> | null;
};
type TimeComMembros = Doc<"times"> & {
gestor: Gestor;
membros: MembroTime[];
};
const client = useConvexClient();
@@ -11,17 +86,23 @@
const usuariosQuery = useQuery(api.usuarios.listar, {});
const funcionariosQuery = useQuery(api.funcionarios.getAll, {});
const times = $derived(timesQuery?.data || []);
const usuarios = $derived(usuariosQuery?.data || []);
const funcionarios = $derived(funcionariosQuery?.data || []);
const times = $derived((timesQuery?.data || []) as TimeComDetalhes[]);
const usuarios = $derived((usuariosQuery?.data || []) as Usuario[]);
const funcionarios = $derived((funcionariosQuery?.data || []) as Funcionario[]);
const carregando = $derived(
timesQuery === undefined ||
usuariosQuery === undefined ||
funcionariosQuery === undefined
);
// Estados
let modoEdicao = $state(false);
let timeEmEdicao = $state<any>(null);
let timeEmEdicao = $state<TimeComDetalhes | null>(null);
let mostrarModalMembros = $state(false);
let timeParaMembros = $state<any>(null);
let timeParaMembros = $state<TimeComMembros | null>(null);
let mostrarConfirmacaoExclusao = $state(false);
let timeParaExcluir = $state<any>(null);
let timeParaExcluir = $state<TimeComDetalhes | null>(null);
let processando = $state(false);
// Form
@@ -32,9 +113,9 @@
// Membros
let membrosDisponiveis = $derived(
funcionarios.filter((f: any) => {
funcionarios.filter((f: Funcionario) => {
// Verificar se o funcionário já está em algum time ativo
const jaNaEquipe = timeParaMembros?.membros?.some((m: any) => m.funcionario?._id === f._id);
const jaNaEquipe = timeParaMembros?.membros?.some((m: MembroTime) => m.funcionario?._id === f._id);
return !jaNaEquipe;
})
);
@@ -60,7 +141,7 @@
formCor = coresDisponiveis[Math.floor(Math.random() * coresDisponiveis.length)];
}
function editarTime(time: any) {
function editarTime(time: TimeComDetalhes) {
modoEdicao = true;
timeEmEdicao = time;
formNome = time.nome;
@@ -91,26 +172,27 @@
id: timeEmEdicao._id,
nome: formNome,
descricao: formDescricao || undefined,
gestorId: formGestorId as any,
gestorId: formGestorId as Id<"usuarios">,
cor: formCor,
});
} else {
await client.mutation(api.times.criar, {
nome: formNome,
descricao: formDescricao || undefined,
gestorId: formGestorId as any,
gestorId: formGestorId as Id<"usuarios">,
cor: formCor,
});
}
cancelarEdicao();
} catch (e: any) {
alert("Erro ao salvar: " + (e.message || e));
} catch (e: unknown) {
const errorMessage = e instanceof Error ? e.message : String(e);
alert("Erro ao salvar: " + errorMessage);
} finally {
processando = false;
}
}
function confirmarExclusao(time: any) {
function confirmarExclusao(time: TimeComDetalhes) {
timeParaExcluir = time;
mostrarConfirmacaoExclusao = true;
}
@@ -123,17 +205,20 @@
await client.mutation(api.times.desativar, { id: timeParaExcluir._id });
mostrarConfirmacaoExclusao = false;
timeParaExcluir = null;
} catch (e: any) {
alert("Erro ao excluir: " + (e.message || e));
} catch (e: unknown) {
const errorMessage = e instanceof Error ? e.message : String(e);
alert("Erro ao excluir: " + errorMessage);
} finally {
processando = false;
}
}
async function abrirGerenciarMembros(time: any) {
async function abrirGerenciarMembros(time: TimeComDetalhes) {
const detalhes = await client.query(api.times.obterPorId, { id: time._id });
timeParaMembros = detalhes;
mostrarModalMembros = true;
if (detalhes) {
timeParaMembros = detalhes as TimeComMembros;
mostrarModalMembros = true;
}
}
async function adicionarMembro(funcionarioId: string) {
@@ -143,14 +228,17 @@
try {
await client.mutation(api.times.adicionarMembro, {
timeId: timeParaMembros._id,
funcionarioId: funcionarioId as any,
funcionarioId: funcionarioId as Id<"funcionarios">,
});
// Recarregar detalhes do time
const detalhes = await client.query(api.times.obterPorId, { id: timeParaMembros._id });
timeParaMembros = detalhes;
} catch (e: any) {
alert("Erro: " + (e.message || e));
if (detalhes) {
timeParaMembros = detalhes as TimeComMembros;
}
} catch (e: unknown) {
const errorMessage = e instanceof Error ? e.message : String(e);
alert("Erro: " + errorMessage);
} finally {
processando = false;
}
@@ -161,13 +249,18 @@
processando = true;
try {
await client.mutation(api.times.removerMembro, { membroId: membroId as any });
await client.mutation(api.times.removerMembro, { membroId: membroId as Id<"timesMembros"> });
// Recarregar detalhes do time
const detalhes = await client.query(api.times.obterPorId, { id: timeParaMembros._id });
timeParaMembros = detalhes;
} catch (e: any) {
alert("Erro: " + (e.message || e));
if (timeParaMembros) {
const detalhes = await client.query(api.times.obterPorId, { id: timeParaMembros._id });
if (detalhes) {
timeParaMembros = detalhes as TimeComMembros;
}
}
} catch (e: unknown) {
const errorMessage = e instanceof Error ? e.message : String(e);
alert("Erro: " + errorMessage);
} finally {
processando = false;
}
@@ -179,7 +272,8 @@
}
</script>
<main class="container mx-auto px-4 py-6 max-w-7xl">
<ProtectedRoute allowedRoles={["ti_master", "admin", "ti_usuario"]} maxLevel={2}>
<main class="container mx-auto px-4 py-6 max-w-7xl">
<!-- Breadcrumb -->
<div class="text-sm breadcrumbs mb-4">
<ul>
@@ -192,14 +286,14 @@
<div class="mb-6">
<div class="flex items-center justify-between">
<div class="flex items-center gap-4">
<div class="p-3 bg-blue-500/20 rounded-xl">
<svg xmlns="http://www.w3.org/2000/svg" class="h-8 w-8 text-blue-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<div class="p-3 bg-secondary/10 rounded-xl">
<svg xmlns="http://www.w3.org/2000/svg" class="h-8 w-8 text-secondary" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z" />
</svg>
</div>
<div>
<h1 class="text-3xl font-bold text-primary">Gestão de Times</h1>
<p class="text-base-content/70">Organize funcionários em equipes e defina gestores</p>
<h1 class="text-3xl font-bold text-base-content">Gestão de Times</h1>
<p class="text-base-content/60 mt-1">Organize funcionários em equipes e defina gestores</p>
</div>
</div>
<div class="flex gap-2">
@@ -297,20 +391,27 @@
{/if}
<!-- Lista de Times -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
{#each times as time}
{#if time.ativo}
<div class="card bg-base-100 shadow-xl hover:shadow-2xl transition-all border-l-4" style="border-color: {time.cor}">
{#if carregando}
<div class="flex justify-center items-center py-20">
<span class="loading loading-spinner loading-lg text-primary"></span>
</div>
{:else}
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
{#each times.filter((t: TimeComDetalhes) => t.ativo) as time}
<div class="card bg-base-100 shadow-xl hover:shadow-2xl transition-all border-l-4" style="border-color: {time.cor || '#3B82F6'}">
<div class="card-body">
<div class="flex items-start justify-between">
<h2 class="card-title text-lg">{time.nome}</h2>
<div class="flex items-start justify-between mb-2">
<div class="flex items-center gap-2">
<div class="w-3 h-3 rounded-full" style="background-color: {time.cor || '#3B82F6'}"></div>
<h2 class="card-title text-lg">{time.nome}</h2>
</div>
<div class="dropdown dropdown-end">
<button tabindex="0" class="btn btn-ghost btn-sm btn-square" aria-label="Menu do time">
<button type="button" class="btn btn-ghost btn-sm btn-square" aria-label="Menu do time">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 5v.01M12 12v.01M12 19v.01M12 6a1 1 0 110-2 1 1 0 010 2zm0 7a1 1 0 110-2 1 1 0 010 2zm0 7a1 1 0 110-2 1 1 0 010 2z" />
</svg>
</button>
<ul tabindex="0" class="dropdown-content z-[1] menu p-2 shadow-xl bg-base-100 rounded-box w-52 border border-base-300">
<ul role="menu" class="dropdown-content z-[1] menu p-2 shadow-xl bg-base-100 rounded-box w-52 border border-base-300">
<li>
<button type="button" onclick={() => editarTime(time)}>
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
@@ -339,7 +440,7 @@
</div>
</div>
<p class="text-sm text-base-content/70 mb-3">{time.descricao || "Sem descrição"}</p>
<p class="text-sm text-base-content/70 mb-3 min-h-[2rem]">{time.descricao || "Sem descrição"}</p>
<div class="divider my-2"></div>
@@ -348,31 +449,38 @@
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 text-primary" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5.121 17.804A13.937 13.937 0 0112 16c2.5 0 4.847.655 6.879 1.804M15 10a3 3 0 11-6 0 3 3 0 016 0zm6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
<span><strong>Gestor:</strong> {time.gestor?.nome}</span>
<span class="text-base-content/70"><strong>Gestor:</strong> {time.gestor?.nome || "Não definido"}</span>
</div>
<div class="flex items-center gap-2 text-sm">
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 text-primary" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z" />
</svg>
<span><strong>Membros:</strong> {time.totalMembros || 0}</span>
<span class="text-base-content/70"><strong>Membros:</strong> <span class="badge badge-primary badge-sm">{time.totalMembros || 0}</span></span>
</div>
</div>
<div class="card-actions justify-end mt-4">
<button class="btn btn-sm btn-outline btn-primary" onclick={() => abrirGerenciarMembros(time)}>
Ver Detalhes
</button>
</div>
</div>
</div>
{/if}
{/each}
{/each}
{#if times.filter((t: any) => t.ativo).length === 0}
<div class="col-span-full">
<div class="alert">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="stroke-info shrink-0 w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
</svg>
<span>Nenhum time cadastrado. Clique em "Novo Time" para começar.</span>
{#if times.filter((t: TimeComDetalhes) => t.ativo).length === 0}
<div class="col-span-full">
<div class="flex flex-col items-center justify-center py-16 text-center">
<svg xmlns="http://www.w3.org/2000/svg" class="h-16 w-16 text-base-content/30" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z" />
</svg>
<h3 class="text-xl font-semibold mt-4">Nenhum time cadastrado</h3>
<p class="text-base-content/60 mt-2">Clique em "Novo Time" para criar seu primeiro time</p>
</div>
</div>
</div>
{/if}
</div>
{/if}
</div>
{/if}
<!-- Modal de Gerenciar Membros -->
{#if mostrarModalMembros && timeParaMembros}
@@ -502,4 +610,5 @@
</dialog>
{/if}
</main>
</ProtectedRoute>