checklists.js 11 KB

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