Add environment variables and update email service for different environments

This commit is contained in:
faris Aljohari
2024-12-30 19:41:19 -06:00
parent 8ebedd6d8d
commit 3cff606887
3 changed files with 22 additions and 6 deletions

View File

@ -1,3 +1,5 @@
NODE_ENV=
ACCESS_KEY= ACCESS_KEY=
AZURE_POSTGRESQL_DATABASE= AZURE_POSTGRESQL_DATABASE=
@ -52,6 +54,10 @@ SMTP_SECURE=
SMTP_USER= SMTP_USER=
MAILTRAP_API_TOKEN=
MAILTRAP_INVITATION_TEMPLATE_UUID=
WEBSITES_ENABLE_APP_SERVICE_STORAGE= WEBSITES_ENABLE_APP_SERVICE_STORAGE=
PORT= PORT=

View File

@ -1 +1,3 @@
export const SEND_EMAIL_API_URL = 'https://send.api.mailtrap.io/api/send'; export const SEND_EMAIL_API_URL_PROD = 'https://send.api.mailtrap.io/api/send/';
export const SEND_EMAIL_API_URL_DEV =
'https://sandbox.api.mailtrap.io/api/send/2634012';

View File

@ -2,7 +2,10 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import * as nodemailer from 'nodemailer'; import * as nodemailer from 'nodemailer';
import axios from 'axios'; import axios from 'axios';
import { SEND_EMAIL_API_URL } from '../constants/mail-trap'; import {
SEND_EMAIL_API_URL_DEV,
SEND_EMAIL_API_URL_PROD,
} from '../constants/mail-trap';
@Injectable() @Injectable()
export class EmailService { export class EmailService {
@ -41,16 +44,20 @@ export class EmailService {
email: string, email: string,
emailInvitationData: any, emailInvitationData: any,
): Promise<void> { ): Promise<void> {
const isProduction = process.env.NODE_ENV === 'production';
const API_TOKEN = this.configService.get<string>( const API_TOKEN = this.configService.get<string>(
'email-config.MAILTRAP_API_TOKEN', 'email-config.MAILTRAP_API_TOKEN',
); );
const API_URL = isProduction
? SEND_EMAIL_API_URL_PROD
: SEND_EMAIL_API_URL_DEV;
const TEMPLATE_UUID = this.configService.get<string>( const TEMPLATE_UUID = this.configService.get<string>(
'email-config.MAILTRAP_INVITATION_TEMPLATE_UUID', 'email-config.MAILTRAP_INVITATION_TEMPLATE_UUID',
); );
const emailData = { const emailData = {
from: { from: {
email: this.configService.get<string>('email-config.SMTP_SENDER'), email: this.smtpConfig.sender,
}, },
to: [ to: [
{ {
@ -62,7 +69,7 @@ export class EmailService {
}; };
try { try {
await axios.post(SEND_EMAIL_API_URL, emailData, { await axios.post(API_URL, emailData, {
headers: { headers: {
Authorization: `Bearer ${API_TOKEN}`, Authorization: `Bearer ${API_TOKEN}`,
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -70,8 +77,9 @@ export class EmailService {
}); });
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Error sending email using Mailtrap template', error.response?.data?.message ||
error.status || HttpStatus.INTERNAL_SERVER_ERROR, 'Error sending email using Mailtrap template',
error.response?.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }