2
0

invitationCodes.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. InvitationCodes = new Mongo.Collection('invitation_codes');
  2. InvitationCodes.attachSchema(
  3. new SimpleSchema({
  4. code: {
  5. type: String,
  6. },
  7. email: {
  8. type: String,
  9. unique: true,
  10. regEx: SimpleSchema.RegEx.Email,
  11. },
  12. createdAt: {
  13. type: Date,
  14. denyUpdate: false,
  15. optional: true,
  16. // eslint-disable-next-line consistent-return
  17. autoValue() {
  18. if (this.isInsert) {
  19. return new Date();
  20. } else {
  21. this.unset();
  22. }
  23. },
  24. },
  25. modifiedAt: {
  26. type: Date,
  27. denyUpdate: false,
  28. // eslint-disable-next-line consistent-return
  29. autoValue() {
  30. if (this.isInsert || this.isUpsert || this.isUpdate) {
  31. return new Date();
  32. } else {
  33. this.unset();
  34. }
  35. },
  36. },
  37. // always be the admin if only one admin
  38. authorId: {
  39. type: String,
  40. },
  41. boardsToBeInvited: {
  42. type: [String],
  43. optional: true,
  44. },
  45. valid: {
  46. type: Boolean,
  47. defaultValue: true,
  48. },
  49. })
  50. );
  51. InvitationCodes.helpers({
  52. author() {
  53. return Users.findOne(this.authorId);
  54. },
  55. });
  56. InvitationCodes.before.update((userId, doc, fieldNames, modifier, options) => {
  57. modifier.$set = modifier.$set || {};
  58. modifier.$set.modifiedAt = Date.now();
  59. });
  60. // InvitationCodes.before.insert((userId, doc) => {
  61. // doc.createdAt = new Date();
  62. // doc.authorId = userId;
  63. // });
  64. if (Meteor.isServer) {
  65. Meteor.startup(() => {
  66. InvitationCodes._collection._ensureIndex({ modifiedAt: -1 });
  67. });
  68. Boards.deny({
  69. fetch: ['members'],
  70. });
  71. }
  72. export default InvitationCodes;