/
home
/
infinitibizsol
/
.trash
/
File Upload :
llllll
Current File: /home/infinitibizsol/.trash/test.js
const mongoose = require("mongoose"); const { Schema } = mongoose; // Define phoneNumberSchema const phoneNumberSchema = new Schema({ phone_no: { type: String, required: true }, type: { type: String, required: true }, is_primary: { type: Boolean, default: false }, }); // Define emailSchema const emailSchema = new Schema({ email_address: { type: String, required: true }, type: { type: String, required: true }, is_primary: { type: Boolean, default: false }, }); // Define BasicContactInfoSchema const BasicContactInfoSchema = new 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 <= 2; }, 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 }, }); const BasicContactInfo = mongoose.model( "BasicContactInfo", BasicContactInfoSchema ); // Connect to MongoDB and perform CRUD operations mongoose .connect("mongodb://localhost:27017/mydatabase", { useNewUrlParser: true, useUnifiedTopology: true, }) .then(async () => { console.log("Connected to MongoDB"); // Use CRUD operations here const userData = { user_id: new mongoose.Types.ObjectId(), // Replace with actual user ID phone_numbers: [ { phone_no: "123-456-7890", type: "Mobile", is_primary: true }, { phone_no: "987-654-3210", type: "Home", is_primary: false }, ], emails: [ { email_address: "john.doe@example.com", type: "Personal", is_primary: true, }, { email_address: "johndoe@workplace.com", type: "Work", is_primary: false, }, ], }; try { // Create a new contact info const newContactInfo = await createContactInfo(userData); console.log("Created Contact Info:", newContactInfo); // Get all contact infos const allContactInfos = await getAllContactInfos(); console.log("All Contact Infos:", allContactInfos); // Get a specific contact info by ID const fetchedContactInfo = await getContactInfoById(newContactInfo._id); console.log("Fetched Contact Info by ID:", fetchedContactInfo); // Update the contact info const updatedData = { ...userData, phone_numbers: [ { phone_no: "111-222-3333", type: "Work", is_primary: true }, ], }; const updatedContactInfo = await updateContactInfo( newContactInfo._id, updatedData ); console.log("Updated Contact Info:", updatedContactInfo); // Delete the contact info const deletedContactInfo = await deleteContactInfo( updatedContactInfo._id ); console.log("Deleted Contact Info:", deletedContactInfo); } catch (error) { console.error("Error:", error); } mongoose.disconnect(); }) .catch((error) => console.error("Error:", error)); // Function implementations // Create a new contact info async function createContactInfo(data) { const contactInfo = new BasicContactInfo(data); return contactInfo.save(); } // Get all contact infos async function getAllContactInfos() { return BasicContactInfo.find( {}, { __v: false, createdOn: false, modifiedOn: false } ) .populate("user_id", ["_id", "first_name", "last_name"]) .populate("contact_id", ["_id", "first_name", "last_name"]); } // Get a specific contact info by ID async function getContactInfoById(id) { return BasicContactInfo.findById(id) .populate("user_id", ["_id", "first_name", "last_name"]) .populate("contact_id", ["_id", "first_name", "last_name"]); } // Update a specific contact info by ID async function updateContactInfo(id, data) { return BasicContactInfo.findByIdAndUpdate( id, { $set: data }, { new: true, runValidators: true } ); } // Delete a specific contact info by ID async function deleteContactInfo(id) { return BasicContactInfo.findByIdAndDelete(id); }
Copyright ©2k19 -
Hexid
|
Tex7ure