checklists.js 10 KB

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