Files
sgse-app/packages/backend/convex/dashboard.ts
killer-cf dac559d9fd refactor: remove access request functionality and related components
- Deleted the solicitacoesAcesso route and its associated components to streamline the dashboard.
- Updated dashboard stats to remove references to access requests, ensuring accurate data representation.
- Refactored backend queries to eliminate access request data handling, enhancing performance and maintainability.
- Adjusted type definitions to reflect the removal of access request functionalities.
2025-11-19 12:30:42 -03:00

153 lines
4.0 KiB
TypeScript

import { query } from "./_generated/server";
import { v } from "convex/values";
// Obter estatísticas gerais do sistema
export const getStats = query({
args: {},
returns: v.object({
totalFuncionarios: v.number(),
totalSimbolos: v.number(),
funcionariosAtivos: v.number(),
funcionariosDesligados: v.number(),
cargoComissionado: v.number(),
funcaoGratificada: v.number(),
}),
handler: async (ctx) => {
// Contar funcionários
const funcionarios = await ctx.db.query("funcionarios").collect();
const totalFuncionarios = funcionarios.length;
// Funcionários ativos (sem data de desligamento)
const funcionariosAtivos = funcionarios.filter(
(f) => !f.desligamentoData
).length;
// Funcionários desligados
const funcionariosDesligados = funcionarios.filter(
(f) => f.desligamentoData
).length;
// Contar por tipo de símbolo
const cargoComissionado = funcionarios.filter(
(f) => f.simboloTipo === "cargo_comissionado"
).length;
const funcaoGratificada = funcionarios.filter(
(f) => f.simboloTipo === "funcao_gratificada"
).length;
// Contar símbolos
const simbolos = await ctx.db.query("simbolos").collect();
const totalSimbolos = simbolos.length;
return {
totalFuncionarios,
totalSimbolos,
funcionariosAtivos,
funcionariosDesligados,
cargoComissionado,
funcaoGratificada,
};
},
});
// Obter atividades recentes (últimas 24 horas)
export const getRecentActivity = query({
args: {},
returns: v.object({
funcionariosCadastrados24h: v.number(),
simbolosCadastrados24h: v.number(),
}),
handler: async (ctx) => {
const now = Date.now();
const last24h = now - 24 * 60 * 60 * 1000;
// Funcionários cadastrados nas últimas 24h
const funcionarios = await ctx.db.query("funcionarios").collect();
const funcionariosCadastrados24h = funcionarios.filter(
(f) => f._creationTime >= last24h
).length;
// Símbolos cadastrados nas últimas 24h
const simbolos = await ctx.db.query("simbolos").collect();
const simbolosCadastrados24h = simbolos.filter(
(s) => s._creationTime >= last24h
).length;
return {
funcionariosCadastrados24h,
simbolosCadastrados24h,
};
},
});
// Obter distribuição de funcionários por cidade
export const getFuncionariosPorCidade = query({
args: {},
returns: v.array(
v.object({
cidade: v.string(),
quantidade: v.number(),
})
),
handler: async (ctx) => {
const funcionarios = await ctx.db.query("funcionarios").collect();
const cidadesMap: Record<string, number> = {};
for (const func of funcionarios) {
if (!func.desligamentoData) {
cidadesMap[func.cidade] = (cidadesMap[func.cidade] || 0) + 1;
}
}
const result = Object.entries(cidadesMap)
.map(([cidade, quantidade]) => ({ cidade, quantidade }))
.sort((a, b) => b.quantidade - a.quantidade)
.slice(0, 5); // Top 5 cidades
return result;
},
});
// Obter evolução de cadastros por mês
export const getEvolucaoCadastros = query({
args: {},
returns: v.array(
v.object({
mes: v.string(),
funcionarios: v.number(),
})
),
handler: async (ctx) => {
const funcionarios = await ctx.db.query("funcionarios").collect();
const now = new Date();
const meses: Array<{ mes: string; funcionarios: number }> = [];
// Últimos 6 meses
for (let i = 5; i >= 0; i--) {
const date = new Date(now.getFullYear(), now.getMonth() - i, 1);
const nextDate = new Date(now.getFullYear(), now.getMonth() - i + 1, 1);
const mesNome = date.toLocaleDateString("pt-BR", {
month: "short",
year: "2-digit",
});
const funcCount = funcionarios.filter(
(f) => f._creationTime >= date.getTime() && f._creationTime < nextDate.getTime()
).length;
meses.push({
mes: mesNome,
funcionarios: funcCount,
});
}
return meses;
},
});