103 lines
2.6 KiB
TypeScript
103 lines
2.6 KiB
TypeScript
import {
|
|
Controller,
|
|
Post,
|
|
Body,
|
|
Headers,
|
|
UnauthorizedException,
|
|
} from '@nestjs/common';
|
|
import { MailService } from './mail.service';
|
|
import { ApiTags } from '@nestjs/swagger';
|
|
import { ApiSendMail } from './mail.decorators';
|
|
import { SistemaService } from 'src/sistema/sistema.service';
|
|
import { SendCorreoDto } from 'src/mail/dto/send-email.dto';
|
|
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');
|
|
}
|
|
|
|
const result = await this.mailService
|
|
.sendMail(body, headers.token)
|
|
.then((value) => {
|
|
console.log('ya acabe el correo', value);
|
|
})
|
|
.catch((err) => {
|
|
console.log(err);
|
|
});
|
|
|
|
// sistem
|
|
return result;
|
|
}
|
|
|
|
@Post('masivo')
|
|
async enviarMasivo(
|
|
@Body() data: SendCorreoDto[],
|
|
@Headers() headers: { token: string },
|
|
) {
|
|
return await this.mailService.sendBulkMail(data, headers.token);
|
|
}
|
|
|
|
/*
|
|
|
|
@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)' };
|
|
}
|
|
*/
|
|
}
|