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

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