summaryrefslogtreecommitdiffstats
path: root/models/checklistItems.js
diff options
context:
space:
mode:
authorLauri Ojansivu <x@xet7.org>2018-03-20 22:58:22 +0200
committerLauri Ojansivu <x@xet7.org>2018-03-20 22:58:22 +0200
commita7855f3f582d193b0a6dbd70b5da6f59384f9520 (patch)
tree8bb0c9d19a23dfb374a251241ba83f8e7b448bae /models/checklistItems.js
parent90f3f7ba163ef5d015bc473946b02c23e263a011 (diff)
parentac19904ee86541165b0017dcaaee33629904806b (diff)
downloadwekan-a7855f3f582d193b0a6dbd70b5da6f59384f9520.tar.gz
wekan-a7855f3f582d193b0a6dbd70b5da6f59384f9520.tar.bz2
wekan-a7855f3f582d193b0a6dbd70b5da6f59384f9520.zip
Merge branch 'andresmanelli-checklistItems' into devel
Diffstat (limited to 'models/checklistItems.js')
-rw-r--r--models/checklistItems.js95
1 files changed, 95 insertions, 0 deletions
diff --git a/models/checklistItems.js b/models/checklistItems.js
new file mode 100644
index 00000000..3c01d476
--- /dev/null
+++ b/models/checklistItems.js
@@ -0,0 +1,95 @@
+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 } };
+ },
+ move(checklistId, sortIndex) {
+ const cardId = Checklists.findOne(checklistId).cardId;
+ const mutatedFields = {
+ cardId,
+ checklistId,
+ sort: sortIndex,
+ };
+
+ return {$set: mutatedFields};
+ },
+});
+
+// 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);
+ });
+}