checklists.js 8.9 KB

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