const config = require('../config'); const mongoose = require('mongoose'); const userSchema = new mongoose.Schema({ username: String, password: String, email: String, role: String }); userSchema.method('getPublicObject', function() { return { username: this.username, permissionLevel: config.roleMap[this.role], role: this.role, _id: this._id } }); userSchema.method('getFullObject', function() { return { username: this.username, password: this.password, email: this.email, permissionLevel: config.roleMap[this.role], role: this.role, _id: this._id } }); const User = mongoose.model('User', userSchema); // NOTE(hippoz): These are all actually material design colors, taken from https://material-ui.com/customization/color/#playground const colors = [ '#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3', '#03a9f4', '#00bcd4', '#009688', '#4caf50', '#8bc34a', '#cddc39', '#ffeb3b', '#ffc107', '#ff9800', '#ff5722' ]; User.generateColorFromUsername = function(username) { let sum = 0; for (let i in username) { sum += username.charCodeAt(i); } const colorIndex = sum % colors.length; return colors[colorIndex]; }; User.findByUsername = async function(username) { return await User.findOne({ username }).exec(); }; User.getPulicFields = function() { return 'username role _id'; }; module.exports = User;