lists.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. Lists = new Mongo.Collection('lists');
  2. Lists.attachSchema(new SimpleSchema({
  3. title: {
  4. type: String
  5. },
  6. archived: {
  7. type: Boolean
  8. },
  9. boardId: {
  10. type: String
  11. },
  12. createdAt: {
  13. type: Date,
  14. denyUpdate: true
  15. },
  16. sort: {
  17. type: Number,
  18. decimal: true,
  19. // XXX We should probably provide a default
  20. optional: true
  21. },
  22. updatedAt: {
  23. type: Date,
  24. denyInsert: true,
  25. optional: true
  26. }
  27. }));
  28. if (Meteor.isServer) {
  29. Lists.allow({
  30. insert: function(userId, doc) {
  31. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  32. },
  33. update: function(userId, doc) {
  34. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  35. },
  36. remove: function(userId, doc) {
  37. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  38. },
  39. fetch: ['boardId']
  40. });
  41. }
  42. Lists.helpers({
  43. cards: function() {
  44. return Cards.find(Filter.mongoSelector({
  45. listId: this._id,
  46. archived: false
  47. }), { sort: ['sort'] });
  48. },
  49. board: function() {
  50. return Boards.findOne(this.boardId);
  51. }
  52. });
  53. // HOOKS
  54. Lists.hookOptions.after.update = { fetchPrevious: false };
  55. Lists.before.insert(function(userId, doc) {
  56. doc.createdAt = new Date();
  57. doc.archived = false;
  58. if (! doc.userId)
  59. doc.userId = userId;
  60. });
  61. Lists.before.update(function(userId, doc, fieldNames, modifier) {
  62. modifier.$set = modifier.$set || {};
  63. modifier.$set.modifiedAt = new Date();
  64. });
  65. if (Meteor.isServer) {
  66. Lists.after.insert(function(userId, doc) {
  67. Activities.insert({
  68. type: 'list',
  69. activityType: 'createList',
  70. boardId: doc.boardId,
  71. listId: doc._id,
  72. userId: userId
  73. });
  74. });
  75. Lists.after.update(function(userId, doc) {
  76. if (doc.archived) {
  77. Activities.insert({
  78. type: 'list',
  79. activityType: 'archivedList',
  80. listId: doc._id,
  81. boardId: doc.boardId,
  82. userId: userId
  83. });
  84. }
  85. });
  86. }