checklists.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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(function(
  72. item,
  73. ) {
  74. item._id = null;
  75. item.checklistId = newChecklistId;
  76. item.cardId = newCardId;
  77. ChecklistItems.insert(item);
  78. });
  79. },
  80. itemCount() {
  81. return ChecklistItems.find({ checklistId: this._id }).count();
  82. },
  83. items() {
  84. return ChecklistItems.find(
  85. {
  86. checklistId: this._id,
  87. },
  88. { sort: ['sort'] },
  89. );
  90. },
  91. lastItem() {
  92. const allItems = this.items().fetch();
  93. const ret = allItems[allItems.length - 1];
  94. return ret;
  95. },
  96. finishedCount() {
  97. return ChecklistItems.find({
  98. checklistId: this._id,
  99. isFinished: true,
  100. }).count();
  101. },
  102. /** returns the finished percent of the checklist */
  103. finishedPercent() {
  104. const checklistItems = ChecklistItems.find({ checklistId: this._id });
  105. const count = checklistItems.count();
  106. const checklistItemsFinished = checklistItems.fetch().filter(checklistItem => checklistItem.isFinished);
  107. let ret = 0;
  108. if (count > 0) {
  109. ret = Math.round(checklistItemsFinished.length / count * 100);
  110. }
  111. return ret;
  112. },
  113. isFinished() {
  114. return 0 !== this.itemCount() && this.itemCount() === this.finishedCount();
  115. },
  116. checkAllItems() {
  117. const checkItems = ChecklistItems.find({ checklistId: this._id });
  118. checkItems.forEach(function(item) {
  119. item.check();
  120. });
  121. },
  122. uncheckAllItems() {
  123. const checkItems = ChecklistItems.find({ checklistId: this._id });
  124. checkItems.forEach(function(item) {
  125. item.uncheck();
  126. });
  127. },
  128. itemIndex(itemId) {
  129. const items = self.findOne({ _id: this._id }).items;
  130. return _.pluck(items, '_id').indexOf(itemId);
  131. },
  132. });
  133. Checklists.allow({
  134. insert(userId, doc) {
  135. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  136. },
  137. update(userId, doc) {
  138. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  139. },
  140. remove(userId, doc) {
  141. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  142. },
  143. fetch: ['userId', 'cardId'],
  144. });
  145. Checklists.before.insert((userId, doc) => {
  146. doc.createdAt = new Date();
  147. if (!doc.userId) {
  148. doc.userId = userId;
  149. }
  150. });
  151. Checklists.mutations({
  152. setTitle(title) {
  153. return { $set: { title } };
  154. },
  155. /** move the checklist to another card
  156. * @param newCardId move the checklist to this cardId
  157. */
  158. move(newCardId) {
  159. // update every activity
  160. Activities.find(
  161. {checklistId: this._id}
  162. ).forEach(activity => {
  163. Activities.update(activity._id, {
  164. $set: {
  165. cardId: newCardId,
  166. },
  167. });
  168. });
  169. // update every checklist-item
  170. ChecklistItems.find(
  171. {checklistId: this._id}
  172. ).forEach(checklistItem => {
  173. ChecklistItems.update(checklistItem._id, {
  174. $set: {
  175. cardId: newCardId,
  176. },
  177. });
  178. });
  179. // update the checklist itself
  180. return {
  181. $set: {
  182. cardId: newCardId,
  183. },
  184. };
  185. },
  186. });
  187. if (Meteor.isServer) {
  188. Meteor.startup(() => {
  189. Checklists._collection._ensureIndex({ modifiedAt: -1 });
  190. Checklists._collection._ensureIndex({ cardId: 1, createdAt: 1 });
  191. });
  192. Checklists.after.insert((userId, doc) => {
  193. const card = Cards.findOne(doc.cardId);
  194. Activities.insert({
  195. userId,
  196. activityType: 'addChecklist',
  197. cardId: doc.cardId,
  198. boardId: card.boardId,
  199. checklistId: doc._id,
  200. checklistName: doc.title,
  201. listId: card.listId,
  202. swimlaneId: card.swimlaneId,
  203. });
  204. });
  205. Checklists.before.remove((userId, doc) => {
  206. const activities = Activities.find({ checklistId: doc._id });
  207. const card = Cards.findOne(doc.cardId);
  208. if (activities) {
  209. activities.forEach(activity => {
  210. Activities.remove(activity._id);
  211. });
  212. }
  213. Activities.insert({
  214. userId,
  215. activityType: 'removeChecklist',
  216. cardId: doc.cardId,
  217. boardId: Cards.findOne(doc.cardId).boardId,
  218. checklistId: doc._id,
  219. checklistName: doc.title,
  220. listId: card.listId,
  221. swimlaneId: card.swimlaneId,
  222. });
  223. });
  224. }
  225. if (Meteor.isServer) {
  226. /**
  227. * @operation get_all_checklists
  228. * @summary Get the list of checklists attached to a card
  229. *
  230. * @param {string} boardId the board ID
  231. * @param {string} cardId the card ID
  232. * @return_type [{_id: string,
  233. * title: string}]
  234. */
  235. JsonRoutes.add(
  236. 'GET',
  237. '/api/boards/:boardId/cards/:cardId/checklists',
  238. function(req, res) {
  239. Authentication.checkUserId(req.userId);
  240. const paramBoardId = req.params.boardId;
  241. const paramCardId = req.params.cardId;
  242. const checklists = Checklists.find({ cardId: paramCardId }).map(function(
  243. doc,
  244. ) {
  245. return {
  246. _id: doc._id,
  247. title: doc.title,
  248. };
  249. });
  250. if (checklists) {
  251. JsonRoutes.sendResult(res, {
  252. code: 200,
  253. data: checklists,
  254. });
  255. } else {
  256. JsonRoutes.sendResult(res, {
  257. code: 500,
  258. });
  259. }
  260. },
  261. );
  262. /**
  263. * @operation get_checklist
  264. * @summary Get a checklist
  265. *
  266. * @param {string} boardId the board ID
  267. * @param {string} cardId the card ID
  268. * @param {string} checklistId the ID of the checklist
  269. * @return_type {cardId: string,
  270. * title: string,
  271. * finishedAt: string,
  272. * createdAt: string,
  273. * sort: number,
  274. * items: [{_id: string,
  275. * title: string,
  276. * isFinished: boolean}]}
  277. */
  278. JsonRoutes.add(
  279. 'GET',
  280. '/api/boards/:boardId/cards/:cardId/checklists/:checklistId',
  281. function(req, res) {
  282. Authentication.checkUserId(req.userId);
  283. const paramBoardId = req.params.boardId;
  284. const paramChecklistId = req.params.checklistId;
  285. const paramCardId = req.params.cardId;
  286. const checklist = Checklists.findOne({
  287. _id: paramChecklistId,
  288. cardId: paramCardId,
  289. });
  290. if (checklist) {
  291. checklist.items = ChecklistItems.find({
  292. checklistId: checklist._id,
  293. }).map(function(doc) {
  294. return {
  295. _id: doc._id,
  296. title: doc.title,
  297. isFinished: doc.isFinished,
  298. };
  299. });
  300. JsonRoutes.sendResult(res, {
  301. code: 200,
  302. data: checklist,
  303. });
  304. } else {
  305. JsonRoutes.sendResult(res, {
  306. code: 500,
  307. });
  308. }
  309. },
  310. );
  311. /**
  312. * @operation new_checklist
  313. * @summary create a new checklist
  314. *
  315. * @param {string} boardId the board ID
  316. * @param {string} cardId the card ID
  317. * @param {string} title the title of the new checklist
  318. * @param {string} [items] the list of items on the new checklist
  319. * @return_type {_id: string}
  320. */
  321. JsonRoutes.add(
  322. 'POST',
  323. '/api/boards/:boardId/cards/:cardId/checklists',
  324. function(req, res) {
  325. Authentication.checkUserId(req.userId);
  326. // Check user is logged in
  327. //Authentication.checkLoggedIn(req.userId);
  328. const paramBoardId = req.params.boardId;
  329. // Check user has permission to add checklist to the card
  330. const board = Boards.findOne({
  331. _id: paramBoardId,
  332. });
  333. const addPermission = allowIsBoardMemberCommentOnly(req.userId, board);
  334. Authentication.checkAdminOrCondition(req.userId, addPermission);
  335. const paramCardId = req.params.cardId;
  336. const id = Checklists.insert({
  337. title: req.body.title,
  338. cardId: paramCardId,
  339. sort: 0,
  340. });
  341. if (id) {
  342. let items = req.body.items || [];
  343. if (_.isString(items)) {
  344. if (items === '') {
  345. items = [];
  346. } else {
  347. items = [items];
  348. }
  349. }
  350. items.forEach(function(item, idx) {
  351. ChecklistItems.insert({
  352. cardId: paramCardId,
  353. checklistId: id,
  354. title: item,
  355. sort: idx,
  356. });
  357. });
  358. JsonRoutes.sendResult(res, {
  359. code: 200,
  360. data: {
  361. _id: id,
  362. },
  363. });
  364. } else {
  365. JsonRoutes.sendResult(res, {
  366. code: 400,
  367. });
  368. }
  369. },
  370. );
  371. /**
  372. * @operation delete_checklist
  373. * @summary Delete a checklist
  374. *
  375. * @description The checklist will be removed, not put in the recycle bin.
  376. *
  377. * @param {string} boardId the board ID
  378. * @param {string} cardId the card ID
  379. * @param {string} checklistId the ID of the checklist to remove
  380. * @return_type {_id: string}
  381. */
  382. JsonRoutes.add(
  383. 'DELETE',
  384. '/api/boards/:boardId/cards/:cardId/checklists/:checklistId',
  385. function(req, res) {
  386. Authentication.checkUserId(req.userId);
  387. const paramBoardId = req.params.boardId;
  388. const paramChecklistId = req.params.checklistId;
  389. Checklists.remove({ _id: paramChecklistId });
  390. JsonRoutes.sendResult(res, {
  391. code: 200,
  392. data: {
  393. _id: paramChecklistId,
  394. },
  395. });
  396. },
  397. );
  398. }
  399. export default Checklists;