File size: 3,385 Bytes
b63786a
 
 
4c888b7
b63786a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c888b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b63786a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c888b7
 
 
 
b63786a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const { normalizeEmail, normalizePhone, validateEmail, validatePhone } = require('../utils/validators');
const { DEFAULT_STARTING_TOKENS, normalizeReferralCode } = require('../utils/tokenUtils');

const userSchema = new mongoose.Schema({
  username: {
    type: String,
    required: true,
    unique: true,
    trim: true,
    minlength: 3,
  },
  email: {
    type: String,
    unique: true,
    sparse: true,
    lowercase: true,
    trim: true,
    validate: {
      validator: (value) => !value || validateEmail(value),
      message: 'Please enter a valid email',
    },
  },
  phone: {
    type: String,
    unique: true,
    sparse: true,
    trim: true,
    validate: {
      validator: (value) => !value || validatePhone(value),
      message: 'Please enter a valid mobile number',
    },
  },
  password: {
    type: String,
    required: true,
    minlength: 8,
  },
  avatarUrl: {
    type: String,
    default: '',
  },
  followers: {
    type: [{
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
    }],
    default: [],
  },
  following: {
    type: [{
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
    }],
    default: [],
  },
  tokenBalance: {
    type: Number,
    default: DEFAULT_STARTING_TOKENS,
    min: 0,
  },
  referralCode: {
    type: String,
    unique: true,
    sparse: true,
    trim: true,
    uppercase: true,
  },
  referredByUserId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    default: null,
  },
  rewardedLikePostIds: {
    type: [{
      type: mongoose.Schema.Types.ObjectId,
      ref: 'GalleryPost',
    }],
    default: [],
  },
  rewardedFollowUserIds: {
    type: [{
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
    }],
    default: [],
  },
  tokenHistory: {
    type: [{
      amount: {
        type: Number,
        required: true,
      },
      type: {
        type: String,
        enum: ['credit', 'debit'],
        required: true,
      },
      reason: {
        type: String,
        required: true,
        trim: true,
      },
      note: {
        type: String,
        default: '',
        trim: true,
      },
      createdAt: {
        type: Date,
        default: Date.now,
      },
    }],
    default: [],
  },
  createdAt: {
    type: Date,
    default: Date.now,
  },
});

userSchema.pre('validate', function (next) {
  if (typeof this.email === 'string') {
    const nextEmail = normalizeEmail(this.email);
    this.email = nextEmail || undefined;
  }

  if (typeof this.phone === 'string') {
    const nextPhone = normalizePhone(this.phone);
    this.phone = nextPhone || undefined;
  }

  if (typeof this.referralCode === 'string') {
    this.referralCode = normalizeReferralCode(this.referralCode) || undefined;
  }

  next();
});

// Hash password before saving
userSchema.pre('save', async function (next) {
  if (!this.isModified('password')) return next();
  try {
    const salt = await bcrypt.genSalt(10);
    this.password = await bcrypt.hash(this.password, salt);
    next();
  } catch (err) {
    next(err);
  }
});

// Method to compare passwords
userSchema.methods.comparePassword = async function (passwordAttempt) {
  return await bcrypt.compare(passwordAttempt, this.password);
};

module.exports = mongoose.model('User', userSchema);