diff options
Diffstat (limited to 'models/checklists.js')
-rw-r--r-- | models/checklists.js | 313 |
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; |