Files
correos_api/src/mail/mail.controller.ts
T

100 lines
2.8 KiB
TypeScript
Raw Normal View History

2025-02-27 12:41:56 -06:00
import {
Controller,
Post,
Body,
UploadedFile,
2025-03-15 12:02:41 -06:00
Headers,
UnauthorizedException,
2025-02-27 12:41:56 -06:00
} from '@nestjs/common';
import { MailService } from './mail.service';
import { ExcelService } from 'src/excel/excel.service';
2025-02-27 12:41:56 -06:00
import { ApiTags } from '@nestjs/swagger';
2025-03-11 15:47:32 -06:00
import { ApiSendMail, ApiSendMailExcel} from './mail.decorators';
2025-03-15 12:02:41 -06:00
import { Repository } from 'typeorm';
import { Sistema } from 'src/typeorm/votacionesPrueba.entity';
import { InjectRepository } from '@nestjs/typeorm';
2025-03-11 15:47:32 -06:00
2025-02-27 12:41:56 -06:00
@ApiTags('Mail') // Grupo de endpoints
@Controller('mail')
export class MailController {
constructor(
private readonly mailService: MailService,
2025-03-15 12:02:41 -06:00
private readonly excelService: ExcelService,
@InjectRepository(Sistema) private readonly sistemaRepository: Repository<Sistema>,
) {}
2025-02-27 12:41:56 -06:00
// Endpoint para enviar correos a través de una solicitud POST
2025-03-11 15:47:32 -06:00
2025-02-27 12:41:56 -06:00
@Post('send')
2025-03-11 15:47:32 -06:00
@ApiSendMail()
2025-03-15 12:02:41 -06:00
async sendMail(
@Headers() headers:{password:string},
2025-04-03 11:17:00 -06:00
@Body() body: { to: string; subject: string; text: string; fecha_recibido: Date, html:string, adjuntos:any }) {
2025-03-15 12:02:41 -06:00
/*
// 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
2025-03-15 12:02:41 -06:00
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');
} */
2025-02-27 12:41:56 -06:00
2025-04-03 11:17:00 -06:00
console.log("estos son los archivos adjuntos en el controller",body);
2025-03-15 12:02:41 -06:00
2025-04-03 11:17:00 -06:00
const { to, subject, text, html, fecha_recibido, adjuntos } = body;
2025-03-15 12:02:41 -06:00
2025-04-03 11:17:00 -06:00
const result = await this.mailService.sendMail(to, subject, text,html, fecha_recibido,1,adjuntos); // sistem
2025-02-27 12:41:56 -06:00
return result;
2025-03-15 12:02:41 -06:00
2025-02-27 12:41:56 -06:00
}
@Post('send-excel')
2025-03-11 15:47:32 -06:00
@ApiSendMailExcel()
2025-03-15 12:02:41 -06:00
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');
}
2025-03-11 15:47:32 -06:00
const emails=this.excelService.readEmailsFromExcel(file);
2025-03-07 14:14:37 -06:00
let fechaActual: Date = new Date();
2025-03-08 00:56:14 -06:00
for (const email of await emails) {
try {
2025-04-03 11:17:00 -06:00
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);
}
}
}
2025-02-27 12:41:56 -06:00
}