Skip to content

Commit

Permalink
cleanup(code)
Browse files Browse the repository at this point in the history
cleanup(dedupe)
  • Loading branch information
polonel committed Sep 20, 2018
1 parent 6f432db commit b39719e
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 139 deletions.
48 changes: 20 additions & 28 deletions src/controllers/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,9 @@ accountsController.bindLdap = function(req, res) {

mappedUsernames = _.map(foundUsers, 'username');

for (var i = 0; i < mappedUsernames.length; i++) {
var u = _.find(entries, function(f) { return f.sAMAccountName.toLowerCase() === mappedUsernames[i].toLowerCase(); });

_.each(mappedUsernames, function(mappedUsername) {
var u = _.find(entries, function(f) { return f.sAMAccountName.toLowerCase() === mappedUsername.toLowerCase(); });

if (u) {
var clonedUser = _.find(foundUsers, function(g) { return g.username.toLowerCase() === u.sAMAccountName.toLowerCase();});
Expand All @@ -223,8 +224,8 @@ accountsController.bindLdap = function(req, res) {
}
}

_.remove(entries, function(k) { return k.sAMAccountName.toLowerCase() === mappedUsernames[i].toLowerCase(); });
}
_.remove(entries, function(k) { return k.sAMAccountName.toLowerCase() === mappedUsername.toLowerCase(); });
});

_.remove(entries, function(e) { return _.isUndefined(e.mail); });

Expand All @@ -235,6 +236,19 @@ accountsController.bindLdap = function(req, res) {
});
};

function processUsers(addedUserArray, updatedUserArray, item, callback) {
userSchema.getUserByUsername(item.username, function(err, user) {
if (err) return callback(err);

if (user)
updatedUserArray.push(item);
else
addedUserArray.push(item);

return callback();
});
}

