Skip to content

Commit

Permalink
Ticket Subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Nov 7, 2015
1 parent fd42738 commit b75be7f
Show file tree
Hide file tree
Showing 18 changed files with 385 additions and 82 deletions.
41 changes: 40 additions & 1 deletion src/controllers/api/v1/tickets.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ api_tickets.get = function(req, res) {
* @apiVersion 0.1.0
* @apiGroup Ticket
* @apiHeader {string} accesstoken The access token for the logged in user
*
* @apiParamExample {json} Request-Example:
* {
* "subject": "Subject",
* }
*
* @apiExample Example usage:
* curl -H "accesstoken: {accesstoken}" -l http://localhost/api/v1/tickets
*
Expand Down Expand Up @@ -159,6 +165,7 @@ api_tickets.create = function(req, res) {
ticket.issue = marked(tIssue);
ticket.tags = tags;
ticket.history = [HistoryItem];
ticket.subscribers = [req.user._id];
ticket.save(function(err, t) {
if (err) {
response.success = false;
Expand Down Expand Up @@ -405,7 +412,6 @@ api_tickets.removeAttachment = function(req, res) {
var fs = require('fs');
var path = require('path');
var dir = path.join(__dirname, '../../../../public', a.path);
console.log(dir);
if (fs.existsSync(dir)) fs.unlinkSync(dir);

ticket.save(function(err, t) {
Expand All @@ -417,4 +423,37 @@ api_tickets.removeAttachment = function(req, res) {
});
};

api_tickets.subscribe = function(req, res) {
var ticketId = req.params.id;
var data = req.body;
if (_.isUndefined(data.user) || _.isUndefined(data.subscribe)) return res.status(400).json({'error': 'Invalid Payload.'});

var ticketModel = require('../../../models/ticket');
ticketModel.getTicketById(ticketId, function(err, ticket) {
if (err) return res.status(400).json({'error': 'Invalid Ticket Id'});

async.series([
function(callback) {
if (data.subscribe) {
ticket.addSubscriber(data.user, function() {
callback();
});
} else {
ticket.removeSubscriber(data.user, function() {
callback();
});
}
}
], function() {
ticket.save(function(err) {
if (err) return res.status(400).json({'error': err});

emitter.emit('ticket:subscribers:update');

res.json({'success': true});
});
});
});
};

module.exports = api_tickets;
6 changes: 6 additions & 0 deletions src/controllers/api/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ api_users.update = function(req, res) {
* @apiExample Example usage:
* curl -H "Content-Type: application/json" -H "accesstoken: {accesstoken}" -X PUT -d "{\"preference\":\"{preference_name}\",\"value\":{value}}" -l http://localhost/api/v1/users/{username}/updatepreferences
*
* @apiParamExample {json} Request:
* {
* "preference": "preference_name",
* "value": "preference_value"
* }
*
* @apiSuccess {object} user Saved User Object [Stripped]
*
* @apiError InvalidPostData The data was invalid
Expand Down
73 changes: 73 additions & 0 deletions src/controllers/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
. .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
========================================================================
Created: 11/06/2015
Author: Chris Brame
**/

var async = require('async');
var path = require('path');
var _ = require('underscore');
var _s = require('underscore.string');
var flash = require('connect-flash');
var userSchema = require('../models/user');
var reports = require('../models/report');
var permissions = require('../permissions');
var mongoose = require('mongoose');
var winston = require('winston');

var debugController = {};

debugController.content = {};

debugController.sendmail = function(req, res, next) {
var mailer = require('../mailer');
var emailTemplates = require('email-templates');
var templateDir = path.resolve(__dirname, '..', 'mailer', 'templates');

emailTemplates(templateDir, function(err, template) {
if (err) {
winston.error(err);
} else {

template('ticket-updated', function(err, html) {
if (err) {
winston.error(err);
} else {
var mailOptions = {
from: 'no-reply@trudesk.io',
to: 'chris.brame@granvillecounty.org',
subject: 'Trudesk Launch',
html: html,
generateTextFromHTML: true
};

mailer.sendMail(mailOptions, function(err, info) {
if (err) {
winston.warn(err);
return res.send(err);
}


return res.status(200).send('OK');
});
}
});
}
});
};

function handleError(res, err) {
if (err) {
return res.render('error', {layout: false, error: err, message: err.message});
}
}

module.exports = debugController;
4 changes: 3 additions & 1 deletion src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ var Controllers = {
groups: require('./groups'),
reports: require('./reports'),
notices: require('./notices'),
api: require('./api')
api: require('./api'),

debug: require('./debug')
};

module.exports = Controllers;
62 changes: 34 additions & 28 deletions src/emitter/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ var notifications = require('../notifications'); // Load Push Events
var mailer = require('../mailer');
var emails = [];
async.each(ticket.subscribers, function(member, cb) {
if (_.isUndefined(member.email)) return cb();
if (member._id.toString() == comment.owner._id.toString()) return cb();
if (_.isUndefined(member) || _.isUndefined(member.email)) return cb();
if (member._id.toString() == comment.owner.toString()) return cb();

emails.push(member.email);

Expand All @@ -212,38 +212,44 @@ var notifications = require('../notifications'); // Load Push Events
winston.warn('[trudesk:events:sendSubscriberEmail] - Error: ' + err.message);
return c(err);
} else {
var locals = {
ticket: ticket,
comment: comment
};

template('ticket-comment-added', locals, function(err, html) {
if (err) {
winston.warn('[trudesk:events:sendSubscriberEmail] - Error: ' + err.message);
return c(err);
} else {
var mailOptions = {
to: emails.join(),
subject: 'Updated: Ticket #' + ticket.uid + '-' + ticket.subject,
html: html,
generateTextFromHTML: true
};

mailer.sendMail(mailOptions, function(err, info) {
if (err) {
return c(err, null);
}

return c(null, info);
});
}
ticket.populate('comments.owner', function(err, ticket) {
if (err) return true;

var locals = {
ticket: ticket,
comment: comment
};

template('ticket-comment-added', locals, function(err, html) {
if (err) {
winston.warn('[trudesk:events:sendSubscriberEmail] - Error: ' + err.message);
return c(err);
} else {
var mailOptions = {
to: emails.join(),
subject: 'Updated: Ticket #' + ticket.uid + '-' + ticket.subject,
html: html,
generateTextFromHTML: true
};

mailer.sendMail(mailOptions, function(err, info) {
if (err) {
winston.warn('[trudesk:events:sendSubscriberEmail] - Error: ' + err.message);
return c();
}

winston.debug('Sent Subscriber Mail.');
return c(null, info);
});
}
});
});
}
});
});
}
], function(err, result) {

//Blank
});
});
})();
11 changes: 11 additions & 0 deletions src/helpers/hbs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,8 @@ var helpers = {

checkPerm: function(user, perm, options) {
var P = require('../../permissions');
if (_.isUndefined(user)) return options.inverse(this);

if (P.canThis(user.role, perm)) {
return options.fn(this);
} else {
Expand Down Expand Up @@ -528,6 +530,15 @@ var helpers = {
} else {
return options.inverse(this);
}
},

isSubscribed: function(arr, value) {
var result = _.some(arr, function(i) {
if (_.isUndefined(i) || _.isUndefined(value)) return false;
return i._id.toString() == value.toString();
});

return !!result;
}
};

Expand Down
8 changes: 8 additions & 0 deletions src/helpers/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ module.exports.sendToSelf = function (socket, method, data) {
socket.emit(method, data);
};

module.exports._sendToSelf = function(io, socketId, method, data) {
__.each(io.sockets.sockets, function(socket) {
if (socket.id == socketId) {
socket.emit(method, data);
}
});
};

module.exports.sendToAllConnectedClients = function (io, method, data) {
io.sockets.emit(method, data);
};
Expand Down
1 change: 1 addition & 0 deletions src/mailer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var transporter = nodeMailer.createTransport({
pass: nconf.get('mailer:password') ? nconf.get('mailer:password') : ''
},
tls: {
rejectUnauthorized: false,
ciphers: 'SSLv3'
}
});
Expand Down
49 changes: 49 additions & 0 deletions src/mailer/templates/ticket-comment-added/html.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Updated: Ticket #{{ticket.uid}}</title>
<style>
h1,h2,h3,h4,h5,h6 {
margin-bottom: 2px;
}
hr {
display: block;
margin: 0;
padding: 0;
width: 100%;
background: #ccc;
height: 3px;
border: none;
}
</style>
</head>
<body>
<div>
<h2>Updated: {{{ticket.subject}}}</h2>
<h5>Ticket Group: {{ticket.group.name}}</h5>
<h5>Ticket Submitted by: {{ticket.owner.fullname}}</h5>
<h5>Submitted Date: {{ticket.date}}</h5>
<h5>Ticket Type: {{ticket.type.name}}</h5>
<h5>Ticket Priority: {{ticket.priority}}</h5>
<h5>Tags: {{ticket.tags}}</h5>

