Skip to content

Commit

Permalink
Updates patchMeta method to add archive events to history (#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Emilio Urena authored May 17, 2019
1 parent 228c46e commit 1801729
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/routes/_pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ let route = _.bindAll({
* @param {Object} res
*/
patchMeta(req, res) {
responses.expectJSON(() => metaController.patchMeta(req.uri, req.body), res);
responses.expectJSON(() => metaController.patchMeta(req.uri, req.body, res.locals), res);
}
}, [
'post',
Expand Down
19 changes: 18 additions & 1 deletion lib/services/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,33 @@ function putMeta(uri, data) {
*
* @param {String} uri
* @param {Object} data
* @param {Object} locals
* @returns {Promise}
*/
function patchMeta(uri, data) {
function patchMeta(uri = '', data = {}, locals = {}) {
const id = replaceVersion(uri.replace('/meta', ''));

if (data.shouldPatchArchive) addArchiveEvent(data, locals.user);

return db.patchMeta(id, data)
.then(() => getMeta(uri))
.then(pubToBus(id));
}

/**
* Adds the archive events to the metadata history.
* @param {Object} data
* @param {Object} user
* @returns {void}
*/
function addArchiveEvent(data, user = {}) {
const action = data.archived ? 'archive' : 'unarchive',
historyEntry = { action, timestamp: new Date().toISOString(), users: [userOrRobot(user)] };

data.history.push(historyEntry);
delete data.shouldPatchArchive;
}

/**
* Check metadata property or properties for truthy values.
* Returns true if all properties have truthy values.
Expand Down
42 changes: 41 additions & 1 deletion lib/services/metadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,53 @@ describe(_.startCase(filename), function () {
db.patchMeta.returns(Promise.resolve());
db.getMeta.returns(Promise.resolve());

return fn('domain.com/_pages/id/meta', {})
return fn('domain.com/_pages/id/meta', {}, {})
.then(() => {
sinon.assert.calledWith(db.patchMeta, 'domain.com/_pages/id', {});
sinon.assert.calledWith(db.getMeta, 'domain.com/_pages/id');
sinon.assert.calledOnce(bus.publish);
});
});

it('adds archive event to history if specified', function () {
const data = {
history: [],
archived: true,
shouldPatchArchive: true
};

db.patchMeta.returns(Promise.resolve());
db.getMeta.returns(Promise.resolve());

return fn('domain.com/_pages/id/meta', data, {})
.then(() => {
const item = data.history[0];

expect(data.history).length(1);
expect(data.shouldPatchArchive).to.be.undefined;
expect(item.action).to.equal('archive');
});
});

it('adds unarchive event to history if specified', function () {
const data = {
history: [],
archived: false,
shouldPatchArchive: true
};

db.patchMeta.returns(Promise.resolve());
db.getMeta.returns(Promise.resolve());

return fn('domain.com/_pages/id/meta', data, {})
.then(() => {
const item = data.history[0];

expect(data.history).length(1);
expect(data.shouldPatchArchive).to.be.undefined;
expect(item.action).to.equal('unarchive');
});
});
});

describe('createPage', function () {
Expand Down

0 comments on commit 1801729

Please sign in to comment.