rules.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { Meteor } from 'meteor/meteor';
  3. Rules = new Mongo.Collection('rules');
  4. Rules.attachSchema(
  5. new SimpleSchema({
  6. title: {
  7. type: String,
  8. optional: false,
  9. },
  10. triggerId: {
  11. type: String,
  12. optional: false,
  13. },
  14. actionId: {
  15. type: String,
  16. optional: false,
  17. },
  18. boardId: {
  19. type: String,
  20. optional: false,
  21. },
  22. createdAt: {
  23. type: Date,
  24. optional: true,
  25. // eslint-disable-next-line consistent-return
  26. autoValue() {
  27. if (this.isInsert) {
  28. return new Date();
  29. } else if (this.isUpsert) {
  30. return { $setOnInsert: new Date() };
  31. } else {
  32. this.unset();
  33. }
  34. },
  35. },
  36. modifiedAt: {
  37. type: Date,
  38. denyUpdate: false,
  39. // eslint-disable-next-line consistent-return
  40. autoValue() {
  41. if (this.isInsert || this.isUpsert || this.isUpdate) {
  42. return new Date();
  43. } else {
  44. this.unset();
  45. }
  46. },
  47. },
  48. }),
  49. );
  50. Rules.mutations({
  51. rename(description) {
  52. return { $set: { description } };
  53. },
  54. });
  55. Rules.helpers({
  56. getAction() {
  57. return ReactiveCache.getAction(this.actionId);
  58. },
  59. getTrigger() {
  60. return ReactiveCache.getTrigger(this.triggerId);
  61. },
  62. board() {
  63. return ReactiveCache.getBoard(this.boardId);
  64. },
  65. trigger() {
  66. return ReactiveCache.getTrigger(this.triggerId);
  67. },
  68. action() {
  69. return ReactiveCache.getAction(this.actionId);
  70. },
  71. });
  72. Rules.allow({
  73. insert(userId, doc) {
  74. return allowIsBoardAdmin(userId, ReactiveCache.getBoard(doc.boardId));
  75. },
  76. update(userId, doc) {
  77. return allowIsBoardAdmin(userId, ReactiveCache.getBoard(doc.boardId));
  78. },
  79. remove(userId, doc) {
  80. return allowIsBoardAdmin(userId, ReactiveCache.getBoard(doc.boardId));
  81. },
  82. });
  83. if (Meteor.isServer) {
  84. Meteor.startup(() => {
  85. Rules._collection.createIndex({ modifiedAt: -1 });
  86. });
  87. }
  88. export default Rules;