sending email is already in the project
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
import { IsEmail } from "class-validator"
|
||||
|
||||
export class EmailDto {
|
||||
|
||||
subject:string
|
||||
|
||||
text:string
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { EmailController } from './email.controller';
|
||||
|
||||
describe('EmailController', () => {
|
||||
let controller: EmailController;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [EmailController],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<EmailController>(EmailController);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Body, Controller, Param, ParseIntPipe, Post } from '@nestjs/common';
|
||||
import { EmailDto } from './dto/emailDto.dto';
|
||||
import { EmailService } from './email.service';
|
||||
|
||||
@Controller('email')
|
||||
export class EmailController {
|
||||
constructor(private readonly emailService: EmailService) {}
|
||||
|
||||
@Post('/evento/:id')
|
||||
async sendEmail(
|
||||
@Param('id', ParseIntPipe) id: number,
|
||||
@Body() emailDto: EmailDto,
|
||||
): Promise<any> {
|
||||
return this.emailService.sendEmail(id, emailDto);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { EventosModule } from 'src/eventos/eventos.module';
|
||||
import { Participante } from 'src/participante/participante.entity';
|
||||
import { EmailController } from './email.controller';
|
||||
import { EmailService } from './email.service';
|
||||
|
||||
@Module({
|
||||
imports:[TypeOrmModule.forFeature([Participante])],
|
||||
controllers:[EmailController],
|
||||
providers:[EmailService]
|
||||
})
|
||||
export class EmailModule {}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { EmailService } from './email.service';
|
||||
|
||||
describe('EmailService', () => {
|
||||
let service: EmailService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [EmailService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<EmailService>(EmailService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,52 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import * as nodemailer from 'nodemailer';
|
||||
import { EventosService } from 'src/eventos/eventos.service';
|
||||
import { Participante } from 'src/participante/participante.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
import { EmailDto } from './dto/emailDto.dto';
|
||||
|
||||
@Injectable()
|
||||
export class EmailService {
|
||||
private transporter;
|
||||
|
||||
constructor(
|
||||
@InjectRepository(Participante)
|
||||
private participanteRepository: Repository<Participante>,
|
||||
private configService: ConfigService,
|
||||
) {
|
||||
this.transporter = nodemailer.createTransport({
|
||||
service: this.configService.get('NODEMAILER_SERVICE'),
|
||||
auth: {
|
||||
user: this.configService.get<string>('NODEMAILER_USER'),
|
||||
pass: this.configService.get<string>('NODEMAILER_PASWORD'),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async sendEmail(id: number, message: EmailDto) {
|
||||
const participantes = await this.getEmailDeParticipantes(id);
|
||||
participantes.map((res) => {
|
||||
const emailDestinatario = res.email;
|
||||
this.transporter.sendMail({
|
||||
to: emailDestinatario,
|
||||
subject: message.subject,
|
||||
text: message.text,
|
||||
});
|
||||
/*console.log("Email enviado a: ", res)*/
|
||||
});
|
||||
}
|
||||
|
||||
async getEmailDeParticipantes(idEvento: number) {
|
||||
const participantes = await this.participanteRepository
|
||||
.createQueryBuilder('participante')
|
||||
.innerJoin('participante.eventosParticipante', 'eventoParticipante')
|
||||
.innerJoin('eventoParticipante.evento', 'evento')
|
||||
.where('evento.id_evento = :idEvento', { idEvento })
|
||||
.select(['participante.email'])
|
||||
.getMany();
|
||||
|
||||
return participantes;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user