Se generalizó envio de correos a diferentes sistemas
This commit is contained in:
+57
-35
@@ -6,24 +6,30 @@ import {
|
||||
Headers,
|
||||
UnauthorizedException,
|
||||
Get,
|
||||
UseInterceptors,
|
||||
} from '@nestjs/common';
|
||||
import { MailService } from './mail.service';
|
||||
import { ExcelService } from 'src/excel/excel.service';
|
||||
import { ApiTags } from '@nestjs/swagger';
|
||||
import { ApiFindAllMails, ApiSendMail, ApiSendMailExcel} from './mail.decorators';
|
||||
import { ApiSendMail} from './mail.decorators';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Sistema } from 'src/typeorm/votacionesPrueba.entity';
|
||||
import { Sistema } from 'src/typeorm/entidades';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { SistemaService } from 'src/sistema/sistema.service';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { SendCorreoDto } from 'src/mail/dto/send-email.dto';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { AuthService } from 'src/auth/auth.service';
|
||||
|
||||
|
||||
@ApiTags('Mail') // Grupo de endpoints
|
||||
@Controller('mail')
|
||||
export class MailController {
|
||||
constructor(
|
||||
private readonly sistemaService: SistemaService,
|
||||
private readonly mailService: MailService,
|
||||
private readonly excelService: ExcelService,
|
||||
@InjectRepository(Sistema) private readonly sistemaRepository: Repository<Sistema>,
|
||||
// private readonly excelService: ExcelService,
|
||||
private readonly authService: AuthService
|
||||
|
||||
) {}
|
||||
|
||||
@@ -35,22 +41,32 @@ export class MailController {
|
||||
@ApiSendMail()
|
||||
async sendMail(
|
||||
@Headers() headers:{token:string},
|
||||
@Body() body: { to: string; subject: string; text: string; fecha_recibido: Date, html:string, adjuntos:any }
|
||||
@Body() body: SendCorreoDto
|
||||
) {
|
||||
|
||||
if (!headers.token) {
|
||||
throw new UnauthorizedException('Header "token" es requerido');
|
||||
}
|
||||
const sistem=await this.sistemaRepository.findOne({where:{token:headers.token}})
|
||||
if(!sistem){
|
||||
throw new UnauthorizedException('Password incorrecto');
|
||||
}
|
||||
let payload
|
||||
|
||||
const { to, subject, text, html, fecha_recibido, adjuntos } = body;
|
||||
payload = await this.authService.verificarToken(headers.token);
|
||||
|
||||
|
||||
if (!payload || !payload.nombre) {
|
||||
throw new UnauthorizedException('Token inválido o incompleto');
|
||||
}
|
||||
|
||||
const sistem= await this.sistemaService.findByNombre(payload.nombre)
|
||||
|
||||
|
||||
if(!sistem){
|
||||
throw new UnauthorizedException('sistema No encontrado');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const result = await this.mailService.sendMail(to, subject, text,html, fecha_recibido,sistem,adjuntos); // sistem
|
||||
const result = await this.mailService.sendMail(body,sistem); // sistem
|
||||
return result;
|
||||
|
||||
|
||||
@@ -58,42 +74,48 @@ export class MailController {
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@Post('send-excel')
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
@ApiSendMailExcel()
|
||||
async sendMailExcel(@Headers() headers:{password:string},@Body() file: Express.Multer.File) {
|
||||
|
||||
if (!headers.password) {
|
||||
throw new UnauthorizedException('Header "Password" es requerido');
|
||||
async sendMailExcel(
|
||||
@Headers() headers: { token: string },
|
||||
@UploadedFile() file: Express.Multer.File
|
||||
) {
|
||||
if (!headers.token) {
|
||||
throw new UnauthorizedException('Header "token" es requerido');
|
||||
}
|
||||
|
||||
const sistem = await this.sistemaRepository.findOne({ where: { token: headers.password } });
|
||||
|
||||
|
||||
let payload;
|
||||
try {
|
||||
payload = await this.jwtService.verify(headers.token);
|
||||
} catch {
|
||||
throw new UnauthorizedException('Token inválido');
|
||||
}
|
||||
|
||||
const sistem = await this.sistemaService.findByNombre(payload.nombre);
|
||||
if (!sistem) {
|
||||
throw new UnauthorizedException('Password incorrecto');
|
||||
throw new UnauthorizedException('Sistema no encontrado');
|
||||
}
|
||||
|
||||
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) {
|
||||
}
|
||||
|
||||
const emails = await this.excelService.readEmailsFromExcel(file);
|
||||
const fechaActual: Date = new Date();
|
||||
|
||||
for (const email of emails) {
|
||||
try {
|
||||
await this.mailService.sendMail(email, "subject", "text","html", fechaActual,sistem);
|
||||
console.log(`📧 Enviado a: ${email}`);
|
||||
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);
|
||||
console.error(`Error enviando a ${email}:`, error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiFindAllMails()
|
||||
async findAll(@Headers() headers:{token:string}){
|
||||
|
||||
return await this.mailService.findAll(headers.token)
|
||||
return { message: 'Correos enviados (si no hubo errores)' };
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user