checklists.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. // The future is now
  157. Checklists.after.update((userId, doc, fieldNames, modifier) => {
  158. if (fieldNames.includes('items')) {
  159. Activities.insert({
  160. userId,
  161. activityType: 'addChecklistItem',
  162. cardId: doc.cardId,
  163. boardId: Cards.findOne(doc.cardId).boardId,
  164. checklistId: doc._id,
  165. checklistItemId: modifier.$addToSet.items._id,
  166. });
  167. }
  168. });
  169. Checklists.before.remove((userId, doc) => {
  170. const activity = Activities.findOne({ checklistId: doc._id });
  171. if (activity) {
  172. Activities.remove(activity._id);
  173. }
  174. });
  175. }
  176. //CARD COMMENT REST API
  177. if (Meteor.isServer) {
  178. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res, next) {
  179. Authentication.checkUserId( req.userId);
  180. const paramCardId = req.params.cardId;
  181. JsonRoutes.sendResult(res, {
  182. code: 200,
  183. data: Checklists.find({ cardId: paramCardId }).map(function (doc) {
  184. return {
  185. _id: doc._id,
  186. title: doc.title,
  187. };
  188. }),
  189. });
  190. });
  191. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res, next) {
  192. Authentication.checkUserId( req.userId);
  193. const paramChecklistId = req.params.checklistId;
  194. const paramCardId = req.params.cardId;
  195. JsonRoutes.sendResult(res, {
  196. code: 200,
  197. data: Checklists.findOne({ _id: paramChecklistId, cardId: paramCardId }),
  198. });
  199. });
  200. JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res, next) {
  201. Authentication.checkUserId( req.userId);
  202. const paramCardId = req.params.cardId;
  203. const checklistToSend = {};
  204. checklistToSend.cardId = paramCardId;
  205. checklistToSend.title = req.body.title;
  206. checklistToSend.items = [];
  207. const id = Checklists.insert(checklistToSend);
  208. const checklist = Checklists.findOne({_id: id});
  209. req.body.items.forEach(function (item) {
  210. checklist.addItem(item);
  211. }, this);
  212. JsonRoutes.sendResult(res, {
  213. code: 200,
  214. data: {
  215. _id: id,
  216. },
  217. });
  218. });
  219. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res, next) {
  220. Authentication.checkUserId( req.userId);
  221. const paramCommentId = req.params.commentId;
  222. const paramCardId = req.params.cardId;
  223. Checklists.remove({ _id: paramCommentId, cardId: paramCardId });
  224. JsonRoutes.sendResult(res, {
  225. code: 200,
  226. data: {
  227. _id: paramCardId,
  228. },
  229. });
  230. });
  231. }