summaryrefslogtreecommitdiffstats
path: root/collections/lists.js
blob: 1a30dbbafb23e2f49b1e420009b4b48e3cbd1b83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
Lists = new Mongo.Collection('lists');

Lists.attachSchema(new SimpleSchema({
  title: {
    type: String
  },
  archived: {
    type: Boolean
  },
  boardId: {
    type: String
  },
  createdAt: {
    type: Date,
    denyUpdate: true
  },
  sort: {
    type: Number,
    decimal: true,
    // XXX We should probably provide a default
    optional: true
  },
  updatedAt: {
    type: Date,
    denyInsert: true,
    optional: true
  }
}));

if (Meteor.isServer) {
  Lists.allow({
    insert: function(userId, doc) {
      return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
    },
    update: function(userId, doc) {
      return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
    },
    remove: function(userId, doc) {
      return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
    },
    fetch: ['boardId']
  });
}

Lists.helpers({
  cards: function() {
    return Cards.find(Filter.mongoSelector({
      listId: this._id,
      archived: false
    }), { sort: ['sort'] });
  },
  board: function() {
    return Boards.findOne(this.boardId);
  }
});

// HOOKS
Lists.hookOptions.after.update = { fetchPrevious: false };

Lists.before.insert(function(userId, doc) {
  doc.createdAt = new Date();
  doc.archived = false;
  if (! doc.userId)
    doc.userId = userId;
});

Lists.before.update(function(userId, doc, fieldNames, modifier) {
  modifier.$set = modifier.$set || {};
  modifier.$set.modifiedAt = new Date();
});

if (Meteor.isServer) {
  Lists.after.insert(function(userId, doc) {
    Activities.insert({
      type: 'list',
      activityType: 'createList',
      boardId: doc.boardId,
      listId: doc._id,
      userId: userId
    });
  });

  Lists.after.update(function(userId, doc) {
    if (doc.archived) {
      Activities.insert({
        type: 'list',
        activityType: 'archivedList',
        listId: doc._id,
        boardId: doc.boardId,
        userId: userId
      });
    }
  });
}