checklists.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. Checklists = new Mongo.Collection('checklists');
  2. Checklists.attachSchema(new SimpleSchema({
  3. cardId: {
  4. type: String,
  5. },
  6. title: {
  7. type: String,
  8. },
  9. finishedAt: {
  10. type: Date,
  11. optional: true,
  12. },
  13. createdAt: {
  14. type: Date,
  15. denyUpdate: false,
  16. autoValue() { // eslint-disable-line consistent-return
  17. if (this.isInsert) {
  18. return new Date();
  19. } else {
  20. this.unset();
  21. }
  22. },
  23. },
  24. sort: {
  25. type: Number,
  26. decimal: true,
  27. },
  28. }));
  29. Checklists.helpers({
  30. itemCount() {
  31. return ChecklistItems.find({ checklistId: this._id }).count();
  32. },
  33. items() {
  34. return ChecklistItems.find(Filter.mongoSelector({
  35. checklistId: this._id,
  36. }), { sort: ['sort'] });
  37. },
  38. finishedCount() {
  39. return ChecklistItems.find({
  40. checklistId: this._id,
  41. isFinished: true,
  42. }).count();
  43. },
  44. isFinished() {
  45. return 0 !== this.itemCount() && this.itemCount() === this.finishedCount();
  46. },
  47. itemIndex(itemId) {
  48. const items = self.findOne({_id : this._id}).items;
  49. return _.pluck(items, '_id').indexOf(itemId);
  50. },
  51. });
  52. Checklists.allow({
  53. insert(userId, doc) {
  54. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  55. },
  56. update(userId, doc) {
  57. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  58. },
  59. remove(userId, doc) {
  60. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  61. },
  62. fetch: ['userId', 'cardId'],
  63. });
  64. Checklists.before.insert((userId, doc) => {
  65. doc.createdAt = new Date();
  66. if (!doc.userId) {
  67. doc.userId = userId;
  68. }
  69. });
  70. Checklists.mutations({
  71. setTitle(title) {
  72. return { $set: { title } };
  73. },
  74. });
  75. if (Meteor.isServer) {
  76. Meteor.startup(() => {
  77. Checklists._collection._ensureIndex({ cardId: 1, createdAt: 1 });
  78. });
  79. Checklists.after.insert((userId, doc) => {
  80. Activities.insert({
  81. userId,
  82. activityType: 'addChecklist',
  83. cardId: doc.cardId,
  84. boardId: Cards.findOne(doc.cardId).boardId,
  85. checklistId: doc._id,
  86. });
  87. });
  88. Checklists.before.remove((userId, doc) => {
  89. const activities = Activities.find({ checklistId: doc._id });
  90. if (activities) {
  91. activities.forEach((activity) => {
  92. Activities.remove(activity._id);
  93. });
  94. }
  95. });
  96. }
  97. if (Meteor.isServer) {
  98. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) {
  99. try {
  100. Authentication.checkUserId( req.userId);
  101. const paramCardId = req.params.cardId;
  102. JsonRoutes.sendResult(res, {
  103. code: 200,
  104. data: Checklists.find({ cardId: paramCardId }).map(function (doc) {
  105. return {
  106. _id: doc._id,
  107. title: doc.title,
  108. };
  109. }),
  110. });
  111. }
  112. catch (error) {
  113. JsonRoutes.sendResult(res, {
  114. code: 200,
  115. data: error,
  116. });
  117. }
  118. });
  119. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
  120. try {
  121. Authentication.checkUserId( req.userId);
  122. const paramChecklistId = req.params.checklistId;
  123. const paramCardId = req.params.cardId;
  124. JsonRoutes.sendResult(res, {
  125. code: 200,
  126. data: Checklists.findOne({ _id: paramChecklistId, cardId: paramCardId }),
  127. });
  128. }
  129. catch (error) {
  130. JsonRoutes.sendResult(res, {
  131. code: 200,
  132. data: error,
  133. });
  134. }
  135. });
  136. JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) {
  137. try {
  138. Authentication.checkUserId( req.userId);
  139. const paramCardId = req.params.cardId;
  140. const checklistToSend = {};
  141. checklistToSend.cardId = paramCardId;
  142. checklistToSend.title = req.body.title;
  143. checklistToSend.items = [];
  144. const id = Checklists.insert(checklistToSend);
  145. const checklist = Checklists.findOne({_id: id});
  146. req.body.items.forEach(function (item) {
  147. checklist.addItem(item);
  148. }, this);
  149. JsonRoutes.sendResult(res, {
  150. code: 200,
  151. data: {
  152. _id: id,
  153. },
  154. });
  155. }
  156. catch (error) {
  157. JsonRoutes.sendResult(res, {
  158. code: 200,
  159. data: error,
  160. });
  161. }
  162. });
  163. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
  164. try {
  165. Authentication.checkUserId( req.userId);
  166. const paramCommentId = req.params.commentId;
  167. const paramCardId = req.params.cardId;
  168. Checklists.remove({ _id: paramCommentId, cardId: paramCardId });
  169. JsonRoutes.sendResult(res, {
  170. code: 200,
  171. data: {
  172. _id: paramCardId,
  173. },
  174. });
  175. }
  176. catch (error) {
  177. JsonRoutes.sendResult(res, {
  178. code: 200,
  179. data: error,
  180. });
  181. }
  182. });
  183. }