diff --git a/sample.pdf b/sample.pdf new file mode 100644 index 0000000..c01805e Binary files /dev/null and b/sample.pdf differ diff --git a/src/mail/mail.controller.ts b/src/mail/mail.controller.ts index edb15f0..800f91d 100644 --- a/src/mail/mail.controller.ts +++ b/src/mail/mail.controller.ts @@ -5,6 +5,7 @@ import { UploadedFile, Headers, UnauthorizedException, + Get, } from '@nestjs/common'; import { MailService } from './mail.service'; import { ExcelService } from 'src/excel/excel.service'; @@ -13,6 +14,7 @@ import { ApiSendMail, ApiSendMailExcel} from './mail.decorators'; import { Repository } from 'typeorm'; import { Sistema } from 'src/typeorm/votacionesPrueba.entity'; import { InjectRepository } from '@nestjs/typeorm'; +import { SistemaService } from 'src/sistema/sistema.service'; @ApiTags('Mail') // Grupo de endpoints @@ -21,43 +23,35 @@ export class MailController { constructor( private readonly mailService: MailService, private readonly excelService: ExcelService, + private readonly sistemaService: SistemaService, @InjectRepository(Sistema) private readonly sistemaRepository: Repository, ) {} // Endpoint para enviar correos a través de una solicitud POST + + @Post('send') @ApiSendMail() async sendMail( @Headers() headers:{token:string}, - @Body() body: { to: string; subject: string; text: string; fecha_recibido: Date, html:string, adjuntos:any }) { - - + @Body() body: { to: string; subject: string; text: string; fecha_recibido: Date, html:string, adjuntos:any } + ) { -// esta parte hay que rehacerla, se tiene que obtener la contraseña del sistema en base a la clave del sistema. -// debe de existir un modulo para crear contraseñas para las apis y administrar esas contraseñas - - - /* if (!headers.token) { - throw new UnauthorizedException('Header "Password" es requerido'); - } */ - - /* const sistem = await this.sistemaRepository.findOne({ where: { token: headers.token } }); - - if (!sistem) { + 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'); - } */ + } - - - console.log("estos son los archivos adjuntos en el controller",body); - - - const { to, subject, text, html, fecha_recibido, adjuntos } = body; + + - const result = await this.mailService.sendMail(to, subject, text,html, fecha_recibido,1,adjuntos); // sistem + const result = await this.mailService.sendMail(to, subject, text,html, fecha_recibido,sistem,adjuntos); // sistem return result; @@ -96,4 +90,9 @@ export class MailController { } } } + + @Get() + async findAll(){ + return await this.mailService.findAll() + } } diff --git a/src/mail/mail.service.ts b/src/mail/mail.service.ts index 8df1185..fd7845b 100644 --- a/src/mail/mail.service.ts +++ b/src/mail/mail.service.ts @@ -3,7 +3,7 @@ import * as nodemailer from 'nodemailer'; import * as dotenv from 'dotenv'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; -import { Correo, Status } from '../typeorm/votacionesPrueba.entity'; +import { Correo, Sistema, Status } from '../typeorm/votacionesPrueba.entity'; dotenv.config(); @@ -56,9 +56,14 @@ export class MailService { } // Función para generar el reporte de correos enviados y fallidos - + async findAll(){ + const result= await this.correoRepository.find({relations:['id_sistema','id_status']}) + return result + } async sendMail(to: string, subject: string, text: string, html: string, fecha_recibido: Date, sistema, adjuntos?: any[] ) { + + console.log("estos son los archivos adjuntos en el service",adjuntos); @@ -100,7 +105,7 @@ export class MailService { let resMail = await this.transporter.sendMail(mailOptions); const statusTexto = resMail.accepted.length > 0 ? "Enviado" : "Fallido"; - /* + let status = await this.statusRepository.findOne({ where: { status: statusTexto } }); @@ -117,13 +122,13 @@ export class MailService { destinatario: to, remitente: process.env.USER_GMAIL, - }); */ + }); if(!resMail){ throw new Error("fallo") } - //this.correoRepository.save(info); + this.correoRepository.save(info); diff --git a/src/sistema/dto/comprobar-sistema.ts b/src/sistema/dto/comprobar-sistema.ts new file mode 100644 index 0000000..b6c3ba9 --- /dev/null +++ b/src/sistema/dto/comprobar-sistema.ts @@ -0,0 +1,8 @@ +import { IsString, IsOptional, IsBoolean, IsNotEmpty } from 'class-validator'; + +export class ComprobarSistemaDto { + @IsNotEmpty() + @IsString() + token: string; + +} diff --git a/src/sistema/dto/create-sistema.dto.ts b/src/sistema/dto/create-sistema.dto.ts index 5ce14e6..074e579 100644 --- a/src/sistema/dto/create-sistema.dto.ts +++ b/src/sistema/dto/create-sistema.dto.ts @@ -1,4 +1,4 @@ -import { IsString, IsOptional, IsBoolean, IsNotEmpty } from 'class-validator'; +import { IsString, IsNotEmpty } from 'class-validator'; export class CreateSistemaDto { @IsNotEmpty() @@ -9,8 +9,8 @@ export class CreateSistemaDto { @IsString() ip:string; - @IsNotEmpty() - @IsString() - password: string; + @IsNotEmpty() + @IsString() + password: string; } diff --git a/src/sistema/sistema.controller.ts b/src/sistema/sistema.controller.ts index 70c1d06..5ea30fa 100644 --- a/src/sistema/sistema.controller.ts +++ b/src/sistema/sistema.controller.ts @@ -10,7 +10,7 @@ export class SistemaController { @Post() @ApiCreateSistema() - create(@Body() createSistemaDto: CreateSistemaDto, password:string) { + create(@Body() createSistemaDto: CreateSistemaDto) { return this.sistemaService.create(createSistemaDto); } diff --git a/src/sistema/sistema.module.ts b/src/sistema/sistema.module.ts index e6c1ec3..ae72cf0 100644 --- a/src/sistema/sistema.module.ts +++ b/src/sistema/sistema.module.ts @@ -11,6 +11,6 @@ import { Sistema } from 'src/typeorm/votacionesPrueba.entity'; TypeOrmModule.forFeature([ Sistema]) ], - exports:[SistemaModule] + exports:[SistemaModule,SistemaService] }) export class SistemaModule {} diff --git a/src/sistema/sistema.service.ts b/src/sistema/sistema.service.ts index 5c1fd51..5271580 100644 --- a/src/sistema/sistema.service.ts +++ b/src/sistema/sistema.service.ts @@ -5,6 +5,7 @@ import { Sistema } from 'src/typeorm/votacionesPrueba.entity'; import { Repository } from 'typeorm'; import { randomBytes } from 'crypto'; +import { ComprobarSistemaDto } from './dto/comprobar-sistema'; @Injectable() export class SistemaService { @@ -35,6 +36,15 @@ export class SistemaService { return sistema.token; } + async acceso(TOKEN:string){ + const result = await this.sistemaRepository.findOne({where:{token:TOKEN}}) + if(!result){ + throw new UnauthorizedException('token erroneo') + } + return true + + } + async create(createSistemaDto: CreateSistemaDto) { if(createSistemaDto.password!==process.env.ADMIN_PASSWORD){ throw new UnauthorizedException('Password erroneo'); diff --git a/src/typeorm/votacionesPrueba.entity.ts b/src/typeorm/votacionesPrueba.entity.ts index 605a3ff..b9dc081 100644 --- a/src/typeorm/votacionesPrueba.entity.ts +++ b/src/typeorm/votacionesPrueba.entity.ts @@ -13,7 +13,7 @@ export class Sistema { nombre_sistema: string; @Column({ type: 'varchar', length: 50 }) - ip:string + ip:string; @OneToMany(() => Correo, (correo) => correo.id_sistema) correos: Correo[];