checklists.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. });
  131. });
  132. Checklists.before.remove((userId, doc) => {
  133. const activities = Activities.find({ checklistId: doc._id });
  134. if (activities) {
  135. activities.forEach((activity) => {
  136. Activities.remove(activity._id);
  137. });
  138. }
  139. Activities.insert({
  140. userId,
  141. activityType: 'removeChecklist',
  142. cardId: doc.cardId,
  143. boardId: Cards.findOne(doc.cardId).boardId,
  144. checklistId: doc._id,
  145. checklistName:doc.title,
  146. });
  147. });
  148. }
  149. if (Meteor.isServer) {
  150. /**
  151. * @operation get_all_checklists
  152. * @summary Get the list of checklists attached to a card
  153. *
  154. * @param {string} boardId the board ID
  155. * @param {string} cardId the card ID
  156. * @return_type [{_id: string,
  157. * title: string}]
  158. */
  159. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) {
  160. Authentication.checkUserId( req.userId);
  161. const paramCardId = req.params.cardId;
  162. const checklists = Checklists.find({ cardId: paramCardId }).map(function (doc) {
  163. return {
  164. _id: doc._id,
  165. title: doc.title,
  166. };
  167. });
  168. if (checklists) {
  169. JsonRoutes.sendResult(res, {
  170. code: 200,
  171. data: checklists,
  172. });
  173. } else {
  174. JsonRoutes.sendResult(res, {
  175. code: 500,
  176. });
  177. }
  178. });
  179. /**
  180. * @operation get_checklist
  181. * @summary Get a checklist
  182. *
  183. * @param {string} boardId the board ID
  184. * @param {string} cardId the card ID
  185. * @param {string} checklistId the ID of the checklist
  186. * @return_type {cardId: string,
  187. * title: string,
  188. * finishedAt: string,
  189. * createdAt: string,
  190. * sort: number,
  191. * items: [{_id: string,
  192. * title: string,
  193. * isFinished: boolean}]}
  194. */
  195. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
  196. Authentication.checkUserId( req.userId);
  197. const paramChecklistId = req.params.checklistId;
  198. const paramCardId = req.params.cardId;
  199. const checklist = Checklists.findOne({ _id: paramChecklistId, cardId: paramCardId });
  200. if (checklist) {
  201. checklist.items = ChecklistItems.find({checklistId: checklist._id}).map(function (doc) {
  202. return {
  203. _id: doc._id,
  204. title: doc.title,
  205. isFinished: doc.isFinished,
  206. };
  207. });
  208. JsonRoutes.sendResult(res, {
  209. code: 200,
  210. data: checklist,
  211. });
  212. } else {
  213. JsonRoutes.sendResult(res, {
  214. code: 500,
  215. });
  216. }
  217. });
  218. /**
  219. * @operation new_checklist
  220. * @summary create a new checklist
  221. *
  222. * @param {string} boardId the board ID
  223. * @param {string} cardId the card ID
  224. * @param {string} title the title of the new checklist
  225. * @return_type {_id: string}
  226. */
  227. JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) {
  228. Authentication.checkUserId( req.userId);
  229. const paramCardId = req.params.cardId;
  230. const id = Checklists.insert({
  231. title: req.body.title,
  232. cardId: paramCardId,
  233. sort: 0,
  234. });
  235. if (id) {
  236. req.body.items.forEach(function (item, idx) {
  237. ChecklistItems.insert({
  238. cardId: paramCardId,
  239. checklistId: id,
  240. title: item.title,
  241. sort: idx,
  242. });
  243. });
  244. JsonRoutes.sendResult(res, {
  245. code: 200,
  246. data: {
  247. _id: id,
  248. },
  249. });
  250. } else {
  251. JsonRoutes.sendResult(res, {
  252. code: 400,
  253. });
  254. }
  255. });
  256. /**
  257. * @operation delete_checklist
  258. * @summary Delete a checklist
  259. *
  260. * @description The checklist will be removed, not put in the recycle bin.
  261. *
  262. * @param {string} boardId the board ID
  263. * @param {string} cardId the card ID
  264. * @param {string} checklistId the ID of the checklist to remove
  265. * @return_type {_id: string}
  266. */
  267. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
  268. Authentication.checkUserId( req.userId);
  269. const paramChecklistId = req.params.checklistId;
  270. Checklists.remove({ _id: paramChecklistId });
  271. JsonRoutes.sendResult(res, {
  272. code: 200,
  273. data: {
  274. _id: paramChecklistId,
  275. },
  276. });
  277. });
  278. }