import { MAIL_CONFIG } from "../../config/default.config" ; const marked = require ( "marked" ) ; const nodemailer = require ( "nodemailer" ) ; const nunjucks = require ( "nunjucks" ) ; const path = require ( "path" ) ; const transporter = nodemailer . createTransport ( { host : MAIL_CONFIG . service , secureConnection : true , port : MAIL_CONFIG . port , auth : { user : MAIL_CONFIG . user_email , pass : MAIL_CONFIG . auth_code , } , } ) ; const htmlModel = ( params ) => { const { storyMail , exitInfo , summitUser , iterationMail } = params ; const html = nunjucks . render ( path . join ( __dirname , "./emailTpl/email.njk" ) , { storyMail , exitInfo , summitUser , iterationMail , } ) ; return html ; } ; const htmlPassModel = ( { storyMail , testInfo , testUser , iterationMail } ) => { const html = nunjucks . render ( path . join ( __dirname , "./emailTpl/pass.njk" ) , { storyMail , testInfo , testUser , iterationMail , } ) ; return html ; } ; const htmlRepulseModel = ( { storyMail , repulseUser , repulseInfo , iterationMail , } ) => { const html = nunjucks . render ( path . join ( __dirname , "./emailTpl/repulse.njk" ) , { storyMail , repulseUser , repulseInfo , iterationMail , } ) ; return html ; } ; interface mailInterface { toEmail : string ; subject : string ; cc ? : string ; text ? : string ; html ? : any ; attachments ? : any ; storyMail ? : any ; exitInfo ? : any ; summitUser ? : String ; iterationMail ? : any ; } const sendMail = async ( mailOptions ) => { const { toEmail , subject , cc , text , attachments , storyMail , exitInfo , summitUser , iterationMail , } = mailOptions ; Object . keys ( exitInfo ) . forEach ( ( key ) => { exitInfo [ key ] = marked ( exitInfo [ key ] ) ; } ) ; const html = htmlModel ( { storyMail , exitInfo , summitUser , iterationMail } ) ; const mailOpts = { from : MAIL_CONFIG . user_email , to : toEmail , subject , cc , text , html , attachments , } ; try { transporter . sendMail ( mailOpts ) ; return true ; } catch ( err ) { console . log ( err ) ; return false ; } } ; const sendPassMail = async ( { toEmail , subject , cc , text , attachments , storyMail , testInfo , testUser , iterationMail , } ) => { testInfo = marked ( testInfo ) ; const html = htmlPassModel ( { storyMail , testInfo , testUser , iterationMail } ) ; const mailOpts = { from : MAIL_CONFIG . user_email , to : toEmail , subject , cc , text , html , attachments , } ; try { transporter . sendMail ( mailOpts ) ; return true ; } catch ( err ) { console . log ( err ) ; return false ; } } ; const sendRepulseMail = async ( { toEmail , subject , cc , text , attachments , storyMail , repulseUser , repulseInfo , iterationMail , } ) => { repulseInfo = marked ( repulseInfo ) ; const html = htmlRepulseModel ( { storyMail , repulseUser , repulseInfo , iterationMail , } ) ; const mailOpts = { from : MAIL_CONFIG . user_email , to : toEmail , subject , cc , text , html , attachments , } ; try { transporter . sendMail ( mailOpts ) ; return true ; } catch ( err ) { console . log ( err ) ; return false ; } } ; export default { sendMail , sendPassMail , sendRepulseMail } ;