-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from GoLembrar/feat-messaging
Feat messaging
- Loading branch information
Showing
42 changed files
with
3,892 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { EmailConsumer } from './email.consumer'; | ||
import { EmailModule } from '../email/email.module'; | ||
|
||
@Module({ | ||
imports: [EmailModule], | ||
providers: [EmailConsumer], | ||
}) | ||
export class EmailConsumerModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { ClientRMQ, MessagePattern } from '@nestjs/microservices'; | ||
import { EmailService } from '../email/email.service'; | ||
import { Injectable } from '@nestjs/common'; | ||
import * as amqp from 'amqplib'; | ||
import { QueueList } from '../../src/queue/utils/queue-list'; | ||
@Injectable() | ||
export class EmailConsumer { | ||
private channel: amqp.Channel; | ||
|
||
constructor(private readonly emailService: EmailService) { | ||
this.receiveEmail(); | ||
} | ||
|
||
private async receiveEmail() { | ||
const connection = await amqp.connect( | ||
`amqp://${process.env.USER_RABBITMQ}:${process.env.PASSWORD_RABBITMQ}@localhost`, | ||
); | ||
this.channel = await connection.createChannel(); | ||
console.log('EmailConsumer: conexão feita com sucesso!'); | ||
// Consume messages from the queue | ||
this.channel.consume(QueueList.EMAIL, async (msg: any) => { | ||
if (msg) { | ||
const data = JSON.parse(msg.content.toString()); | ||
console.log(data); | ||
const email = data.data.email; | ||
console.log(email); | ||
|
||
// Call the email service to send email | ||
await this.emailService.sendEmail( | ||
email, | ||
'Mensagem de boas vindas', | ||
'Bem vindo ao GoLembrar', | ||
); | ||
|
||
/* new Promise((resolve, reject) => { | ||
setTimeout( | ||
() => { | ||
resolve(console.log(`EMAIL ENVIADO COM SUCESSO PARA: ${email}`)); | ||
}, | ||
Math.floor(Math.random() * 5000 + 1000), | ||
); | ||
}); */ | ||
|
||
// Acknowledge the message | ||
this.channel.ack(msg); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { config } from 'dotenv'; | ||
import { MailerModule } from '@nestjs-modules/mailer'; | ||
import { Module } from '@nestjs/common'; | ||
import { EmailService } from './email.service'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
|
||
config(); | ||
@Module({ | ||
imports: [ | ||
ConfigModule.forRoot(), | ||
MailerModule.forRoot({ | ||
transport: { | ||
host: process.env.EMAIL_HOST, | ||
port: parseInt(process.env.EMAIL_SERVICE_PORT, 10), | ||
auth: { | ||
user: process.env.EMAIL_AUTH_USER, | ||
pass: process.env.EMAIL_AUTH_PASSWORD, | ||
}, | ||
}, | ||
}), | ||
], | ||
providers: [EmailService], | ||
exports: [EmailService], | ||
}) | ||
export class EmailModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { config } from 'dotenv'; | ||
import { MailerService } from '@nestjs-modules/mailer'; | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class EmailService { | ||
constructor(private readonly mailerService: MailerService) {} | ||
|
||
async sendEmail( | ||
email: string, | ||
subject: string, | ||
context: string, | ||
): Promise<void> { | ||
await this.mailerService.sendMail({ | ||
to: email, | ||
from: process.env.EMAIL_USER, | ||
subject: subject, | ||
html: `<b>${context}</b>`, | ||
}); | ||
console.log('mensagem enviada com sucesso'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// worker.ts | ||
import { NestFactory } from '@nestjs/core'; | ||
import { MicroserviceOptions } from '@nestjs/microservices'; | ||
import { EmailConsumerModule } from './consumers/email.consumer.module'; | ||
|
||
async function bootstrap() { | ||
const app = | ||
await NestFactory.createMicroservice<MicroserviceOptions>( | ||
EmailConsumerModule, | ||
); | ||
|
||
app.listen(); | ||
} | ||
|
||
bootstrap(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
### create one user account | ||
|
||
GET http://localhost:3000/send-email HTTP/1.1 | ||
Content-Type: application/json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.