diff --git a/src/controllers/api/v1/tickets.js b/src/controllers/api/v1/tickets.js index a04168b33..356eef146 100644 --- a/src/controllers/api/v1/tickets.js +++ b/src/controllers/api/v1/tickets.js @@ -1635,7 +1635,7 @@ apiTickets.getOverdue = function(req, res) { ticketSchema.getOverdue(grps, function (err, objs) { if (err) return res.status(400).json({success: false, error: err.message}); - var sorted = _.sortBy(objs, 'updated').reverse(); + var sorted = _.sortBy(objs, 'uid').reverse(); return res.json({success: true, tickets: sorted}); }); diff --git a/src/models/ticket.js b/src/models/ticket.js index 17ffcf829..5bb7c10b9 100644 --- a/src/models/ticket.js +++ b/src/models/ticket.js @@ -946,31 +946,97 @@ ticketSchema.statics.getOverdue = function(grpId, callback) { if (_.isUndefined(grpId)) return callback('Invalid Group Ids - TicketSchema.GetOverdue()', null); var self = this; - var grpHash = hash(grpId); - var cache = global.cache; - if (cache) { - var overdue = cache.get('tickets:overdue:' + grpHash); - if (overdue) - return callback(null, overdue); - } + // Disable cache (TEMP 01/04/2019) + // var grpHash = hash(grpId); + // var cache = global.cache; + // if (cache) { + // var overdue = cache.get('tickets:overdue:' + grpHash); + // if (overdue) + // return callback(null, overdue); + // } - var q = self.model(COLLECTION).find({group: {$in: grpId}, status: 1, deleted: false}) - .$where(function() { + async.waterfall([ + function(next) { + return self.model(COLLECTION) + .find({group: {$in: grpId}, status: {$in: [0,1]}, deleted: false}) + .select('_id date updated') + .lean() + .exec(next); + }, + function(tickets, next) { + var t = _.map(tickets, function(i) { + return _.transform(i, function(result, value, key) { + if (key === '_id') result._id = value; + if (key === 'priority') result.overdueIn = value.overdueIn; + if (key === 'date') result.date = value; + if (key === 'updated') result.updated = value; + }, {}); + }); + + return next(null, t); + }, + function(tickets, next) { var now = new Date(); - var updated = new Date(this.updated); - var timeout = new Date(updated); - timeout.setDate(timeout.getDate() + 2); - return now > timeout; - }).select('_id uid subject updated'); + var ids = _.filter(tickets, function(t) { + if (!t.date && !t.updated) + return false; + + var timeout = null; + if (t.updated) { + var updated = new Date(t.updated); + timeout = new Date(updated); + timeout.setMinutes(updated.getMinutes() + t.overdueIn); + } else { + var date = new Date(t.date); + timeout = new Date(date); + timeout.setMinutes(date.getMinutes() + t.overdueIn); + } + + return now > timeout; + }); + + ids = _.map(ids, '_id'); - q.lean().exec(function(err, results) { + return next(null, ids); + }, + function (ids, next) { + return self.model(COLLECTION).find({_id: {$in: ids}}) + .limit(50) + .select('_id uid subject updated date') + .lean() + .exec(next); + } + ], function(err, tickets) { if (err) return callback(err, null); - if (cache) cache.set('tickets:overdue:' + grpHash, results, 600); //10min + // Disable cache (TEMP 01/04/2019) + // if (cache) cache.set('tickets:overdue:' + grpHash, tickets, 600); //10min - return callback(null, results); + return callback(null, tickets); }); + // var q = self.model(COLLECTION).find({group: {$in: grpId}, status: {$in: [0,1]}, deleted: false}) + // .$where(function() { + // return this.priority.overdueIn === undefined; + // var now = new Date(); + // var timeout = null; + // if (this.updated) { + // timeout = new Date(this.updated); + // timeout.setMinutes(timeout.getMinutes() + this.priority.overdueIn); + // } else { + // timeout = new Date(this.date); + // timeout.setMinutes(timeout.getMinutes() + this.priority.overdueIn); + // } + // return now > timeout; + // }).select('_id uid subject updated'); + // + // q.lean().exec(function(err, results) { + // if (err) return callback(err, null); + // if (cache) cache.set('tickets:overdue:' + grpHash, results, 600); //10min + // + // return callback(null, results); + // }); + //TODO: Turn on when REDIS is impl // This will be pres through server reload // redisCache.getCache('$trudesk:tickets:overdue' + grpHash, function(err, value) { diff --git a/src/public/js/pages/dashboard.js b/src/public/js/pages/dashboard.js index 063d20beb..9c87f7b2d 100644 --- a/src/public/js/pages/dashboard.js +++ b/src/public/js/pages/dashboard.js @@ -68,12 +68,15 @@ define('pages/dashboard', [ html += 'T#' + ticket.uid + ''; html += 'Open'; html += '' + ticket.subject + ''; - html += '' + moment(ticket.updated).format('MM.DD.YYYY') + ''; + if (ticket.updated) + html += '' + moment(ticket.updated).format('MM.DD.YYYY') + ''; + else + html += '' + moment(ticket.date).format('MM.DD.YYYY') + ''; html += ''; }); $overdueTableBody.append(html); - + $overdueTableBody.ajaxify(); overdueSpinner.animate({opacity: 0}, 600, function() { $(this).hide(); });