Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding optional audit meta fields to track createdBy/updatedBy user #490

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ List.prototype.addPattern = function(pattern) {
});
break;

case 'audit meta':
schemaPlugins.auditMeta.apply(this);
break;

}

return this;
Expand Down
31 changes: 31 additions & 0 deletions lib/schemaPlugins.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var keystone = require('../'),
Field = require('./field'),
_ = require('underscore'),
async = require('async'),
utils = require('keystone-utils');
Expand Down Expand Up @@ -137,6 +138,36 @@ exports.autokey = function() {

};

exports.auditMeta = function() {
var userModel = keystone.get('user model'),
list = this;

if (userModel) {
if(!list.schema.path('createdOn') && !list.schema.path('updatedOn')) {
list.addPattern('standard meta');
}

list.add({
createdBy: { type: Field.Types.Relationship, ref: userModel, hidden: true, index: true },
updatedBy: { type: Field.Types.Relationship, ref: userModel, hidden: true, index: true }
});
list.map('createdBy', 'createdBy');
list.map('modifiedBy', 'updatedBy');

list.schema.method('auditMetaUpdate', function(item, isNew, userId) {
if (list.mappings.createdBy && isNew) {
item.set(list.mappings.createdBy, userId);
}
if (list.mappings.modifiedBy) {
item.set(list.mappings.modifiedBy, userId);
}
return item;
});
}


};

methods.getRelated = function(paths, callback, nocollapse) {

var item = this,
Expand Down
5 changes: 5 additions & 0 deletions lib/updateHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ UpdateHandler.prototype.process = function(data, options, callback) {

}, this);

// check for audit meta fields (mapped to createdBy/modifiedBy)
if (this.item.auditMetaUpdate) {
this.item.auditMetaUpdate(this.item, this.item.isNew, this.user._id);
}

progress();

};
Expand Down
79 changes: 79 additions & 0 deletions test/auditMeta.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
var demand = require('must'),
keystone = require('../').init(),
Types = keystone.Field.Types,
Field = require('../lib/field'),
Test = keystone.List('AuditMetaTest'),
mongoose = require('mongoose'),
createdByUserId = mongoose.Types.ObjectId('53d70dbf66100abc147f0000'),
updatedByUserId = mongoose.Types.ObjectId('53d70dbf66100abc147f0001'),
item;

before(function() {
keystone.set('user model', 'User');
Test.add({
name: { type: String }
});
});

describe('auditMeta schemaPlugin', function() {
describe('testing when "audit meta" pattern is not added', function() {

it('should not have an auditMetaUpdate() method', function() {
demand(Test.schema.methods.auditMetaUpdate).be.undefined();
});
});

describe('testing when "audit meta" pattern is added', function() {
before(function() {
Test.addPattern('audit meta');
Test.register();
});

it('should have an auditMetaUpdate() method', function() {
demand(Test.schema.methods.auditMetaUpdate).be.a.function();
});

it('should have a createdBy field', function() {
demand(Test.fields.createdBy).be.an.instanceOf(Field);
});

it('should have a updatedBy field', function() {
demand(Test.fields.updatedBy).be.an.instanceOf(Field);
});

it('should have a createdBy mapping', function() {
demand(Test.mappings.createdBy).be('createdBy');
});

it('should have a modifiedBy mapping', function() {
demand(Test.mappings.modifiedBy).be('updatedBy');
});

describe('testing an item created from model', function() {
before(function() {
item = new Test.model();
});

it('should have an auditMetaUpdate() method', function() {
demand(item.auditMetaUpdate).be.a.function();
});

describe('calling auditMetaUpdate(item, isNew, userId)', function() {

it('should update both createdBy and updatedBy when isNew = true', function() {
item.auditMetaUpdate(item, true, createdByUserId);
demand(item.get('createdBy')).be(createdByUserId);
demand(item.get('updatedBy')).be(createdByUserId);
});

it('should update only the updatedBy when isNew = false', function() {
item.auditMetaUpdate(item, false, updatedByUserId);
demand(item.get('createdBy')).be(createdByUserId);
demand(item.get('updatedBy')).be(updatedByUserId);
});
});

});

});
});