<br/><br/>
<h4>Ticket Issue</h4>
<hr/>
<p class="issue">{{{ticket.issue}}}</p>

<br/><br/>
<h4>Comments</h4>
<hr/>
{{#each ticket.comments}}
<h5>{{owner.fullname}}</h5>
<h5>{{date}}</h5>
<br/>
<p class="issue">{{{comment}}}</p>
<br/>
<hr/>
{{/each}}
</div>
</body>
</html>
37 changes: 37 additions & 0 deletions src/mailer/templates/ticket-updated/html.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Ticket #{{ticket.uid}}</title>
<style>
h1,h2,h3,h4,h5,h6 {
margin-bottom: 2px;
}
hr {
display: block;
margin: 0;
padding: 0;
width: 100%;
background: #ccc;
height: 3px;
border: none;
}
</style>
</head>
<body>
<div>
<h2>{{{ticket.subject}}}</h2>
<h5>Ticket Group: {{ticket.group.name}}</h5>
<h5>Ticket Submitted by: {{ticket.owner.fullname}}</h5>
<h5>Submitted Date: {{ticket.date}}</h5>
<h5>Ticket Type: {{ticket.type.name}}</h5>
<h5>Ticket Priority: {{ticket.priority}}</h5>
<h5>Tags: {{ticket.tags}}</h5>

<br/><br/>
<h4>Ticket Issue</h4>
<hr/>
<p class="issue">{{{ticket.issue}}}</p>
</div>
</body>
</html>
Loading

0 comments on commit b75be7f

Please sign in to comment.