summaryrefslogtreecommitdiff
path: root/lib/models
diff options
context:
space:
mode:
authorWu Cheng-Han2016-09-18 16:50:20 +0800
committerWu Cheng-Han2016-09-18 16:50:20 +0800
commit79fd2d13649180c7c67a0bf37b90518f9fa5a5a3 (patch)
treef259f1fa300f8c9a678b51784f83d7ad13d0d720 /lib/models
parentf2347782d82e34f8029cade522952598fed554a9 (diff)
Update to add revision saving policy
Diffstat (limited to 'lib/models')
-rw-r--r--lib/models/revision.js26
1 files changed, 23 insertions, 3 deletions
diff --git a/lib/models/revision.js b/lib/models/revision.js
index 5d66f9fe..b497f0ed 100644
--- a/lib/models/revision.js
+++ b/lib/models/revision.js
@@ -167,7 +167,7 @@ module.exports = function (sequelize, DataTypes) {
checkAllNotesRevision: function (callback) {
Revision.saveAllNotesRevision(function (err, notes) {
if (err) return callback(err, null);
- if (notes.length <= 0) {
+ if (!notes || notes.length <= 0) {
return callback(null, notes);
} else {
Revision.checkAllNotesRevision(callback);
@@ -176,6 +176,7 @@ module.exports = function (sequelize, DataTypes) {
},
saveAllNotesRevision: function (callback) {
sequelize.models.Note.findAll({
+ // query all notes that need to save for revision
where: {
$and: [
{
@@ -201,11 +202,30 @@ module.exports = function (sequelize, DataTypes) {
}
}).then(function (notes) {
if (notes.length <= 0) return callback(null, notes);
+ var savedNotes = [];
async.each(notes, function (note, _callback) {
- Revision.saveNoteRevision(note, _callback);
+ // revision saving policy: note not been modified for 5 mins or not save for 10 mins
+ if (note.lastchangeAt && note.savedAt) {
+ var lastchangeAt = moment(note.lastchangeAt);
+ var savedAt = moment(note.savedAt);
+ if (moment().isAfter(lastchangeAt.add(1, 'minutes'))) {
+ savedNotes.push(note);
+ Revision.saveNoteRevision(note, _callback);
+ } else if (lastchangeAt.isAfter(savedAt.add(2, 'minutes'))) {
+ savedNotes.push(note);
+ Revision.saveNoteRevision(note, _callback);
+ } else {
+ return _callback(null, null);
+ }
+ } else {
+ savedNotes.push(note);
+ Revision.saveNoteRevision(note, _callback);
+ }
}, function (err) {
if (err) return callback(err, null);
- return callback(null, notes);
+ // return null when no notes need saving at this moment but have delayed tasks to be done
+ var result = ((savedNotes.length == 0) && (notes.length > savedNotes.length)) ? null : savedNotes;
+ return callback(null, result);
});
}).catch(function (err) {
return callback(err, null);