forked from mimani/mongoose-diff-history
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffHistory.js
184 lines (171 loc) · 6.47 KB
/
diffHistory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
var History = require("./diffHistoryModel");
var async = require("async");
var jsondiffpatch = require("jsondiffpatch").create();
var saveHistoryObject = function (history, callback){
history.save(function (err) {
if (err) {
err.message = "Mongo Error :" + err.message;
}
callback();
});
};
var saveDiffObject = function(currentObject, original, updated, user, reason, callback){
var diff = jsondiffpatch.diff(JSON.parse(JSON.stringify(original)),
JSON.parse(JSON.stringify(updated)));
if (diff) {
History.findOne({collectionName: currentObject.constructor.modelName, collectionId: currentObject._id}).sort("-version").exec(function (err, lastHistory) {
if (err) {
err.message = "Mongo Error :" + err.message;
return callback();
}
var history = new History({
collectionName: currentObject.constructor.modelName,
collectionId: currentObject._id,
diff: diff,
user: user,
reason: reason,
version: lastHistory ? lastHistory.version + 1 : 0
});
saveHistoryObject(history, callback);
});
}
else{
callback();
}
};
var saveDiffHistory = function(queryObject, currentObject, callback) {
currentObject.constructor.findOne({_id: currentObject._id}, function (err, selfObject) {
if(selfObject){
var dbObject = {}, updateParams;
updateParams = queryObject._update["$set"] ? queryObject._update["$set"] : queryObject._update;
Object.keys(updateParams).forEach(function(key) {
dbObject[key] = selfObject[key];
});
saveDiffObject(currentObject, dbObject, updateParams, queryObject.options.__user, queryObject.options.__reason, function(){
callback();
});
}
});
};
var saveDiffs = function(self, next) {
var queryObject = self;
queryObject.find(queryObject._conditions, function (err, results) {
if (err) {
err.message = "Mongo Error :" + err.message;
return next();
}
async.eachSeries(results, function (result, callback) {
if (err) {
err.message = "Mongo Error :" + err.message;
return next();
}
saveDiffHistory(queryObject, result, callback);
}, function done() {
return next();
});
});
};
var getVersion = function (model, id, version, callback) {
model.findOne({_id: id}, function (err, latest) {
if (err) {
console.error(err);
return callback(err, null);
}
History.find({collectionName: model.modelName, collectionId: id, version: {$gte : parseInt(version, 10)}},
{diff: 1, version: 1}, {sort: "-version"}, function (err, histories) {
if (err) {
console.error(err);
return callback(err, null);
}
var object = latest ? latest : {};
async.each(histories, function(history, eachCallback){
jsondiffpatch.unpatch(object, history.diff);
eachCallback();
}, function(err){
if (err) {
console.error(err);
return callback(err, null);
}
callback(null, object);
});
})
});
};
var getHistories = function (modelName, id, expandableFields, callback) {
History.find({collectionName: modelName, collectionId: id}, function (err, histories) {
if (err) {
console.error(err);
return callback(err, null);
}
async.map(histories, function (history, mapCallback) {
var changedValues = [];
var changedFields = [];
for (var key in history.diff) {
if (history.diff.hasOwnProperty(key)) {
if (expandableFields.indexOf(key) > -1) {
//var oldDate = new Date(history.diff[key][0]);
//var newDate = new Date(history.diff[key][1]);
//if (oldDate != "Invalid Date" && newDate != "Invalid Date") {
// oldValue = oldDate.getFullYear() + "-" + (oldDate.getMonth() + 1) + "-" + oldDate.getDate();
// newValue = newDate.getFullYear() + "-" + (newDate.getMonth() + 1) + "-" + newDate.getDate();
//}
//else {
var oldValue = history.diff[key][0];
var newValue = history.diff[key][1];
//}
changedValues.push(key + " from " + oldValue + " to " + newValue);
}
else {
changedFields.push(key);
}
}
}
var comment = "modified " + changedFields.concat(changedValues).join(", ");
return mapCallback(null, {
changedBy: history.user,
changedAt: history.createdAt,
updatedAt: history.updatedAt,
reason: history.reason,
comment: comment
})
}, function (err, output) {
if (err) {
console.error(err);
return callback(err, null);
}
return callback(null, output);
});
});
};
var plugin = function lastModifiedPlugin(schema, options) {
schema.pre("save", function (next) {
var self = this;
if(self.isNew) {
next();
}else{
self.constructor.findOne({_id: self._id}, function (err, original) {
saveDiffObject(self, original, self, self.__user, self.__reason, function(){
next();
});
});
}
});
schema.pre("findOneAndUpdate", function (next) {
saveDiffs(this, function(){
next();
});
});
schema.pre("update", function (next) {
saveDiffs(this, function(){
next();
});
});
schema.pre("remove", function(next) {
saveDiffObject(this, this, {}, this.__user, this.__reason, function(){
next();
})
});
};
module.exports.plugin = plugin;
module.exports.getHistories = getHistories;
module.exports.getVersion = getVersion;