lists.js 1.8 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(userId, doc) {
  31. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  32. },
  33. update(userId, doc) {
  34. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  35. },
  36. remove(userId, doc) {
  37. return allowIsBoardMember(userId, Boards.findOne(doc.boardId));
  38. },
  39. fetch: ['boardId'],
  40. });
  41. }
  42. Lists.helpers({
  43. cards() {
  44. return Cards.find(Filter.mongoSelector({
  45. listId: this._id,
  46. archived: false,
  47. }), { sort: ['sort'] });
  48. },
  49. board() {
  50. return Boards.findOne(this.boardId);
  51. },
  52. });
  53. // HOOKS
  54. Lists.hookOptions.after.update = { fetchPrevious: false };
  55. Lists.before.insert((userId, doc) => {
  56. doc.createdAt = new Date();
  57. doc.archived = false;
  58. if (!doc.userId)
  59. doc.userId = userId;
  60. });
  61. Lists.before.update((userId, doc, fieldNames, modifier) => {
  62. modifier.$set = modifier.$set || {};
  63. modifier.$set.modifiedAt = new Date();
  64. });
  65. if (Meteor.isServer) {
  66. Lists.after.insert((userId, doc) => {
  67. Activities.insert({
  68. userId,
  69. type: 'list',
  70. activityType: 'createList',
  71. boardId: doc.boardId,
  72. listId: doc._id,
  73. });
  74. });
  75. Lists.after.update((userId, doc) => {
  76. if (doc.archived) {
  77. Activities.insert({
  78. userId,
  79. type: 'list',
  80. activityType: 'archivedList',
  81. listId: doc._id,
  82. boardId: doc.boardId,
  83. });
  84. }
  85. });
  86. }