checklists.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. import { ReactiveCache, ReactiveMiniMongoIndex } from '/imports/reactiveCache';
  2. Checklists = new Mongo.Collection('checklists');
  3. /**
  4. * A Checklist
  5. */
  6. Checklists.attachSchema(
  7. new SimpleSchema({
  8. cardId: {
  9. /**
  10. * The ID of the card the checklist is in
  11. */
  12. type: String,
  13. },
  14. title: {
  15. /**
  16. * the title of the checklist
  17. */
  18. type: String,
  19. defaultValue: 'Checklist',
  20. },
  21. finishedAt: {
  22. /**
  23. * When was the checklist finished
  24. */
  25. type: Date,
  26. optional: true,
  27. },
  28. createdAt: {
  29. /**
  30. * Creation date of the checklist
  31. */
  32. type: Date,
  33. denyUpdate: false,
  34. // eslint-disable-next-line consistent-return
  35. autoValue() {
  36. if (this.isInsert) {
  37. return new Date();
  38. } else if (this.isUpsert) {
  39. return { $setOnInsert: new Date() };
  40. } else {
  41. this.unset();
  42. }
  43. },
  44. },
  45. modifiedAt: {
  46. type: Date,
  47. denyUpdate: false,
  48. // eslint-disable-next-line consistent-return
  49. autoValue() {
  50. if (this.isInsert || this.isUpsert || this.isUpdate) {
  51. return new Date();
  52. } else {
  53. this.unset();
  54. }
  55. },
  56. },
  57. sort: {
  58. /**
  59. * sorting value of the checklist
  60. */
  61. type: Number,
  62. decimal: true,
  63. },
  64. hideCheckedChecklistItems: {
  65. /**
  66. * hide the checked checklist-items?
  67. */
  68. type: Boolean,
  69. optional: true,
  70. },
  71. hideAllChecklistItems: {
  72. /**
  73. * hide all checklist items ?
  74. */
  75. type: Boolean,
  76. optional: true,
  77. },
  78. }),
  79. );
  80. Checklists.helpers({
  81. copy(newCardId) {
  82. let copyObj = Object.assign({}, this);
  83. delete copyObj._id;
  84. copyObj.cardId = newCardId;
  85. const newChecklistId = Checklists.insert(copyObj);
  86. ReactiveCache.getChecklistItems({ checklistId: this._id }).forEach(function(
  87. item,
  88. ) {
  89. item._id = null;
  90. item.checklistId = newChecklistId;
  91. item.cardId = newCardId;
  92. ChecklistItems.insert(item);
  93. });
  94. },
  95. itemCount() {
  96. const ret = this.items().length;
  97. return ret;
  98. },
  99. items() {
  100. const ret = ReactiveMiniMongoIndex.getChecklistItemsWithChecklistId(this._id, {}, { sort: ['sort'] });
  101. return ret;
  102. },
  103. firstItem() {
  104. const ret = _.first(this.items());
  105. return ret;
  106. },
  107. lastItem() {
  108. const ret = _.last(this.items());
  109. return ret;
  110. },
  111. finishedCount() {
  112. const ret = this.items().filter(_item => _item.isFinished).length;
  113. return ret;
  114. },
  115. /** returns the finished percent of the checklist */
  116. finishedPercent() {
  117. const count = this.itemCount();
  118. const checklistItemsFinished = this.finishedCount();
  119. let ret = 0;
  120. if (count > 0) {
  121. ret = Math.round(checklistItemsFinished / count * 100);
  122. }
  123. return ret;
  124. },
  125. isFinished() {
  126. let ret = this.hideAllChecklistItems;
  127. if (!ret) {
  128. ret = 0 !== this.itemCount() && this.itemCount() === this.finishedCount();
  129. }
  130. return ret;
  131. },
  132. showChecklist(hideFinishedChecklistIfItemsAreHidden) {
  133. let ret = true;
  134. if (this.isFinished() && hideFinishedChecklistIfItemsAreHidden === true && (this.hideCheckedChecklistItems === true || this.hideAllChecklistItems)) {
  135. ret = false;
  136. }
  137. return ret;
  138. },
  139. checkAllItems() {
  140. const checkItems = ReactiveCache.getChecklistItems({ checklistId: this._id });
  141. checkItems.forEach(function(item) {
  142. item.check();
  143. });
  144. },
  145. uncheckAllItems() {
  146. const checkItems = ReactiveCache.getChecklistItems({ checklistId: this._id });
  147. checkItems.forEach(function(item) {
  148. item.uncheck();
  149. });
  150. },
  151. itemIndex(itemId) {
  152. const items = ReactiveCache.getChecklist({ _id: this._id }).items;
  153. return _.pluck(items, '_id').indexOf(itemId);
  154. },
  155. });
  156. Checklists.allow({
  157. insert(userId, doc) {
  158. return allowIsBoardMemberByCard(userId, ReactiveCache.getCard(doc.cardId));
  159. },
  160. update(userId, doc) {
  161. return allowIsBoardMemberByCard(userId, ReactiveCache.getCard(doc.cardId));
  162. },
  163. remove(userId, doc) {
  164. return allowIsBoardMemberByCard(userId, ReactiveCache.getCard(doc.cardId));
  165. },
  166. fetch: ['userId', 'cardId'],
  167. });
  168. Checklists.before.insert((userId, doc) => {
  169. doc.createdAt = new Date();
  170. if (!doc.userId) {
  171. doc.userId = userId;
  172. }
  173. });
  174. Checklists.mutations({
  175. setTitle(title) {
  176. return { $set: { title } };
  177. },
  178. /** move the checklist to another card
  179. * @param newCardId move the checklist to this cardId
  180. */
  181. move(newCardId) {
  182. // update every activity
  183. ReactiveCache.getActivities(
  184. {checklistId: this._id}
  185. ).forEach(activity => {
  186. Activities.update(activity._id, {
  187. $set: {
  188. cardId: newCardId,
  189. },
  190. });
  191. });
  192. // update every checklist-item
  193. ReactiveCache.getChecklistItems(
  194. {checklistId: this._id}
  195. ).forEach(checklistItem => {
  196. ChecklistItems.update(checklistItem._id, {
  197. $set: {
  198. cardId: newCardId,
  199. },
  200. });
  201. });
  202. // update the checklist itself
  203. return {
  204. $set: {
  205. cardId: newCardId,
  206. },
  207. };
  208. },
  209. toggleHideCheckedChecklistItems() {
  210. return {
  211. $set: {
  212. hideCheckedChecklistItems: !this.hideCheckedChecklistItems,
  213. }
  214. };
  215. },
  216. toggleHideAllChecklistItems() {
  217. return {
  218. $set: {
  219. hideAllChecklistItems: !this.hideAllChecklistItems,
  220. }
  221. };
  222. },
  223. });
  224. if (Meteor.isServer) {
  225. Meteor.startup(() => {
  226. Checklists._collection.createIndex({ modifiedAt: -1 });
  227. Checklists._collection.createIndex({ cardId: 1, createdAt: 1 });
  228. });
  229. Checklists.after.insert((userId, doc) => {
  230. const card = ReactiveCache.getCard(doc.cardId);
  231. Activities.insert({
  232. userId,
  233. activityType: 'addChecklist',
  234. cardId: doc.cardId,
  235. boardId: card.boardId,
  236. checklistId: doc._id,
  237. checklistName: doc.title,
  238. listId: card.listId,
  239. swimlaneId: card.swimlaneId,
  240. });
  241. });
  242. Checklists.before.remove((userId, doc) => {
  243. const activities = ReactiveCache.getActivities({ checklistId: doc._id });
  244. const card = ReactiveCache.getCard(doc.cardId);
  245. if (activities) {
  246. activities.forEach(activity => {
  247. Activities.remove(activity._id);
  248. });
  249. }
  250. Activities.insert({
  251. userId,
  252. activityType: 'removeChecklist',
  253. cardId: doc.cardId,
  254. boardId: ReactiveCache.getCard(doc.cardId).boardId,
  255. checklistId: doc._id,
  256. checklistName: doc.title,
  257. listId: card.listId,
  258. swimlaneId: card.swimlaneId,
  259. });
  260. });
  261. }
  262. if (Meteor.isServer) {
  263. /**
  264. * @operation get_all_checklists
  265. * @summary Get the list of checklists attached to a card
  266. *
  267. * @param {string} boardId the board ID
  268. * @param {string} cardId the card ID
  269. * @return_type [{_id: string,
  270. * title: string}]
  271. */
  272. JsonRoutes.add(
  273. 'GET',
  274. '/api/boards/:boardId/cards/:cardId/checklists',
  275. function(req, res) {
  276. const paramBoardId = req.params.boardId;
  277. const paramCardId = req.params.cardId;
  278. Authentication.checkBoardAccess(req.userId, paramBoardId);
  279. const checklists = ReactiveCache.getChecklists({ cardId: paramCardId }).map(function(
  280. doc,
  281. ) {
  282. return {
  283. _id: doc._id,
  284. title: doc.title,
  285. };
  286. });
  287. if (checklists) {
  288. JsonRoutes.sendResult(res, {
  289. code: 200,
  290. data: checklists,
  291. });
  292. } else {
  293. JsonRoutes.sendResult(res, {
  294. code: 500,
  295. });
  296. }
  297. },
  298. );
  299. /**
  300. * @operation get_checklist
  301. * @summary Get a checklist
  302. *
  303. * @param {string} boardId the board ID
  304. * @param {string} cardId the card ID
  305. * @param {string} checklistId the ID of the checklist
  306. * @return_type {cardId: string,
  307. * title: string,
  308. * finishedAt: string,
  309. * createdAt: string,
  310. * sort: number,
  311. * items: [{_id: string,
  312. * title: string,
  313. * isFinished: boolean}]}
  314. */
  315. JsonRoutes.add(
  316. 'GET',
  317. '/api/boards/:boardId/cards/:cardId/checklists/:checklistId',
  318. function(req, res) {
  319. const paramBoardId = req.params.boardId;
  320. const paramChecklistId = req.params.checklistId;
  321. const paramCardId = req.params.cardId;
  322. Authentication.checkBoardAccess(req.userId, paramBoardId);
  323. const checklist = ReactiveCache.getChecklist({
  324. _id: paramChecklistId,
  325. cardId: paramCardId,
  326. });
  327. if (checklist) {
  328. checklist.items = ReactiveCache.getChecklistItems({
  329. checklistId: checklist._id,
  330. }).map(function(doc) {
  331. return {
  332. _id: doc._id,
  333. title: doc.title,
  334. isFinished: doc.isFinished,
  335. };
  336. });
  337. JsonRoutes.sendResult(res, {
  338. code: 200,
  339. data: checklist,
  340. });
  341. } else {
  342. JsonRoutes.sendResult(res, {
  343. code: 500,
  344. });
  345. }
  346. },
  347. );
  348. /**
  349. * @operation new_checklist
  350. * @summary create a new checklist
  351. *
  352. * @param {string} boardId the board ID
  353. * @param {string} cardId the card ID
  354. * @param {string} title the title of the new checklist
  355. * @param {string} [items] the list of items on the new checklist
  356. * @return_type {_id: string}
  357. */
  358. JsonRoutes.add(
  359. 'POST',
  360. '/api/boards/:boardId/cards/:cardId/checklists',
  361. function(req, res) {
  362. // Check user is logged in
  363. //Authentication.checkLoggedIn(req.userId);
  364. const paramBoardId = req.params.boardId;
  365. Authentication.checkBoardAccess(req.userId, paramBoardId);
  366. // Check user has permission to add checklist to the card
  367. const board = ReactiveCache.getBoard(paramBoardId);
  368. const addPermission = allowIsBoardMemberCommentOnly(req.userId, board);
  369. Authentication.checkAdminOrCondition(req.userId, addPermission);
  370. const paramCardId = req.params.cardId;
  371. const id = Checklists.insert({
  372. title: req.body.title,
  373. cardId: paramCardId,
  374. sort: 0,
  375. });
  376. if (id) {
  377. let items = req.body.items || [];
  378. if (_.isString(items)) {
  379. if (items === '') {
  380. items = [];
  381. } else {
  382. items = [items];
  383. }
  384. }
  385. items.forEach(function(item, idx) {
  386. ChecklistItems.insert({
  387. cardId: paramCardId,
  388. checklistId: id,
  389. title: item,
  390. sort: idx,
  391. });
  392. });
  393. JsonRoutes.sendResult(res, {
  394. code: 200,
  395. data: {
  396. _id: id,
  397. },
  398. });
  399. } else {
  400. JsonRoutes.sendResult(res, {
  401. code: 400,
  402. });
  403. }
  404. },
  405. );
  406. /**
  407. * @operation delete_checklist
  408. * @summary Delete a checklist
  409. *
  410. * @description The checklist will be removed, not put in the recycle bin.
  411. *
  412. * @param {string} boardId the board ID
  413. * @param {string} cardId the card ID
  414. * @param {string} checklistId the ID of the checklist to remove
  415. * @return_type {_id: string}
  416. */
  417. JsonRoutes.add(
  418. 'DELETE',
  419. '/api/boards/:boardId/cards/:cardId/checklists/:checklistId',
  420. function(req, res) {
  421. const paramBoardId = req.params.boardId;
  422. const paramChecklistId = req.params.checklistId;
  423. Authentication.checkBoardAccess(req.userId, paramBoardId);
  424. Checklists.remove({ _id: paramChecklistId });
  425. JsonRoutes.sendResult(res, {
  426. code: 200,
  427. data: {
  428. _id: paramChecklistId,
  429. },
  430. });
  431. },
  432. );
  433. }
  434. export default Checklists;