feat: SMTP config en DB, historial mensajes, JWT 90d, email OTP con Prisma
- schema.prisma: modelo message_logs (channel, recipient, body, status, error) - email-otp.service.ts: lee config SMTP desde DB (settings.smtp_config), registra log en message_logs - sms.service.ts: registra log en message_logs tras cada envío (éxito y error) - auth.module.ts: agrega PrismaModule, JWT expira en 90d - settings.controller.ts: GET/PATCH /settings/smtp + GET /settings/message-logs - settings.service.ts: método getMessageLogs con paginación y filtro por canal - settings.module.ts: importa AuthModule + EmailOtpService Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
a9b5e413e3
commit
522adc3b74
@@ -1,5 +1,6 @@
|
||||
import { Injectable, Logger, BadRequestException } from '@nestjs/common';
|
||||
import * as nodemailer from 'nodemailer';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
const OTP_TTL_MS = 10 * 60 * 1000; // 10 minutos
|
||||
|
||||
@@ -8,15 +9,21 @@ export class EmailOtpService {
|
||||
private readonly logger = new Logger(EmailOtpService.name);
|
||||
private readonly otps = new Map<string, { code: string; expiresAt: number }>();
|
||||
|
||||
private createTransport() {
|
||||
const host = process.env.SMTP_HOST;
|
||||
const port = parseInt(process.env.SMTP_PORT || '587');
|
||||
const user = process.env.SMTP_USER;
|
||||
const pass = process.env.SMTP_PASS;
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
private async createTransport() {
|
||||
// Prioridad: DB > env vars
|
||||
const dbConfig = await this.prisma.settings.findUnique({ where: { key: 'smtp_config' } })
|
||||
.then(r => r?.value as any).catch(() => null);
|
||||
|
||||
const host = dbConfig?.host || process.env.SMTP_HOST;
|
||||
const port = parseInt(dbConfig?.port || process.env.SMTP_PORT || '587');
|
||||
const user = dbConfig?.user || process.env.SMTP_USER;
|
||||
const pass = dbConfig?.pass || process.env.SMTP_PASS;
|
||||
|
||||
if (!host || !user || !pass) {
|
||||
throw new BadRequestException(
|
||||
'Correo no configurado. Añade SMTP_HOST, SMTP_USER y SMTP_PASS en las variables de entorno.',
|
||||
'Correo no configurado. Configure el SMTP en Ajustes > Email.',
|
||||
);
|
||||
}
|
||||
|
||||
@@ -28,31 +35,55 @@ export class EmailOtpService {
|
||||
});
|
||||
}
|
||||
|
||||
async getSmtpConfig() {
|
||||
return this.prisma.settings.findUnique({ where: { key: 'smtp_config' } })
|
||||
.then(r => (r?.value as any) ?? null);
|
||||
}
|
||||
|
||||
async saveSmtpConfig(config: { host: string; port: string; user: string; pass: string; from?: string }) {
|
||||
await this.prisma.settings.upsert({
|
||||
where: { key: 'smtp_config' },
|
||||
create: { key: 'smtp_config', value: config as any },
|
||||
update: { value: config as any },
|
||||
});
|
||||
}
|
||||
|
||||
async sendOtp(email: string): Promise<void> {
|
||||
const code = Math.floor(100000 + Math.random() * 900000).toString();
|
||||
this.otps.set(email.toLowerCase(), { code, expiresAt: Date.now() + OTP_TTL_MS });
|
||||
|
||||
const from = process.env.EMAIL_FROM || process.env.SMTP_USER;
|
||||
const transporter = this.createTransport();
|
||||
const dbConfig = await this.prisma.settings.findUnique({ where: { key: 'smtp_config' } })
|
||||
.then(r => r?.value as any).catch(() => null);
|
||||
const from = dbConfig?.from || process.env.EMAIL_FROM || process.env.SMTP_USER;
|
||||
const transporter = await this.createTransport();
|
||||
|
||||
await transporter.sendMail({
|
||||
from: `ProsApp <${from}>`,
|
||||
to: email,
|
||||
subject: 'Código de verificación ProsApp',
|
||||
html: `
|
||||
<div style="font-family:sans-serif;max-width:480px;margin:auto;padding:32px">
|
||||
<img src="https://prosapp.co/img/logo_prosapp.png" height="40" style="margin-bottom:24px">
|
||||
<h2 style="color:#1e293b;margin:0 0 8px">Verifica tu correo</h2>
|
||||
<p style="color:#64748b;margin:0 0 24px">Usa este código para vincular tu correo a ProsApp. Válido por 10 minutos.</p>
|
||||
<div style="background:#f0f8ff;border:1px solid #bae3ff;border-radius:12px;padding:24px;text-align:center">
|
||||
<span style="font-size:36px;font-weight:700;letter-spacing:10px;color:#42A4EF">${code}</span>
|
||||
try {
|
||||
await transporter.sendMail({
|
||||
from: `ProsApp <${from}>`,
|
||||
to: email,
|
||||
subject: 'Código de verificación ProsApp',
|
||||
html: `
|
||||
<div style="font-family:sans-serif;max-width:480px;margin:auto;padding:32px">
|
||||
<img src="https://prosapp.co/img/logo_prosapp.png" height="40" style="margin-bottom:24px">
|
||||
<h2 style="color:#1e293b;margin:0 0 8px">Verifica tu correo</h2>
|
||||
<p style="color:#64748b;margin:0 0 24px">Usa este código para vincular tu correo a ProsApp. Válido por 10 minutos.</p>
|
||||
<div style="background:#f0f8ff;border:1px solid #bae3ff;border-radius:12px;padding:24px;text-align:center">
|
||||
<span style="font-size:36px;font-weight:700;letter-spacing:10px;color:#42A4EF">${code}</span>
|
||||
</div>
|
||||
<p style="color:#94a3b8;font-size:12px;margin-top:24px">Si no solicitaste esto, ignora este correo.</p>
|
||||
</div>
|
||||
<p style="color:#94a3b8;font-size:12px;margin-top:24px">Si no solicitaste esto, ignora este correo.</p>
|
||||
</div>
|
||||
`,
|
||||
});
|
||||
|
||||
this.logger.log(`Email OTP enviado a ${email}`);
|
||||
`,
|
||||
});
|
||||
this.logger.log(`Email OTP enviado a ${email}`);
|
||||
await this.prisma.message_logs.create({
|
||||
data: { channel: 'email', recipient: email, body: `OTP: ${code}`, status: 'sent' },
|
||||
}).catch(() => {});
|
||||
} catch (err) {
|
||||
await this.prisma.message_logs.create({
|
||||
data: { channel: 'email', recipient: email, body: 'OTP', status: 'error', error: String(err) },
|
||||
}).catch(() => {});
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
verifyOtp(email: string, code: string): boolean {
|
||||
|
||||
Reference in New Issue
Block a user