diff --git a/docker-compose.yml b/docker-compose.yml index 10fa3aa1a729c57a209787c8edd01f85ead8197c..cacb2311540b705c730823497b7e836fa42719a7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,8 @@ services: container_name: service-email ports: - 3001:3000 + environment: + - ADMIN_EMAIL=${ADMIN_EMAIL} restart: unless-stopped rabbitmq: image: 'rabbitmq:3-management-alpine' @@ -14,9 +16,8 @@ services: - 5672:5672 # standar port for communication - 15672:15672 # graphique interface environment: - - RABBITMQ_DEFAULT_USER=${RABBITMQ_USERNAME} - - RABBITMQ_DEFAULT_PASS=${RABBITMQ_PASSWORD} - - ADMIN_EMAIL=${ADMIN_EMAIL} + - RABBITMQ_DEFAULT_USER=${RABBITMQ_USERNAME:-user} + - RABBITMQ_DEFAULT_PASS=${RABBITMQ_PASSWORD:-password} volumes: - rabbitmqPersistence:/var/lib/rabbitmq restart: unless-stopped diff --git a/src/email/email.controller.ts b/src/email/email.controller.ts index 4a64511d1d6aa5a0d9bf3b9de231053178e15439..58b743952e76a6af36ffe223ff00aa1870853a87 100644 --- a/src/email/email.controller.ts +++ b/src/email/email.controller.ts @@ -14,13 +14,13 @@ export class EmailController { @ApiOkResponse({ description: 'OK'}) @ApiBadRequestResponse({ description: 'Missing fields'}) 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'}); - } + this.emailService.send(contactForm, (err, result) => { + if (err !== null || result !== true) { + res.status(400).send({error: err}); + } else { + res.status(200).send(); + } + }); } } diff --git a/src/email/email.service.ts b/src/email/email.service.ts index 46d950df5d6a67d3487bcf24f36f496e6710144e..3480d675714a1d3d00796eecf5099f9b279d3233 100644 --- a/src/email/email.service.ts +++ b/src/email/email.service.ts @@ -5,7 +5,7 @@ import { ContactForm, Email } from './email'; @Injectable() export class EmailService { - send(contactForm: ContactForm): boolean { + send(contactForm: ContactForm, done) { const rabbitmqUrl = 'amqp://user:password123@rabbitmq:5672'; const mailerQueue = 'portail-data-send-email'; @@ -18,17 +18,17 @@ export class EmailService { Logger.log(email); // Connect to rabbitmq - return amqp.connect(rabbitmqUrl, (err, conn) => { + amqp.connect(rabbitmqUrl, (err, conn) => { if (err != null) { Logger.log(err); - return false; + done('Couldn\'t connect to rabbitMQ.'); } else { // Create a communication channel - return conn.createChannel((error, ch) => { + conn.createChannel((error, ch) => { if (error != null) { Logger.log(error); - return false; + done('Couldn\'t create channel.'); } else { // Stringify and bufferise message const buffer = Buffer.from(JSON.stringify(email)); @@ -39,9 +39,9 @@ export class EmailService { Logger.log(`sent to queue ${mailerQueue}: ${JSON.stringify(email)}`); - conn.close(); + done(null, true); - return true; + setTimeout(() => { conn.close(); }, 500); } }); } diff --git a/swagger-spec.json b/swagger-spec.json new file mode 100644 index 0000000000000000000000000000000000000000..a84f48ccf9b851975ffb60623a1d25e6f470bc49 --- /dev/null +++ b/swagger-spec.json @@ -0,0 +1 @@ +{"swagger":"2.0","info":{"description":"APIs description for the Email API","version":"0.1","title":"Portail Data Email MicroService API"},"basePath":"/","tags":[],"schemes":["http"],"paths":{"/email/contact":{"post":{"parameters":[{"name":"ContactForm","required":true,"in":"body","schema":{"$ref":"#/definitions/ContactForm"}}],"responses":{"200":{"description":"OK"},"400":{"description":"Missing fields"}},"produces":["application/json"],"consumes":["application/json"]}}},"definitions":{"ContactForm":{"type":"object","properties":{"from":{"type":"string"},"subject":{"type":"string"},"firstname":{"type":"string"},"lastname":{"type":"string"},"text":{"type":"string"}},"required":["from","subject","firstname","lastname","text"]}}} \ No newline at end of file