diff --git a/src/email/email.controller.ts b/src/email/email.controller.ts index 60544a272708e3efddd4f8786650dbf3d661c404..df96e6a3c62444376f1efaa85358de2a3b3014b4 100644 --- a/src/email/email.controller.ts +++ b/src/email/email.controller.ts @@ -1,5 +1,5 @@ import { Controller, Post, Body } from '@nestjs/common'; -import { Email } from './email'; +import { ContactForm } from './email'; import { EmailService } from './email.service'; import { ApiBadRequestResponse, ApiOkResponse } from '@nestjs/swagger'; @@ -10,11 +10,11 @@ export class EmailController { private emailService: EmailService, ) {} - @Post('send') + @Post('contact') @ApiOkResponse({ description: 'OK'}) @ApiBadRequestResponse({ description: 'Missing fields'}) - create(@Body() email: Email) { - return this.emailService.send(email); + create(@Body() contactForm: ContactForm) { + return this.emailService.send(contactForm); } } diff --git a/src/email/email.service.ts b/src/email/email.service.ts index c144e537ae27ffe891797bf682b74d1410de6024..2d6fe0c83b32021b49274bdf6ed94ce97c68f30b 100644 --- a/src/email/email.service.ts +++ b/src/email/email.service.ts @@ -1,14 +1,22 @@ import { Injectable, Logger } from '@nestjs/common'; import * as amqp from 'amqplib/callback_api'; -import { Email } from './email'; +import { ContactForm, Email } from './email'; @Injectable() export class EmailService { - send(email: Email) { + send(contactForm: ContactForm) { 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.subject = contactForm.subject; + email.text = contactForm.text; + + Logger.log(email); + // Connect to rabbitmq amqp.connect(rabbitmqUrl, (err, conn) => { if (err != null) { Logger.log(err); } diff --git a/src/email/email.ts b/src/email/email.ts index b02a53e22f3cc738985390f364313f7030d61234..1448ee36ad6bf186ba5f1f517a4ce3f9d5a32d2d 100644 --- a/src/email/email.ts +++ b/src/email/email.ts @@ -1,6 +1,29 @@ import { IsDefined } from 'class-validator'; 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 { @ApiModelProperty() @@ -24,4 +47,5 @@ export class Email { @ApiModelProperty() @IsDefined() text: string; + } \ No newline at end of file