From bf7de463f1f71f1758b62187667bf3a7e55585ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Manelli?= Date: Mon, 19 Mar 2018 00:25:19 -0300 Subject: Add checklist items model, migration and publication --- models/checklists.js | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-) (limited to 'models/checklists.js') diff --git a/models/checklists.js b/models/checklists.js index 7eb0a24f..d9b15140 100644 --- a/models/checklists.js +++ b/models/checklists.js @@ -7,24 +7,6 @@ Checklists.attachSchema(new SimpleSchema({ title: { type: String, }, - items: { - type: [Object], - defaultValue: [], - }, - 'items.$._id': { - type: String, - }, - 'items.$.title': { - type: String, - }, - 'items.$.sort': { - type: Number, - decimal: true, - }, - 'items.$.isFinished': { - type: Boolean, - defaultValue: false, - }, finishedAt: { type: Date, optional: true, @@ -46,19 +28,20 @@ Checklists.attachSchema(new SimpleSchema({ }, })); -const self = Checklists; - Checklists.helpers({ itemCount() { - return this.items.length; + return ChecklistItems.find({ checklistId: this._id }).count(); }, - getItemsSorted() { - return _.sortBy(this.items, 'sort'); + items() { + return ChecklistItems.find(Filter.mongoSelector({ + checklistId: this._id, + }), { sort: ['sort'] }); }, finishedCount() { - return this.items.filter((item) => { - return item.isFinished; - }).length; + return ChecklistItems.find({ + checklistId: this._id, + isFinished: true, + }).count(); }, isFinished() { return 0 !== this.itemCount() && this.itemCount() === this.finishedCount(); -- cgit v1.2.3-1-g7c22 From ca89442f3a62a7a97044a6e572eea70158edadbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Manelli?= Date: Mon, 19 Mar 2018 15:31:38 -0300 Subject: Clean checklists model --- models/checklists.js | 137 --------------------------------------------------- 1 file changed, 137 deletions(-) (limited to 'models/checklists.js') diff --git a/models/checklists.js b/models/checklists.js index d9b15140..637e280c 100644 --- a/models/checklists.js +++ b/models/checklists.js @@ -46,23 +46,10 @@ Checklists.helpers({ isFinished() { return 0 !== this.itemCount() && this.itemCount() === this.finishedCount(); }, - getItem(_id) { - return _.findWhere(this.items, { _id }); - }, itemIndex(itemId) { const items = self.findOne({_id : this._id}).items; return _.pluck(items, '_id').indexOf(itemId); }, - getNewItemId() { - const itemCount = this.itemCount(); - let idx = 0; - if (itemCount > 0) { - const lastId = this.items[itemCount - 1]._id; - const lastIdSuffix = lastId.substr(this._id.length); - idx = parseInt(lastIdSuffix, 10) + 1; - } - return `${this._id}${idx}`; - }, }); Checklists.allow({ @@ -86,108 +73,9 @@ Checklists.before.insert((userId, doc) => { }); Checklists.mutations({ - //for checklist itself setTitle(title) { return { $set: { title } }; }, - //for items in checklist - addItem(title) { - const _id = this.getNewItemId(); - return { - $addToSet: { - items: { - _id, title, - isFinished: false, - sort: this.itemCount(), - }, - }, - }; - }, - addFullItem(item) { - const itemsUpdate = {}; - this.items.forEach(function(iterItem, index) { - if (iterItem.sort >= item.sort) { - itemsUpdate[`items.${index}.sort`] = iterItem.sort + 1; - } - }); - if (!_.isEmpty(itemsUpdate)) { - self.direct.update({ _id: this._id }, { $set: itemsUpdate }); - } - return { $addToSet: { items: item } }; - }, - removeItem(itemId) { - const item = this.getItem(itemId); - const itemsUpdate = {}; - this.items.forEach(function(iterItem, index) { - if (iterItem.sort > item.sort) { - itemsUpdate[`items.${index}.sort`] = iterItem.sort - 1; - } - }); - if (!_.isEmpty(itemsUpdate)) { - self.direct.update({ _id: this._id }, { $set: itemsUpdate }); - } - return { $pull: { items: { _id: itemId } } }; - }, - editItem(itemId, title) { - if (this.getItem(itemId)) { - const itemIndex = this.itemIndex(itemId); - return { - $set: { - [`items.${itemIndex}.title`]: title, - }, - }; - } - return {}; - }, - finishItem(itemId) { - if (this.getItem(itemId)) { - const itemIndex = this.itemIndex(itemId); - return { - $set: { - [`items.${itemIndex}.isFinished`]: true, - }, - }; - } - return {}; - }, - resumeItem(itemId) { - if (this.getItem(itemId)) { - const itemIndex = this.itemIndex(itemId); - return { - $set: { - [`items.${itemIndex}.isFinished`]: false, - }, - }; - } - return {}; - }, - toggleItem(itemId) { - const item = this.getItem(itemId); - if (item) { - const itemIndex = this.itemIndex(itemId); - return { - $set: { - [`items.${itemIndex}.isFinished`]: !item.isFinished, - }, - }; - } - return {}; - }, - sortItems(itemIDs) { - const validItems = []; - itemIDs.forEach((itemID) => { - if (this.getItem(itemID)) { - validItems.push(this.itemIndex(itemID)); - } - }); - const modifiedValues = {}; - for (let i = 0; i < validItems.length; i++) { - modifiedValues[`items.${validItems[i]}.sort`] = i; - } - return { - $set: modifiedValues, - }; - }, }); if (Meteor.isServer) { @@ -205,30 +93,6 @@ if (Meteor.isServer) { }); }); - //TODO: so there will be no activity for adding item into checklist, maybe will be implemented in the future. - // The future is now - Checklists.after.update((userId, doc, fieldNames, modifier) => { - if (fieldNames.includes('items')) { - if (modifier.$addToSet) { - Activities.insert({ - userId, - activityType: 'addChecklistItem', - cardId: doc.cardId, - boardId: Cards.findOne(doc.cardId).boardId, - checklistId: doc._id, - checklistItemId: modifier.$addToSet.items._id, - }); - } else if (modifier.$pull) { - const activity = Activities.findOne({ - checklistItemId: modifier.$pull.items._id, - }); - if (activity) { - Activities.remove(activity._id); - } - } - } - }); - Checklists.before.remove((userId, doc) => { const activities = Activities.find({ checklistId: doc._id }); if (activities) { @@ -239,7 +103,6 @@ if (Meteor.isServer) { }); } -//CARD COMMENT REST API if (Meteor.isServer) { JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) { try { -- cgit v1.2.3-1-g7c22