tableVisibilityModeSettings.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. TableVisibilityModeSettings = new Mongo.Collection('tableVisibilityModeSettings');
  3. TableVisibilityModeSettings.attachSchema(
  4. new SimpleSchema({
  5. _id: {
  6. type: String,
  7. },
  8. booleanValue: {
  9. type: Boolean,
  10. optional: true,
  11. },
  12. sort: {
  13. type: Number,
  14. decimal: true,
  15. },
  16. createdAt: {
  17. type: Date,
  18. optional: true,
  19. // eslint-disable-next-line consistent-return
  20. autoValue() {
  21. if (this.isInsert) {
  22. return new Date();
  23. } else if (this.isUpsert) {
  24. return { $setOnInsert: new Date() };
  25. } else {
  26. this.unset();
  27. }
  28. },
  29. },
  30. modifiedAt: {
  31. type: Date,
  32. denyUpdate: false,
  33. // eslint-disable-next-line consistent-return
  34. autoValue() {
  35. if (this.isInsert || this.isUpsert || this.isUpdate) {
  36. return new Date();
  37. } else {
  38. this.unset();
  39. }
  40. },
  41. },
  42. }),
  43. );
  44. TableVisibilityModeSettings.allow({
  45. update(userId) {
  46. const user = ReactiveCache.getUser(userId);
  47. return user && user.isAdmin;
  48. },
  49. });
  50. if (Meteor.isServer) {
  51. Meteor.startup(() => {
  52. TableVisibilityModeSettings._collection.createIndex({ modifiedAt: -1 });
  53. TableVisibilityModeSettings.upsert(
  54. { _id: 'tableVisibilityMode-allowPrivateOnly' },
  55. {
  56. $setOnInsert: {
  57. booleanValue: false,
  58. sort: 0,
  59. },
  60. },
  61. );
  62. });
  63. }
  64. TableVisibilityModeSettings.helpers({
  65. allowPrivateOnly() {
  66. return TableVisibilityModeSettings.findOne('tableVisibilityMode-allowPrivateOnly').booleanValue;
  67. },
  68. });
  69. export default TableVisibilityModeSettings;