checklists.js 9.0 KB

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