checklists.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. Checklists = new Mongo.Collection('checklists');
  2. /**
  3. * A Checklist
  4. */
  5. Checklists.attachSchema(
  6. new SimpleSchema({
  7. cardId: {
  8. /**
  9. * The ID of the card the checklist is in
  10. */
  11. type: String,
  12. },
  13. title: {
  14. /**
  15. * the title of the checklist
  16. */
  17. type: String,
  18. defaultValue: 'Checklist',
  19. },
  20. finishedAt: {
  21. /**
  22. * When was the checklist finished
  23. */
  24. type: Date,
  25. optional: true,
  26. },
  27. createdAt: {
  28. /**
  29. * Creation date of the checklist
  30. */
  31. type: Date,
  32. denyUpdate: false,
  33. // eslint-disable-next-line consistent-return
  34. autoValue() {
  35. if (this.isInsert) {
  36. return new Date();
  37. } else if (this.isUpsert) {
  38. return { $setOnInsert: new Date() };
  39. } else {
  40. this.unset();
  41. }
  42. },
  43. },
  44. modifiedAt: {
  45. type: Date,
  46. denyUpdate: false,
  47. // eslint-disable-next-line consistent-return
  48. autoValue() {
  49. if (this.isInsert || this.isUpsert || this.isUpdate) {
  50. return new Date();
  51. } else {
  52. this.unset();
  53. }
  54. },
  55. },
  56. sort: {
  57. /**
  58. * sorting value of the checklist
  59. */
  60. type: Number,
  61. decimal: true,
  62. },
  63. }),
  64. );
  65. Checklists.helpers({
  66. copy(newCardId) {
  67. const oldChecklistId = this._id;
  68. this._id = null;
  69. this.cardId = newCardId;
  70. const newChecklistId = Checklists.insert(this);
  71. ChecklistItems.find({ checklistId: oldChecklistId }).forEach(item => {
  72. item._id = null;
  73. item.checklistId = newChecklistId;
  74. item.cardId = newCardId;
  75. ChecklistItems.insert(item);
  76. });
  77. },
  78. itemCount() {
  79. return ChecklistItems.find({ checklistId: this._id }).count();
  80. },
  81. items() {
  82. return ChecklistItems.find(
  83. {
  84. checklistId: this._id,
  85. },
  86. { sort: ['sort'] },
  87. );
  88. },
  89. finishedCount() {
  90. return ChecklistItems.find({
  91. checklistId: this._id,
  92. isFinished: true,
  93. }).count();
  94. },
  95. isFinished() {
  96. return 0 !== this.itemCount() && this.itemCount() === this.finishedCount();
  97. },
  98. checkAllItems() {
  99. const checkItems = ChecklistItems.find({ checklistId: this._id });
  100. checkItems.forEach(function(item) {
  101. item.check();
  102. });
  103. },
  104. uncheckAllItems() {
  105. const checkItems = ChecklistItems.find({ checklistId: this._id });
  106. checkItems.forEach(function(item) {
  107. item.uncheck();
  108. });
  109. },
  110. itemIndex(itemId) {
  111. const items = self.findOne({ _id: this._id }).items;
  112. return _.pluck(items, '_id').indexOf(itemId);
  113. },
  114. });
  115. Checklists.allow({
  116. insert(userId, doc) {
  117. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  118. },
  119. update(userId, doc) {
  120. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  121. },
  122. remove(userId, doc) {
  123. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  124. },
  125. fetch: ['userId', 'cardId'],
  126. });
  127. Checklists.before.insert((userId, doc) => {
  128. doc.createdAt = new Date();
  129. if (!doc.userId) {
  130. doc.userId = userId;
  131. }
  132. });
  133. Checklists.mutations({
  134. setTitle(title) {
  135. return { $set: { title } };
  136. },
  137. });
  138. if (Meteor.isServer) {
  139. Meteor.startup(() => {
  140. Checklists._collection._ensureIndex({ modifiedAt: -1 });
  141. Checklists._collection._ensureIndex({ cardId: 1, createdAt: 1 });
  142. });
  143. Checklists.after.insert((userId, doc) => {
  144. const card = Cards.findOne(doc.cardId);
  145. Activities.insert({
  146. userId,
  147. activityType: 'addChecklist',
  148. cardId: doc.cardId,
  149. boardId: card.boardId,
  150. checklistId: doc._id,
  151. checklistName: doc.title,
  152. listId: card.listId,
  153. swimlaneId: card.swimlaneId,
  154. });
  155. });
  156. Checklists.before.remove((userId, doc) => {
  157. const activities = Activities.find({ checklistId: doc._id });
  158. const card = Cards.findOne(doc.cardId);
  159. if (activities) {
  160. activities.forEach(activity => {
  161. Activities.remove(activity._id);
  162. });
  163. }
  164. Activities.insert({
  165. userId,
  166. activityType: 'removeChecklist',
  167. cardId: doc.cardId,
  168. boardId: Cards.findOne(doc.cardId).boardId,
  169. checklistId: doc._id,
  170. checklistName: doc.title,
  171. listId: card.listId,
  172. swimlaneId: card.swimlaneId,
  173. });
  174. });
  175. }
  176. if (Meteor.isServer) {
  177. /**
  178. * @operation get_all_checklists
  179. * @summary Get the list of checklists attached to a card
  180. *
  181. * @param {string} boardId the board ID
  182. * @param {string} cardId the card ID
  183. * @return_type [{_id: string,
  184. * title: string}]
  185. */
  186. JsonRoutes.add(
  187. 'GET',
  188. '/api/boards/:boardId/cards/:cardId/checklists',
  189. function(req, res) {
  190. Authentication.checkUserId(req.userId);
  191. const paramCardId = req.params.cardId;
  192. const checklists = Checklists.find({ cardId: paramCardId }).map(function(
  193. doc,
  194. ) {
  195. return {
  196. _id: doc._id,
  197. title: doc.title,
  198. };
  199. });
  200. if (checklists) {
  201. JsonRoutes.sendResult(res, {
  202. code: 200,
  203. data: checklists,
  204. });
  205. } else {
  206. JsonRoutes.sendResult(res, {
  207. code: 500,
  208. });
  209. }
  210. },
  211. );
  212. /**
  213. * @operation get_checklist
  214. * @summary Get a checklist
  215. *
  216. * @param {string} boardId the board ID
  217. * @param {string} cardId the card ID
  218. * @param {string} checklistId the ID of the checklist
  219. * @return_type {cardId: string,
  220. * title: string,
  221. * finishedAt: string,
  222. * createdAt: string,
  223. * sort: number,
  224. * items: [{_id: string,
  225. * title: string,
  226. * isFinished: boolean}]}
  227. */
  228. JsonRoutes.add(
  229. 'GET',
  230. '/api/boards/:boardId/cards/:cardId/checklists/:checklistId',
  231. function(req, res) {
  232. Authentication.checkUserId(req.userId);
  233. const paramChecklistId = req.params.checklistId;
  234. const paramCardId = req.params.cardId;
  235. const checklist = Checklists.findOne({
  236. _id: paramChecklistId,
  237. cardId: paramCardId,
  238. });
  239. if (checklist) {
  240. checklist.items = ChecklistItems.find({
  241. checklistId: checklist._id,
  242. }).map(function(doc) {
  243. return {
  244. _id: doc._id,
  245. title: doc.title,
  246. isFinished: doc.isFinished,
  247. };
  248. });
  249. JsonRoutes.sendResult(res, {
  250. code: 200,
  251. data: checklist,
  252. });
  253. } else {
  254. JsonRoutes.sendResult(res, {
  255. code: 500,
  256. });
  257. }
  258. },
  259. );
  260. /**
  261. * @operation new_checklist
  262. * @summary create a new checklist
  263. *
  264. * @param {string} boardId the board ID
  265. * @param {string} cardId the card ID
  266. * @param {string} title the title of the new checklist
  267. * @param {string} [items] the list of items on the new checklist
  268. * @return_type {_id: string}
  269. */
  270. JsonRoutes.add(
  271. 'POST',
  272. '/api/boards/:boardId/cards/:cardId/checklists',
  273. function(req, res) {
  274. // Check user is logged in
  275. Authentication.checkLoggedIn(req.userId);
  276. const paramBoardId = req.params.boardId;
  277. // Check user has permission to add checklist to the card
  278. const board = Boards.findOne({
  279. _id: paramBoardId,
  280. });
  281. const addPermission = allowIsBoardMemberCommentOnly(req.userId, board);
  282. Authentication.checkAdminOrCondition(req.userId, addPermission);
  283. const paramCardId = req.params.cardId;
  284. const id = Checklists.insert({
  285. title: req.body.title,
  286. cardId: paramCardId,
  287. sort: 0,
  288. });
  289. if (id) {
  290. let items = req.body.items || [];
  291. if (_.isString(items)) {
  292. if (items === '') {
  293. items = [];
  294. } else {
  295. items = [items];
  296. }
  297. }
  298. items.forEach(function(item, idx) {
  299. ChecklistItems.insert({
  300. cardId: paramCardId,
  301. checklistId: id,
  302. title: item,
  303. sort: idx,
  304. });
  305. });
  306. JsonRoutes.sendResult(res, {
  307. code: 200,
  308. data: {
  309. _id: id,
  310. },
  311. });
  312. } else {
  313. JsonRoutes.sendResult(res, {
  314. code: 400,
  315. });
  316. }
  317. },
  318. );
  319. /**
  320. * @operation delete_checklist
  321. * @summary Delete a checklist
  322. *
  323. * @description The checklist will be removed, not put in the recycle bin.
  324. *
  325. * @param {string} boardId the board ID
  326. * @param {string} cardId the card ID
  327. * @param {string} checklistId the ID of the checklist to remove
  328. * @return_type {_id: string}
  329. */
  330. JsonRoutes.add(
  331. 'DELETE',
  332. '/api/boards/:boardId/cards/:cardId/checklists/:checklistId',
  333. function(req, res) {
  334. Authentication.checkUserId(req.userId);
  335. const paramChecklistId = req.params.checklistId;
  336. Checklists.remove({ _id: paramChecklistId });
  337. JsonRoutes.sendResult(res, {
  338. code: 200,
  339. data: {
  340. _id: paramChecklistId,
  341. },
  342. });
  343. },
  344. );
  345. }
  346. export default Checklists;