checklists.js 4.7 KB

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