mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 10:25:23 +00:00
Add Mailtrap disable and enable template UUIDs and update email service
This commit is contained in:
@ -58,6 +58,10 @@ MAILTRAP_API_TOKEN=
|
||||
|
||||
MAILTRAP_INVITATION_TEMPLATE_UUID=
|
||||
|
||||
MAILTRAP_DISABLE_TEMPLATE_UUID=
|
||||
|
||||
MAILTRAP_ENABLE_TEMPLATE_UUID=
|
||||
|
||||
WEBSITES_ENABLE_APP_SERVICE_STORAGE=
|
||||
|
||||
PORT=
|
||||
|
@ -13,5 +13,7 @@ export default registerAs(
|
||||
MAILTRAP_API_TOKEN: process.env.MAILTRAP_API_TOKEN,
|
||||
MAILTRAP_INVITATION_TEMPLATE_UUID:
|
||||
process.env.MAILTRAP_INVITATION_TEMPLATE_UUID,
|
||||
MAILTRAP_DISABLE_TEMPLATE_UUID: process.env.MAILTRAP_DISABLE_TEMPLATE_UUID,
|
||||
MAILTRAP_ENABLE_TEMPLATE_UUID: process.env.MAILTRAP_ENABLE_TEMPLATE_UUID,
|
||||
}),
|
||||
);
|
||||
|
@ -68,6 +68,58 @@ export class EmailService {
|
||||
template_variables: emailInvitationData,
|
||||
};
|
||||
|
||||
try {
|
||||
await axios.post(API_URL, emailData, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${API_TOKEN}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.response?.data?.message ||
|
||||
'Error sending email using Mailtrap template',
|
||||
error.response?.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async sendEmailWithDisableOrEnableTemplate(
|
||||
email: string,
|
||||
name: string,
|
||||
isEnable: boolean,
|
||||
): Promise<void> {
|
||||
console.log(isEnable);
|
||||
|
||||
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 = isEnable
|
||||
? this.configService.get<string>(
|
||||
'email-config.MAILTRAP_ENABLE_TEMPLATE_UUID',
|
||||
)
|
||||
: this.configService.get<string>(
|
||||
'email-config.MAILTRAP_DISABLE_TEMPLATE_UUID',
|
||||
);
|
||||
|
||||
const emailData = {
|
||||
from: {
|
||||
email: this.smtpConfig.sender,
|
||||
},
|
||||
to: [
|
||||
{
|
||||
email: email,
|
||||
},
|
||||
],
|
||||
template_uuid: TEMPLATE_UUID,
|
||||
template_variables: {
|
||||
name: name,
|
||||
},
|
||||
};
|
||||
|
||||
try {
|
||||
await axios.post(API_URL, emailData, {
|
||||
headers: {
|
||||
|
@ -114,7 +114,7 @@ export class InviteUserService {
|
||||
|
||||
await Promise.all(spacePromises);
|
||||
await this.emailService.sendEmailWithInvitationTemplate(email, {
|
||||
name: firstName + ' ' + lastName,
|
||||
name: firstName,
|
||||
invitationCode,
|
||||
role: roleType,
|
||||
spacesList: spaceNamesString,
|
||||
@ -524,7 +524,11 @@ export class InviteUserService {
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
|
||||
await this.emailService.sendEmailWithDisableOrEnableTemplate(
|
||||
userData.email,
|
||||
userData.firstName,
|
||||
disable,
|
||||
);
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
return new SuccessResponseDto({
|
||||
|
Reference in New Issue
Block a user