Sindbad~EG File Manager
let mongoose,db,successResponse,failedResponse,catchAsync,AppError;_ed2.x([["default",()=>_ed2.o]]);_ed2.w("mongoose",[["default",["mongoose"],function(v){mongoose=v}]]);_ed2.w("../../model/index",[["default",["db"],function(v){db=v}]]);_ed2.w("../../utils/responseFormat",[["successResponse",["successResponse"],function(v){successResponse=v}],["failedResponse",["failedResponse"],function(v){failedResponse=v}]]);_ed2.w("../../utils/catchAsync",[["default",["catchAsync"],function(v){catchAsync=v}]]);_ed2.w("../../utils/apiError",[["default",["AppError"],function(v){AppError=v}]]);
const filteredPrimaryData = async (contactId) => {
const filteredData = await db.BasicContactInfo.aggregate([
{
$match: {
contact_id: mongoose.Types.ObjectId(contactId),
},
},
{
$project: {
phone_numbers: {
$cond: {
if: { $isArray: "$phone_numbers" },
then: {
$filter: {
input: "$phone_numbers",
as: "phone",
cond: { $eq: ["$$phone.is_primary", true] },
},
},
else: [],
},
},
emails: {
$cond: {
if: { $isArray: "$emails" },
then: {
$filter: {
input: "$emails",
as: "email",
cond: { $eq: ["$$email.is_primary", true] },
},
},
else: [],
},
},
},
},
{
$project: {
phone_numbers: {
$cond: {
if: { $gt: [{ $size: "$phone_numbers" }, 0] },
then: "$phone_numbers",
else: [
{
phone_no: "",
type: "",
is_primary: false,
_id: "",
},
],
},
},
emails: {
$cond: {
if: { $gt: [{ $size: "$emails" }, 0] },
then: "$emails",
else: [
{
email_address: "",
type: "",
is_primary: false,
_id: "",
},
],
},
},
},
},
]);
return filteredData[0];
};
const index = catchAsync(async (req, res, next) => {
let allData = await db.Contact.find()
.sort({ createdOn: -1 })
.populate("created_by", ["_id", "first_name"])
.exec();
const result = allData.filter((item) => item.created_by !== null);
return successResponse(res, result);
});
const allContacts = catchAsync(async (req, res, next) => {
let allData = await db.Contact.find()
.sort({ createdOn: -1 })
.populate("created_by", ["_id", "first_name"])
.exec();
const result = allData.filter((item) => item.created_by !== null);
const contacts = result.map((user) => {
return {
value: user.first_name + " " + user.last_name,
_id: user._id,
};
});
return successResponse(res, contacts);
});
const add = catchAsync(async (req, res, next) => {
req.body.created_by = req.user._id;
const contactData = req.body;
const contact = new db.Contact(contactData);
await contact.save();
const personalInfo = new db.PersonalInfo({ contact_id: contact._id });
await personalInfo.save();
const linkedAccount = new db.LinkedAccount({ contact_id: contact._id });
await linkedAccount.save();
const socialMedia = new db.SocialMedia({ contact_id: contact._id });
await socialMedia.save();
const accountInfo = new db.AccountInfo({
contact_id: contact._id,
created_by: req.user._id,
agent_id: req.user._id,
});
await accountInfo.save();
const basicInfo = new db.BasicContactInfo({ contact_id: contact._id });
await basicInfo.save();
return res.status(200).json({
message: "Contact created successfully",
contact,
personalInfo,
linkedAccount,
socialMedia,
accountInfo,
basicInfo,
});
});
const edit = catchAsync(async (req, res, next) => {
let result = await db.Contact.findByIdAndUpdate(
{ _id: req.params.id },
{ $set: req.body },
{ new: true }
);
if (!result) {
return next(new AppError("No data found.", 404));
}
return successResponse(res, result, "Contact updated successfully");
});
const view = catchAsync(async (req, res, next) => {
let Contacts = await db.Contact.aggregate([
{
$match: {
_id: mongoose.Types.ObjectId(req.params.id),
},
},
]);
let contact = await db.Contact.findByIdAndUpdate({ _id: req.params.id });
if (!contact) {
return next(new AppError("No data found.", 404));
}
return successResponse(res, Contacts);
});
const profile = catchAsync(async (req, res, next) => {
const _id = req.body.contact_id;
let userDoc = await db.Contact.findById(_id, { password: 0 });
if (!userDoc) {
return next(new AppError("No data found.", 404));
}
const user = userDoc.toObject();
const { __v, ...profile } = user;
const [
basicContactInfo,
filteredData,
additionalContact,
address,
personalInfo,
accountInformation,
linkedAccountsResponse,
expireDates,
employee,
socialMedia,
] = await Promise.all([
db.BasicContactInfo.findOne({ contact_id: _id }),
filteredPrimaryData(_id),
db.AdditionalContact.find({ contact_id: _id }),
db.Address.find({ contact_id: _id }),
db.PersonalInfo.findOne({ contact_id: _id }),
db.AccountInfo.findOne({ contact_id: _id })
.populate("agent_id", ["_id", "first_name", "last_name"])
.exec(),
db.LinkedAccount.findOne({ contact_id: _id }, ["linked_accounts"])
.populate({
path: "linked_accounts.relation_ref",
select: "first_name last_name",
model: "Contact",
})
.exec(),
db.ExpireDate.find({ contact_id: _id }),
db.Employee.find({ contact_id: _id }),
db.SocialMedia.findOne({ contact_id: _id }),
]);
let { linked_accounts: linkedAccount = [] } = linkedAccountsResponse;
profile.filteredData = filteredData;
const formattedData = {
profile,
basicContactInfo,
address,
additionalContact,
accountInformation,
personalInfo,
linkedAccount,
expireDates,
employee,
socialMedia,
};
return successResponse(res, formattedData);
});
const deleteData = catchAsync(async (req, res, next) => {
const contactId = req.params.id;
// Delete the contact itself
const deletedContact = await db.Contact.findById({
_id: contactId,
});
if (!deletedContact) {
return next(new AppError("No data found.", 404));
}
await deletedContact.remove();
return successResponse(
res,
deletedContact,
"Contact and related data deleted successfully."
);
});
_ed2.d({ index, profile, add, edit, view, deleteData, allContacts });
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists