checklists.js 10 KB

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