Skip to content

Commit

Permalink
fix(timezone): option to set local timezone
Browse files Browse the repository at this point in the history
fix(routes): version route added to install server
  • Loading branch information
polonel committed Oct 6, 2018
1 parent 357752d commit e3eb12a
Show file tree
Hide file tree
Showing 20 changed files with 1,424 additions and 33 deletions.
3 changes: 1 addition & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ if (!process.env.FORK) {
winston.info('trudesk v' + pkg.version + ' Copyright (C) 2014-2018 Chris Brame');
winston.info('');
winston.info('Running in: ' + global.env);
winston.info('Time: ' + new Date());
winston.info('Server Time: ' + new Date());
}

var configFile = path.join(__dirname, '/config.json'),
Expand Down Expand Up @@ -141,7 +141,6 @@ function start() {
function dbCallback(err, db) {
if (err)
return start();


ws.init(db, function(err) {
if (err) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"mkdirp": "0.5.1",
"moment": "2.22.2",
"moment-duration-format": "2.2.2",
"moment-timezone": "0.5.21",
"mongoose": "5.2.6",
"nconf": "0.10.0",
"netmask": "1.0.6",
Expand Down
2 changes: 1 addition & 1 deletion src/cache/ticketStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var init = function(tickets, callback) {
ex.e180 = {};
ex.e365 = {};
ex.lifetime = {};
ex.lastUpdated = moment().format('MM-DD-YYYY hh:mm:ssa');
ex.lastUpdated = moment.utc();
var today = moment().hour(23).minute(59).second(59);
var e30 = today.clone().subtract(30, 'd'),
e60 = today.clone().subtract(60, 'd'),
Expand Down
13 changes: 11 additions & 2 deletions src/controllers/api/v1/tickets.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

var async = require('async'),
_ = require('lodash'),
moment = require('moment'),
moment = require('moment-timezone'),
winston = require('winston'),
permissions = require('../../../permissions'),
emitter = require('../../../emitter');
Expand Down Expand Up @@ -1211,8 +1211,17 @@ apiTickets.getTicketStats = function(req, res) {
obj.mostActiveTicket = cache.get('quickstats:mostActiveTicket');

obj.lastUpdated = cache.get('tickets:overview:lastUpdated');
var settingsUtil = require('../../../settings/settingsUtil');
settingsUtil.getSettings(function(err, context) {
if (err)
return res.send(obj);

var tz = context.data.settings.timezone.value;
obj.lastUpdated = moment.utc(obj.lastUpdated).tz(tz).format('MM-DD-YYYY hh:mm:ssa');

return res.send(obj);
return res.send(obj);
});
// return res.send(obj);
};

function parseTicketStats(role, tickets, callback) {
Expand Down
12 changes: 6 additions & 6 deletions src/helpers/hbs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

// node_modules
var _ = require('lodash');
var moment = require('moment');
var moment = require('moment-timezone');
require('moment-duration-format');

// The module to be exported
Expand Down Expand Up @@ -466,15 +466,15 @@ var helpers = {
},

now: function() {
return new moment();
return new moment.utc();
},

formatDate: function(date, format) {
return moment(date).format(format);
return moment.utc(date).tz(global.timezone).format(format);
},

formatDateParse: function(date, parseFormat, returnFormat) {
return moment(date, parseFormat).format(returnFormat);
return moment.utc(date, parseFormat).tz(global.timezone).format(returnFormat);
},

durationFormat: function(duration, parseFormat) {
Expand All @@ -492,7 +492,7 @@ var helpers = {
sameElse: 'L'
}
});
return moment(date).calendar();
return moment.utc(date).tz(global.timezone).calendar();
},

fromNow: function(date) {
Expand All @@ -516,7 +516,7 @@ var helpers = {
}
});

return moment(date).fromNow();
return moment.utc(date).tz(global.timezone).fromNow();
},

firstCap: function(str) {
Expand Down
23 changes: 17 additions & 6 deletions src/helpers/viewdata/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
**/

var async = require('async'),
_ = require('lodash'),
winston = require('winston'),
moment = require('moment'),
permissions = require('../../permissions');
var async = require('async'),
_ = require('lodash'),
winston = require('winston'),
moment = require('moment'),
permissions = require('../../permissions'),
settingSchema = require('../../models/setting');


var viewController = {};
var viewdata = {};
Expand All @@ -30,7 +32,6 @@ viewController.getData = function(request, cb) {
viewdata.hosturl = request.protocol + '://' + request.get('host');

// If hosturl setting is not set. Let's set it.
var settingSchema = require('../../models/setting');
settingSchema.getSetting('gen:siteurl', function(err, setting) {
if (!err && !setting) {
settingSchema.create({
Expand All @@ -44,6 +45,16 @@ viewController.getData = function(request, cb) {

});
},
function(callback) {
settingSchema.getSetting('gen:timezone', function(err, timezone) {
if (!err && timezone)
viewdata.timezone = timezone.value;
else
viewdata.timezone = 'America/New_York';

return callback();
});
},
function(callback) {
viewController.getActiveNotice(function(err, data) {
if (err) return callback(err);
Expand Down
4 changes: 2 additions & 2 deletions src/models/ticket.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ ticketSchema.statics.getAll = function(callback) {

ticketSchema.statics.getForCache = function(callback) {
var self = this;
var t365 = moment().hour(23).minute(59).second(59).subtract(365, 'd').toDate();
var t365 = moment.utc().hour(23).minute(59).second(59).subtract(365, 'd').toDate();
self.model(COLLECTION).find({date: {$gte: t365}, deleted: false})
.select('_id uid date status history comments assignee owner tags')
.sort('date')
Expand Down Expand Up @@ -1128,7 +1128,7 @@ ticketSchema.statics.getTopTicketGroups = function(timespan, top, callback) {

var self = this;

var today = moment().hour(23).minute(59).second(59);
var today = moment.utc().hour(23).minute(59).second(59);
var tsDate = today.clone().subtract(timespan, 'd');
var query = {date: {$gte: tsDate.toDate(), $lte: today.toDate()}, deleted: false};
if (timespan === -1)
Expand Down
34 changes: 33 additions & 1 deletion src/public/js/angularjs/controllers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
**/

define(['angular', 'underscore', 'jquery', 'modules/helpers', 'modules/ui', 'uikit', 'easymde', 'velocity', 'history'], function(angular, _, $, helpers, ui, UIkit, EasyMDE) {
define(['angular', 'underscore', 'jquery', 'modules/helpers', 'modules/ui', 'uikit', 'easymde', 'moment', 'moment_timezone', 'velocity', 'history'], function(angular, _, $, helpers, ui, UIkit, EasyMDE, moment) {
return angular.module('trudesk.controllers.settings', ['ngSanitize'])
.directive('selectize', function($timeout) {
return {
Expand Down Expand Up @@ -108,6 +108,22 @@ define(['angular', 'underscore', 'jquery', 'modules/helpers', 'modules/ui', 'uik
}
});

//TimeZones
$scope.timeZones = moment.tz.names().map(function(name) {
var year = new Date().getUTCFullYear();
var timezoneAtBeginningOfyear = moment.tz(year + '-01-01', name);
return {
utc: timezoneAtBeginningOfyear.utcOffset(),
label: '(GMT' + timezoneAtBeginningOfyear.format('Z') + ') ' + name,
value: name
};
}).sort(function(a, b) { return a.utc - b.utc; });

$timeout(function() {
// Call in next cycle - Timezones generated dynamically
helpers.UI.selectize($('select#tz').parent());
}, 0);

var $privacyPolicy = $('#privacyPolicy');
if ($privacyPolicy.length > 0) {
privacyPolicyMDE = new EasyMDE({
Expand Down Expand Up @@ -337,6 +353,22 @@ define(['angular', 'underscore', 'jquery', 'modules/helpers', 'modules/ui', 'uik
}
};

$scope.timezoneChanged = function() {
$http.put('/api/v1/settings', {
name: 'gen:timezone',
value: $scope.selectedTimezone.replace('string:', '')
}, {
headers: {
'Content-Type': 'application/json'
}
}).then(function successCallback() {
helpers.UI.showSnackbar('Timezone Updated. Please restart server.', false);
}, function errorCallback(err) {
helpers.UI.showSnackbar('Error: ' + err, true);
$log.error(err);
});
};

$scope.saveSiteUrlClicked = function() {
$http.put('/api/v1/settings', {
name: 'gen:siteurl',
Expand Down
36 changes: 34 additions & 2 deletions src/public/js/modules/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ define([
'chosen',
'velocity',
'peity',
'multiselect'
'multiselect',
'moment_timezone'
],
function($, _, moment, UIkit, CountUp, Waves, Selectize, Snackbar, ROLES, Cookies, Tether) {

Expand All @@ -50,6 +51,8 @@ function($, _, moment, UIkit, CountUp, Waves, Selectize, Snackbar, ROLES, Cookie

self.prototypes();

self.setTimezone();

self.resizeFullHeight();
self.setupScrollers();
self.formvalidator();
Expand Down Expand Up @@ -1223,8 +1226,37 @@ function($, _, moment, UIkit, CountUp, Waves, Selectize, Snackbar, ROLES, Cookie
});
};

helpers.setTimezone = function() {
var $timezone = $('#__timezone');
if ($timezone.length < 1)
Cookies.set('$trudesk:timezone', 'America/New_York');
else {
var timezone = Cookies.get('$trudesk:timezone');
var __timezone = $timezone.text();
if (!timezone)
Cookies.set('$trudesk:timezone', __timezone);
else if (timezone !== __timezone)
Cookies.set('$trudesk:timezone',__timezone);
}

moment.tz.setDefault(timezone);
$timezone.remove();
};

helpers.getTimezone = function() {
var timezone = Cookies.get('$trudesk:timezone');
if (!timezone)
timezone = 'America/New_York';

return timezone;
};

helpers.formatDate = function(date, format) {
return moment(date).format(format);
var timezone = this.getTimezone();
if (!timezone)
timezone = 'America/New_York';

return moment.utc(date).tz(timezone).format(format);
};

helpers.setupChosen = function() {
Expand Down
4 changes: 2 additions & 2 deletions src/public/js/pages/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ define('pages/messages', [

convoListItem.find('.message-subject').text(fromName + ': ' + message.body);
$recentMessages[message.conversation] = fromName + ': ' + message.body;

convoListItem.find('.message-date').text(moment().calendar());
var timezone = helpers.getTimezone();
convoListItem.find('.message-date').text(moment.utc().tz(timezone).calendar());
} else {
var convoUL = $('#convo-list > ul.message-items');
if (convoUL.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/public/js/pages/reportsBreakdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ define('pages/reportsBreakdown', [
html += ticket.subject;
html += '</td>';
html += '<td class="uk-width-2-10 uk-text-right uk-text-muted uk-text-small">';
html += moment(ticket.updated).format('MM.DD.YYYY');
html += moment.utc(ticket.updated).tz(helpers.getTimezone()).format('MM.DD.YYYY');
html += '</td>';
html += '</tr>';
});
Expand Down
Loading

0 comments on commit e3eb12a

Please sign in to comment.