checklists.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. Checklists = new Mongo.Collection('checklists');
  2. Checklists.attachSchema(new SimpleSchema({
  3. cardId: {
  4. type: String,
  5. },
  6. title: {
  7. type: String,
  8. },
  9. items: {
  10. type: [Object],
  11. defaultValue: [],
  12. },
  13. 'items.$._id': {
  14. type: String,
  15. },
  16. 'items.$.title': {
  17. type: String,
  18. },
  19. 'items.$.sort': {
  20. type: Number,
  21. decimal: true,
  22. },
  23. 'items.$.isFinished': {
  24. type: Boolean,
  25. defaultValue: false,
  26. },
  27. finishedAt: {
  28. type: Date,
  29. optional: true,
  30. },
  31. createdAt: {
  32. type: Date,
  33. denyUpdate: false,
  34. autoValue() { // eslint-disable-line consistent-return
  35. if (this.isInsert) {
  36. return new Date();
  37. } else {
  38. this.unset();
  39. }
  40. },
  41. },
  42. sort: {
  43. type: Number,
  44. decimal: true,
  45. },
  46. newItemIndex: {
  47. type: Number,
  48. decimal: true,
  49. defaultValue: 0,
  50. },
  51. }));
  52. const self = Checklists;
  53. Checklists.helpers({
  54. itemCount() {
  55. return this.items.length;
  56. },
  57. getItems() {
  58. return this.items.sort(function (itemA, itemB) {
  59. if (itemA.sort < itemB.sort) {
  60. return -1;
  61. }
  62. if (itemA.sort > itemB.sort) {
  63. return 1;
  64. }
  65. return 0;
  66. });
  67. },
  68. finishedCount() {
  69. return this.items.filter((item) => {
  70. return item.isFinished;
  71. }).length;
  72. },
  73. isFinished() {
  74. return 0 !== this.itemCount() && this.itemCount() === this.finishedCount();
  75. },
  76. getItem(_id) {
  77. return _.findWhere(this.items, { _id });
  78. },
  79. itemIndex(itemId) {
  80. const items = self.findOne({_id : this._id}).items;
  81. return _.pluck(items, '_id').indexOf(itemId);
  82. },
  83. });
  84. Checklists.allow({
  85. insert(userId, doc) {
  86. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  87. },
  88. update(userId, doc) {
  89. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  90. },
  91. remove(userId, doc) {
  92. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  93. },
  94. fetch: ['userId', 'cardId'],
  95. });
  96. Checklists.before.insert((userId, doc) => {
  97. doc.createdAt = new Date();
  98. if (!doc.userId) {
  99. doc.userId = userId;
  100. }
  101. });
  102. Checklists.mutations({
  103. //for checklist itself
  104. setTitle(title) {
  105. return { $set: { title } };
  106. },
  107. //for items in checklist
  108. addItem(title) {
  109. const itemCount = this.itemCount();
  110. const _id = `${this._id}${this.newItemIndex}`;
  111. return {
  112. $addToSet: { items: { _id, title, isFinished: false, sort: itemCount } },
  113. $set: { newItemIndex: this.newItemIndex + 1},
  114. };
  115. },
  116. removeItem(itemId) {
  117. return { $pull: { items: { _id: itemId } } };
  118. },
  119. editItem(itemId, title) {
  120. if (this.getItem(itemId)) {
  121. const itemIndex = this.itemIndex(itemId);
  122. return {
  123. $set: {
  124. [`items.${itemIndex}.title`]: title,
  125. },
  126. };
  127. }
  128. return {};
  129. },
  130. finishItem(itemId) {
  131. if (this.getItem(itemId)) {
  132. const itemIndex = this.itemIndex(itemId);
  133. return {
  134. $set: {
  135. [`items.${itemIndex}.isFinished`]: true,
  136. },
  137. };
  138. }
  139. return {};
  140. },
  141. resumeItem(itemId) {
  142. if (this.getItem(itemId)) {
  143. const itemIndex = this.itemIndex(itemId);
  144. return {
  145. $set: {
  146. [`items.${itemIndex}.isFinished`]: false,
  147. },
  148. };
  149. }
  150. return {};
  151. },
  152. toggleItem(itemId) {
  153. const item = this.getItem(itemId);
  154. if (item) {
  155. const itemIndex = this.itemIndex(itemId);
  156. return {
  157. $set: {
  158. [`items.${itemIndex}.isFinished`]: !item.isFinished,
  159. },
  160. };
  161. }
  162. return {};
  163. },
  164. sortItems(itemIDs) {
  165. const validItems = [];
  166. for (const itemID of itemIDs) {
  167. if (this.getItem(itemID)) {
  168. validItems.push(this.itemIndex(itemID));
  169. }
  170. }
  171. const modifiedValues = {};
  172. for (let i = 0; i < validItems.length; i++) {
  173. modifiedValues[`items.${validItems[i]}.sort`] = i;
  174. }
  175. return {
  176. $set: modifiedValues,
  177. };
  178. },
  179. });
  180. if (Meteor.isServer) {
  181. Meteor.startup(() => {
  182. Checklists._collection._ensureIndex({ cardId: 1, createdAt: 1 });
  183. });
  184. Checklists.after.insert((userId, doc) => {
  185. Activities.insert({
  186. userId,
  187. activityType: 'addChecklist',
  188. cardId: doc.cardId,
  189. boardId: Cards.findOne(doc.cardId).boardId,
  190. checklistId: doc._id,
  191. });
  192. });
  193. //TODO: so there will be no activity for adding item into checklist, maybe will be implemented in the future.
  194. // The future is now
  195. Checklists.after.update((userId, doc, fieldNames, modifier) => {
  196. if (fieldNames.includes('items')) {
  197. if (modifier.$addToSet) {
  198. Activities.insert({
  199. userId,
  200. activityType: 'addChecklistItem',
  201. cardId: doc.cardId,
  202. boardId: Cards.findOne(doc.cardId).boardId,
  203. checklistId: doc._id,
  204. checklistItemId: modifier.$addToSet.items._id,
  205. });
  206. } else if (modifier.$pull) {
  207. const activity = Activities.findOne({
  208. checklistItemId: modifier.$pull.items._id,
  209. });
  210. if (activity) {
  211. Activities.remove(activity._id);
  212. }
  213. }
  214. }
  215. });
  216. Checklists.before.remove((userId, doc) => {
  217. const activities = Activities.find({ checklistId: doc._id });
  218. if (activities) {
  219. activities.forEach((activity) => {
  220. Activities.remove(activity._id);
  221. });
  222. }
  223. });
  224. }
  225. //CARD COMMENT REST API
  226. if (Meteor.isServer) {
  227. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res, next) {
  228. Authentication.checkUserId( req.userId);
  229. const paramCardId = req.params.cardId;
  230. JsonRoutes.sendResult(res, {
  231. code: 200,
  232. data: Checklists.find({ cardId: paramCardId }).map(function (doc) {
  233. return {
  234. _id: doc._id,
  235. title: doc.title,
  236. };
  237. }),
  238. });
  239. });
  240. JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res, next) {
  241. Authentication.checkUserId( req.userId);
  242. const paramChecklistId = req.params.checklistId;
  243. const paramCardId = req.params.cardId;
  244. JsonRoutes.sendResult(res, {
  245. code: 200,
  246. data: Checklists.findOne({ _id: paramChecklistId, cardId: paramCardId }),
  247. });
  248. });
  249. JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res, next) {
  250. Authentication.checkUserId( req.userId);
  251. const paramCardId = req.params.cardId;
  252. const checklistToSend = {};
  253. checklistToSend.cardId = paramCardId;
  254. checklistToSend.title = req.body.title;
  255. checklistToSend.items = [];
  256. const id = Checklists.insert(checklistToSend);
  257. const checklist = Checklists.findOne({_id: id});
  258. req.body.items.forEach(function (item) {
  259. checklist.addItem(item);
  260. }, this);
  261. JsonRoutes.sendResult(res, {
  262. code: 200,
  263. data: {
  264. _id: id,
  265. },
  266. });
  267. });
  268. JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res, next) {
  269. Authentication.checkUserId( req.userId);
  270. const paramCommentId = req.params.commentId;
  271. const paramCardId = req.params.cardId;
  272. Checklists.remove({ _id: paramCommentId, cardId: paramCardId });
  273. JsonRoutes.sendResult(res, {
  274. code: 200,
  275. data: {
  276. _id: paramCardId,
  277. },
  278. });
  279. });
  280. }