fix: update email configuration handling and improve type safety
- Changed the mutation for testing SMTP connection to use an action for better handling. - Introduced an internal mutation to update the test timestamp for email configurations. - Enhanced type safety by specifying document types for user and session queries. - Improved error handling in the SMTP connection test to provide clearer feedback on failures.
This commit is contained in:
@@ -8,7 +8,7 @@ import {
|
||||
validarSenha,
|
||||
} from "./auth/utils";
|
||||
import { registrarLogin } from "./logsLogin";
|
||||
import { Id } from "./_generated/dataModel";
|
||||
import { Id, Doc } from "./_generated/dataModel";
|
||||
import type { QueryCtx } from "./_generated/server";
|
||||
|
||||
/**
|
||||
@@ -62,6 +62,7 @@ export const login = mutation({
|
||||
matricula: v.string(),
|
||||
nome: v.string(),
|
||||
email: v.string(),
|
||||
funcionarioId: v.optional(v.id("funcionarios")),
|
||||
role: v.object({
|
||||
_id: v.id("roles"),
|
||||
nome: v.string(),
|
||||
@@ -100,16 +101,19 @@ export const login = mutation({
|
||||
const isEmail = args.matriculaOuEmail.includes("@");
|
||||
|
||||
// Buscar usuário
|
||||
let usuario;
|
||||
let usuario: Doc<"usuarios"> | null = null;
|
||||
if (isEmail) {
|
||||
usuario = await ctx.db
|
||||
.query("usuarios")
|
||||
.withIndex("by_email", (q) => q.eq("email", args.matriculaOuEmail))
|
||||
.first();
|
||||
} else {
|
||||
funcionario = await ctx.db.query("funcionarios").withIndex("by_matricula", (q) => q.eq("matricula", args.matriculaOuEmail)).first();
|
||||
const funcionario: Doc<"funcionarios"> | null = await ctx.db.query("funcionarios").withIndex("by_matricula", (q) => q.eq("matricula", args.matriculaOuEmail)).first();
|
||||
if (funcionario) {
|
||||
usuario = await ctx.db.get(funcionario.usuarioId);
|
||||
usuario = await ctx.db
|
||||
.query("usuarios")
|
||||
.withIndex("by_funcionarioId", (q) => q.eq("funcionarioId", funcionario._id))
|
||||
.first();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,7 +246,7 @@ export const login = mutation({
|
||||
});
|
||||
|
||||
// Buscar role do usuário
|
||||
const role = await ctx.db.get(usuario.roleId);
|
||||
const role: Doc<"roles"> | null = await ctx.db.get(usuario.roleId);
|
||||
if (!role) {
|
||||
return {
|
||||
sucesso: false as const,
|
||||
@@ -359,6 +363,7 @@ export const verificarSessao = query({
|
||||
matricula: v.string(),
|
||||
nome: v.string(),
|
||||
email: v.string(),
|
||||
funcionarioId: v.optional(v.id("funcionarios")),
|
||||
role: v.object({
|
||||
_id: v.id("roles"),
|
||||
nome: v.string(),
|
||||
@@ -375,7 +380,7 @@ export const verificarSessao = query({
|
||||
),
|
||||
handler: async (ctx, args) => {
|
||||
// Buscar sessão
|
||||
const sessao = await ctx.db
|
||||
const sessao: Doc<"sessoes"> | null = await ctx.db
|
||||
.query("sessoes")
|
||||
.withIndex("by_token", (q) => q.eq("token", args.token))
|
||||
.first();
|
||||
@@ -395,7 +400,7 @@ export const verificarSessao = query({
|
||||
}
|
||||
|
||||
// Buscar usuário
|
||||
const usuario = await ctx.db.get(sessao.usuarioId);
|
||||
const usuario: Doc<"usuarios"> | null = await ctx.db.get(sessao.usuarioId);
|
||||
if (!usuario || !usuario.ativo) {
|
||||
return {
|
||||
valido: false as const,
|
||||
@@ -404,7 +409,7 @@ export const verificarSessao = query({
|
||||
}
|
||||
|
||||
// Buscar role
|
||||
const role = await ctx.db.get(usuario.roleId);
|
||||
const role: Doc<"roles"> | null = await ctx.db.get(usuario.roleId);
|
||||
if (!role) {
|
||||
return { valido: false as const, motivo: "Role não encontrada" };
|
||||
}
|
||||
@@ -416,6 +421,7 @@ export const verificarSessao = query({
|
||||
matricula: usuario.matricula,
|
||||
nome: usuario.nome,
|
||||
email: usuario.email,
|
||||
funcionarioId: usuario.funcionarioId,
|
||||
role: {
|
||||
_id: role._id,
|
||||
nome: role.nome,
|
||||
@@ -478,7 +484,7 @@ export const alterarSenha = mutation({
|
||||
),
|
||||
handler: async (ctx, args) => {
|
||||
// Verificar sessão
|
||||
const sessao = await ctx.db
|
||||
const sessao: Doc<"sessoes"> | null = await ctx.db
|
||||
.query("sessoes")
|
||||
.withIndex("by_token", (q) => q.eq("token", args.token))
|
||||
.first();
|
||||
@@ -487,7 +493,7 @@ export const alterarSenha = mutation({
|
||||
return { sucesso: false as const, erro: "Sessão inválida" };
|
||||
}
|
||||
|
||||
const usuario = await ctx.db.get(sessao.usuarioId);
|
||||
const usuario: Doc<"usuarios"> | null = await ctx.db.get(sessao.usuarioId);
|
||||
if (!usuario) {
|
||||
return { sucesso: false as const, erro: "Usuário não encontrado" };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user