summaryrefslogtreecommitdiffstats
path: root/models/checklists.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/checklists.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/checklists.js')
-rw-r--r--models/checklists.js313
1 files changed, 181 insertions, 132 deletions
diff --git a/models/checklists.js b/models/checklists.js
index 653fed4d..6fd22702 100644
--- a/models/checklists.js
+++ b/models/checklists.js
@@ -3,49 +3,64 @@ Checklists = new Mongo.Collection('checklists');
/**
* A Checklist
*/
-Checklists.attachSchema(new SimpleSchema({
- cardId: {
- /**
- * The ID of the card the checklist is in
- */
- type: String,
- },
- title: {
- /**
- * the title of the checklist
- */
- type: String,
- defaultValue: 'Checklist',
- },
- finishedAt: {
- /**
- * When was the checklist finished
- */
- type: Date,
- optional: true,
- },
- createdAt: {
- /**
- * Creation date of the checklist
- */
- type: Date,
- denyUpdate: false,
- autoValue() { // eslint-disable-line consistent-return
- if (this.isInsert) {
- return new Date();
- } else {
- this.unset();
- }
+Checklists.attachSchema(
+ new SimpleSchema({
+ cardId: {
+ /**
+ * The ID of the card the checklist is in
+ */
+ type: String,
},
- },
- sort: {
- /**
- * sorting value of the checklist
- */
- type: Number,
- decimal: true,
- },
-}));
+ title: {
+ /**
+ * the title of the checklist
+ */
+ type: String,
+ defaultValue: 'Checklist',
+ },
+ finishedAt: {
+ /**
+ * When was the checklist finished
+ */
+ type: Date,
+ optional: true,
+ },
+ createdAt: {
+ /**
+ * Creation date of the checklist
+ */
+ type: Date,
+ denyUpdate: false,
+ // 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();
+ }
+ },
+ },
+ sort: {
+ /**
+ * sorting value of the checklist
+ */
+ type: Number,
+ decimal: true,
+ },
+ })
+);
Checklists.helpers({
copy(newCardId) {
@@ -53,7 +68,7 @@ Checklists.helpers({
this._id = null;
this.cardId = newCardId;
const newChecklistId = Checklists.insert(this);
- ChecklistItems.find({checklistId: oldChecklistId}).forEach((item) => {
+ ChecklistItems.find({ checklistId: oldChecklistId }).forEach((item) => {
item._id = null;
item.checklistId = newChecklistId;
item.cardId = newCardId;
@@ -65,9 +80,12 @@ Checklists.helpers({
return ChecklistItems.find({ checklistId: this._id }).count();
},
items() {
- return ChecklistItems.find({
- checklistId: this._id,
- }, { sort: ['sort'] });
+ return ChecklistItems.find(
+ {
+ checklistId: this._id,
+ },
+ { sort: ['sort'] }
+ );
},
finishedCount() {
return ChecklistItems.find({
@@ -78,20 +96,20 @@ Checklists.helpers({
isFinished() {
return 0 !== this.itemCount() && this.itemCount() === this.finishedCount();
},
- checkAllItems(){
- const checkItems = ChecklistItems.find({checklistId: this._id});
- checkItems.forEach(function(item){
+ checkAllItems() {
+ const checkItems = ChecklistItems.find({ checklistId: this._id });
+ checkItems.forEach(function(item) {
item.check();
});
},
- uncheckAllItems(){
- const checkItems = ChecklistItems.find({checklistId: this._id});
- checkItems.forEach(function(item){
+ uncheckAllItems() {
+ const checkItems = ChecklistItems.find({ checklistId: this._id });
+ checkItems.forEach(function(item) {
item.uncheck();
});
},
itemIndex(itemId) {
- const items = self.findOne({_id : this._id}).items;
+ const items = self.findOne({ _id: this._id }).items;
return _.pluck(items, '_id').indexOf(itemId);
},
});
@@ -124,6 +142,7 @@ Checklists.mutations({
if (Meteor.isServer) {
Meteor.startup(() => {
+ Checklists._collection._ensureIndex({ modifiedAt: -1 });
Checklists._collection._ensureIndex({ cardId: 1, createdAt: 1 });
});
@@ -135,12 +154,17 @@ if (Meteor.isServer) {
cardId: doc.cardId,
boardId: card.boardId,
checklistId: doc._id,
- checklistName:doc.title,
+ checklistName: doc.title,
listId: card.listId,
swimlaneId: card.swimlaneId,
});
});
+ Checklists.before.update((userId, doc, fieldNames, modifier, options) => {
+ modifier.$set = modifier.$set || {};
+ modifier.$set.modifiedAt = Date.now();
+ });
+
Checklists.before.remove((userId, doc) => {
const activities = Activities.find({ checklistId: doc._id });
const card = Cards.findOne(doc.cardId);
@@ -155,7 +179,7 @@ if (Meteor.isServer) {
cardId: doc.cardId,
boardId: Cards.findOne(doc.cardId).boardId,
checklistId: doc._id,
- checklistName:doc.title,
+ checklistName: doc.title,
listId: card.listId,
swimlaneId: card.swimlaneId,
});
@@ -172,26 +196,32 @@ if (Meteor.isServer) {
* @return_type [{_id: string,
* title: string}]
*/
- JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) {
- Authentication.checkUserId( req.userId);
- const paramCardId = req.params.cardId;
- const checklists = Checklists.find({ cardId: paramCardId }).map(function (doc) {
- return {
- _id: doc._id,
- title: doc.title,
- };
- });
- if (checklists) {
- JsonRoutes.sendResult(res, {
- code: 200,
- data: checklists,
- });
- } else {
- JsonRoutes.sendResult(res, {
- code: 500,
+ JsonRoutes.add(
+ 'GET',
+ '/api/boards/:boardId/cards/:cardId/checklists',
+ function(req, res) {
+ Authentication.checkUserId(req.userId);
+ const paramCardId = req.params.cardId;
+ const checklists = Checklists.find({ cardId: paramCardId }).map(function(
+ doc
+ ) {
+ return {
+ _id: doc._id,
+ title: doc.title,
+ };
});
+ if (checklists) {
+ JsonRoutes.sendResult(res, {
+ code: 200,
+ data: checklists,
+ });
+ } else {
+ JsonRoutes.sendResult(res, {
+ code: 500,
+ });
+ }
}
- });
+ );
/**
* @operation get_checklist
@@ -209,29 +239,38 @@ if (Meteor.isServer) {
* title: string,
* isFinished: boolean}]}
*/
- JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
- Authentication.checkUserId( req.userId);
- const paramChecklistId = req.params.checklistId;
- const paramCardId = req.params.cardId;
- const checklist = Checklists.findOne({ _id: paramChecklistId, cardId: paramCardId });
- if (checklist) {
- checklist.items = ChecklistItems.find({checklistId: checklist._id}).map(function (doc) {
- return {
- _id: doc._id,
- title: doc.title,
- isFinished: doc.isFinished,
- };
- });
- JsonRoutes.sendResult(res, {
- code: 200,
- data: checklist,
- });
- } else {
- JsonRoutes.sendResult(res, {
- code: 500,
+ JsonRoutes.add(
+ 'GET',
+ '/api/boards/:boardId/cards/:cardId/checklists/:checklistId',
+ function(req, res) {
+ Authentication.checkUserId(req.userId);
+ const paramChecklistId = req.params.checklistId;
+ const paramCardId = req.params.cardId;
+ const checklist = Checklists.findOne({
+ _id: paramChecklistId,
+ cardId: paramCardId,
});
+ if (checklist) {
+ checklist.items = ChecklistItems.find({
+ checklistId: checklist._id,
+ }).map(function(doc) {
+ return {
+ _id: doc._id,
+ title: doc.title,
+ isFinished: doc.isFinished,
+ };
+ });
+ JsonRoutes.sendResult(res, {
+ code: 200,
+ data: checklist,
+ });
+ } else {
+ JsonRoutes.sendResult(res, {
+ code: 500,
+ });
+ }
}
- });
+ );
/**
* @operation new_checklist
@@ -242,36 +281,40 @@ if (Meteor.isServer) {
* @param {string} title the title of the new checklist
* @return_type {_id: string}
*/
- JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) {
- Authentication.checkUserId( req.userId);
+ JsonRoutes.add(
+ 'POST',
+ '/api/boards/:boardId/cards/:cardId/checklists',
+ function(req, res) {
+ Authentication.checkUserId(req.userId);
- const paramCardId = req.params.cardId;
- const id = Checklists.insert({
- title: req.body.title,
- cardId: paramCardId,
- sort: 0,
- });
- if (id) {
- req.body.items.forEach(function (item, idx) {
- ChecklistItems.insert({
- cardId: paramCardId,
- checklistId: id,
- title: item.title,
- sort: idx,
- });
- });
- JsonRoutes.sendResult(res, {
- code: 200,
- data: {
- _id: id,
- },
- });
- } else {
- JsonRoutes.sendResult(res, {
- code: 400,
+ const paramCardId = req.params.cardId;
+ const id = Checklists.insert({
+ title: req.body.title,
+ cardId: paramCardId,
+ sort: 0,
});
+ if (id) {
+ req.body.items.forEach(function(item, idx) {
+ ChecklistItems.insert({
+ cardId: paramCardId,
+ checklistId: id,
+ title: item.title,
+ sort: idx,
+ });
+ });
+ JsonRoutes.sendResult(res, {
+ code: 200,
+ data: {
+ _id: id,
+ },
+ });
+ } else {
+ JsonRoutes.sendResult(res, {
+ code: 400,
+ });
+ }
}
- });
+ );
/**
* @operation delete_checklist
@@ -284,15 +327,21 @@ if (Meteor.isServer) {
* @param {string} checklistId the ID of the checklist to remove
* @return_type {_id: string}
*/
- JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
- Authentication.checkUserId( req.userId);
- const paramChecklistId = req.params.checklistId;
- Checklists.remove({ _id: paramChecklistId });
- JsonRoutes.sendResult(res, {
- code: 200,
- data: {
- _id: paramChecklistId,
- },
- });
- });
+ JsonRoutes.add(
+ 'DELETE',
+ '/api/boards/:boardId/cards/:cardId/checklists/:checklistId',
+ function(req, res) {
+ Authentication.checkUserId(req.userId);
+ const paramChecklistId = req.params.checklistId;
+ Checklists.remove({ _id: paramChecklistId });
+ JsonRoutes.sendResult(res, {
+ code: 200,
+ data: {
+ _id: paramChecklistId,
+ },
+ });
+ }
+ );
}
+
+export default Checklists;