diff --git a/docker-compose.yml b/docker-compose.yml index bc9871a5edef2d4e0163fa165701344c7188dad0..10fa3aa1a729c57a209787c8edd01f85ead8197c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/src/email/email.controller.ts b/src/email/email.controller.ts index df96e6a3c62444376f1efaa85358de2a3b3014b4..4a64511d1d6aa5a0d9bf3b9de231053178e15439 100644 --- a/src/email/email.controller.ts +++ b/src/email/email.controller.ts @@ -1,4 +1,4 @@ -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'}); + } } } diff --git a/src/email/email.service.ts b/src/email/email.service.ts index 2d6fe0c83b32021b49274bdf6ed94ce97c68f30b..d152f13b945ff996a19843b06bcdfed55cea3129 100644 --- a/src/email/email.service.ts +++ b/src/email/email.service.ts @@ -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; + } + }); + } }); }