Skip to content
Snippets Groups Projects
Commit 997e0873 authored by FORESTIER Fabien's avatar FORESTIER Fabien
Browse files

Building email object from contactForm

parent 53c7a62c
No related branches found
No related tags found
No related merge requests found
Pipeline #
import { Controller, Post, Body } from '@nestjs/common'; import { Controller, Post, Body } from '@nestjs/common';
import { Email } from './email'; import { ContactForm } from './email';
import { EmailService } from './email.service'; import { EmailService } from './email.service';
import { ApiBadRequestResponse, ApiOkResponse } from '@nestjs/swagger'; import { ApiBadRequestResponse, ApiOkResponse } from '@nestjs/swagger';
...@@ -10,11 +10,11 @@ export class EmailController { ...@@ -10,11 +10,11 @@ export class EmailController {
private emailService: EmailService, private emailService: EmailService,
) {} ) {}
@Post('send') @Post('contact')
@ApiOkResponse({ description: 'OK'}) @ApiOkResponse({ description: 'OK'})
@ApiBadRequestResponse({ description: 'Missing fields'}) @ApiBadRequestResponse({ description: 'Missing fields'})
create(@Body() email: Email) { create(@Body() contactForm: ContactForm) {
return this.emailService.send(email); return this.emailService.send(contactForm);
} }
} }
import { Injectable, Logger } from '@nestjs/common'; import { Injectable, Logger } from '@nestjs/common';
import * as amqp from 'amqplib/callback_api'; import * as amqp from 'amqplib/callback_api';
import { Email } from './email'; import { ContactForm, Email } from './email';
@Injectable() @Injectable()
export class EmailService { export class EmailService {
send(email: Email) { send(contactForm: ContactForm) {
const rabbitmqUrl = 'amqp://user:password123@rabbitmq:5672'; const rabbitmqUrl = 'amqp://user:password123@rabbitmq:5672';
const mailerQueue = 'portail-data-send-email'; 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.subject = contactForm.subject;
email.text = contactForm.text;
Logger.log(email);
// Connect to rabbitmq // Connect to rabbitmq
amqp.connect(rabbitmqUrl, (err, conn) => { amqp.connect(rabbitmqUrl, (err, conn) => {
if (err != null) { Logger.log(err); } if (err != null) { Logger.log(err); }
......
import { IsDefined } from 'class-validator'; import { IsDefined } from 'class-validator';
import { ApiModelProperty, ApiModelPropertyOptional } from '@nestjs/swagger'; import { ApiModelProperty, ApiModelPropertyOptional } from '@nestjs/swagger';
export class ContactForm {
@ApiModelProperty()
@IsDefined()
from: string;
@ApiModelProperty()
@IsDefined()
subject: string;
@ApiModelProperty()
@IsDefined()
firstname: string;
@ApiModelProperty()
@IsDefined()
lastname: string;
@ApiModelProperty()
@IsDefined()
text: string;
}
export class Email { export class Email {
@ApiModelProperty() @ApiModelProperty()
...@@ -24,4 +47,5 @@ export class Email { ...@@ -24,4 +47,5 @@ export class Email {
@ApiModelProperty() @ApiModelProperty()
@IsDefined() @IsDefined()
text: string; text: string;
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment