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

106 lines
2.5 KiB
TypeScript
Raw Normal View History

2025-02-27 12:41:56 -06:00
import {
Controller,
Post,
Body,
2025-03-15 12:02:41 -06:00
Headers,
UnauthorizedException,
2025-02-27 12:41:56 -06:00
} from '@nestjs/common';
import { MailService } from './mail.service';
import { ApiTags } from '@nestjs/swagger';
import { ApiSendMail} from './mail.decorators';
2025-04-23 15:57:52 -06:00
import { SistemaService } from 'src/sistema/sistema.service';
import { SendCorreoDto } from 'src/mail/dto/send-email.dto';
import { AuthService } from 'src/auth/auth.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 sistemaService: SistemaService,
private readonly mailService: MailService,
// private readonly excelService: ExcelService,
private readonly authService: AuthService
2025-03-15 12:02:41 -06:00
) {}
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},
@Body() body: SendCorreoDto
2025-04-23 15:57:52 -06:00
) {
2025-04-23 15:57:52 -06:00
if (!headers.token) {
throw new UnauthorizedException('Header "token" es requerido');
}
2025-06-27 20:09:46 -06:00
const result =
await this.mailService.sendMail(body,headers.token)
.then((value) => {
console.log("ya acabe el correo", value);
})
.catch((err)=>{
console.log(err)
})
2025-06-27 20:09:46 -06:00
// 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')
@UseInterceptors(FileInterceptor('file'))
2025-03-11 15:47:32 -06:00
@ApiSendMailExcel()
async sendMailExcel(
@Headers() headers: { token: string },
@UploadedFile() file: Express.Multer.File
) {
if (!headers.token) {
throw new UnauthorizedException('Header "token" es requerido');
2025-03-15 12:02:41 -06:00
}
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);
2025-03-15 12:02:41 -06:00
if (!sistem) {
throw new UnauthorizedException('Sistema no encontrado');
2025-03-15 12:02:41 -06:00
}
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);
}
}
2025-04-23 15:57:52 -06:00
return { message: 'Correos enviados (si no hubo errores)' };
2025-04-23 15:57:52 -06:00
}
*/
2025-02-27 12:41:56 -06:00
}