74 lines
2.1 KiB
JavaScript
74 lines
2.1 KiB
JavaScript
import { Schema } from 'mongoose'
|
|
import { regExpPattern } from 'regexp-pattern'
|
|
|
|
export let userSchema = new Schema({
|
|
_id: { type: Schema.Types.ObjectId, required: true, auto: true },
|
|
loginToken: { type: String, index: true, unique: true, sparse: true },
|
|
passwordHash: {
|
|
type: {
|
|
hash: String,
|
|
salt: String,
|
|
keyLength: Number,
|
|
hashMethod: String,
|
|
iterations: Number
|
|
}
|
|
},
|
|
email: { type: String, match: regExpPattern.email, required: true, index: true, unique: true },
|
|
newEmail: { type: String, match: regExpPattern.email },
|
|
imageId: { type: Schema.Types.ObjectId },
|
|
thumbnailImageId: { type: Schema.Types.ObjectId },
|
|
emailToken: {
|
|
type: {
|
|
value: { type: String, index: true, unique: true, sparse: true },
|
|
created: Date
|
|
}
|
|
},
|
|
passwordToken: {
|
|
type: {
|
|
value: { type: String, index: true, unique: true, sparse: true },
|
|
created: Date
|
|
}
|
|
},
|
|
firstName: { type: String, required: true },
|
|
lastName: { type: String, required: true },
|
|
role: { type: String, required: true, enum: {
|
|
values: [ 'administrator', 'normal'],
|
|
message: 'enum validator failed for path `{PATH}` with value `{VALUE}`'
|
|
}},
|
|
}, { timestamps: true, id: false })
|
|
|
|
userSchema.methods.toClient = function(authUser) {
|
|
if (authUser === undefined) {
|
|
authUser = this
|
|
}
|
|
|
|
let user = {
|
|
_id: this._id,
|
|
email: this.email,
|
|
emailValidated: (!!this.emailToken !== true),
|
|
imageId: this.imageId,
|
|
thumbnailImageId: this.thumbnailImageId,
|
|
firstName: this.firstName,
|
|
lastName: this.lastName,
|
|
role: this.role
|
|
}
|
|
|
|
if ((authUser.role === 'administrator' || authUser.role === 'executive') || authUser._id.equals(this._id)) {
|
|
user.zip = this.zip
|
|
user.state = this.state
|
|
user.city = this.city
|
|
user.address1 = this.address1
|
|
user.address2 = this.address2
|
|
user.homePhone = this.homePhone
|
|
user.cellPhone = this.cellPhone
|
|
user.ssn = this.ssn
|
|
user.dateOfBirth = this.dateOfBirth
|
|
user.dateOfHire = this.dateOfHire
|
|
user.numHouseholds = this.numHouseholds
|
|
user.t12 = this.t12
|
|
user.aum = this.aum
|
|
}
|
|
|
|
return user
|
|
}
|