2025-02-27 12:41:56 -06:00
|
|
|
import {
|
|
|
|
|
Controller,
|
|
|
|
|
Post,
|
|
|
|
|
Body,
|
|
|
|
|
UploadedFile,
|
|
|
|
|
UseInterceptors,
|
|
|
|
|
} from '@nestjs/common';
|
|
|
|
|
import { MailService } from './mail.service';
|
2025-02-28 14:44:40 -06:00
|
|
|
import { ExcelService } from 'src/excel/excel.service';
|
2025-02-27 12:41:56 -06:00
|
|
|
import { FileInterceptor } from '@nestjs/platform-express';
|
|
|
|
|
import * as fs from 'fs';
|
|
|
|
|
import * as csv from 'csv-parser';
|
|
|
|
|
import { diskStorage } from 'multer'; // Importar para almacenar en el sistema de archivos
|
|
|
|
|
import { ApiTags } from '@nestjs/swagger';
|
|
|
|
|
|
|
|
|
|
@ApiTags('Mail') // Grupo de endpoints
|
|
|
|
|
@Controller('mail')
|
|
|
|
|
export class MailController {
|
2025-02-28 14:44:40 -06:00
|
|
|
constructor(
|
|
|
|
|
private readonly mailService: MailService,
|
|
|
|
|
private readonly excelService: ExcelService
|
|
|
|
|
) {}
|
2025-02-27 12:41:56 -06:00
|
|
|
|
|
|
|
|
// Endpoint para enviar correos a través de una solicitud POST
|
|
|
|
|
@Post('send')
|
|
|
|
|
async sendMail(@Body() body: { to: string; subject: string; text: string }) {
|
|
|
|
|
const { to, subject, text } = body;
|
|
|
|
|
|
|
|
|
|
const result = await this.mailService.sendMail(to, subject, text);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-28 14:44:40 -06:00
|
|
|
@Post('send-excel')
|
|
|
|
|
async sendMailExcel() {
|
|
|
|
|
|
|
|
|
|
const emails=this.excelService.readEmailsFromExcel('./src/excel_Usuarios/usuarios_fake.xlsx');
|
|
|
|
|
for (const email of emails) {
|
|
|
|
|
try {
|
|
|
|
|
await this.mailService.sendMail(email, "subject", "text");
|
|
|
|
|
console.log(`📧 Enviado a: ${email}`);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`❌ Error enviando a ${email}:`, error.message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-02-27 12:41:56 -06:00
|
|
|
@Post('send-bulk')
|
|
|
|
|
//@UseInterceptors(FileInterceptor('file'))
|
|
|
|
|
@UseInterceptors(
|
|
|
|
|
FileInterceptor('file', {
|
|
|
|
|
storage: diskStorage({
|
|
|
|
|
destination: './uploads', // Carpeta donde guardar los archivos
|
|
|
|
|
filename: (req, file, cb) => {
|
|
|
|
|
// Guardar el archivo con su nombre original
|
|
|
|
|
const filename = file.originalname;
|
|
|
|
|
cb(null, filename);
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
async sendBulkMails(@UploadedFile() file: Express.Multer.File) {
|
|
|
|
|
const emails: string[] = [];
|
|
|
|
|
const subject = 'Correo de prueba';
|
|
|
|
|
const text = 'Este es un correo enviado de manera masiva.';
|
|
|
|
|
|
|
|
|
|
// Leer el archivo CSV
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
fs.createReadStream(file.path)
|
|
|
|
|
.pipe(csv())
|
|
|
|
|
.on('data', (row) => {
|
|
|
|
|
emails.push(row.email); // Agregar cada correo a la lista
|
|
|
|
|
})
|
|
|
|
|
.on('end', async () => {
|
|
|
|
|
// Enviar correos en bulk usando el servicio de correos
|
|
|
|
|
const result = await this.mailService.sendBulkMails(
|
|
|
|
|
emails,
|
|
|
|
|
subject,
|
|
|
|
|
text,
|
|
|
|
|
);
|
|
|
|
|
resolve(result);
|
|
|
|
|
})
|
|
|
|
|
.on('error', (error) => {
|
|
|
|
|
reject(error);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|