checklists.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. Checklists = new Mongo.Collection('checklists');
  2. /**
  3. * A Checklist
  4. */
  5. Checklists.attachSchema(new SimpleSchema({
  6. cardId: {
  7. /**
  8. * The ID of the card the checklist is in
  9. */
  10. type: String,
  11. },
  12. title: {
  13. /**
  14. * the title of the checklist
  15. */
  16. type: String,
  17. defaultValue: 'Checklist',
  18. },
  19. finishedAt: {
  20. /**
  21. * When was the checklist finished
  22. */
  23. type: Date,
  24. optional: true,
  25. },
  26. createdAt: {
  27. /**
  28. * Creation date of the checklist
  29. */
  30. type: Date,
  31. denyUpdate: false,
  32. autoValue() { // eslint-disable-line consistent-return
  33. if (this.isInsert) {
  34. return new Date();
  35. } else {
  36. this.unset();
  37. }
  38. },
  39. },
  40. sort: {
  41. /**
  42. * sorting value of the checklist
  43. */
  44. type: Number,
  45. decimal: true,
  46. },
  47. }));
  48. Checklists.helpers({
  49. itemCount() {
  50. return ChecklistItems.find({ checklistId: this._id }).count();
  51. },
  52. items() {
  53. return ChecklistItems.find({
  54. checklistId: this._id,
  55. }, { sort: ['sort'] });
  56. },
  57. finishedCount() {
  58. return ChecklistItems.find({
  59. checklistId: this._id,
  60. isFinished: true,
  61. }).count();
  62. },
  63. isFinished() {
  64. return 0 !== this.itemCount() && this.itemCount() === this.finishedCount();
  65. },
  66. checkAllItems(){
  67. const checkItems = ChecklistItems.find({checklistId: this._id});
  68. checkItems.forEach(function(item){
  69. item.check();
  70. });
  71. },
  72. uncheckAllItems(){
  73. const checkItems = ChecklistItems.find({checklistId: this._id});
  74. checkItems.forEach(function(item){
  75. item.uncheck();
  76. });
  77. },
  78. itemIndex(itemId) {
  79. const items = self.findOne({_id : this._id}).items;
  80. return _.pluck(items, '_id').indexOf(itemId);
  81. },
  82. });
  83. Checklists.allow({
  84. insert(userId, doc) {
  85. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  86. },
  87. update(userId, doc) {
  88. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  89. },
  90. remove(userId, doc) {
  91. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  92. },
  93. fetch: ['userId', 'cardId'],
  94. });
  95. Checklists.before.insert((userId, doc) => {
  96. doc.createdAt = new Date();
  97. if (!doc.userId) {
  98. doc.userId = userId;
  99. }
  100. });
  101. Checklists.mutations({
  102. setTitle(title) {
  103. return { $set: { title } };
  104. },
  105. });
  106. if (Meteor.isServer) {
  107. Meteor.startup(() => {
  108. Checklists._collection._ensureIndex({ cardId: 1, createdAt: 1 });
  109. });
  110. Checklists.after.insert((userId, doc) => {
  111. Activities.insert({
  112. userId,
  113. activityType: 'addChecklist',
  114. cardId: doc.cardId,
  115. boardId: Cards.findOne(doc.cardId).boardId,
  116. checklistId: doc._id,
  117. checklistName:doc.title,
  118. });
  119. });
  120. Checklists.before.remove((userId, doc) => {
  121. const activities = Activities.find({ checklistId: doc._id });
  122. if (activities) {
  123. activities.forEach((activity) => {
  124. Activities.remove(activity._id);
  125. });
  126. }
  127. Activities.insert({
  128. userId,
  129. activityType: 'removeChecklist',
  130. cardId: doc.cardId,
  131. boardId: Cards.findOne(doc.cardId).boardId,
  132. checklistId: doc._id,
  133. checklistName:doc.title,
  134. });
  135. });
  136. }
  137. if (Meteor.isServer) {
  138. /**
  139. * @operation get_all_checklists
  140. * @summary Get the list of checklists attached to a card
  141. *
  142. * @param {string} boardId the board ID
  143. * @param {string} cardId the card ID
  144. * @return_type [{_id: string,
  145. * title: string}]
  146. */
  147. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) {
  148. Authentication.checkUserId( req.userId);
  149. const paramCardId = req.params.cardId;
  150. const checklists = Checklists.find({ cardId: paramCardId }).map(function (doc) {
  151. return {
  152. _id: doc._id,
  153. title: doc.title,
  154. };
  155. });
  156. if (checklists) {
  157. JsonRoutes.sendResult(res, {
  158. code: 200,
  159. data: checklists,
  160. });
  161. } else {
  162. JsonRoutes.sendResult(res, {
  163. code: 500,
  164. });
  165. }
  166. });
  167. /**
  168. * @operation get_checklist
  169. * @summary Get a checklist
  170. *
  171. * @param {string} boardId the board ID
  172. * @param {string} cardId the card ID
  173. * @param {string} checklistId the ID of the checklist
  174. * @return_type {cardId: string,
  175. * title: string,
  176. * finishedAt: string,
  177. * createdAt: string,
  178. * sort: number,
  179. * items: [{_id: string,
  180. * title: string,
  181. * isFinished: boolean}]}
  182. */
  183. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
  184. Authentication.checkUserId( req.userId);
  185. const paramChecklistId = req.params.checklistId;
  186. const paramCardId = req.params.cardId;
  187. const checklist = Checklists.findOne({ _id: paramChecklistId, cardId: paramCardId });
  188. if (checklist) {
  189. checklist.items = ChecklistItems.find({checklistId: checklist._id}).map(function (doc) {
  190. return {
  191. _id: doc._id,
  192. title: doc.title,
  193. isFinished: doc.isFinished,
  194. };
  195. });
  196. JsonRoutes.sendResult(res, {
  197. code: 200,
  198. data: checklist,
  199. });
  200. } else {
  201. JsonRoutes.sendResult(res, {
  202. code: 500,
  203. });
  204. }
  205. });
  206. /**
  207. * @operation new_checklist
  208. * @summary create a new checklist
  209. *
  210. * @param {string} boardId the board ID
  211. * @param {string} cardId the card ID
  212. * @param {string} title the title of the new checklist
  213. * @return_type {_id: string}
  214. */
  215. JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) {
  216. Authentication.checkUserId( req.userId);
  217. const paramCardId = req.params.cardId;
  218. const id = Checklists.insert({
  219. title: req.body.title,
  220. cardId: paramCardId,
  221. sort: 0,
  222. });
  223. if (id) {
  224. req.body.items.forEach(function (item, idx) {
  225. ChecklistItems.insert({
  226. cardId: paramCardId,
  227. checklistId: id,
  228. title: item.title,
  229. sort: idx,
  230. });
  231. });
  232. JsonRoutes.sendResult(res, {
  233. code: 200,
  234. data: {
  235. _id: id,
  236. },
  237. });
  238. } else {
  239. JsonRoutes.sendResult(res, {
  240. code: 400,
  241. });
  242. }
  243. });
  244. /**
  245. * @operation delete_checklist
  246. * @summary Delete a checklist
  247. *
  248. * @description The checklist will be removed, not put in the recycle bin.
  249. *
  250. * @param {string} boardId the board ID
  251. * @param {string} cardId the card ID
  252. * @param {string} checklistId the ID of the checklist to remove
  253. * @return_type {_id: string}
  254. */
  255. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
  256. Authentication.checkUserId( req.userId);
  257. const paramChecklistId = req.params.checklistId;
  258. Checklists.remove({ _id: paramChecklistId });
  259. JsonRoutes.sendResult(res, {
  260. code: 200,
  261. data: {
  262. _id: paramChecklistId,
  263. },
  264. });
  265. });
  266. }