checklists.js 11 KB

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