100 lines
2.8 KiB
TypeScript
100 lines
2.8 KiB
TypeScript
import {
|
|
Controller,
|
|
Post,
|
|
Body,
|
|
UploadedFile,
|
|
Headers,
|
|
UnauthorizedException,
|
|
} from '@nestjs/common';
|
|
import { MailService } from './mail.service';
|
|
import { ExcelService } from 'src/excel/excel.service';
|
|
import { ApiTags } from '@nestjs/swagger';
|
|
import { ApiSendMail, ApiSendMailExcel} from './mail.decorators';
|
|
import { Repository } from 'typeorm';
|
|
import { Sistema } from 'src/typeorm/votacionesPrueba.entity';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
|
|
@ApiTags('Mail') // Grupo de endpoints
|
|
@Controller('mail')
|
|
export class MailController {
|
|
constructor(
|
|
private readonly mailService: MailService,
|
|
private readonly excelService: ExcelService,
|
|
@InjectRepository(Sistema) private readonly sistemaRepository: Repository<Sistema>,
|
|
|
|
) {}
|
|
|
|
// Endpoint para enviar correos a través de una solicitud POST
|
|
|
|
@Post('send')
|
|
@ApiSendMail()
|
|
async sendMail(
|
|
@Headers() headers:{password:string},
|
|
@Body() body: { to: string; subject: string; text: string; fecha_recibido: Date, html:string, adjuntos:any }) {
|
|
|
|
/*
|
|
|
|
// esta parte hay que rehacerla, se tiene que obtener la contraseña del sistema en base a la clave del sistema.
|
|
// debe de existir un modulo para crear contraseñas para las apis y administrar esas contraseñas
|
|
|
|
|
|
if (!headers.password) {
|
|
throw new UnauthorizedException('Header "Password" es requerido');
|
|
}
|
|
|
|
const sistem = await this.sistemaRepository.findOne({ where: { password: headers.password } });
|
|
|
|
if (!sistem) {
|
|
throw new UnauthorizedException('Password incorrecto');
|
|
} */
|
|
|
|
|
|
|
|
console.log("estos son los archivos adjuntos en el controller",body);
|
|
|
|
|
|
|
|
const { to, subject, text, html, fecha_recibido, adjuntos } = body;
|
|
|
|
const result = await this.mailService.sendMail(to, subject, text,html, fecha_recibido,1,adjuntos); // sistem
|
|
return result;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@Post('send-excel')
|
|
@ApiSendMailExcel()
|
|
async sendMailExcel(@Headers() headers:{password:string},@Body() file: Express.Multer.File) {
|
|
|
|
if (!headers.password) {
|
|
throw new UnauthorizedException('Header "Password" es requerido');
|
|
}
|
|
|
|
const sistem = await this.sistemaRepository.findOne({ where: { password: headers.password } });
|
|
|
|
if (!sistem) {
|
|
throw new UnauthorizedException('Password incorrecto');
|
|
}
|
|
|
|
if (!file) {
|
|
|
|
throw new Error('Archivo Excel no encontrado');
|
|
}
|
|
|
|
const emails=this.excelService.readEmailsFromExcel(file);
|
|
let fechaActual: Date = new Date();
|
|
|
|
for (const email of await emails) {
|
|
try {
|
|
await this.mailService.sendMail(email, "subject", "text","html", fechaActual,sistem);
|
|
console.log(`📧 Enviado a: ${email}`);
|
|
} catch (error) {
|
|
console.error(`❌ Error enviando a ${email}:`, error.message);
|
|
}
|
|
}
|
|
}
|
|
}
|