ソースを参照

Add checklist items model, migration and publication

Andrés Manelli 7 年 前
コミット
bf7de463f1
4 ファイル変更116 行追加26 行削除
  1. 85 0
      models/checklistItems.js
  2. 9 26
      models/checklists.js
  3. 21 0
      server/migrations.js
  4. 1 0
      server/publications/boards.js

+ 85 - 0
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);
+  });
+}

+ 9 - 26
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();

+ 21 - 0
server/migrations.js

@@ -187,3 +187,24 @@ Migrations.add('add-views', () => {
     }
   });
 });
+
+Migrations.add('add-checklist-items', () => {
+  Checklists.find().forEach((checklist) => {
+    // Create new items
+    _.sortBy(checklist.items, 'sort').forEach((item) => {
+      ChecklistItems.direct.insert({
+        title: item.title,
+        sort: item.sort,
+        isFinished: item.isFinished,
+        checklistId: checklist._id,
+        cardId: checklist.cardId,
+      });
+    });
+
+    // Delete old ones
+    Checklists.direct.update({ _id: checklist._id },
+      { $unset: { items : 1 } },
+      noValidate
+    );
+  });
+});

+ 1 - 0
server/publications/boards.js

@@ -101,6 +101,7 @@ Meteor.publishRelations('board', function(boardId) {
       this.cursor(CardComments.find({ cardId }));
       this.cursor(Attachments.find({ cardId }));
       this.cursor(Checklists.find({ cardId }));
+      this.cursor(ChecklistItems.find({ cardId }));
     });
 
     if (board.members) {