All files / src/services email.ts

86% Statements 43/50
50% Branches 3/6
76.92% Functions 10/13
84.44% Lines 38/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 1421x 1x 1x   1x         1x   1x 1x 1x         1x 1x             1x 1x 3x                       1x 1x 1x           1x     1x                   1x                     1x 1x           1x       1x 1x           1x       1x 3x 3x 3x 3x     3x         3x           1x 3x                                     3x 3x     3x 3x        
import AWS from 'aws-sdk';
import ejs from 'ejs';
import path from 'path';
import {sendQuestion} from '../controllers/types';
import errorMessages from '../utils/errorMessages';
 
// const nodemailer = require('nodemailer');
// const sendInBlue = require('nodemailer-sendinblue-transport');
// const transporter = nodemailer.createTransport(sendInBlue({ apiKey: process.env.MAIL_API_KEY }));
const otpExpiresIn: any = process.env.OTP_EXPIRES_IN
// const mailFrom = process.env.MAIL_FROM
const SESsource = process.env.SES_SOURCE || "Buzz@SawSells.com"
const salesMail = process.env.SALES_MAIL || "sales@saw.com"
let sesConfig = {
  accessKeyId: process.env.SES_ACCESS_KEY,
  secretAccessKey: process.env.SES_SECRET_KEY,
  region: process.env.SES_REGION
}
AWS.config.update(sesConfig);
console.log(process.env.SES_SOURCE)
// interface emailPayload {
//   email: string,
//   name: string
// }
 
interface UserType {name: string, email: string, UID: string}
const ses = new AWS.SES({apiVersion: "2010-12-01"});
let templateObject = (user: UserType, payload?: any) => {
  return {
    userName: user.name,
    appURL: process.env.APP_URL,
    unsubscribelink: process.env.APP_URL + "/unsubscribe",
    confirmationEmail: process.env.APP_URL + "/verify-email?UID=" + user.UID,
    aboutLink: process.env.APP_URL + "/about",
    eBookLink: process.env.API_URL + "/assets/saw-e-book.pdf",
    appLogo: process.env.API_URL + "/assets/saw-logo.png",
    hireAgentLink: process.env.APP_URL + "/hire-consultant",
    ...payload
  }
}
let templateDir = path.resolve(__dirname, '../../src/templates')
export const sendPromoCodeMail = async (user: UserType, promoDetails: {promoCode: string, validThru: string}) => {
  let emailPayload = {
    user,
    htmlObject: promoDetails,
    subject: 'Saw Buyer Broker - Promo code',
    fileName: "/promoCode.ejs"
  }
  return await htmlConfiguration(emailPayload)
};
 
export const sendContactUsMail = async (body: sendQuestion) => {
  let emailPayload = {
    user: {UID: "", name: "", email: salesMail},
    htmlObject: {userEmail: body.email, userComments: body.comments},
    subject: 'Saw Buyer Broker - FAQ',
    fileName: "/sendQuestion.ejs"
  }
  return await htmlConfiguration(emailPayload)
};
 
export const sendPaidSuccessEmail = async (user: UserType) => {
  let surveyLink = process.env.APP_URL + `/survey?email=${user.email}`
  const mailOptions = {
    user,
    subject: 'Saw Buyer Broker - Survey ', // Subject line
    htmlObject: {surveyLink: surveyLink, },
    fileName: "/paid-response.ejs"
  };
  return await htmlConfiguration(mailOptions)
};
 
export const sendOTP = async (user: UserType, OTP: number): Promise<any> => {
  const mailOptions = {
    user,
    subject: 'Saw Buyer Broker - OTP', // Subject line
    htmlObject: {OTP, otpExpiresIn: otpExpiresIn / 60000},
    fileName: "/forgotpassword.ejs"
  };
  return await htmlConfiguration(mailOptions)
 
};
 
export const welcomeEmail = (user: any) => {
  const mailOptions = {
    user,
    subject: 'Please Verify Email – Saw.com Brokerage', // Subject line
    htmlObject: user,
    fileName: "/verify-email.ejs"
  };
  htmlConfiguration(mailOptions)
 
};
 
const htmlConfiguration = async (payload: {user: UserType, htmlObject: any, subject: string, fileName: string}) => {
  let {user, htmlObject, subject, fileName} = payload
  return new Promise(async (resolve, reject) => {
    await ejs.renderFile(templateDir + fileName, templateObject(user, htmlObject), (err, data) => {
      Iif (err) {
        reject(err)
      } else {
        const mailOptions = {
          to: user.email,
          subject,
          html: data
        };
        resolve(sendSESail(mailOptions))
      }
    })
  })
}
 
const sendSESail = async (mailOptions: {fromEmail?: string, to: string, subject: string, html: string}) => {
  const params = {
    Destination: {
      ToAddresses: [mailOptions.to]
    },
    Message: {
      Body: {
        Html: {
          Charset: "UTF-8",
          Data: mailOptions.html
        },
      },
      Subject: {
        Charset: "UTF-8",
        Data: mailOptions.subject
      }
    },
    Source: SESsource
  };
 
  return new Promise((resolve, reject) => {
    ses.sendEmail(params).promise().then(data => {
      resolve(data)
    }).catch(error => {
      reject(error)
      console.error(errorMessages.errorInSendingMailTo + ": " + mailOptions.to, error)
    });
  })
}