/
home
/
infinitibizsol
/
.trash
/
controllers.8
/
common
/
File Upload :
llllll
Current File: /home/infinitibizsol/.trash/controllers.8/common/basicContactInfo.js
import db from "../../model/index"; import AppError from "../../utils/apiError"; import { successResponse } from "../../utils/responseFormat"; import catchAsync from "../../utils/catchAsync"; const findOne = async (userId) => { let data = await db.BasicContactInfo.findOne( { user_id: userId }, { __v: false, modifiedOn: false, createdOn: false, } ); if (!data) { return next(new AppError("No data found.", 404)); } return data; }; const index = catchAsync(async (req, res, next) => { const query = req.query; query.user_id = query.contact_id ? undefined : req.user._id; let allData = await db.BasicContactInfo.find(query, { __v: false, modifiedOn: false, createdOn: false, }) .populate("user_id", ["_id", "first_name", "last_name"]) .populate("contact_id", ["_id", "first_name", "last_name"]) .exec(); return successResponse(res, allData); }); const add = catchAsync(async (req, res, next) => { const { phone_numbers, emails } = req.body; const userId = req.user._id; let basicContactInfo = await findOne(userId); // Fetch user's contact info if (!basicContactInfo) { // Create new contact info if none exists req.body.user_id = userId; basicContactInfo = new db.BasicContactInfo(req.body); } else { // Update existing contact info if (phone_numbers && Array.isArray(phone_numbers)) { basicContactInfo.phone_numbers.push(...phone_numbers); } if (emails && Array.isArray(emails)) { basicContactInfo.emails.push(...emails); } } await basicContactInfo.save(); return successResponse( res, basicContactInfo, "Basic Contact Info saved successfully" ); }); const edit = catchAsync(async (req, res, next) => { const { phone_numbers, emails } = req.body; const userId = req.user._id; let phoneNumbersUpdate, emailsUpdate; if (phone_numbers) { phoneNumbersUpdate = await db.BasicContactInfo.findOneAndUpdate( { user_id: userId, "phone_numbers._id": phone_numbers._id, }, { $set: { "phone_numbers.$.phone_no": phone_numbers.phone_no, "phone_numbers.$.is_primary": phone_numbers.is_primary, "phone_numbers.$.type": phone_numbers.type, }, }, { new: true } ); } if (emails) { emailsUpdate = await db.BasicContactInfo.findOneAndUpdate( { user_id: userId, "emails._id": emails._id, }, { $set: { "emails.$.email_address": emails.email_address, "emails.$.is_primary": emails.is_primary, "emails.$.type": emails.type, }, }, { new: true } ); } if (!phone_numbers && !emails) { return next(new AppError("No data provided to update.", 404)); } if ((phone_numbers && !phoneNumbersUpdate) || (emails && !emailsUpdate)) { return next(new AppError("No data found.", 404)); } return successResponse( res, { phoneNumbersUpdate, emailsUpdate, }, "Data updated successfully" ); }); const view = catchAsync(async (req, res, next) => { const query = req.query; let contact = await db.BasicContactInfo.find({ _id: req.params.id }); if (!contact) { return next(new AppError("No data found.", 404)); } return successResponse(res, contact); }); const deleteData = catchAsync(async (req, res, next) => { const { phone_numbers, emails } = req.body; if (!phone_numbers && !emails) { return next(new AppError("No data provided to update.", 404)); } let phoneNumbersResult, emailsResult; if (phone_numbers) { phoneNumbersResult = await db.BasicContactInfo.findOneAndUpdate( { "phone_numbers._id": phone_numbers.phone_id, }, { $pull: { phone_numbers: { _id: phone_numbers.phone_id, phone_no: phone_numbers.phone_no, }, }, }, { new: true } ); } if (emails) { emailsResult = await db.BasicContactInfo.findOneAndUpdate( { "emails._id": emails.email_id }, { $pull: { emails: { _id: emails.email_id, email_address: emails.email_address, }, }, }, { new: true } ); } if ((phone_numbers && !phoneNumbersResult) || (emails && !emailsResult)) { return next(new AppError("No data found.", 404)); } return successResponse( res, { phoneNumbersResult, emailsResult }, "BasicContactInfo and related data deleted successfully." ); }); const saveBasicContact = catchAsync(async (req, res, next) => { let { contact_id, phone_numbers, emails, contact_preference, user_id } = req.normalizedData; let contact; contact = await db.BasicContactInfo.findOne({ user_id, contact_id, }); if (contact) { // Update the existing record contact.phone_numbers = phone_numbers; contact.emails = emails; contact.contact_preference = contact_preference; } else { user_id = contact_id ? undefined : user_id; // Create a new record contact = new db.BasicContactInfo({ user_id, contact_id, phone_numbers, emails, contact_preference, }); } await contact.save(); return successResponse( res, contact, "Contact information saved successfully." ); }); export default { index, add, edit, view, deleteData, saveBasicContact };
Copyright ©2k19 -
Hexid
|
Tex7ure