checklists.js 11 KB

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