2
0

checklistItems.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ChecklistItems = new Mongo.Collection('checklistItems');
  2. ChecklistItems.attachSchema(new SimpleSchema({
  3. title: {
  4. type: String,
  5. },
  6. sort: {
  7. type: Number,
  8. decimal: true,
  9. },
  10. isFinished: {
  11. type: Boolean,
  12. defaultValue: false,
  13. },
  14. checklistId: {
  15. type: String,
  16. },
  17. cardId: {
  18. type: String,
  19. },
  20. }));
  21. ChecklistItems.allow({
  22. insert(userId, doc) {
  23. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  24. },
  25. update(userId, doc) {
  26. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  27. },
  28. remove(userId, doc) {
  29. return allowIsBoardMemberByCard(userId, Cards.findOne(doc.cardId));
  30. },
  31. fetch: ['userId', 'cardId'],
  32. });
  33. ChecklistItems.before.insert((userId, doc) => {
  34. if (!doc.userId) {
  35. doc.userId = userId;
  36. }
  37. });
  38. // Mutations
  39. ChecklistItems.mutations({
  40. setTitle(title) {
  41. return { $set: { title } };
  42. },
  43. toggleItem() {
  44. return { $set: { isFinished: !this.isFinished } };
  45. },
  46. move(checklistId, sortIndex) {
  47. const cardId = Checklists.findOne(checklistId).cardId;
  48. const mutatedFields = {
  49. cardId,
  50. checklistId,
  51. sort: sortIndex,
  52. };
  53. return {$set: mutatedFields};
  54. },
  55. });
  56. // Activities helper
  57. function itemCreation(userId, doc) {
  58. const card = Cards.findOne(doc.cardId);
  59. const boardId = card.boardId;
  60. Activities.insert({
  61. userId,
  62. activityType: 'addChecklistItem',
  63. cardId: doc.cardId,
  64. boardId,
  65. checklistId: doc.checklistId,
  66. checklistItemId: doc._id,
  67. });
  68. }
  69. function itemRemover(userId, doc) {
  70. Activities.remove({
  71. checklistItemId: doc._id,
  72. });
  73. }
  74. // Activities
  75. if (Meteor.isServer) {
  76. Meteor.startup(() => {
  77. ChecklistItems._collection._ensureIndex({ checklistId: 1 });
  78. });
  79. ChecklistItems.after.insert((userId, doc) => {
  80. itemCreation(userId, doc);
  81. });
  82. ChecklistItems.after.remove((userId, doc) => {
  83. itemRemover(userId, doc);
  84. });
  85. }