checklists.js 10 KB

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