usersessiondata.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. SessionData = new Mongo.Collection('sessiondata');
  2. /**
  3. * A UserSessionData in Wekan. Organization in Trello.
  4. */
  5. SessionData.attachSchema(
  6. new SimpleSchema({
  7. _id: {
  8. /**
  9. * the organization id
  10. */
  11. type: Number,
  12. optional: true,
  13. // eslint-disable-next-line consistent-return
  14. autoValue() {
  15. if (this.isInsert && !this.isSet) {
  16. return incrementCounter('counters', 'orgId', 1);
  17. }
  18. },
  19. },
  20. userId: {
  21. /**
  22. * userId of the user
  23. */
  24. type: String,
  25. optional: false,
  26. },
  27. sessionId: {
  28. /**
  29. * unique session ID
  30. */
  31. type: String,
  32. optional: false,
  33. },
  34. totalHits: {
  35. /**
  36. * total number of hits in the last report query
  37. */
  38. type: Number,
  39. optional: true,
  40. },
  41. lastHit: {
  42. /**
  43. * the last hit returned from a report query
  44. */
  45. type: Number,
  46. optional: true,
  47. },
  48. cards: {
  49. type: [String],
  50. optional: true,
  51. },
  52. errorMessages: {
  53. type: [String],
  54. optional: true,
  55. },
  56. createdAt: {
  57. /**
  58. * creation date of the team
  59. */
  60. type: Date,
  61. // eslint-disable-next-line consistent-return
  62. autoValue() {
  63. if (this.isInsert) {
  64. return new Date();
  65. } else if (this.isUpsert) {
  66. return { $setOnInsert: new Date() };
  67. } else {
  68. this.unset();
  69. }
  70. },
  71. },
  72. modifiedAt: {
  73. type: Date,
  74. denyUpdate: false,
  75. // eslint-disable-next-line consistent-return
  76. autoValue() {
  77. if (this.isInsert || this.isUpsert || this.isUpdate) {
  78. return new Date();
  79. } else {
  80. this.unset();
  81. }
  82. },
  83. },
  84. }),
  85. );
  86. if (!Meteor.isServer) {
  87. SessionData.getSessionId = () => {
  88. let sessionId = Session.get('sessionId');
  89. if (!sessionId) {
  90. sessionId = `${String(Meteor.userId())}-${String(Math.random())}`;
  91. Session.set('sessionId', sessionId);
  92. }
  93. return sessionId;
  94. };
  95. }
  96. export default SessionData;