summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorAndrés Manelli <andresmanelli@gmail.com>2018-03-19 00:25:19 -0300
committerAndrés Manelli <andresmanelli@gmail.com>2018-03-19 01:25:33 -0300
commitbf7de463f1f71f1758b62187667bf3a7e55585ff (patch)
tree61c06deb85d6bd401d2acb7cc9960777cb7f65f4 /models
parent83848dbee2e6e283e33667cfa9649aafc7c07f4e (diff)
downloadwekan-bf7de463f1f71f1758b62187667bf3a7e55585ff.tar.gz
wekan-bf7de463f1f71f1758b62187667bf3a7e55585ff.tar.bz2
wekan-bf7de463f1f71f1758b62187667bf3a7e55585ff.zip
Add checklist items model, migration and publication
Diffstat (limited to 'models')
-rw-r--r--models/checklistItems.js85
-rw-r--r--models/checklists.js35
2 files changed, 94 insertions, 26 deletions
diff --git a/models/checklistItems.js b/models/checklistItems.js
new file mode 100644
index 00000000..47e37c87
--- /dev/null
+++ b/models/checklistItems.js
@@ -0,0 +1,85 @@
+ChecklistItems = new Mongo.Collection('checklistItems');
+
+ChecklistItems.attachSchema(new SimpleSchema({
+ title: {
+ type: String,
+ },
+ sort: {
+ type: Number,
+ decimal: true,
+ },
+ isFinished: {
+ type: Boolean,
+ defaultValue: false,
+ },
+ checklistId: {
+ type: String,
+ },
+ cardId: {
+ type: String,
+ },
+}));
+
+ChecklistItems.allow({
+ insert(userId, doc) {
+ return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
+ },
+ update(userId, doc) {
+ return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
+ },
+ remove(userId, doc) {
+ return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
+ },
+ fetch: ['userId', 'cardId'],
+});
+
+ChecklistItems.before.insert((userId, doc) => {
+ if (!doc.userId) {
+ doc.userId = userId;
+ }
+});
+
+// Mutations
+ChecklistItems.mutations({
+ setTitle(title) {
+ return { $set: { title } };
+ },
+ toggleItem() {
+ return { $set: { isFinished: !this.isFinished } };
+ },
+});
+
+// Activities helper
+function itemCreation(userId, doc) {
+ const card = Cards.findOne(doc.cardId);
+ const boardId = card.boardId;
+ Activities.insert({
+ userId,
+ activityType: 'addChecklistItem',
+ cardId: doc.cardId,
+ boardId,
+ checklistId: doc.checklistId,
+ checklistItemId: doc._id,
+ });
+}
+
+function itemRemover(userId, doc) {
+ Activities.remove({
+ checklistItemId: doc._id,
+ });
+}
+
+// Activities
+if (Meteor.isServer) {
+ Meteor.startup(() => {
+ ChecklistItems._collection._ensureIndex({ checklistId: 1 });
+ });
+
+ ChecklistItems.after.insert((userId, doc) => {
+ itemCreation(userId, doc);
+ });
+
+ ChecklistItems.after.remove((userId, doc) => {
+ itemRemover(userId, doc);
+ });
+}
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();