Skip to content

Commit

Permalink
Merge pull request #42 from GoLembrar/feat-messaging
Browse files Browse the repository at this point in the history
Feat messaging
  • Loading branch information
MatheusVict authored Jun 2, 2024
2 parents 77f0aa0 + f3b1cbf commit a6f9522
Show file tree
Hide file tree
Showing 42 changed files with 3,892 additions and 345 deletions.
17 changes: 16 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/test_db?schema=public"
DATABASE_URL="postgresql://test:test@localhost:5432/golembrar"

#CONFIGURAÇÃO para conectar ao RabbitMQ
USER_RABBITMQ="test"
PASSWORD_RABBITMQ="test"

# EMAIL_HOST="smtp.gmail.com"
# MAIL_PORT=587

#CONFIGURAÇÃO ABAIXO PARA UTILIZAR O SERVIÇO DE EMAIL DO MAILTRAP
EMAIL_HOST="sandbox.smtp.mailtrap.io"
EMAIL_AUTH_USER="<CREDENTIAL_TO_ACESS_THE_USER>"
EMAIL_AUTH_PASSWORD="<CREDENTIAL_PASSWORD>"
EMAIL_SERVICE_PORT=2525


JWT_SECRET="variosnumerosletrasesimbolosmisturadosparasegurançadosecret"
# A variável abaixo é referente ao tempo que o token vai durar, "1h" no caso é uma hora.
# Possíveis valores: Xm Xh Xd Xw XM onde X é qualquer valor, m são minutos, h são horas
Expand Down
12 changes: 9 additions & 3 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ services:
start_period: 10s

rabbitmq:
image: rabbitmq:3-management-alpine
image: rabbitmq:3-management
container_name: rabbitmq-service
ports:
- 5672:5672
- 15672:15672
- 5672:5672
- 15672:15672
volumes:
- rabbitmq_data:/var/lib/rabbitmq
environment:
- RABBITMQ_DEFAULT_USER=test
- RABBITMQ_DEFAULT_PASS=test
Expand All @@ -43,3 +46,6 @@ networks:
default:
driver: bridge
name: golembrar

volumes:
rabbitmq_data:
9 changes: 9 additions & 0 deletions consumer-queue-email/consumers/email.consumer.module.ts
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 {}
49 changes: 49 additions & 0 deletions consumer-queue-email/consumers/email.consumer.ts
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);
}
});
}
}
25 changes: 25 additions & 0 deletions consumer-queue-email/email/email.module.ts
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 { }
22 changes: 22 additions & 0 deletions consumer-queue-email/email/email.service.ts
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');
}
}
15 changes: 15 additions & 0 deletions consumer-queue-email/worker.ts
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();
2 changes: 1 addition & 1 deletion http/auth-request.http
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ POST {{host}}/auth HTTP/1.1
Content-Type: application/json

{
"email": "test@email.com",
"email": "jhone.test14@gmail.com",
"password": "senhaForte"
}

Expand Down
4 changes: 4 additions & 0 deletions http/email-requests .http
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
13 changes: 9 additions & 4 deletions http/user-requests.http
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ POST http://localhost:3000/user HTTP/1.1
Content-Type: application/json

{
"email" : "test@email.com",
"phone" : "19987851003",
"email" : "jhone.test14@gmail.com",
"phone" : "19987851009",
"name" : "jhone",
"password" : "senhaForte"
}

Expand All @@ -23,7 +24,11 @@ Authorization: Bearer {{token}}

###

DELETE http://localhost:3000/user/11
DELETE http://localhost:3000/user
Authorization: Bearer {{token}}
Content-Type: application/json



### get one user account

Expand All @@ -36,7 +41,7 @@ Content-Type: application/json

{
"email" : "test@email.com",
"password" : "senhaForte1"
"password" : "senhaForte"
}

### get all users account
Expand Down
Loading

0 comments on commit a6f9522

Please sign in to comment.