checklists.js 8.2 KB

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