translation.js 2.8 KB

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