Sindbad~EG File Manager

Current Path : /home/infinitibizsol/.trash/model/
Upload File :
Current File : /home/infinitibizsol/.trash/model/basicontactInfo.js

const mongoose = require("mongoose");

const phoneNumberSchema = new mongoose.Schema({
  phone_no: { type: String, required: true },
  type: {
    type: String,
    // enum: ["Home", "Work", "Mobile", "Other"],
    required: true,
  },
  is_primary: { type: Boolean, default: false },
});

const emailSchema = new mongoose.Schema({
  email_address: { type: String, required: true },
  type: {
    type: String,
    // enum: ["Home", "Work", "Mobile", "Other"],
    required: true,
  },
  is_primary: { type: Boolean, default: false },
});

const BasicContactInfo = new mongoose.Schema({
  user_id: {
    type: mongoose.Schema.ObjectId,
    ref: "User",
  },
  contact_id: {
    type: mongoose.Schema.ObjectId,
    ref: "Contact",
  },
  phone_numbers: {
    type: [phoneNumberSchema],
    validate: [
      {
        validator: function (v) {
          return v.length <= 4;
        },
        message: (props) => `An agent can have up to 4 phone numbers!`,
      },
      {
        validator: function (v) {
          const primaryCount = v.filter((phone) => phone.is_primary).length;
          return primaryCount <= 1;
        },
        message: (props) => `Only one phone number can be marked as primary!`,
      },
    ],
  },
  emails: {
    type: [emailSchema],
    validate: [
      {
        validator: function (v) {
          return v.length <= 4;
        },
        message: (props) => `An agent can have up to 2 emails!`,
      },
      {
        validator: function (v) {
          const primaryCount = v.filter((email) => email.is_primary).length;
          return primaryCount <= 1;
        },
        message: (props) => `Only one email can be marked as primary!`,
      },
    ],
  },

  createdOn: { type: Date, default: Date.now },
  modifiedOn: { type: Date, default: Date.now },
});

export default mongoose.model("BasicContactInfo", BasicContactInfo);

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists