checklists.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. copy(newCardId) {
  50. const oldChecklistId = this._id;
  51. this._id = null;
  52. this.cardId = newCardId;
  53. const newChecklistId = Checklists.insert(this);
  54. ChecklistItems.find({checklistId: oldChecklistId}).forEach((item) => {
  55. item._id = null;
  56. item.checklistId = newChecklistId;
  57. item.cardId = newCardId;
  58. ChecklistItems.insert(item);
  59. });
  60. },
  61. itemCount() {
  62. return ChecklistItems.find({ checklistId: this._id }).count();
  63. },
  64. items() {
  65. return ChecklistItems.find({
  66. checklistId: this._id,
  67. }, { sort: ['sort'] });
  68. },
  69. finishedCount() {
  70. return ChecklistItems.find({
  71. checklistId: this._id,
  72. isFinished: true,
  73. }).count();
  74. },
  75. isFinished() {
  76. return 0 !== this.itemCount() && this.itemCount() === this.finishedCount();
  77. },
  78. checkAllItems(){
  79. const checkItems = ChecklistItems.find({checklistId: this._id});
  80. checkItems.forEach(function(item){
  81. item.check();
  82. });
  83. },
  84. uncheckAllItems(){
  85. const checkItems = ChecklistItems.find({checklistId: this._id});
  86. checkItems.forEach(function(item){
  87. item.uncheck();
  88. });
  89. },
  90. itemIndex(itemId) {
  91. const items = self.findOne({_id : this._id}).items;
  92. return _.pluck(items, '_id').indexOf(itemId);
  93. },
  94. });
  95. Checklists.allow({
  96. insert(userId, doc) {
  97. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  98. },
  99. update(userId, doc) {
  100. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  101. },
  102. remove(userId, doc) {
  103. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  104. },
  105. fetch: ['userId', 'cardId'],
  106. });
  107. Checklists.before.insert((userId, doc) => {
  108. doc.createdAt = new Date();
  109. if (!doc.userId) {
  110. doc.userId = userId;
  111. }
  112. });
  113. Checklists.mutations({
  114. setTitle(title) {
  115. return { $set: { title } };
  116. },
  117. });
  118. if (Meteor.isServer) {
  119. Meteor.startup(() => {
  120. Checklists._collection._ensureIndex({ cardId: 1, createdAt: 1 });
  121. });
  122. Checklists.after.insert((userId, doc) => {
  123. const card = Cards.findOne(doc.cardId);
  124. Activities.insert({
  125. userId,
  126. activityType: 'addChecklist',
  127. cardId: doc.cardId,
  128. boardId: card.boardId,
  129. checklistId: doc._id,
  130. checklistName:doc.title,
  131. listId: card.listId,
  132. swimlaneId: card.swimlaneId,
  133. });
  134. });
  135. Checklists.before.remove((userId, doc) => {
  136. const activities = Activities.find({ checklistId: doc._id });
  137. const card = Cards.findOne(doc.cardId);
  138. if (activities) {
  139. activities.forEach((activity) => {
  140. Activities.remove(activity._id);
  141. });
  142. }
  143. Activities.insert({
  144. userId,
  145. activityType: 'removeChecklist',
  146. cardId: doc.cardId,
  147. boardId: Cards.findOne(doc.cardId).boardId,
  148. checklistId: doc._id,
  149. checklistName:doc.title,
  150. listId: card.listId,
  151. swimlaneId: card.swimlaneId,
  152. });
  153. });
  154. }
  155. if (Meteor.isServer) {
  156. /**
  157. * @operation get_all_checklists
  158. * @summary Get the list of checklists attached to a card
  159. *
  160. * @param {string} boardId the board ID
  161. * @param {string} cardId the card ID
  162. * @return_type [{_id: string,
  163. * title: string}]
  164. */
  165. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) {
  166. Authentication.checkUserId( req.userId);
  167. const paramCardId = req.params.cardId;
  168. const checklists = Checklists.find({ cardId: paramCardId }).map(function (doc) {
  169. return {
  170. _id: doc._id,
  171. title: doc.title,
  172. };
  173. });
  174. if (checklists) {
  175. JsonRoutes.sendResult(res, {
  176. code: 200,
  177. data: checklists,
  178. });
  179. } else {
  180. JsonRoutes.sendResult(res, {
  181. code: 500,
  182. });
  183. }
  184. });
  185. /**
  186. * @operation get_checklist
  187. * @summary Get a checklist
  188. *
  189. * @param {string} boardId the board ID
  190. * @param {string} cardId the card ID
  191. * @param {string} checklistId the ID of the checklist
  192. * @return_type {cardId: string,
  193. * title: string,
  194. * finishedAt: string,
  195. * createdAt: string,
  196. * sort: number,
  197. * items: [{_id: string,
  198. * title: string,
  199. * isFinished: boolean}]}
  200. */
  201. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
  202. Authentication.checkUserId( req.userId);
  203. const paramChecklistId = req.params.checklistId;
  204. const paramCardId = req.params.cardId;
  205. const checklist = Checklists.findOne({ _id: paramChecklistId, cardId: paramCardId });
  206. if (checklist) {
  207. checklist.items = ChecklistItems.find({checklistId: checklist._id}).map(function (doc) {
  208. return {
  209. _id: doc._id,
  210. title: doc.title,
  211. isFinished: doc.isFinished,
  212. };
  213. });
  214. JsonRoutes.sendResult(res, {
  215. code: 200,
  216. data: checklist,
  217. });
  218. } else {
  219. JsonRoutes.sendResult(res, {
  220. code: 500,
  221. });
  222. }
  223. });
  224. /**
  225. * @operation new_checklist
  226. * @summary create a new checklist
  227. *
  228. * @param {string} boardId the board ID
  229. * @param {string} cardId the card ID
  230. * @param {string} title the title of the new checklist
  231. * @return_type {_id: string}
  232. */
  233. JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) {
  234. Authentication.checkUserId( req.userId);
  235. const paramCardId = req.params.cardId;
  236. const id = Checklists.insert({
  237. title: req.body.title,
  238. cardId: paramCardId,
  239. sort: 0,
  240. });
  241. if (id) {
  242. req.body.items.forEach(function (item, idx) {
  243. ChecklistItems.insert({
  244. cardId: paramCardId,
  245. checklistId: id,
  246. title: item.title,
  247. sort: idx,
  248. });
  249. });
  250. JsonRoutes.sendResult(res, {
  251. code: 200,
  252. data: {
  253. _id: id,
  254. },
  255. });
  256. } else {
  257. JsonRoutes.sendResult(res, {
  258. code: 400,
  259. });
  260. }
  261. });
  262. /**
  263. * @operation delete_checklist
  264. * @summary Delete a checklist
  265. *
  266. * @description The checklist will be removed, not put in the recycle bin.
  267. *
  268. * @param {string} boardId the board ID
  269. * @param {string} cardId the card ID
  270. * @param {string} checklistId the ID of the checklist to remove
  271. * @return_type {_id: string}
  272. */
  273. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
  274. Authentication.checkUserId( req.userId);
  275. const paramChecklistId = req.params.checklistId;
  276. Checklists.remove({ _id: paramChecklistId });
  277. JsonRoutes.sendResult(res, {
  278. code: 200,
  279. data: {
  280. _id: paramChecklistId,
  281. },
  282. });
  283. });
  284. }