Se agregó la contraseña de acceso
This commit is contained in:
@@ -3,12 +3,16 @@ import {
|
||||
Post,
|
||||
Body,
|
||||
UploadedFile,
|
||||
UseInterceptors,
|
||||
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
|
||||
@@ -16,30 +20,66 @@ import { ApiSendMail, ApiSendMailExcel} from './mail.decorators';
|
||||
export class MailController {
|
||||
constructor(
|
||||
private readonly mailService: MailService,
|
||||
private readonly excelService: ExcelService
|
||||
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(@Body() body: { to: string; subject: string; text: string; fecha_recibido: Date }) {
|
||||
const { to, subject, text, fecha_recibido } = body;
|
||||
async sendMail(
|
||||
@Headers() headers:{password:string},
|
||||
@Body() body: { to: string; subject: string; text: string; fecha_recibido: Date }) {
|
||||
|
||||
const result = await this.mailService.sendMail(to, subject, text, fecha_recibido);
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
|
||||
const { to, subject, text, fecha_recibido } = body;
|
||||
|
||||
const result = await this.mailService.sendMail(to, subject, text, fecha_recibido,sistem);
|
||||
return result;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Post('send-excel')
|
||||
@ApiSendMailExcel()
|
||||
async sendMailExcel(@UploadedFile() file: Express.Multer.File) {
|
||||
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", fechaActual);
|
||||
await this.mailService.sendMail(email, "subject", "text", fechaActual,sistem);
|
||||
console.log(`📧 Enviado a: ${email}`);
|
||||
} catch (error) {
|
||||
console.error(`❌ Error enviando a ${email}:`, error.message);
|
||||
|
||||
Reference in New Issue
Block a user