122 lines
3.0 KiB
TypeScript
122 lines
3.0 KiB
TypeScript
import {
|
|
Controller,
|
|
Post,
|
|
Body,
|
|
UploadedFile,
|
|
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 { ApiSendMail} from './mail.decorators';
|
|
import { Repository } from 'typeorm';
|
|
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,
|
|
private readonly authService: AuthService
|
|
|
|
) {}
|
|
|
|
// Endpoint para enviar correos a través de una solicitud POST
|
|
|
|
|
|
|
|
@Post('send')
|
|
@ApiSendMail()
|
|
async sendMail(
|
|
@Headers() headers:{token:string},
|
|
@Body() body: SendCorreoDto
|
|
) {
|
|
|
|
if (!headers.token) {
|
|
throw new UnauthorizedException('Header "token" es requerido');
|
|
}
|
|
let payload
|
|
|
|
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(body,sistem); // sistem
|
|
return result;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
@Post('send-excel')
|
|
@UseInterceptors(FileInterceptor('file'))
|
|
@ApiSendMailExcel()
|
|
async sendMailExcel(
|
|
@Headers() headers: { token: string },
|
|
@UploadedFile() file: Express.Multer.File
|
|
) {
|
|
if (!headers.token) {
|
|
throw new UnauthorizedException('Header "token" es requerido');
|
|
}
|
|
|
|
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('Sistema no encontrado');
|
|
}
|
|
|
|
if (!file) {
|
|
throw new Error('Archivo Excel no encontrado');
|
|
}
|
|
|
|
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}`);
|
|
} catch (error) {
|
|
console.error(`Error enviando a ${email}:`, error.message);
|
|
}
|
|
}
|
|
|
|
return { message: 'Correos enviados (si no hubo errores)' };
|
|
}
|
|
*/
|
|
}
|