Source

models/User.js

/**
 * User Model
 * 
 * Mongoose schema for user accounts in the Social Media Dashboard.
 * Handles user authentication, profile information, and social media account associations.
 * 
 * @author Social Media Dashboard Team
 * @version 1.0.0
 */

const mongoose = require('mongoose');

/**
 * User Schema Definition
 * 
 * Defines the structure for user documents in MongoDB. Includes authentication fields,
 * profile information, and social media account references.
 * 
 * @type {mongoose.Schema}
 */
const userSchema = new mongoose.Schema({
  /** User's email address - used for authentication and communication */
  email: {
    type: String,
    required: true,
    unique: true,
    lowercase: true
  },
  /** User's display name shown in the dashboard */
  display_name: {
    type: String,
    required: true
  },
  /** URL to user's profile avatar image */
  avatar_url: {
    type: String,
    default: null
  },
  /** Google OAuth ID for Google sign-in integration */
  google_id: {
    type: String,
    unique: true,
    sparse: true
  },
  /** Timestamp of user's last login for activity tracking */
  last_login: {
    type: Date,
    default: Date.now
  }
}, {
  timestamps: true
});

// Add static methods
userSchema.statics.findByEmail = async function(email) {
  return await this.findOne({ email });
};

userSchema.statics.create = async function(userData) {
  const user = new this(userData);
  return await user.save();
};

userSchema.statics.updateProfile = async function(userId, updateData) {
  return await this.findByIdAndUpdate(userId, updateData, { new: true });
};

userSchema.statics.delete = async function(userId) {
  return await this.findByIdAndDelete(userId);
};

const User = mongoose.model('User', userSchema);

module.exports = User;