Commit 0a8fd155 authored by FORESTIER Fabien's avatar FORESTIER Fabien
Browse files

Improving error handling

parent 541a5844
Pipeline #2511 passed with stages
in 1 minute and 36 seconds
......@@ -16,7 +16,7 @@ services:
environment:
- RABBITMQ_DEFAULT_USER=${RABBITMQ_USERNAME}
- RABBITMQ_DEFAULT_PASS=${RABBITMQ_PASSWORD}
- EMAIL_CONTACT=${EMAIL_CONTACT}
- ADMIN_EMAIL=${ADMIN_EMAIL}
volumes:
- rabbitmqPersistence:/var/lib/rabbitmq
restart: unless-stopped
......
import { Controller, Post, Body } from '@nestjs/common';
import { Controller, Post, Body, Res } from '@nestjs/common';
import { ContactForm } from './email';
import { EmailService } from './email.service';
import { ApiBadRequestResponse, ApiOkResponse } from '@nestjs/swagger';
......@@ -13,8 +13,14 @@ export class EmailController {
@Post('contact')
@ApiOkResponse({ description: 'OK'})
@ApiBadRequestResponse({ description: 'Missing fields'})
create(@Body() contactForm: ContactForm) {
return this.emailService.send(contactForm);
create(@Body() contactForm: ContactForm, @Res() res) {
const created = this.emailService.send(contactForm);
if (created === true) {
res.status(200).send();
} else {
res.status(400).send({error: 'Couldn\'t send the email'});
}
}
}
......@@ -5,39 +5,46 @@ import { ContactForm, Email } from './email';
@Injectable()
export class EmailService {
send(contactForm: ContactForm) {
send(contactForm: ContactForm): boolean {
const rabbitmqUrl = 'amqp://user:password123@rabbitmq:5672';
const mailerQueue = 'portail-data-send-email';
const email = new Email();
email.from = `${contactForm.firstname} ${contactForm.lastname} ${contactForm.from}`;
email.to = [process.env.EMAIL_CONTACT];
email.to = [process.env.ADMIN_EMAIL];
email.subject = contactForm.subject;
email.text = contactForm.text;
Logger.log(email);
// Connect to rabbitmq
amqp.connect(rabbitmqUrl, (err, conn) => {
if (err != null) { Logger.log(err); }
// Create a communication channel
conn.createChannel((error, ch) => {
return amqp.connect(rabbitmqUrl, (err, conn) => {
if (err != null) {
Logger.log(err);
return false;
} else {
// Create a communication channel
conn.createChannel((error, ch) => {
if (error != null) Logger.log(error);
// Stringify and bufferise message
const buffer = Buffer.from(JSON.stringify(email));
if (error != null) {
Logger.log(error);
return false;
} else {
// Stringify and bufferise message
const buffer = Buffer.from(JSON.stringify(email));
ch.assertQueue(mailerQueue, { durable: true });
ch.assertQueue(mailerQueue, { durable: true });
ch.sendToQueue(mailerQueue, buffer, { persistent: true });
ch.sendToQueue(mailerQueue, buffer, { persistent: true });
Logger.log(`sent to queue ${mailerQueue}: ${JSON.stringify(email)}`);
Logger.log(`sent to queue ${mailerQueue}: ${JSON.stringify(email)}`);
});
conn.close();
setTimeout(() => {
conn.close();
}, 500);
return true;
}
});
}
});
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment