mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
Add environment variables and update email service for different environments
This commit is contained in:
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user