rules.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 {
  29. this.unset();
  30. }
  31. },
  32. },
  33. modifiedAt: {
  34. type: Date,
  35. denyUpdate: false,
  36. // eslint-disable-next-line consistent-return
  37. autoValue() {
  38. if (this.isInsert || this.isUpsert || this.isUpdate) {
  39. return new Date();
  40. } else {
  41. this.unset();
  42. }
  43. },
  44. },
  45. }),
  46. );
  47. Rules.mutations({
  48. rename(description) {
  49. return { $set: { description } };
  50. },
  51. });
  52. Rules.helpers({
  53. getAction() {
  54. return Actions.findOne({ _id: this.actionId });
  55. },
  56. getTrigger() {
  57. return Triggers.findOne({ _id: this.triggerId });
  58. },
  59. });
  60. Rules.allow({
  61. insert(userId, doc) {
  62. return allowIsBoardAdmin(userId, Boards.findOne(doc.boardId));
  63. },
  64. update(userId, doc) {
  65. return allowIsBoardAdmin(userId, Boards.findOne(doc.boardId));
  66. },
  67. remove(userId, doc) {
  68. return allowIsBoardAdmin(userId, Boards.findOne(doc.boardId));
  69. },
  70. });
  71. if (Meteor.isServer) {
  72. Meteor.startup(() => {
  73. Rules._collection._ensureIndex({ modifiedAt: -1 });
  74. });
  75. }
  76. export default Rules;