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:
@@ -1,8 +1,8 @@
|
||||
import { v } from "convex/values";
|
||||
import { mutation, query } from "./_generated/server";
|
||||
import { mutation, query, action, internalMutation } from "./_generated/server";
|
||||
import { encryptSMTPPassword } from "./auth/utils";
|
||||
import { registrarAtividade } from "./logsAtividades";
|
||||
import { api } from "./_generated/api";
|
||||
import { api, internal } from "./_generated/api";
|
||||
|
||||
/**
|
||||
* Obter configuração de email ativa (senha mascarada)
|
||||
@@ -127,9 +127,25 @@ export const salvarConfigEmail = mutation({
|
||||
});
|
||||
|
||||
/**
|
||||
* Testar conexão SMTP (mutation que chama action real)
|
||||
* Mutation interna para atualizar testadoEm
|
||||
*/
|
||||
export const testarConexaoSMTP = mutation({
|
||||
export const atualizarTestadoEm = internalMutation({
|
||||
args: {
|
||||
configId: v.id("configuracaoEmail"),
|
||||
},
|
||||
returns: v.null(),
|
||||
handler: async (ctx, args) => {
|
||||
await ctx.db.patch(args.configId, {
|
||||
testadoEm: Date.now(),
|
||||
});
|
||||
return null;
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Testar conexão SMTP (action que chama action real)
|
||||
*/
|
||||
export const testarConexaoSMTP = action({
|
||||
args: {
|
||||
servidor: v.string(),
|
||||
porta: v.number(),
|
||||
@@ -142,7 +158,7 @@ export const testarConexaoSMTP = mutation({
|
||||
v.object({ sucesso: v.literal(true) }),
|
||||
v.object({ sucesso: v.literal(false), erro: v.string() })
|
||||
),
|
||||
handler: async (ctx, args) => {
|
||||
handler: async (ctx, args): Promise<{ sucesso: true } | { sucesso: false; erro: string }> => {
|
||||
// Validações básicas
|
||||
if (!args.servidor || args.servidor.trim().length === 0) {
|
||||
return { sucesso: false as const, erro: "Servidor SMTP não pode estar vazio" };
|
||||
@@ -167,7 +183,7 @@ export const testarConexaoSMTP = mutation({
|
||||
|
||||
// Chamar action de teste real (que usa nodemailer)
|
||||
try {
|
||||
const resultado = await ctx.scheduler.runAfter(0, api.actions.smtp.testarConexao, {
|
||||
const resultado: { sucesso: true } | { sucesso: false; erro: string } = await ctx.runAction(api.actions.smtp.testarConexao, {
|
||||
servidor: args.servidor,
|
||||
porta: args.porta,
|
||||
usuario: args.usuario,
|
||||
@@ -178,23 +194,21 @@ export const testarConexaoSMTP = mutation({
|
||||
|
||||
// Se o teste foi bem-sucedido e há uma config ativa, atualizar testadoEm
|
||||
if (resultado.sucesso) {
|
||||
const configAtiva = await ctx.db
|
||||
.query("configuracaoEmail")
|
||||
.withIndex("by_ativo", (q) => q.eq("ativo", true))
|
||||
.first();
|
||||
|
||||
const configAtiva = await ctx.runQuery(api.configuracaoEmail.obterConfigEmail, {});
|
||||
|
||||
if (configAtiva) {
|
||||
await ctx.db.patch(configAtiva._id, {
|
||||
testadoEm: Date.now(),
|
||||
await ctx.runMutation(internal.configuracaoEmail.atualizarTestadoEm, {
|
||||
configId: configAtiva._id,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return resultado;
|
||||
} catch (error: any) {
|
||||
} catch (error: unknown) {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
return {
|
||||
sucesso: false as const,
|
||||
erro: error.message || "Erro ao conectar com o servidor SMTP"
|
||||
erro: errorMessage || "Erro ao conectar com o servidor SMTP"
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user