checklists.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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({
  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. checkAllItems(){
  49. const checkItems = ChecklistItems.find({checklistId: this._id});
  50. checkItems.forEach(function(item){
  51. item.check();
  52. });
  53. },
  54. uncheckAllItems(){
  55. const checkItems = ChecklistItems.find({checklistId: this._id});
  56. checkItems.forEach(function(item){
  57. item.uncheck();
  58. });
  59. },
  60. itemIndex(itemId) {
  61. const items = self.findOne({_id : this._id}).items;
  62. return _.pluck(items, '_id').indexOf(itemId);
  63. },
  64. });
  65. Checklists.allow({
  66. insert(userId, doc) {
  67. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  68. },
  69. update(userId, doc) {
  70. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  71. },
  72. remove(userId, doc) {
  73. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  74. },
  75. fetch: ['userId', 'cardId'],
  76. });
  77. Checklists.before.insert((userId, doc) => {
  78. doc.createdAt = new Date();
  79. if (!doc.userId) {
  80. doc.userId = userId;
  81. }
  82. });
  83. Checklists.mutations({
  84. setTitle(title) {
  85. return { $set: { title } };
  86. },
  87. });
  88. if (Meteor.isServer) {
  89. Meteor.startup(() => {
  90. Checklists._collection._ensureIndex({ cardId: 1, createdAt: 1 });
  91. });
  92. Checklists.after.insert((userId, doc) => {
  93. Activities.insert({
  94. userId,
  95. activityType: 'addChecklist',
  96. cardId: doc.cardId,
  97. boardId: Cards.findOne(doc.cardId).boardId,
  98. checklistId: doc._id,
  99. checklistName:doc.title,
  100. });
  101. });
  102. Checklists.before.remove((userId, doc) => {
  103. const activities = Activities.find({ checklistId: doc._id });
  104. if (activities) {
  105. activities.forEach((activity) => {
  106. Activities.remove(activity._id);
  107. });
  108. }
  109. Activities.insert({
  110. userId,
  111. activityType: 'removeChecklist',
  112. cardId: doc.cardId,
  113. boardId: Cards.findOne(doc.cardId).boardId,
  114. checklistId: doc._id,
  115. checklistName:doc.title,
  116. });
  117. });
  118. }
  119. if (Meteor.isServer) {
  120. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) {
  121. Authentication.checkUserId( req.userId);
  122. const paramCardId = req.params.cardId;
  123. const checklists = Checklists.find({ cardId: paramCardId }).map(function (doc) {
  124. return {
  125. _id: doc._id,
  126. title: doc.title,
  127. };
  128. });
  129. if (checklists) {
  130. JsonRoutes.sendResult(res, {
  131. code: 200,
  132. data: checklists,
  133. });
  134. } else {
  135. JsonRoutes.sendResult(res, {
  136. code: 500,
  137. });
  138. }
  139. });
  140. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
  141. Authentication.checkUserId( req.userId);
  142. const paramChecklistId = req.params.checklistId;
  143. const paramCardId = req.params.cardId;
  144. const checklist = Checklists.findOne({ _id: paramChecklistId, cardId: paramCardId });
  145. if (checklist) {
  146. checklist.items = ChecklistItems.find({checklistId: checklist._id}).map(function (doc) {
  147. return {
  148. _id: doc._id,
  149. title: doc.title,
  150. isFinished: doc.isFinished,
  151. };
  152. });
  153. JsonRoutes.sendResult(res, {
  154. code: 200,
  155. data: checklist,
  156. });
  157. } else {
  158. JsonRoutes.sendResult(res, {
  159. code: 500,
  160. });
  161. }
  162. });
  163. JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) {
  164. Authentication.checkUserId( req.userId);
  165. const paramCardId = req.params.cardId;
  166. const id = Checklists.insert({
  167. title: req.body.title,
  168. cardId: paramCardId,
  169. sort: 0,
  170. });
  171. if (id) {
  172. req.body.items.forEach(function (item, idx) {
  173. ChecklistItems.insert({
  174. cardId: paramCardId,
  175. checklistId: id,
  176. title: item.title,
  177. sort: idx,
  178. });
  179. });
  180. JsonRoutes.sendResult(res, {
  181. code: 200,
  182. data: {
  183. _id: id,
  184. },
  185. });
  186. } else {
  187. JsonRoutes.sendResult(res, {
  188. code: 400,
  189. });
  190. }
  191. });
  192. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
  193. Authentication.checkUserId( req.userId);
  194. const paramChecklistId = req.params.checklistId;
  195. Checklists.remove({ _id: paramChecklistId });
  196. JsonRoutes.sendResult(res, {
  197. code: 200,
  198. data: {
  199. _id: paramChecklistId,
  200. },
  201. });
  202. });
  203. }