Skip to content

Commit

Permalink
fix(mailer): missing data elements in mailer template #480
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Feb 26, 2022
1 parent cb72e75 commit cd54631
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
42 changes: 25 additions & 17 deletions src/controllers/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
var _ = require('lodash')
var async = require('async')
var path = require('path')
var winston = require('winston')
var winston = require('../logger')

var debugController = {}

Expand Down Expand Up @@ -468,30 +468,38 @@ debugController.sendmail = function (req, res) {
var mailer = require('../mailer')
var templateSchema = require('../models/template')
var Email = require('email-templates')
// var templateDir = path.resolve(__dirname, '..', 'mailer', 'templates')
var templateDir = path.resolve(__dirname, '..', 'mailer', 'templates')

var to = req.query.email
if (to === undefined) {
return res.status(400).send('Invalid Email in querystring "email"')
}

var email = new Email({
render: function (view, locals) {
return new Promise(function (resolve, reject) {
if (!global.Handlebars) return reject(new Error('Could not load global.Handlebars'))
templateSchema.findOne({ name: view }, function (err, template) {
if (err) return reject(err)
if (!template) return reject(new Error('Invalid Template'))
var html = global.Handlebars.compile(template.data['gjs-fullHtml'])(locals)
console.log(html)
email.juiceResources(html).then(resolve)
})
})
views: {
root: templateDir,
options: {
extension: 'handlebars'
}
}
// This is to test the new email templates (beta feature)
// render: function (view, locals) {
// return new Promise(function (resolve, reject) {
// if (!global.Handlebars) return reject(new Error('Could not load global.Handlebars'))
// templateSchema.findOne({ name: view }, function (err, template) {
// if (err) return reject(err)
// if (!template) return reject(new Error('Invalid Template'))
// var html = global.Handlebars.compile(template.data['gjs-fullHtml'])(locals)
// console.log(html)
// email.juiceResources(html).then(resolve)
// })
// })
// }
})

var ticket = {
uid: 100001,
issue: 'This is the test issue',
comments: [
{
date: new Date(),
Expand All @@ -505,11 +513,11 @@ debugController.sendmail = function (req, res) {
}

email
.render('ticket-updated', { base_url: global.TRUDESK_BASEURL, ticket: ticket })
.render('ticket-comment-added', { base_url: global.TRUDESK_BASEURL, ticket: ticket, comment: ticket.comments[0] })
.then(function (html) {
var mailOptions = {
to: to,
subject: 'Trudesk Test Email [Debugger]',
subject: 'Trudesk Test Email #' + ticket.uid + ' [Debugger]',
html: html,
generateTextFromHTML: true
}
Expand All @@ -521,8 +529,8 @@ debugController.sendmail = function (req, res) {
})
})
.catch(function (err) {
winston.warn(err)
res.status(400).send(err)
console.log(err)
res.status(400).json({ error: err })
})
}

Expand Down
8 changes: 8 additions & 0 deletions src/controllers/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ mainController.forgotL2Auth = function (req, res) {
}
})

savedUser = savedUser.toJSON()

var data = {
base_url: req.protocol + '://' + req.get('host'),
user: savedUser
Expand Down Expand Up @@ -277,6 +279,8 @@ mainController.forgotPass = function (req, res) {

var email = null

savedUser = savedUser.toJSON()

var data = {
base_url: req.protocol + '://' + req.get('host'),
user: savedUser
Expand Down Expand Up @@ -412,6 +416,8 @@ mainController.resetl2auth = function (req, res) {
}
})

updated = updated.toJSON()

email
.render('l2auth-cleared', user)
.then(function (html) {
Expand Down Expand Up @@ -491,6 +497,8 @@ mainController.resetPass = function (req, res) {
}
})

updated = updated.toJSON()

var data = {
password: gPass,
user: updated
Expand Down
4 changes: 4 additions & 0 deletions src/emitter/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ var notifications = require('../notifications') // Load Push Events
if (err) return c(err)
if (!template) return c()

ticket = ticket.toJSON()

var context = { base_url: baseUrl, ticket: ticket }

email
Expand Down Expand Up @@ -504,6 +506,8 @@ var notifications = require('../notifications') // Load Push Events
if (err) winston.warn(err)
if (err) return c()

ticket = ticket.toJSON()

email
.render('ticket-comment-added', {
ticket: ticket,
Expand Down

0 comments on commit cd54631

Please sign in to comment.