2
0

checklists.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. Authentication.checkUserId( req.userId);
  101. const paramCardId = req.params.cardId;
  102. const checklists = Checklists.find({ cardId: paramCardId }).map(function (doc) {
  103. return {
  104. _id: doc._id,
  105. title: doc.title,
  106. };
  107. });
  108. if (checklists) {
  109. JsonRoutes.sendResult(res, {
  110. code: 200,
  111. data: checklists,
  112. });
  113. } else {
  114. JsonRoutes.sendResult(res, {
  115. code: 500,
  116. });
  117. }
  118. });
  119. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
  120. Authentication.checkUserId( req.userId);
  121. const paramChecklistId = req.params.checklistId;
  122. const paramCardId = req.params.cardId;
  123. const checklist = Checklists.findOne({ _id: paramChecklistId, cardId: paramCardId });
  124. if (checklist) {
  125. checklist.items = ChecklistItems.find({checklistId: checklist._id}).map(function (doc) {
  126. return {
  127. _id: doc._id,
  128. title: doc.title,
  129. };
  130. });
  131. JsonRoutes.sendResult(res, {
  132. code: 200,
  133. data: checklist,
  134. });
  135. } else {
  136. JsonRoutes.sendResult(res, {
  137. code: 500,
  138. });
  139. }
  140. });
  141. JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) {
  142. Authentication.checkUserId( req.userId);
  143. const paramCardId = req.params.cardId;
  144. const id = Checklists.insert({
  145. title: req.body.title,
  146. cardId: paramCardId,
  147. sort: 0,
  148. });
  149. if (id) {
  150. const checklist = Checklists.findOne({_id: id});
  151. req.body.items.forEach(function (item, idx) {
  152. ChecklistItems.insert({
  153. cardId: paramCardId,
  154. checklistId: id,
  155. title: item.title,
  156. sort: idx,
  157. });
  158. });
  159. JsonRoutes.sendResult(res, {
  160. code: 200,
  161. data: {
  162. _id: id,
  163. },
  164. });
  165. } else {
  166. JsonRoutes.sendResult(res, {
  167. code: 400,
  168. });
  169. }
  170. });
  171. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
  172. Authentication.checkUserId( req.userId);
  173. const paramChecklistId = req.params.checklistId;
  174. Checklists.remove({ _id: paramChecklistId });
  175. JsonRoutes.sendResult(res, {
  176. code: 200,
  177. data: {
  178. _id: paramChecklistId,
  179. },
  180. });
  181. });
  182. }