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

99 lines
2.7 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-04-23 15:57:52 -06:00
Get,
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-04-23 15:57:52 -06:00
import { SistemaService } from 'src/sistema/sistema.service';
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,
2025-04-23 15:57:52 -06:00
private readonly sistemaService: SistemaService,
2025-03-15 12:02:41 -06:00
@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-04-23 15:57:52 -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(
2025-04-09 11:10:28 -06:00
@Headers() headers:{token:string},
2025-04-23 15:57:52 -06:00
@Body() body: { to: string; subject: string; text: string; fecha_recibido: Date, html:string, adjuntos:any }
) {
2025-04-23 15:57:52 -06:00
if (!headers.token) {
throw new UnauthorizedException('Header "token" es requerido');
}
const sistem=await this.sistemaRepository.findOne({where:{token:headers.token}})
if(!sistem){
2025-03-15 12:02:41 -06:00
throw new UnauthorizedException('Password incorrecto');
2025-04-23 15:57:52 -06:00
}
2025-02-27 12:41:56 -06:00
2025-04-23 15:57:52 -06:00
const { to, subject, text, html, fecha_recibido, adjuntos } = body;
2025-04-03 11:17:00 -06:00
2025-04-23 15:57:52 -06:00
const result = await this.mailService.sendMail(to, subject, text,html, fecha_recibido,sistem,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');
}
2025-04-09 11:10:28 -06:00
const sistem = await this.sistemaRepository.findOne({ where: { token: headers.password } });
2025-03-15 12:02:41 -06:00
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-04-23 15:57:52 -06:00
@Get()
async findAll(){
return await this.mailService.findAll()
}
2025-02-27 12:41:56 -06:00
}