Skip to content

Commit

Permalink
chore(editor): mail templates beta
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Jan 30, 2019
1 parent 4668a9f commit 47fde44
Show file tree
Hide file tree
Showing 29 changed files with 9,456 additions and 307 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ sonar-project.properties
public/uploads/assets/*.*
/src/backup/bin/
backups/

public/uploads/assets/upload/
24 changes: 19 additions & 5 deletions src/controllers/api/v1/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@
**/

var async = require('async')

var _ = require('lodash')

var winston = require('winston')

var sanitizeHtml = require('sanitize-html')

var SettingsSchema = require('../../../models/setting')

var settingsUtil = require('../../../settings/settingsUtil')

var apiSettings = {}
Expand Down Expand Up @@ -119,6 +114,25 @@ apiSettings.testMailer = function (req, res) {
})
}

apiSettings.updateTemplateSubject = function (req, res) {
var templateSchema = require('../../../models/template')
var id = req.params.id
var subject = req.body.subject
if (!subject) return res.status(400).json({ sucess: false, error: 'Invalid PUT data' })
subject = subject.trim()

templateSchema.findOne({ _id: id }, function (err, template) {
if (err) return defaultApiResponse(err, res)
if (!template) return res.status(404).json({ success: false, error: 'No Template Found' })

template.subject = subject

template.save(function (err) {
return defaultApiResponse(err, res)
})
})
}

apiSettings.buildsass = function (req, res) {
var buildsass = require('../../../sass/buildsass')
buildsass.build(function (err) {
Expand Down
35 changes: 28 additions & 7 deletions src/controllers/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,25 +463,46 @@ function randomDate (start, end) {

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({
views: {
root: templateDir,
options: {
extension: 'handlebars'
}
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,
comments: [
{
date: new Date(),
comment: 'TESTING',
owner: {
fullname: 'test user',
email: '[email protected]'
}
}
]
}

email
.render('ticket-updated', {})
.render('ticket-updated', { base_url: global.TRUDESK_BASEURL, ticket: ticket })
.then(function (html) {
var mailOptions = {
to: to,
Expand Down
180 changes: 180 additions & 0 deletions src/controllers/editor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* . .o8 oooo
* .o8 "888 `888
* .o888oo oooo d8b oooo oooo .oooo888 .ooooo. .oooo.o 888 oooo
* 888 `888""8P `888 `888 d88' `888 d88' `88b d88( "8 888 .8P'
* 888 888 888 888 888 888 888ooo888 `"Y88b. 888888.
* 888 . 888 888 888 888 888 888 .o o. )88b 888 `88b.
* "888" d888b `V88V"V8P' `Y8bod88P" `Y8bod8P' 8""888P' o888o o888o
* ========================================================================
* Author: Chris Brame
* Updated: 1/24/19 11:50 PM
* Copyright (c) 2014-2019. All rights reserved.
*/

var _ = require('lodash')
var path = require('path')
var fs = require('fs-extra')
var Busboy = require('busboy')
var templateSchema = require('../models/template')

var editor = {}

editor.page = function (req, res) {
var content = {}
content.title = 'Editor'
content.nav = 'settings'

content.data = {}
content.data.user = req.user
content.data.common = req.viewdata
content.data.template = req.params.template

return res.render('editor', content)
}

editor.getAssets = function (req, res) {
var imageExts = ['.gif', '.png', '.jpg', '.jpeg', '.ico', '.bmp']

fs.readdir(path.join(__dirname, '../../public/uploads/assets/upload'), function (err, files) {
if (err) return res.status(400).json({ success: false, error: err })

files = files.filter(function (file) {
return _.indexOf(imageExts, path.extname(file).toLowerCase() !== -1)
})

files = _.map(files, function (i) {
return { src: '/uploads/assets/upload/' + i }
})

return res.json({ success: true, assets: files })
})
}

editor.removeAsset = function (req, res) {
var id = req.body.fileUrl
if (!id) return res.status(400).json({ success: false, error: 'Invalid File' })

var file = path.basename(id)
fs.unlink(path.join(__dirname, '../../public/uploads/assets/upload', file), function (err) {
if (err) return res.status(500).json({ success: false, error: err })

return res.json({ success: true })
})
}

editor.assetsUpload = function (req, res) {
// var chance = new Chance()
var busboy = new Busboy({
headers: req.headers,
limits: {
files: 1,
fileSize: 5 * 1024 * 1024 // 5mb limit
}
})

var object = {}
var error

busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
if (mimetype.indexOf('image/') === -1) {
error = {
status: 500,
message: 'Invalid File Type'
}

return file.resume()
}

// var ext = path.extname(filename)

var savePath = path.join(__dirname, '../../public/uploads/assets/upload')
// var sanitizedFilename = chance.hash({ length: 20 }) + ext
if (!fs.existsSync(savePath)) fs.ensureDirSync(savePath)

object.filePath = path.join(savePath, filename)
object.filename = filename
object.mimetype = mimetype

if (fs.existsSync(object.filePath)) {
error = {
status: 500,
message: 'File already exists'
}

return file.resume()
}

file.on('limit', function () {
error = {
status: 500,
message: 'File too large'
}

return file.resume()
})

file.pipe(fs.createWriteStream(object.filePath))
})

busboy.on('finish', function () {
if (error) return res.status(error.status).json({ success: false, error: error })

if (_.isUndefined(object.filename) || _.isUndefined(object.filePath)) {
return res.status(400).json({ success: false, error: { message: 'Invalid Form Data' } })
}

// Everything Checks out lets make sure the file exists and then add it to the attachments array
if (!fs.existsSync(object.filePath))
return res.status(500).json({ success: false, error: { message: 'File Failed to Save to Disk' } })

var includePort = global.TRUDESK_PORT && global.TRUDESK_PORT !== (80 || 443)

var fileUrl =
req.protocol +
'://' +
req.hostname +
(includePort ? ':' + global.TRUDESK_PORT.toString() : '') +
'/uploads/assets/upload/' +
object.filename

// var dimensions = sizeOf(fileUrl)

return res.json({
success: true,
data: [fileUrl]
})
})

req.pipe(busboy)
}

editor.load = function (req, res) {
templateSchema.get(req.params.id, function (err, template) {
if (err) return res.status(400).json({ success: false, error: err })

if (!template)
return res.status(400).json({ success: false, invalid: true, error: { message: 'Invalid Template.' } })

template.data.id = 'gjs-'

return res.json(template.data)
})
}

editor.save = function (req, res) {
var name = req.body.template
delete req.body.template
templateSchema.findOneAndUpdate(
{ name: name },
{ name: name, data: req.body },
{ new: true, upsert: true },
function (err, template) {
if (err) return res.status(500).json({ success: false, error: err })

return res.json({ success: true, tempalte: template })
}
)
}

module.exports = editor
1 change: 1 addition & 0 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var Controllers = {
notices: require('./notices'),
plugins: require('./plugins'),
settings: require('./settings'),
editor: require('./editor'),
backuprestore: require('./backuprestore'),
api: require('./api'),

Expand Down
Loading

0 comments on commit 47fde44

Please sign in to comment.