rules.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. });
  62. Rules.allow({
  63. insert(userId, doc) {
  64. return allowIsBoardAdmin(userId, Boards.findOne(doc.boardId));
  65. },
  66. update(userId, doc) {
  67. return allowIsBoardAdmin(userId, Boards.findOne(doc.boardId));
  68. },
  69. remove(userId, doc) {
  70. return allowIsBoardAdmin(userId, Boards.findOne(doc.boardId));
  71. },
  72. });
  73. if (Meteor.isServer) {
  74. Meteor.startup(() => {
  75. Rules._collection._ensureIndex({ modifiedAt: -1 });
  76. });
  77. }
  78. export default Rules;