lists.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. Lists = new Mongo.Collection('lists');
  2. Lists.attachSchema(new SimpleSchema({
  3. title: {
  4. type: String,
  5. },
  6. archived: {
  7. type: Boolean,
  8. autoValue() { // eslint-disable-line consistent-return
  9. if (this.isInsert && !this.isSet) {
  10. return false;
  11. }
  12. },
  13. },
  14. boardId: {
  15. type: String,
  16. },
  17. createdAt: {
  18. type: Date,
  19. autoValue() { // eslint-disable-line consistent-return
  20. if (this.isInsert) {
  21. return new Date();
  22. } else {
  23. this.unset();
  24. }
  25. },
  26. },
  27. sort: {
  28. type: Number,
  29. decimal: true,
  30. // XXX We should probably provide a default
  31. optional: true,
  32. },
  33. updatedAt: {
  34. type: Date,
  35. optional: true,
  36. autoValue() { // eslint-disable-line consistent-return
  37. if (this.isUpdate) {
  38. return new Date();
  39. } else {
  40. this.unset();
  41. }
  42. },
  43. },
  44. }));
  45. Lists.allow({
  46. insert(userId, doc) {
  47. return allowIsBoardMemberNonComment(userId, Boards.findOne(doc.boardId));
  48. },
  49. update(userId, doc) {
  50. return allowIsBoardMemberNonComment(userId, Boards.findOne(doc.boardId));
  51. },
  52. remove(userId, doc) {
  53. return allowIsBoardMemberNonComment(userId, Boards.findOne(doc.boardId));
  54. },
  55. fetch: ['boardId'],
  56. });
  57. Lists.helpers({
  58. cards() {
  59. return Cards.find(Filter.mongoSelector({
  60. listId: this._id,
  61. archived: false,
  62. }), { sort: ['sort'] });
  63. },
  64. allCards() {
  65. return Cards.find({ listId: this._id });
  66. },
  67. board() {
  68. return Boards.findOne(this.boardId);
  69. },
  70. });
  71. Lists.mutations({
  72. rename(title) {
  73. return { $set: { title } };
  74. },
  75. archive() {
  76. return { $set: { archived: true } };
  77. },
  78. restore() {
  79. return { $set: { archived: false } };
  80. },
  81. });
  82. Lists.hookOptions.after.update = { fetchPrevious: false };
  83. if (Meteor.isServer) {
  84. Meteor.startup(() => {
  85. Lists._collection._ensureIndex({ boardId: 1 });
  86. });
  87. Lists.after.insert((userId, doc) => {
  88. Activities.insert({
  89. userId,
  90. type: 'list',
  91. activityType: 'createList',
  92. boardId: doc.boardId,
  93. listId: doc._id,
  94. });
  95. });
  96. Lists.before.remove((userId, doc) => {
  97. Activities.insert({
  98. userId,
  99. type: 'list',
  100. activityType: 'removeList',
  101. boardId: doc.boardId,
  102. listId: doc._id,
  103. title: doc.title,
  104. });
  105. });
  106. Lists.after.update((userId, doc) => {
  107. if (doc.archived) {
  108. Activities.insert({
  109. userId,
  110. type: 'list',
  111. activityType: 'archivedList',
  112. listId: doc._id,
  113. boardId: doc.boardId,
  114. });
  115. }
  116. });
  117. }
  118. //LISTS REST API
  119. if (Meteor.isServer) {
  120. JsonRoutes.add('GET', '/api/boards/:boardId/lists', function (req, res, next) {
  121. const paramBoardId = req.params.boardId;
  122. Authentication.checkBoardAccess( req.userId, paramBoardId);
  123. JsonRoutes.sendResult(res, {
  124. code: 200,
  125. data: Lists.find({ boardId: paramBoardId, archived: false }).map(function (doc) {
  126. return {
  127. _id: doc._id,
  128. title: doc.title,
  129. };
  130. }),
  131. });
  132. });
  133. JsonRoutes.add('GET', '/api/boards/:boardId/lists/:listId', function (req, res, next) {
  134. const paramBoardId = req.params.boardId;
  135. const paramListId = req.params.listId;
  136. Authentication.checkBoardAccess( req.userId, paramBoardId);
  137. JsonRoutes.sendResult(res, {
  138. code: 200,
  139. data: Lists.findOne({ _id: paramListId, boardId: paramBoardId, archived: false }),
  140. });
  141. });
  142. JsonRoutes.add('POST', '/api/boards/:boardId/lists', function (req, res, next) {
  143. Authentication.checkUserId( req.userId);
  144. const paramBoardId = req.params.boardId;
  145. const id = Lists.insert({
  146. title: req.body.title,
  147. boardId: paramBoardId,
  148. });
  149. JsonRoutes.sendResult(res, {
  150. code: 200,
  151. data: {
  152. _id: id,
  153. },
  154. });
  155. });
  156. JsonRoutes.add('DELETE', '/api/boards/:boardId/lists/:listId', function (req, res, next) {
  157. Authentication.checkUserId( req.userId);
  158. const paramBoardId = req.params.boardId;
  159. const paramListId = req.params.listId;
  160. Lists.remove({ _id: paramListId, boardId: paramBoardId });
  161. JsonRoutes.sendResult(res, {
  162. code: 200,
  163. data: {
  164. _id: paramListId,
  165. },
  166. });
  167. });
  168. }