Skip to content

Commit

Permalink
Merge pull request #252 from bcgov/FEATURE/flag-location-with-incorre…
Browse files Browse the repository at this point in the history
…ct-info

use client for email notification
  • Loading branch information
alateefah authored Jan 17, 2023
2 parents a623bfa + 2fc7d25 commit 783cae7
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions packages/bcer-api/app/src/note/emailService.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
import { Logger } from '@nestjs/common';
import Axios from 'axios';
import { LocationEntity } from 'src/location/entities/location.entity';
import { BCERNotifyClient } from 'src/notification/notification.client';
import { UserEntity } from 'src/user/entities/user.entity';

export class EmailService {


private readonly apiEndpoint =
process.env.EMAIL_API_ENDPOINT || 'https://api.notification.canada.ca/v2/notifications/email';
process.env.EMAIL_API_ENDPOINT || 'https://api.notification.canada.ca';
private readonly apiKey =
process.env.TEXT_API_KEY;
private readonly templateId =
process.env.EMAIL_GENERIC_NOTIFICATION_TEMPLATE_ID;
private readonly vapingEmail =
process.env.VAPING_NOTIFICATION_EMAIL;
private readonly reference = process.env.EMAIL_REFERENCE || 'emailRef123';

private messageClient: any;

constructor() {
this.messageClient = new BCERNotifyClient(this.apiEndpoint, this.apiKey);

if(process.env.TEXT_API_PROXY) {
this.messageClient.setProxy({
host: process.env.TEXT_API_PROXY,
port: parseInt(process.env.TEXT_API_PROXY_PORT || '80', 10),
})
}
}

async sendMail(message: string, user: UserEntity, location: LocationEntity) {
return await Axios.post(this.apiEndpoint, {
email_address: this.vapingEmail,
template_id: this.templateId,
personalisation: {
address: location.addressLine1,
sendersName: `${user.firstName || ""} ${user.lastName || ""}`,
sendersEmail: `${user.email || ""}`,
message
}
},
{
headers: {
Authorization: `ApiKey-v1 ${this.apiKey}`,
'Content-Type': 'application/json',
}
})
return await this.messageClient.sendEmail(
this.templateId,
this.vapingEmail,
{
personalisation: {
address: location.addressLine1,
sendersName: `${user.firstName || ""} ${user.lastName || ""}`,
sendersEmail: `${user.email || ""}`,
message
},
reference: this.reference,
},
)
.then(function (response) {
Logger.log(`Email successfully sent`);
Logger.log(`Status: ${response.status} ${response.statusText}`)
Expand All @@ -40,7 +50,8 @@ export class EmailService {
return 'ok'
})
.catch(function (error) {
Logger.error(error)
Logger.error(`Code: ${error.code}`)
Logger.error(`Message: ${error.message}`)
Logger.error(`Status: ${error.response.status} ${error.response.statusText}`)
Logger.error(`Error Sending Email: ${error.response.config.data}`)

Expand Down

0 comments on commit 783cae7

Please sign in to comment.