summaryrefslogtreecommitdiffstats
path: root/models/unsavedEdits.js
diff options
context:
space:
mode:
authorJustin Reynolds <justinr1234@gmail.com>2019-06-26 17:47:27 -0500
committerJustin Reynolds <justinr1234@gmail.com>2019-06-27 09:13:20 -0500
commitc60e80d25baa6a81b28f6090ca848553d20b2bb7 (patch)
tree64bcea66932f4b4635d5df6901f18d5ba6db0a37 /models/unsavedEdits.js
parentfb728baf0c87bae5fa39d92089b667ff1ed69fa6 (diff)
downloadwekan-c60e80d25baa6a81b28f6090ca848553d20b2bb7.tar.gz
wekan-c60e80d25baa6a81b28f6090ca848553d20b2bb7.tar.bz2
wekan-c60e80d25baa6a81b28f6090ca848553d20b2bb7.zip
Add createdAt and modifiedAt to all collections
Diffstat (limited to 'models/unsavedEdits.js')
-rw-r--r--models/unsavedEdits.js73
1 files changed, 55 insertions, 18 deletions
diff --git a/models/unsavedEdits.js b/models/unsavedEdits.js
index d4f3616a..122b2cd2 100644
--- a/models/unsavedEdits.js
+++ b/models/unsavedEdits.js
@@ -2,31 +2,66 @@
// `UnsavedEdits` API on the client.
UnsavedEditCollection = new Mongo.Collection('unsaved-edits');
-UnsavedEditCollection.attachSchema(new SimpleSchema({
- fieldName: {
- type: String,
- },
- docId: {
- type: String,
- },
- value: {
- type: String,
- },
- userId: {
- type: String,
- autoValue() { // eslint-disable-line consistent-return
- if (this.isInsert && !this.isSet) {
- return this.userId;
- }
+UnsavedEditCollection.attachSchema(
+ new SimpleSchema({
+ fieldName: {
+ type: String,
},
- },
-}));
+ docId: {
+ type: String,
+ },
+ value: {
+ type: String,
+ },
+ userId: {
+ type: String,
+ // eslint-disable-next-line consistent-return
+ autoValue() {
+ if (this.isInsert && !this.isSet) {
+ return this.userId;
+ }
+ },
+ },
+ createdAt: {
+ type: Date,
+ optional: true,
+ // eslint-disable-next-line consistent-return
+ autoValue() {
+ if (this.isInsert) {
+ return new Date();
+ } else {
+ this.unset();
+ }
+ },
+ },
+ modifiedAt: {
+ type: Date,
+ denyUpdate: false,
+ // eslint-disable-next-line consistent-return
+ autoValue() {
+ if (this.isInsert || this.isUpsert || this.isUpdate) {
+ return new Date();
+ } else {
+ this.unset();
+ }
+ },
+ },
+ })
+);
+
+UnsavedEditCollection.before.update(
+ (userId, doc, fieldNames, modifier, options) => {
+ modifier.$set = modifier.$set || {};
+ modifier.$set.modifiedAt = Date.now();
+ }
+);
if (Meteor.isServer) {
function isAuthor(userId, doc, fieldNames = []) {
return userId === doc.userId && fieldNames.indexOf('userId') === -1;
}
Meteor.startup(() => {
+ UnsavedEditCollection._collection._ensureIndex({ modifiedAt: -1 });
UnsavedEditCollection._collection._ensureIndex({ userId: 1 });
});
UnsavedEditCollection.allow({
@@ -36,3 +71,5 @@ if (Meteor.isServer) {
fetch: ['userId'],
});
}
+
+export default UnsavedEditCollection;