checklists.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. let idx = 0;
  85. if (itemCount > 0) {
  86. const lastId = this.items[itemCount - 1]._id;
  87. const lastIdSuffix = lastId.substr(this._id.length);
  88. idx = parseInt(lastIdSuffix, 10) + 1;
  89. }
  90. const _id = `${this._id}${idx}`;
  91. return { $addToSet: { items: { _id, title, isFinished: false } } };
  92. },
  93. removeItem(itemId) {
  94. return { $pull: { items: { _id: itemId } } };
  95. },
  96. editItem(itemId, title) {
  97. if (this.getItem(itemId)) {
  98. const itemIndex = this.itemIndex(itemId);
  99. return {
  100. $set: {
  101. [`items.${itemIndex}.title`]: title,
  102. },
  103. };
  104. }
  105. return {};
  106. },
  107. finishItem(itemId) {
  108. if (this.getItem(itemId)) {
  109. const itemIndex = this.itemIndex(itemId);
  110. return {
  111. $set: {
  112. [`items.${itemIndex}.isFinished`]: true,
  113. },
  114. };
  115. }
  116. return {};
  117. },
  118. resumeItem(itemId) {
  119. if (this.getItem(itemId)) {
  120. const itemIndex = this.itemIndex(itemId);
  121. return {
  122. $set: {
  123. [`items.${itemIndex}.isFinished`]: false,
  124. },
  125. };
  126. }
  127. return {};
  128. },
  129. toggleItem(itemId) {
  130. const item = this.getItem(itemId);
  131. if (item) {
  132. const itemIndex = this.itemIndex(itemId);
  133. return {
  134. $set: {
  135. [`items.${itemIndex}.isFinished`]: !item.isFinished,
  136. },
  137. };
  138. }
  139. return {};
  140. },
  141. });
  142. if (Meteor.isServer) {
  143. Meteor.startup(() => {
  144. Checklists._collection._ensureIndex({ cardId: 1, createdAt: 1 });
  145. });
  146. Checklists.after.insert((userId, doc) => {
  147. Activities.insert({
  148. userId,
  149. activityType: 'addChecklist',
  150. cardId: doc.cardId,
  151. boardId: Cards.findOne(doc.cardId).boardId,
  152. checklistId: doc._id,
  153. });
  154. });
  155. //TODO: so there will be no activity for adding item into checklist, maybe will be implemented in the future.
  156. // Checklists.after.update((userId, doc) => {
  157. // console.log('update:', doc)
  158. // Activities.insert({
  159. // userId,
  160. // activityType: 'addChecklist',
  161. // boardId: doc.boardId,
  162. // cardId: doc.cardId,
  163. // checklistId: doc._id,
  164. // });
  165. // });
  166. Checklists.before.remove((userId, doc) => {
  167. const activity = Activities.findOne({ checklistId: doc._id });
  168. if (activity) {
  169. Activities.remove(activity._id);
  170. }
  171. });
  172. }
  173. //CARD COMMENT REST API
  174. if (Meteor.isServer) {
  175. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res, next) {
  176. Authentication.checkUserId( req.userId);
  177. const paramCardId = req.params.cardId;
  178. JsonRoutes.sendResult(res, {
  179. code: 200,
  180. data: Checklists.find({ cardId: paramCardId }).map(function (doc) {
  181. return {
  182. _id: doc._id,
  183. title: doc.title,
  184. };
  185. }),
  186. });
  187. });
  188. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res, next) {
  189. Authentication.checkUserId( req.userId);
  190. const paramChecklistId = req.params.checklistId;
  191. const paramCardId = req.params.cardId;
  192. JsonRoutes.sendResult(res, {
  193. code: 200,
  194. data: Checklists.findOne({ _id: paramChecklistId, cardId: paramCardId }),
  195. });
  196. });
  197. JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res, next) {
  198. Authentication.checkUserId( req.userId);
  199. const paramCardId = req.params.cardId;
  200. const checklistToSend = {};
  201. checklistToSend.cardId = paramCardId;
  202. checklistToSend.title = req.body.title;
  203. checklistToSend.items = [];
  204. const id = Checklists.insert(checklistToSend);
  205. const checklist = Checklists.findOne({_id: id});
  206. req.body.items.forEach(function (item) {
  207. checklist.addItem(item);
  208. }, this);
  209. JsonRoutes.sendResult(res, {
  210. code: 200,
  211. data: {
  212. _id: id,
  213. },
  214. });
  215. });
  216. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res, next) {
  217. Authentication.checkUserId( req.userId);
  218. const paramCommentId = req.params.commentId;
  219. const paramCardId = req.params.cardId;
  220. Checklists.remove({ _id: paramCommentId, cardId: paramCardId });
  221. JsonRoutes.sendResult(res, {
  222. code: 200,
  223. data: {
  224. _id: paramCardId,
  225. },
  226. });
  227. });
  228. }