Skip to content

Commit

Permalink
fix(overdue): overdue card showing incorrect tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
polonel committed Jan 5, 2019
1 parent dc8106c commit 72e2584
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/controllers/api/v1/tickets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
});
Expand Down
100 changes: 83 additions & 17 deletions src/models/ticket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions src/public/js/pages/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ define('pages/dashboard', [
html += '<td class="uk-width-1-10 uk-text-nowrap"><a href="/tickets/'+ ticket.uid + '">T#' + ticket.uid + '</a></td>';
html += '<td class="uk-width-1-10 uk-text-nowrap"><span class="uk-badge ticket-status-open uk-width-1-1">Open</span></td>';
html += '<td class="uk-width-6-10">' + ticket.subject + '</td>';
html += '<td class="uk-width-2-10 uk-text-right uk-text-muted uk-text-small">' + moment(ticket.updated).format('MM.DD.YYYY') + '</td>';
if (ticket.updated)
html += '<td class="uk-width-2-10 uk-text-right uk-text-muted uk-text-small">' + moment(ticket.updated).format('MM.DD.YYYY') + '</td>';
else
html += '<td class="uk-width-2-10 uk-text-right uk-text-muted uk-text-small">' + moment(ticket.date).format('MM.DD.YYYY') + '</td>';
html += '</tr>';
});

$overdueTableBody.append(html);

$overdueTableBody.ajaxify();
overdueSpinner.animate({opacity: 0}, 600, function() {
$(this).hide();
});
Expand Down

0 comments on commit 72e2584

Please sign in to comment.