From 169defd19476648c769a56fe2bbc6b062597f99b Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 2 Dec 2016 12:34:58 -0500 Subject: [PATCH] #5232, update flag history to save new value --- src/flags.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/flags.js b/src/flags.js index 54f6f0bd30..893bba4442 100644 --- a/src/flags.js +++ b/src/flags.js @@ -290,14 +290,18 @@ Flags.targetExists = function (type, id, callback) { Flags.update = function (flagId, uid, changeset, callback) { // Retrieve existing flag data to compare for history-saving purposes var fields = ['state', 'assignee']; + var history = []; async.waterfall([ async.apply(db.getObjectFields.bind(db), 'flag:' + flagId, fields), function (current, next) { - for(var prop in changeset) { + for (var prop in changeset) { if (changeset.hasOwnProperty(prop)) { if (current[prop] === changeset[prop]) { delete changeset[prop]; + } else { + // Append to history payload + history.push(prop + ':' + changeset[prop]); } } } @@ -311,7 +315,7 @@ Flags.update = function (flagId, uid, changeset, callback) { // Save new object to db (upsert) async.apply(db.setObject, 'flag:' + flagId, changeset), // Append history - async.apply(Flags.appendHistory, flagId, uid, Object.keys(changeset)) + async.apply(Flags.appendHistory, flagId, uid, history) ], function (err, data) { return next(err); }); @@ -334,9 +338,18 @@ Flags.getHistory = function (flagId, callback) { uids.push(entry.value[0]); + // Deserialise field object + var fields = entry.value[1].map(function (field) { + field = field.toString().split(':'); + return { + "attribute": field[0], + "value": field[1] === undefined ? null : field[1] + }; + }); + return { uid: entry.value[0], - fields: entry.value[1], + fields: fields, datetime: entry.score, datetimeISO: new Date(entry.score).toISOString() }; @@ -349,6 +362,7 @@ Flags.getHistory = function (flagId, callback) { return callback(err); } + // Append user data to each history event history = history.map(function (event, idx) { event.user = users[idx]; return event;