accountsController.uploadCSV = function(req, res) {
var csv = require('fast-csv');
var Busboy = require('busboy');
Expand Down Expand Up @@ -299,17 +313,7 @@ accountsController.uploadCSV = function(req, res) {
var updatedUsers = [];

async.each(object.csv, function(item, next) {
userSchema.getUserByUsername(item.username, function(err, user) {
if (err) return next(err);

if (user)
updatedUsers.push(item);
else
addedUsers.push(item);


return next();
});
return processUsers(addedUsers, updatedUsers, item, next);
}, function(err) {
if (err) {
winston.warn(err.message);
Expand All @@ -318,8 +322,6 @@ accountsController.uploadCSV = function(req, res) {

return res.json({success: true, contents: object.csv, addedUsers: addedUsers, updatedUsers: updatedUsers});
});


});

req.pipe(busboy);
Expand Down Expand Up @@ -359,17 +361,7 @@ accountsController.uploadJSON = function(req, res) {
return res.status(400).json({success: false, error: 'No accounts defined in JSON file.'});

async.eachSeries(accounts, function(item, next) {
userSchema.getUserByUsername(item.username, function(err, user) {
if (err) return next(err);

if (user)
updatedUsers.push(item);
else
addedUsers.push(item);


return next();
});
return processUsers(addedUsers, updatedUsers, item, next);
}, function(err) {
if (err)
return res.status(400).json({success: false, error: err});
Expand Down
101 changes: 7 additions & 94 deletions src/controllers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var jsStringEscape = require('js-string-escape');
var settingSchema = require('../models/setting');
var ticketTypeSchema = require('../models/tickettype');
var permissions = require('../permissions');
var settingsUtil = require('../settings/settingsUtil');

var settingsController = {};

Expand Down Expand Up @@ -52,103 +53,15 @@ function handleError(res, err) {
return res.render('error', {layout: false, error: err, message: err.message});
}

function parseSetting(settings, name, defaultValue) {
var s = _.find(settings, function(x) { return x.name === name; });
s = _.isUndefined(s) ? {value: defaultValue} : s;

return s;
}

function getSettings(content, callback) {
settingSchema.getSettings(function(err, settings) {
if (err) return callback('Invalid Settings');

var s = {};

s.siteUrl = parseSetting(settings, 'gen:siteurl', '');

s.defaultTicketType = parseSetting(settings, 'ticket:type:default', '');

s.mailerEnabled = parseSetting(settings, 'mailer:enable', false);
s.mailerHost = parseSetting(settings, 'mailer:host', '');
s.mailerSSL = parseSetting(settings, 'mailer:ssl', false);
s.mailerPort = parseSetting(settings, 'mailer:port', 25);
s.mailerUsername = parseSetting(settings, 'mailer:username', '');
s.mailerPassword = parseSetting(settings, 'mailer:password', '');
s.mailerFrom = parseSetting(settings, 'mailer:from', '');

s.mailerCheckEnabled = parseSetting(settings, 'mailer:check:enable', false);
s.mailerCheckHost = parseSetting(settings, 'mailer:check:host', '');
s.mailerCheckPort = parseSetting(settings, 'mailer:check:port', 143);
s.mailerCheckUsername = parseSetting(settings, 'mailer:check:username', '');
s.mailerCheckPassword = parseSetting(settings, 'mailer:check:password', '');
s.mailerCheckTicketType = parseSetting(settings, 'mailer:check:ticketype', '');
s.mailerCheckTicketPriority = parseSetting(settings, 'mailer:check:ticketpriority', '');
s.mailerCheckCreateAccount = parseSetting(settings, 'mailer:check:createaccount', false);
s.mailerCheckDeleteMessage = parseSetting(settings, 'mailer:check:deletemessage', true);

s.showTour = parseSetting(settings, 'showTour:enable', false);
s.showOverdueTickets = parseSetting(settings, 'showOverdueTickets:enable', true);

s.tpsEnabled = parseSetting(settings, 'tps:enable', false);
s.tpsUsername = parseSetting(settings, 'tps:username', '');
s.tpsApiKey = parseSetting(settings, 'tps:apikey', '');

s.allowPublicTickets = parseSetting(settings, 'allowPublicTickets:enable', false);
s.allowUserRegistration = parseSetting(settings, 'allowUserRegistration:enable', false);

s.privacyPolicy = parseSetting(settings, 'legal:privacypolicy', '');
s.privacyPolicy.value = jsStringEscape(s.privacyPolicy.value);

content.data.settings = s;

async.parallel([
function(done) {
ticketTypeSchema.getTypes(function(err, types) {
if (err) return done(err);

content.data.ticketTypes = _.sortBy(types, function(o){ return o.name; });
_.each(content.data.ticketTypes, function(type) {
type.priorities = _.sortBy(type.priorities, ['migrationNum', 'name']);
});

return done();
});
},
function(done) {
var ticketPrioritySchema = require('../models/ticketpriority');
ticketPrioritySchema.getPriorities(function(err, priorities) {
if (err) return done(err);

content.data.priorities = _.sortBy(priorities, ['migrationNum', 'name']);

return done();
});
},
function(done) {
var tagSchema = require('../models/tag');
tagSchema.getTagCount(function(err, count) {
if (err) return done(err);

content.data.tags = {
count: count
};

return done();
});
}
], function(err) {
if (err) return callback(err);

return callback();
});
});
}

function renderView(res, content) {
getSettings(content, function(err) {
settingsUtil.getSettings(function(err, returnedContent) {
if (err) return handleError(res, err);

content.data.settings = returnedContent.data.settings;
content.data.ticketTypes = returnedContent.data.ticketTypes;
content.data.priorities = returnedContent.data.priorities;
content.data.tags = returnedContent.data.tags;

return res.render('settings', content);
});
}
Expand Down
9 changes: 5 additions & 4 deletions src/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
**/

var mongoose = require('mongoose'),
nconf = require('nconf'),
winston = require('winston');
var _ = require('lodash'),
mongoose = require('mongoose'),
nconf = require('nconf'),
winston = require('winston');

var db = {};

Expand All @@ -36,7 +37,7 @@ var options = { keepAlive: 1, connectTimeoutMS: 30000, useNewUrlParser: true };
module.exports.init = function(callback, connectionString, opts) {
if (connectionString) CONNECTION_URI = connectionString;
if (opts) options = opts;
if (process.env.MONGOHQ_URL !== undefined) CONNECTION_URI = process.env.MONGOHQ_URL.trim();
if (!_.isUndefined(process.env.MONGOHQ_URL)) CONNECTION_URI = process.env.MONGOHQ_URL.trim();

if (db.connection)
return callback(null, db);
Expand Down
2 changes: 1 addition & 1 deletion src/install/mongotest.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ winston.add(winston.transports.Console, {
(function() {
var CONNECTION_URI = process.env.MONGOTESTURI;
if (!CONNECTION_URI) return process.send({error: {message: 'Invalid connection uri'}});
var options = { keepAlive: 0, auto_reconnect: false, connectTimeoutMS: 5000 };
var options = { keepAlive: 0, auto_reconnect: false, connectTimeoutMS: 5000, useNewUrlParser: true };
database.init(function(e, db) {
if (e) {
process.send({error: e});
Expand Down
5 changes: 2 additions & 3 deletions src/middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ module.exports = function(app, db, callback) {
app.use('/uploads/tickets', function(req, res, next) {
if (!req.user)
return res.redirect('/');


next();
return next();
});

//CORS
Expand All @@ -112,7 +111,7 @@ module.exports = function(app, db, callback) {
var fs = require('fs');
if (fs.existsSync(path.join(dir, 'plugin.json'))) {
var plugin = require(path.join(dir, 'plugin.json'));
if (_.find(global.plugins, {'name': plugin.name}) !== undefined)
if (!_.isUndefined(_.find(global.plugins, {'name': plugin.name})))
throw new Error('Unable to load plugin with duplicate name: ' + plugin.name);

global.plugins.push({name: plugin.name.toLowerCase(), version: plugin.version});
Expand Down
3 changes: 2 additions & 1 deletion src/passport/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
**/

var _ = require('lodash');
var passport = require('passport');
var Local = require('passport-local').Strategy;
var TotpStrategy = require('passport-totp').Strategy;
Expand Down Expand Up @@ -67,7 +68,7 @@ module.exports = function() {
User.findOne({_id: user._id}, '+tOTPKey +tOTPPeriod', function(err, user) {
if (err) return done(err);

if (user.tOTPPeriod === null || user.tOTPPeriod === undefined)
if (!user.tOTPPeriod)
user.tOTPPeriod = 30;

return done(null, base32.decode(user.tOTPKey).toString(), user.tOTPPeriod);
Expand Down
16 changes: 8 additions & 8 deletions src/public/js/pages/tickets.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ define('pages/tickets', [


//Overdue Tickets
var hexDigits = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'];
// var hexDigits = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'];

//Function to convert hex format to a rgb color
function rgb2hex(rgb) {
rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
return '#' + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]).toLowerCase();
}
// function rgb2hex(rgb) {
// rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
// return '#' + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]).toLowerCase();
// }

function hex(x) {
return isNaN(x) ? '00' : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
}
// function hex(x) {
// return isNaN(x) ? '00' : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
// }

$('tr.overdue td').velocity({backgroundColor: '#b71c1c', color: '#ffffff'}, {
loop: true,
Expand Down
Loading

0 comments on commit b39719e

Please sign in to comment.