Skip to content

Commit

Permalink
Merge branch 'refactor' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Jan 15, 2019
2 parents c4d6a75 + 46c7169 commit c252b81
Show file tree
Hide file tree
Showing 46 changed files with 1,991 additions and 1,770 deletions.
2 changes: 1 addition & 1 deletion public/css/plugins.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/css/plugins.min.css

Large diffs are not rendered by default.

Binary file added public/sounds/click.ogg
Binary file not shown.
Binary file added public/sounds/ding.ogg
Binary file not shown.
Binary file added public/sounds/error.ogg
Binary file not shown.
Binary file added public/sounds/messagereceived.ogg
Binary file not shown.
Binary file added public/sounds/messagesent.ogg
Binary file not shown.
Binary file added public/sounds/success.ogg
Binary file not shown.
6 changes: 3 additions & 3 deletions src/backup/backup.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ function createZip(callback) {
output.on('end', callback);

archive.on('warning', function(err) {
if (err.code === 'ENOENT')
if (err.code === 'ENOENT')
winston.warn(err);
else {
winston.error(err);
else {
winston.error(err) ;
return callback(err);
}
});
Expand Down
21 changes: 9 additions & 12 deletions src/backup/restore.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,19 @@ function runRestore(file, callback) {
var FILE = process.env.FILE;
if (!FILE) return process.send({success: false, error: 'Invalid File'});

if (!fs.existsSync(path.join(__dirname, '../../backups', FILE))) {
process.send({success: false, error: 'FILE NOT FOUND'});
return;
}
if (!fs.existsSync(path.join(__dirname, '../../backups', FILE)))
return process.send({success: false, error: 'FILE NOT FOUND'});


var options = { keepAlive: 0, auto_reconnect: false, connectTimeoutMS: 5000, useNewUrlParser: true };
database.init(function(e, db) {
if (e) {
process.send({success: false, error: e});
return;
}
if (e)
return process.send({success: false, error: e});


if (!db) {
process.send({success: false, error: {message: 'Unable to open database'}});
return;
}
if (!db)
return process.send({success: false, error: {message: 'Unable to open database'}});


databaseName = database.db.connection.db.databaseName;

Expand Down
73 changes: 0 additions & 73 deletions src/controllers/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,79 +246,6 @@ apiController.logout = function(req, res) {
});
};

/**
* @name apiController.devices
* @description Stores all device related functions
* @namespace
*/
apiController.devices = {};

/**
* Sets the device token for a given account via access token.
*
* @param {object} req Express Request
* @param {object} res Express Response
* @return {JSON} Json object with token
* @example
* //Return
* {
* success: {boolean},
* error: {string},
* token: {string}
* }
*/
apiController.devices.setDeviceToken = function(req, res) {
var accessToken = req.headers.accesstoken;
var token = req.body.token;

if (_.isUndefined(accessToken) || _.isNull(accessToken)) return res.status(401).json({error: 'Invalid Access Token'});
if (_.isUndefined(token) || _.isNull(token)) return res.status(400).json({error: 'Invalid Device Token'});

userSchema.getUserByAccessToken(accessToken, function(err, user) {
if (err) return res.status(401).json({error: err.message});
if (!user) return res.status(401).json({error: 'Unknown User'});

user.addDeviceToken(token, 1, function(err) {
if (err) return res.status(400).json({error: err.message});

res.json({success: true, token: token});
});
});
};

apiController.devices.testApn = function(req, res) {
var notification = {};
notification.title = 'Test Push Notification [trudesk]';

var userModel = require('../models/user');
var ticketModel = require('../models/ticket');
userModel.getUser('5472dbcd925a4d04c80089ee', function(err, user) {
if (err) {
winston.warn(err);
return true;
}

notification.owner = user;

ticketModel.getTicketByUid(1777, function(err, ticket) {
if (err) {
winston.warn(err);
return true;
}

notification.data = {};
notification.data.ticket = ticket;

var apn = require('../notifications');
apn.pushNotification(notification);
});

});

return res.send('Sent!');

};

/**
* @name apiController.roles
* @description Stores all role/permission related static functions
Expand Down
14 changes: 6 additions & 8 deletions src/controllers/api/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,12 @@ apiUsers.create = function(req, res) {

var postData = req.body;

if (_.isUndefined(postData) ||
!_.isObject(postData) ||
_.isUndefined(postData.aUsername) ||
_.isUndefined(postData.aPass) ||
_.isUndefined(postData.aPassConfirm) ||
_.isUndefined(postData.aFullname) ||
_.isUndefined(postData.aEmail) ||
_.isUndefined(postData.aRole))
if (_.isUndefined(postData) || !_.isObject(postData))
return res.status(400).json({'success': false, error: 'Invalid Post Data'});

var propCheck = ['aUsername', 'aPass', 'aPassConfirm', 'aFullname', 'aEmail', 'aRole'];

if (!_.every(propCheck, function(x) { return x in postData; }))
return res.status(400).json({'success': false, error: 'Invalid Post Data'});

if (_.isUndefined(postData.aGrps) || _.isNull(postData.aGrps) || !_.isArray(postData.aGrps))
Expand Down
31 changes: 16 additions & 15 deletions src/controllers/backuprestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ var _ = require('lodash');
var fs = require('fs-extra');
var path = require('path');
var async = require('async');
var winston = require('winston');
var moment = require('moment');

var backup_restore = {};
var backupRestore = {};

function formatBytes(bytes, fixed) {
if (!fixed) fixed = 2;
if(bytes < 1024) return bytes + ' Bytes';
else if(bytes < 1048576) return(bytes / 1024).toFixed(fixed) + ' KB';
else if(bytes < 1073741824) return(bytes / 1048576).toFixed(fixed) + ' MB';
else return(bytes / 1073741824).toFixed(fixed) + ' GB';
if (bytes < 1024) return bytes + ' Bytes';
if (bytes < 1048576) return(bytes / 1024).toFixed(fixed) + ' KB';
if (bytes < 1073741824) return(bytes / 1048576).toFixed(fixed) + ' MB';

return (bytes / 1073741824).toFixed(fixed) + ' GB';
}

backup_restore.getBackups = function(req, res) {
backupRestore.getBackups = function(req, res) {
fs.readdir(path.join(__dirname, '../../backups'), function(err, files) {
if (err) return res.status(400).json({error: err});

Expand Down Expand Up @@ -60,7 +60,7 @@ backup_restore.getBackups = function(req, res) {
});
};

backup_restore.runBackup = function(req, res) {
backupRestore.runBackup = function(req, res) {
var database = require('../database');
var child = require('child_process').fork(path.join(__dirname, '../../src/backup/backup'), { env: { FORK: 1, NODE_ENV: global.env, MONGOURI: database.connectionuri } });
global.forks.push({name: 'backup', fork: child});
Expand All @@ -71,12 +71,13 @@ backup_restore.runBackup = function(req, res) {
child.kill('SIGINT');
global.forks = _.remove(global.forks, function(f) { return f.fork !== child; });

if (data.error) return res.status(400).json({success: false, error: data.error});
if (data.error)
result = {success: false, error: data.error};

if (data.success)
result = {success: true};
else
result = {success: false, error: data.error};
result = {success: false, error: data};
});

child.on('close', function() {
Expand All @@ -90,7 +91,7 @@ backup_restore.runBackup = function(req, res) {
});
};

backup_restore.deleteBackup = function(req, res) {
backupRestore.deleteBackup = function(req, res) {
var filename = req.params.backup;
if (_.isUndefined(filename) || !fs.existsSync(path.join(__dirname, '../../backups/', filename)))
return res.status(400).json({success: false, error: 'Invalid Filename'});
Expand All @@ -102,7 +103,7 @@ backup_restore.deleteBackup = function(req, res) {
});
};

backup_restore.restoreBackup = function(req, res) {
backupRestore.restoreBackup = function(req, res) {
var database = require('../database');

var file = req.body.file;
Expand Down Expand Up @@ -154,7 +155,7 @@ backup_restore.restoreBackup = function(req, res) {
});
};

backup_restore.hasBackupTools = function(req, res) {
backupRestore.hasBackupTools = function(req, res) {
if (require('os').platform() === 'win32')
return res.json({success: true});

Expand All @@ -165,7 +166,7 @@ backup_restore.hasBackupTools = function(req, res) {
});
};

backup_restore.uploadBackup = function(req, res) {
backupRestore.uploadBackup = function(req, res) {
var Busboy = require('busboy');
var busboy = new Busboy({
headers: req.headers,
Expand Down Expand Up @@ -216,4 +217,4 @@ backup_restore.uploadBackup = function(req, res) {
req.pipe(busboy);
};

module.exports = backup_restore;
module.exports = backupRestore;
1 change: 1 addition & 0 deletions src/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports.init = function(callback, connectionString, opts) {
return callback(null, db);

mongoose.Promise = global.Promise;
mongoose.set('useFindAndModify', false);
mongoose.connect(CONNECTION_URI, options).then(function() {
if (!process.env.FORK)
winston.info('Connected to MongoDB');
Expand Down
22 changes: 0 additions & 22 deletions src/docGlobals.js

This file was deleted.

7 changes: 4 additions & 3 deletions src/helpers/hbs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ var helpers = {
concat: function(a, b, space, comma) {
if (space && (comma === false || _.isObject(comma)))
return a.toString() + ' ' + b.toString();
else if (comma === true)

if (comma === true)
return a.toString() + ', ' + b.toString();
else
return a.toString() + b.toString();

return a.toString() + b.toString();
},

contains: function (str, pattern, options) {
Expand Down
7 changes: 6 additions & 1 deletion src/helpers/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ module.exports._sendToSelf = function(io, socketId, method, data) {
_.each(io.sockets.sockets, function(socket) {
if (socket.id === socketId)
socket.emit(method, data);

});
};

Expand Down Expand Up @@ -59,4 +58,10 @@ module.exports.sendToAllExcept = function(io, exceptSocketId, method, data) {
socket.emit(method, data);

});
};

module.exports.disconnectAllClients = function(io) {
Object.keys(io.sockets.sockets).forEach(function(sock) {
io.sockets.sockets[sock].disconnect(true);
});
};
38 changes: 19 additions & 19 deletions src/middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,26 @@ module.exports = function(app, db, callback) {

app.use(express.static(path.join(__dirname, '../../public')));

//Remove to enable plugins
// Uncomment to enable plugins
return next(null, store);
global.plugins = [];
var dive = require('dive');
dive(path.join(__dirname, '../../plugins'), {directories: true, files: false, recursive: false}, function(err, dir) {
if (err) throw err;
var fs = require('fs');
if (fs.existsSync(path.join(dir, 'plugin.json'))) {
var plugin = require(path.join(dir, 'plugin.json'));
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});
var pluginPublic = path.join(dir, '/public');
app.use('/plugins/' + plugin.name, express.static(pluginPublic));
winston.debug('Detected Plugin: ' + plugin.name.toLowerCase() + '-' + plugin.version);
}
}, function() {
next(null, store);
});
// global.plugins = [];
// var dive = require('dive');
// dive(path.join(__dirname, '../../plugins'), {directories: true, files: false, recursive: false}, function(err, dir) {
// if (err) throw err;
// var fs = require('fs');
// if (fs.existsSync(path.join(dir, 'plugin.json'))) {
// var plugin = require(path.join(dir, 'plugin.json'));
// 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});
// var pluginPublic = path.join(dir, '/public');
// app.use('/plugins/' + plugin.name, express.static(pluginPublic));
// winston.debug('Detected Plugin: ' + plugin.name.toLowerCase() + '-' + plugin.version);
// }
// }, function() {
// next(null, store);
// });
}
], function(err, s) {
if (err) {
Expand Down
11 changes: 6 additions & 5 deletions src/middleware/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,12 @@ middleware.ensurel2Auth = function(req, res, next) {
return res.redirect('/dashboard');

return res.redirect('/tickets');
} else
return next();
} else
return res.redirect('/l2auth');

}

return next();
}

return res.redirect('/l2auth');
};

//Common
Expand Down
2 changes: 1 addition & 1 deletion src/models/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ notificationSchema.statics.clearNotifications = function(oId, callback) {
return callback('Invalid UserId - NotificationSchema.ClearNotifications()', null);


return this.model(COLLECTION).remove({owner: oId}, callback);
return this.model(COLLECTION).deleteOne({owner: oId}, callback);
};

module.exports = mongoose.model(COLLECTION, notificationSchema);
2 changes: 1 addition & 1 deletion src/models/ticket.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ ticketSchema.methods.setTicketType = function(ownerId, typeId, callback) {
* @param {TicketCallback} callback Callback with the updated ticket.
*/
ticketSchema.methods.setTicketPriority = function(ownerId, priority, callback) {
if (_.isUndefined(priority)) return callback('Priority must be a Object.', null);
if (_.isUndefined(priority) || !_.isObject(priority)) return callback('Priority must be a PriorityObject.', null);

var self = this;
self.priority = priority._id;
Expand Down
Loading

0 comments on commit c252b81

Please sign in to comment.