import * as AWS from 'aws-sdk' ; import { debugBase } from '../debuggers' ; import messageBroker from '../messageBroker' ; import { Configs , DeliveryReports , Stats } from '../models' ; import { ISESConfig } from '../models/Configs' ; export const getApi = async ( type ) : Promise < any > => { const config = await Configs . getSESConfigs ( ) ; if ( ! config ) { return ; } AWS . config . update ( config ) ; if ( type === 'ses' ) { return new AWS . SES ( ) ; } return new AWS . SNS ( ) ; } ; const handleMessage = async ( message ) => { let parsedMessage ; try { parsedMessage = JSON . parse ( message ) ; } catch ( e ) { parsedMessage = message ; } const { eventType , mail } = parsedMessage ; const { headers } = mail ; const engageMessageId = headers . find ( header => header . name === 'Engagemessageid' ) ; const mailId = headers . find ( header => header . name === 'Mailmessageid' ) ; const customerId = headers . find ( header => header . name === 'Customerid' ) ; const emailDeliveryId = headers . find ( header => header . name === 'Emaildeliveryid' ) ; const type = eventType . toLowerCase ( ) ; if ( emailDeliveryId ) { return messageBroker ( ) . sendMessage ( 'engagesNotification' , { action : 'transactionEmail' , data : { emailDeliveryId : emailDeliveryId . value , status : type } , } ) ; } const mailHeaders = { engageMessageId : engageMessageId . value , mailId : mailId . value , customerId : customerId . value , } ; await Stats . updateStats ( mailHeaders . engageMessageId , type ) ; const rejected = await DeliveryReports . updateOrCreateReport ( mailHeaders , type ) ; if ( rejected === 'reject' ) { await messageBroker ( ) . sendMessage ( 'engagesNotification' , { action : 'setDoNotDisturb' , data : { customerId : mail . customerId } , } ) ; } return true ; } ; export const trackEngages = expressApp => { expressApp . post ( `/service/engage/tracker` , async ( req , res ) => { const chunks = [ ] ; req . setEncoding ( 'utf8' ) ; req . on ( 'data' , chunk => { chunks . push ( chunk ) ; } ) ; req . on ( 'end' , async ( ) => { const message = JSON . parse ( chunks . join ( '' ) ) ; debugBase ( 'receiving on tracker:' , message ) ; const { Type = '' , Message = { } , Token = '' , TopicArn = '' } = message ; if ( Type === 'SubscriptionConfirmation' ) { await getApi ( 'sns' ) . then ( api => api . confirmSubscription ( { Token , TopicArn } ) . promise ( ) ) ; return res . end ( 'success' ) ; } if ( Message === 'Successfully validated SNS topic for Amazon SES event publishing.' ) { res . end ( 'success' ) ; } await handleMessage ( Message ) ; return res . end ( 'success' ) ; } ) ; } ) ; } ; export const awsRequests = { async getVerifiedEmails ( ) { const api = await getApi ( 'ses' ) ; return new Promise ( ( resolve , reject ) => { api . listVerifiedEmailAddresses ( ( error , data ) => { if ( error ) { return reject ( error ) ; } return resolve ( data . VerifiedEmailAddresses ) ; } ) ; } ) ; } , async verifyEmail ( email ) { const api = await getApi ( 'ses' ) ; return new Promise ( ( resolve , reject ) => { api . verifyEmailAddress ( { EmailAddress : email } , ( error , data ) => { if ( error ) { return reject ( error ) ; } return resolve ( data ) ; } ) ; } ) ; } , async removeVerifiedEmail ( email ) { const api = await getApi ( 'ses' ) ; return new Promise ( ( resolve , reject ) => { api . deleteVerifiedEmailAddress ( { EmailAddress : email } , ( error , data ) => { if ( error ) { return reject ( error ) ; } return resolve ( data ) ; } ) ; } ) ; } , } ;