translation.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. Translation = new Mongo.Collection('translation');
  2. /**
  3. * A Organization User in wekan
  4. */
  5. Translation.attachSchema(
  6. new SimpleSchema({
  7. language: {
  8. /**
  9. * the language
  10. */
  11. type: String,
  12. max: 5,
  13. },
  14. text: {
  15. /**
  16. * the text
  17. */
  18. type: String,
  19. },
  20. translationText: {
  21. /**
  22. * the translation text
  23. */
  24. type: String,
  25. optional: true,
  26. },
  27. createdAt: {
  28. /**
  29. * creation date of the translation custom string
  30. */
  31. type: Date,
  32. // eslint-disable-next-line consistent-return
  33. autoValue() {
  34. if (this.isInsert) {
  35. return new Date();
  36. } else if (this.isUpsert) {
  37. return { $setOnInsert: new Date() };
  38. } else {
  39. this.unset();
  40. }
  41. },
  42. },
  43. modifiedAt: {
  44. type: Date,
  45. denyUpdate: false,
  46. // eslint-disable-next-line consistent-return
  47. autoValue() {
  48. if (this.isInsert || this.isUpsert || this.isUpdate) {
  49. return new Date();
  50. } else {
  51. this.unset();
  52. }
  53. },
  54. },
  55. }),
  56. );
  57. if (Meteor.isServer) {
  58. Translation.allow({
  59. insert(userId, doc) {
  60. const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser();
  61. if (user?.isAdmin)
  62. return true;
  63. if (!user) {
  64. return false;
  65. }
  66. return doc._id === userId;
  67. },
  68. update(userId, doc) {
  69. const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser();
  70. if (user?.isAdmin)
  71. return true;
  72. if (!user) {
  73. return false;
  74. }
  75. return doc._id === userId;
  76. },
  77. remove(userId, doc) {
  78. const user = ReactiveCache.getUser(userId) || ReactiveCache.getCurrentUser();
  79. if (user?.isAdmin)
  80. return true;
  81. if (!user) {
  82. return false;
  83. }
  84. return doc._id === userId;
  85. },
  86. fetch: [],
  87. });
  88. Meteor.methods({
  89. setCreateTranslation(
  90. language,
  91. text,
  92. translationText,
  93. ) {
  94. check(language, String);
  95. check(text, String);
  96. check(translationText, String);
  97. const nTexts = ReactiveCache.getTranslations({ language, text }).length;
  98. if (nTexts > 0) {
  99. throw new Meteor.Error('text-already-taken');
  100. } else {
  101. Translation.insert({
  102. language,
  103. text,
  104. translationText,
  105. });
  106. }
  107. },
  108. setTranslationText(translation, translationText) {
  109. check(translation, Object);
  110. check(translationText, String);
  111. Translation.update(translation, {
  112. $set: { translationText: translationText },
  113. });
  114. },
  115. });
  116. }
  117. if (Meteor.isServer) {
  118. // Index for Organization User.
  119. Meteor.startup(() => {
  120. Translation._collection.createIndex({ modifiedAt: -1 });
  121. });
  122. }
  123. export default Translation;