rules.js 1.9 KB

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