checklists.js 7.3 KB

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