Untitled

mail@pastecode.io avatar
unknown
plain_text
22 days ago
3.9 kB
0
Indexable
Never
import { Schema, model } from 'mongoose';

import { countries } from '../data/enum.data.js';
import type { IUser } from '../types/user.type.js';
const userSchema = new Schema({
  firstName: { type: String, required: false },
  lastName: { type: String, required: false },
  email: { type: String, required: false, unique: true },
  //accessToken: { type: String, select: false },
  //refreshToken: { type: String, select: false },
  dateOfBirth: { type: Date, required: false },
  phoneNumber: {
    number: {
      type: String,
      required: [false, 'Phone number is required'],
      validate: {
        validator: function (phoneNumber: string) {
          return /^[0-9]{6,14}$/.test(phoneNumber);
        },
        message: 'Phone number does not match the criteria',
      },
    },
    local: {
      dialCode: {
        type: String,
        required: [false, 'Dial code is required'],
        match: [/^\+\d+$/, 'Please fill a valid dial code'],
        validate: {
          validator: function (dialCode: string) {
            return countries.some((country) => `+${country.dial_code}` === dialCode);
          },
          message: 'Please fill a valid dial code',
        },
      },
      countryCode: {
        type: String,
        required: [false, 'Country code is required'],
        validate: {
          validator: function (countryCode: string) {
            return countries.some((country) => country.code === countryCode);
          },
          message: 'Please provide a valid ISO country code',
        },
      },
    },
  },
  address: { type: String, required: false },
  verificationToken: String,
  isBanned: { type: Boolean, default: false },
  banDuration: { type: Number },

  banHistory: [
    {
      dateOfBan: { type: Date, required: true },
      bannedBy: { type: Schema.Types.ObjectId, ref: 'User' },
      banReason: { type: String, required: true },
      banDuration: { type: Number, required: true },
    },
  ],
  //isVerified: { type: Boolean, default: false },
  status: { type: String, enum: ['offline', 'online', 'occupied'], default: 'offline' },
  userType: {
    type: String,
    enum: ['admin', 'agent', 'partner', 'driver', 'manager', 'customer'],
    required: true,
    default: 'customer',
  },
  lastLoggedIn: { type: Date, default: null },
  createdAt: { type: Date, default: Date.now },
  // driver
  driver_license: { type: String },
  driver_license_issueDate: { type: Date },
  carName: { type: String },
  matriculation: { type: String },
  //customer
  budget: { type: Number },
  // 'partner'
  PartnerCompanyName: { type: String },
  // 'Manager'
  ManagerCompanyName: { type: String },
  // 'agent'
  agencyName: { type: String },
  permissions: {
    type: Object,
    category: {
      type: [{ type: String }],
    },
  },
});
userSchema.pre('save', async function (this: IUser, next) {
  const userType = this.userType;

  if (userType === 'driver') {
    // driver
    this.driver_license = this.driver_license ?? 'default_license';
    this.driver_license_issueDate = this.driver_license_issueDate ?? new Date();
    this.carName = this.carName ?? 'default_car';
    this.matriculation = this.matriculation ?? 'default_matriculation';
  } else if (userType === 'customer') {
    //customer
    this.budget = this.budget ?? 0;
  } else if (userType === 'manager') {
    // manager and partner
    this.ManagerCompanyName = this.ManagerCompanyName ?? 'ManagerCompanyName';
  } else if (userType === 'partner') {
    // manager and partner
    this.PartnerCompanyName = this.PartnerCompanyName ?? 'default_company_name';
  } else if (userType === 'agent') {
    //agent
    this.agencyName = this.agencyName ?? 'default_agency_name';
  }

  next();
});

const UserModel = model<IUser>('User', userSchema);

export default UserModel;
Leave a Comment