@everytravel/shared
Version:
A comprehensive shared package for Everytravel containing Mongoose models and CRUD operations for hotel booking, user management, and transaction handling. Updated with improved model syntax and enhanced error handling.
67 lines (61 loc) • 2.92 kB
JavaScript
import mongoose from "mongoose";
const { Schema } = mongoose;
import { isEmail, trimIfString } from '../validation/index.js';
/**
* @module User.model
* @description Mongoose schema for the User collection.
*
* Fields:
* - firstName: User's first name (String)
* - lastName: User's last name (String)
* - email: User's email address (String)
* - phoneNumber: Object containing countryCode and number (String)
* - password: User's hashed password (String)
* - dateOfBirth: User's date of birth (Date)
* - gender: User's gender (String)
* - verifiedEmail: Whether the user's email is verified (Boolean)
* - country: User's country (String)
* - address: User's address (String)
* - profilePicUrl: URL to user's profile picture (String)
* - googleId: Google OAuth user ID (String)
* - facebookId: Facebook OAuth user ID (String)
* - appleId: Apple OAuth user ID (String)
* - wallet: Reference to user's Wallet document (ObjectId)
* - savedBookings: Array of Booking references (ObjectId[])
* - savedRides: Array of Ride references (ObjectId[])
* - savedApartments: Array of Suite references (ObjectId[])
* - cards: Array of Card references (ObjectId[])
* - transactions: Array of Transaction references (ObjectId[])
* - offers: Array of Discount references (ObjectId[])
*/
const UserSchema = new Schema({
firstName: { type: String, set: trimIfString },
lastName: { type: String, set: trimIfString },
email: { type: String, set: (v) => (typeof v === 'string' ? v.trim().toLowerCase() : v), validate: { validator: (v) => !v || isEmail(v), message: 'Invalid email' }, index: true },
phoneNumber: {
countryCode: { type: String, set: trimIfString },
number: { type: String, set: trimIfString },
},
password: { type: String },
dateOfBirth: Date,
gender: { type: String, set: trimIfString },
verifiedEmail: Boolean,
country: { type: String, set: trimIfString },
address: { type: String, set: trimIfString },
profilePicUrl: { type: String, set: trimIfString },
// OAuth provider IDs
googleId: { type: String, index: true },
facebookId: { type: String, index: true },
appleId: { type: String, index: true },
wallet: { type: Schema.Types.ObjectId, ref: 'Wallet' },
savedBookings: [{ type: Schema.Types.ObjectId, ref: 'Booking' }],
savedRides: [{ type: Schema.Types.ObjectId, ref: 'Ride' }],
savedApartments: [{ type: Schema.Types.ObjectId, ref: 'Suite' }],
cards: [{ type: Schema.Types.ObjectId, ref: 'Card' }],
transactions: [{ type: Schema.Types.ObjectId, ref: 'Transaction' }],
offers: [{ type: Schema.Types.ObjectId, ref: 'Discount' }],
});
// Indexes to optimize lookups
// Note: field-level indexes already defined on email, googleId, facebookId, appleId
const User = mongoose.model('User', UserSchema);
export default User;