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

Add send endpoint

parent c10d71d5
Pipeline #2498 passed with stages
in 23 seconds
......@@ -7,7 +7,9 @@ services:
ports:
- ${DEV_SERVICE_EMAIL_PORT}:3000
environment:
- NODE_ENV=${NODE_ENV}
- ADMIN_EMAILS=${DEV_ADMIN_EMAILS}
- PLATEFORM_DATA_EMAIL=${DEV_PLATEFORM_DATA_EMAIL}
- RABBITMQ_USER=${DEV_RABBITMQ_USER}
- RABBITMQ_PASSWORD=${DEV_RABBITMQ_PASSWORD}
restart: unless-stopped
......
This diff is collapsed.
......@@ -21,6 +21,7 @@ export class ConfigService {
this._config.rabbitMQ.user = process.env.RABBITMQ_USER;
this._config.rabbitMQ.password = process.env.RABBITMQ_PASSWORD;
this._config.adminEmails = process.env.ADMIN_EMAILS.split(',');
this._config.plateformDataEmail = process.env.PLATEFORM_DATA_EMAIL;
}
get config() {
......
......@@ -7,4 +7,5 @@ export const config = {
},
mailerQueue: 'portail-data-send-email',
adminEmails: [],
plateformDataEmail: '',
};
\ No newline at end of file
import { Controller, Post, Body, Res, HttpException, InternalServerErrorException, Logger, HttpCode } from '@nestjs/common';
import { ContactForm } from './email';
import { ContactForm, Email, EmailWithtoutFrom } from './email';
import { EmailService } from './email.service';
import { ApiBadRequestResponse, ApiOkResponse, ApiUseTags, ApiOperation, ApiInternalServerErrorResponse } from '@nestjs/swagger';
......@@ -19,7 +19,22 @@ export class EmailController {
@HttpCode(200)
async create(@Body() contactForm: ContactForm) {
try {
return await this.emailService.send(contactForm);
return await this.emailService.sendToAdmin(contactForm);
} catch (error) {
Logger.log(error);
throw new InternalServerErrorException();
}
}
@Post('send')
@ApiOperation({ title: 'Send email.' })
@ApiOkResponse({ description: 'OK' })
@ApiBadRequestResponse({ description: 'Missing fields' })
@ApiInternalServerErrorResponse({ description: 'Internal error, this is probably a rabbitMQ related error (unreachable service...)'})
@HttpCode(200)
async createEmail(@Body() email: EmailWithtoutFrom) {
try {
return await this.emailService.send(email);
} catch (error) {
Logger.log(error);
throw new InternalServerErrorException();
......
import { Injectable, Logger, InternalServerErrorException, BadRequestException } from '@nestjs/common';
import * as amqp from 'amqplib';
import { ContactForm, Email } from './email';
import { ContactForm, Email, EmailWithtoutFrom, EmailWithHtmlContent } from './email';
import { ConfigService } from 'configuration/config.service';
@Injectable()
......@@ -11,7 +11,7 @@ export class EmailService {
this.config = this.configService.config;
}
async send(contactForm: ContactForm) {
async sendToAdmin(contactForm: ContactForm) {
let conn, ch;
// tslint:disable-next-line:max-line-length
const rabbitmqUrl = `amqp://${this.config.rabbitMQ.user}:${this.config.rabbitMQ.password}@${this.config.rabbitMQ.host}:${this.config.rabbitMQ.port}`;
......@@ -66,4 +66,61 @@ export class EmailService {
return;
}
async send(emailInfo: EmailWithtoutFrom) {
let conn, ch;
// tslint:disable-next-line:max-line-length
const rabbitmqUrl = `amqp://${this.config.rabbitMQ.user}:${this.config.rabbitMQ.password}@${this.config.rabbitMQ.host}:${this.config.rabbitMQ.port}`;
const mailerQueue = this.config.mailerQueue;
Logger.log(this.config);
const email = new EmailWithHtmlContent();
email.from = this.config.plateformDataEmail;
email.to = emailInfo.to;
email.subject = emailInfo.subject;
email.html = emailInfo.html;
Logger.log('[-] send method');
Logger.log(email);
// Connect to rabbitmq
try {
conn = await amqp.connect(rabbitmqUrl);
} catch (error) {
Logger.error(' [x] Error connecting to RabbitMQ: ', JSON.stringify(error));
throw new InternalServerErrorException('Could not connect to rabbitMQ.');
}
try {
// Create a communication channel
ch = await conn.createChannel();
} catch (error) {
Logger.error(' [x] Error creating channel: ', JSON.stringify(error));
throw new InternalServerErrorException('Could not create channel.');
}
// Stringify and bufferise message
const buffer = Buffer.from(JSON.stringify(email));
try {
await ch.assertQueue(mailerQueue, { durable: true });
} catch (error) {
Logger.error(' [x] Error creating channel: ', JSON.stringify(error));
throw new InternalServerErrorException('Could not assert channel.');
}
try {
await ch.sendToQueue(mailerQueue, buffer, { persistent: true });
} catch (error) {
Logger.error(' [x] Error sending to queue: ', JSON.stringify(error));
throw new InternalServerErrorException('Could not send to queue.');
}
Logger.log(`Sent to queue ${mailerQueue}: ${JSON.stringify(email)}`);
setTimeout(() => { conn.close(); }, 500);
return;
}
}
......@@ -51,4 +51,52 @@ export class Email {
@IsDefined()
text: string;
}
export class EmailWithHtmlContent {
@ApiModelProperty()
@IsDefined()
to: string[];
@ApiModelProperty()
@IsDefined()
from: string;
@ApiModelPropertyOptional()
cc: string[];
@ApiModelPropertyOptional()
bcc: string[];
@ApiModelProperty()
@IsDefined()
subject: string;
@ApiModelProperty()
@IsDefined()
html: string;
}
export class EmailWithtoutFrom {
@ApiModelProperty()
@IsDefined()
to: string[];
@ApiModelPropertyOptional()
cc: string[];
@ApiModelPropertyOptional()
bcc: string[];
@ApiModelProperty()
@IsDefined()
subject: string;
@ApiModelProperty()
@IsDefined()
html: string;
}
\ No newline at end of file
Markdown is supported
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