checklists.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. Checklists = new Mongo.Collection('checklists');
  2. Checklists.attachSchema(new SimpleSchema({
  3. cardId: {
  4. type: String,
  5. },
  6. title: {
  7. type: String,
  8. },
  9. items: {
  10. type: [Object],
  11. defaultValue: [],
  12. },
  13. 'items.$._id': {
  14. type: String,
  15. },
  16. 'items.$.title': {
  17. type: String,
  18. },
  19. 'items.$.isFinished': {
  20. type: Boolean,
  21. defaultValue: false,
  22. },
  23. finishedAt: {
  24. type: Date,
  25. optional: true,
  26. },
  27. createdAt: {
  28. type: Date,
  29. denyUpdate: false,
  30. autoValue() { // eslint-disable-line consistent-return
  31. if (this.isInsert) {
  32. return new Date();
  33. } else {
  34. this.unset();
  35. }
  36. },
  37. },
  38. }));
  39. Checklists.helpers({
  40. itemCount() {
  41. return this.items.length;
  42. },
  43. finishedCount() {
  44. return this.items.filter((item) => {
  45. return item.isFinished;
  46. }).length;
  47. },
  48. isFinished() {
  49. return 0 !== this.itemCount() && this.itemCount() === this.finishedCount();
  50. },
  51. getItem(_id) {
  52. return _.findWhere(this.items, { _id });
  53. },
  54. itemIndex(itemId) {
  55. return _.pluck(this.items, '_id').indexOf(itemId);
  56. },
  57. });
  58. Checklists.allow({
  59. insert(userId, doc) {
  60. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  61. },
  62. update(userId, doc) {
  63. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  64. },
  65. remove(userId, doc) {
  66. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  67. },
  68. fetch: ['userId', 'cardId'],
  69. });
  70. Checklists.before.insert((userId, doc) => {
  71. doc.createdAt = new Date();
  72. if (!doc.userId) {
  73. doc.userId = userId;
  74. }
  75. });
  76. Checklists.mutations({
  77. //for checklist itself
  78. setTitle(title) {
  79. return { $set: { title } };
  80. },
  81. //for items in checklist
  82. addItem(title) {
  83. const itemCount = this.itemCount();
  84. const _id = `${this._id}${itemCount}`;
  85. return { $addToSet: { items: { _id, title, isFinished: false } } };
  86. },
  87. removeItem(itemId) {
  88. return { $pull: { items: { _id: itemId } } };
  89. },
  90. editItem(itemId, title) {
  91. if (this.getItem(itemId)) {
  92. const itemIndex = this.itemIndex(itemId);
  93. return {
  94. $set: {
  95. [`items.${itemIndex}.title`]: title,
  96. },
  97. };
  98. }
  99. return {};
  100. },
  101. finishItem(itemId) {
  102. if (this.getItem(itemId)) {
  103. const itemIndex = this.itemIndex(itemId);
  104. return {
  105. $set: {
  106. [`items.${itemIndex}.isFinished`]: true,
  107. },
  108. };
  109. }
  110. return {};
  111. },
  112. resumeItem(itemId) {
  113. if (this.getItem(itemId)) {
  114. const itemIndex = this.itemIndex(itemId);
  115. return {
  116. $set: {
  117. [`items.${itemIndex}.isFinished`]: false,
  118. },
  119. };
  120. }
  121. return {};
  122. },
  123. toggleItem(itemId) {
  124. const item = this.getItem(itemId);
  125. if (item) {
  126. const itemIndex = this.itemIndex(itemId);
  127. return {
  128. $set: {
  129. [`items.${itemIndex}.isFinished`]: !item.isFinished,
  130. },
  131. };
  132. }
  133. return {};
  134. },
  135. });
  136. if (Meteor.isServer) {
  137. Meteor.startup(() => {
  138. Checklists._collection._ensureIndex({ cardId: 1, createdAt: 1 });
  139. });
  140. Checklists.after.insert((userId, doc) => {
  141. Activities.insert({
  142. userId,
  143. activityType: 'addChecklist',
  144. cardId: doc.cardId,
  145. boardId: Cards.findOne(doc.cardId).boardId,
  146. checklistId: doc._id,
  147. });
  148. });
  149. //TODO: so there will be no activity for adding item into checklist, maybe will be implemented in the future.
  150. // Checklists.after.update((userId, doc) => {
  151. // console.log('update:', doc)
  152. // Activities.insert({
  153. // userId,
  154. // activityType: 'addChecklist',
  155. // boardId: doc.boardId,
  156. // cardId: doc.cardId,
  157. // checklistId: doc._id,
  158. // });
  159. // });
  160. Checklists.before.remove((userId, doc) => {
  161. const activity = Activities.findOne({ checklistId: doc._id });
  162. if (activity) {
  163. Activities.remove(activity._id);
  164. }
  165. });
  166. }
  167. //CARD COMMENT REST API
  168. if (Meteor.isServer) {
  169. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res, next) {
  170. Authentication.checkUserId( req.userId);
  171. const paramCardId = req.params.cardId;
  172. JsonRoutes.sendResult(res, {
  173. code: 200,
  174. data: Checklists.find({ cardId: paramCardId }).map(function (doc) {
  175. return {
  176. _id: doc._id,
  177. title: doc.title,
  178. };
  179. }),
  180. });
  181. });
  182. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res, next) {
  183. Authentication.checkUserId( req.userId);
  184. const paramChecklistId = req.params.checklistId;
  185. const paramCardId = req.params.cardId;
  186. JsonRoutes.sendResult(res, {
  187. code: 200,
  188. data: Checklists.findOne({ _id: paramChecklistId, cardId: paramCardId }),
  189. });
  190. });
  191. JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res, next) {
  192. Authentication.checkUserId( req.userId);
  193. const paramCardId = req.params.cardId;
  194. const checklistToSend = {};
  195. checklistToSend.cardId = paramCardId;
  196. checklistToSend.title = req.body.title;
  197. checklistToSend.items = [];
  198. const id = Checklists.insert(checklistToSend);
  199. const checklist = Checklists.findOne({_id: id});
  200. req.body.items.forEach(function (item) {
  201. checklist.addItem(item);
  202. }, this);
  203. JsonRoutes.sendResult(res, {
  204. code: 200,
  205. data: {
  206. _id: id,
  207. },
  208. });
  209. });
  210. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res, next) {
  211. Authentication.checkUserId( req.userId);
  212. const paramCommentId = req.params.commentId;
  213. const paramCardId = req.params.cardId;
  214. Checklists.remove({ _id: paramCommentId, cardId: paramCardId });
  215. JsonRoutes.sendResult(res, {
  216. code: 200,
  217. data: {
  218. _id: paramCardId,
  219. },
  220. });
  221. });
  222. }