Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 40 additions & 20 deletions src/destination/destinationHandlers/sesHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ export class SESService implements Handler {
const providersSet = new Set(providerObjects);
this.sesConfig = null
for (const element of providersSet) {
if (element['dest'] === "ses") {
if ((element['dest'] === "ses") && (element['configId'] != 0)) {
await this.getConfigById(element['configId'], providersSet, event, sesTemplate, setting, destinationMap, configsMap)
break
} else if (element['dest'] === "ses") {
await this.getDefaultConfig(providersSet, event, sesTemplate, setting, destinationMap, configsMap)
break
}
Expand All @@ -84,16 +87,13 @@ export class SESService implements Handler {
if(this.sesConfig && this.sesConfig.from_email){
for (const p of providersSet) {
if (p['dest'] == "ses") {
let userId = p['configId']
let recipient = p['recipient']
let configKey = '';
if(recipient) {
configKey = p['dest'] + '-' + recipient
}else{
configKey = p['dest'] + '-' + userId
}
if (!configsMap.get(configKey)) {
await this.processNotification(userId, recipient, event, sesTemplate, setting, p, emailMap)
await this.processNotification(recipient, event, sesTemplate, setting, p, emailMap)
configsMap.set(configKey, true)
}
}
Expand All @@ -105,6 +105,36 @@ export class SESService implements Handler {
}
}

private async getConfigById(id,providersSet, event: Event, sesTemplate: NotificationTemplates, setting: NotificationSettings, emailMap: Map<string, boolean>, configsMap: Map<string, boolean> ){
try {
const config = await this.sesConfigRepository.findBySESConfigId(id)
this.sesConfig = {
region: config['region'],
access_key: config['access_key'],
secret_access_key: config['secret_access_key'],
from_email: config['from_email']
}
if(this.sesConfig && this.sesConfig.from_email){
for (const p of providersSet) {
if (p['dest'] == "ses") {
let recipient = p['recipient']
let configKey = '';
if(recipient) {
configKey = p['dest'] + '-' + recipient
}
if (!configsMap.get(configKey)) {
await this.processNotification(recipient, event, sesTemplate, setting, p, emailMap)
configsMap.set(configKey, true)
}
}
};
}
} catch (error) {
this.logger.error('getDefaultConfig', error)
throw new CustomError("Unable to send SES notification",500);
}
}

private async preparePayloadAndSend(event: Event, sesTemplate: NotificationTemplates, setting: NotificationSettings, p: string){
let sdk: NotifmeSdk = new NotifmeSdk({
channels: {
Expand Down Expand Up @@ -163,22 +193,12 @@ export class SESService implements Handler {
}
}

private async processNotification(userId: number, recipient: string, event: Event, sesTemplate: NotificationTemplates, setting: NotificationSettings, p: string, emailMap: Map<string, boolean>) {
if(userId) {
const user = await this.usersRepository.findByUserId(userId)
if (!user) {
this.logger.info('no user found for id - ' + userId)
this.logger.info(event.correlationId)
return
}
await this.sendEmailIfNotDuplicate(user['email_id'], event, sesTemplate, setting, p, emailMap)
}else{
if (!recipient) {
this.logger.error('recipient is blank')
return
}
await this.sendEmailIfNotDuplicate(recipient, event, sesTemplate, setting, p, emailMap)
private async processNotification(recipient: string, event: Event, sesTemplate: NotificationTemplates, setting: NotificationSettings, p: string, emailMap: Map<string, boolean>) {
if (!recipient) {
this.logger.error('recipient is blank')
return
}
await this.sendEmailIfNotDuplicate(recipient, event, sesTemplate, setting, p, emailMap)
}

private async sendEmailIfNotDuplicate(recipient : string, event: Event, sesTemplate: NotificationTemplates, setting: NotificationSettings, p: string, emailMap: Map<string, boolean>) {
Expand Down
67 changes: 44 additions & 23 deletions src/destination/destinationHandlers/smtpHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ export class SMTPService implements Handler {
const providersSet = new Set(providerObjects);
this.smtpConfig = null
for (const element of providersSet) {
if (element['dest'] === "smtp") {
if ((element['dest'] === "smtp") && (element['configId'] != 0)) {
await this.getConfigById(element['configId'], providersSet, event, sesTemplate, setting, destinationMap, configsMap)
break
} else if (element['dest'] === "smtp") {
await this.getDefaultConfig(providersSet, event, sesTemplate, setting, destinationMap, configsMap)
break
}
Expand All @@ -85,20 +88,48 @@ export class SMTPService implements Handler {
if(this.smtpConfig && this.smtpConfig.from_email){
for (const p of providersSet) {
if (p['dest'] == "smtp") {
let userId = p['configId']
let recipient = p['recipient']
let configKey = '';
if(recipient) {
configKey = p['dest'] + '-' + recipient
}else{
configKey = p['dest'] + '-' + userId
}
if (!configsMap.get(configKey)) {
await this.processNotification(userId, recipient, event, sesTemplate, setting, p, emailMap)
await this.processNotification(recipient, event, sesTemplate, setting, p, emailMap)
configsMap.set(configKey, true)
}
}
};
}
}
} catch (error) {
this.logger.error('getDefaultConfig', error)
throw new CustomError("Unable to send SMTP notification",500);
}
}

private async getConfigById(id,providersSet, event: Event, sesTemplate: NotificationTemplates, setting: NotificationSettings, emailMap: Map<string, boolean>, configsMap: Map<string, boolean> ){
try {
const config = await this.smtpConfigRepository.findBySMTPConfigId(id)
this.smtpConfig = {
port: config['port'],
host: config['host'],
auth_user: config['auth_user'],
auth_password: config['auth_password'],
from_email: config['from_email']
}
if(this.smtpConfig && this.smtpConfig.from_email){
for (const p of providersSet) {
if (p['dest'] == "smtp") {
let recipient = p['recipient']
let configKey = '';
if(recipient) {
configKey = p['dest'] + '-' + recipient
}
if (!configsMap.get(configKey)) {
await this.processNotification(recipient, event, sesTemplate, setting, p, emailMap)
configsMap.set(configKey, true)
}
}
}
}
} catch (error) {
this.logger.error('getDefaultConfig', error)
Expand Down Expand Up @@ -150,34 +181,24 @@ export class SMTPService implements Handler {
catch(error: any) {
this.logger.error(error.message);
await this.saveNotificationEventFailureLog(event, p, setting);
};
}
} else {
try {
const result = this.sendNotification(event, sdk, smtpTemplate.template_payload)
await this.saveNotificationEventSuccessLog(result, event, p, setting);}
catch(error: any) {
this.logger.error(error.message);
await this.saveNotificationEventFailureLog(event, p, setting);
};
}
}
}

private async processNotification(userId: number, recipient: string, event: Event, smtpTemplate: NotificationTemplates, setting: NotificationSettings, p: string, emailMap: Map<string, boolean>) {
if(userId) {
const user = await this.usersRepository.findByUserId(userId)
if (!user) {
this.logger.info('no user found for id - ' + userId)
this.logger.info(event.correlationId)
return
}
await this.sendEmailIfNotDuplicate(user['email_id'], event, smtpTemplate, setting, p, emailMap)
}else{
if (!recipient) {
this.logger.error('recipient is blank')
return
}
await this.sendEmailIfNotDuplicate(recipient, event, smtpTemplate, setting, p, emailMap)
private async processNotification(recipient: string, event: Event, smtpTemplate: NotificationTemplates, setting: NotificationSettings, p: string, emailMap: Map<string, boolean>) {
if (!recipient) {
this.logger.error('recipient is blank')
return
}
await this.sendEmailIfNotDuplicate(recipient, event, smtpTemplate, setting, p, emailMap)
}

private async sendEmailIfNotDuplicate(recipient : string, event: Event, smtpTemplate: NotificationTemplates, setting: NotificationSettings, p: string, emailMap: Map<string, boolean>) {
Expand Down