checklists.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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.update((userId, doc, fieldNames, modifier, options) => {
  155. modifier.$set = modifier.$set || {};
  156. modifier.$set.modifiedAt = Date.now();
  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. * @return_type {_id: string}
  270. */
  271. JsonRoutes.add(
  272. 'POST',
  273. '/api/boards/:boardId/cards/:cardId/checklists',
  274. function(req, res) {
  275. Authentication.checkUserId(req.userId);
  276. const paramCardId = req.params.cardId;
  277. const id = Checklists.insert({
  278. title: req.body.title,
  279. cardId: paramCardId,
  280. sort: 0,
  281. });
  282. if (id) {
  283. req.body.items.forEach(function(item, idx) {
  284. ChecklistItems.insert({
  285. cardId: paramCardId,
  286. checklistId: id,
  287. title: item.title,
  288. sort: idx,
  289. });
  290. });
  291. JsonRoutes.sendResult(res, {
  292. code: 200,
  293. data: {
  294. _id: id,
  295. },
  296. });
  297. } else {
  298. JsonRoutes.sendResult(res, {
  299. code: 400,
  300. });
  301. }
  302. }
  303. );
  304. /**
  305. * @operation delete_checklist
  306. * @summary Delete a checklist
  307. *
  308. * @description The checklist will be removed, not put in the recycle bin.
  309. *
  310. * @param {string} boardId the board ID
  311. * @param {string} cardId the card ID
  312. * @param {string} checklistId the ID of the checklist to remove
  313. * @return_type {_id: string}
  314. */
  315. JsonRoutes.add(
  316. 'DELETE',
  317. '/api/boards/:boardId/cards/:cardId/checklists/:checklistId',
  318. function(req, res) {
  319. Authentication.checkUserId(req.userId);
  320. const paramChecklistId = req.params.checklistId;
  321. Checklists.remove({ _id: paramChecklistId });
  322. JsonRoutes.sendResult(res, {
  323. code: 200,
  324. data: {
  325. _id: paramChecklistId,
  326. },
  327. });
  328. }
  329. );
  330. }
  331. export default Checklists;