rules.js 1.6 KB

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