Sindbad~EG File Manager
let db,bcrypt,successResponse,mongoose,decodeHtmlEntities,catchAsync,AppError;_56c.x([["default",()=>_56c.o]]);_56c.w("../../model/index",[["default",["db"],function(v){db=v}]]);_56c.w("bcrypt",[["default",["bcrypt"],function(v){bcrypt=v}]]);_56c.w("../../utils/responseFormat",[["successResponse",["successResponse"],function(v){successResponse=v}]]);_56c.w("mongoose",[["default",["mongoose"],function(v){mongoose=v}]]);_56c.w("../../utils/decodeHTMLEntities",[["default",["decodeHtmlEntities"],function(v){decodeHtmlEntities=v}]]);_56c.w("../../utils/catchAsync",[["default",["catchAsync"],function(v){catchAsync=v}]]);_56c.w("../../utils/apiError",[["default",["AppError"],function(v){AppError=v}]]);
const filteredPrimaryData = async (userId) => {
const filteredData = await db.BasicContactInfo.aggregate([
{
$match: {
user_id: mongoose.Types.ObjectId(userId),
},
},
{
$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 viewAll = catchAsync(async (req, res, next) => {
let result = await db.User.find().sort({ createdOn: -1 });
return successResponse(res, result, "All Users");
});
const allUsersByRole = catchAsync(async (req, res, next) => {
const { role = "agent" } = req.body;
const { _id } = req.user; // Logged-in user's ID
let query = role !== "admin" ? { _id: { $ne: _id } } : {};
let _users = await db.User.find(query).sort({ createdOn: -1 });
if (role !== "admin") {
_users = _users.filter((user) => user.role === "agent");
}
const users = _users.map((user) => {
return {
value: user.first_name + " " + user.last_name,
_id: user._id,
};
});
return successResponse(res, users, "All Users retrieved successfully");
});
const view = catchAsync(async (req, res, next) => {
let user = await db.User.findById({ _id: req.params.id });
if (!user) {
return next(new AppError("No data found.", 404));
}
return successResponse(res, user);
});
const edit = catchAsync(async (req, res, next) => {
if (req.body.password) {
req.body.password = await bcrypt.hash(req.body.password, 10);
}
let result = await db.User.findByIdAndUpdate(
{ _id: req.user._id },
{
$set: req.body,
}
);
return successResponse(res, result, "User updated successfully.");
});
const deleteData = catchAsync(async (req, res, next) => {
const userId = req.params.id;
const user = await db.User.findById(userId);
if (!user) {
return next(new AppError("No data found.", 404));
}
if (user.role === "admin") {
await user.remove();
return successResponse(res, user, "User deleted successfully.");
} else {
res.send({ message: "Admin can not deleted" });
}
});
const profile = catchAsync(async (req, res, next) => {
let user_id;
if (req.query.user_id) {
user_id = req.query.user_id;
} else {
user_id = req.user._id;
}
const userDoc = await db.User.findById(user_id, {
__v: 0,
password: 0,
});
if (!userDoc) {
return next(new AppError("No data found.", 404));
}
const user = userDoc.toObject();
const { basic_contact_info, ...profile } = user;
profile.email_signature = decodeHtmlEntities(profile.email_signature);
const queryPromises = [
db.Address.find({ user_id }),
db.BasicContactInfo.findOne({ user_id }),
filteredPrimaryData(user_id),
db.Goal.find({ user_id }),
db.SocialMedia.findOne({ user_id }),
db.EmployeeInfo.findOne({ user_id })
.populate("created_by", ["first_name", "last_name"])
.exec(),
db.EmploymentInfo.findOne({ user_id }),
db.System.findOne({ user_id }),
db.Signature.find({ user_id }),
db.EmailSetup.find({ user_id }),
];
const [
address,
basicContactInfo,
filteredData,
goals,
socialMedia,
employeeInfo,
employmentInfo,
system,
signature,
emailSetup,
] = await Promise.all(queryPromises);
profile.filteredData = filteredData;
const newFormat = {
profile,
basicContactInfo,
goals,
socialMedia,
address,
employeeInfo,
employmentInfo,
signature,
system,
emailSetup,
};
return successResponse(res, newFormat);
});
const basicProfile = catchAsync(async (req, res, next) => {
let user_id = req.user._id;
const userDoc = await db.User.findById(user_id, {
__v: 0,
password: 0,
});
if (!userDoc) {
return next(new AppError("No data found.", 404));
}
const user = userDoc.toObject();
const { basic_contact_info, ...profile } = user;
profile.email_signature = decodeHtmlEntities(profile.email_signature);
const queryPromises = [
db.Address.find({ user_id }),
db.BasicContactInfo.findOne({ user_id }),
filteredPrimaryData(user_id),
db.Goal.find({ user_id }),
db.SocialMedia.findOne({ user_id }),
db.EmployeeInfo.findOne({ user_id })
.populate("created_by", ["first_name", "last_name"])
.exec(),
];
const [
address,
basicContactInfo, //basic
filteredData,
goals, //basic
socialMedia, //basic
employeeInfo, //basic
] = await Promise.all(queryPromises);
profile.filteredData = filteredData;
const newFormat = {
profile,
address,
basicContactInfo,
goals,
socialMedia,
employeeInfo,
};
return successResponse(res, newFormat);
});
const createAgent = catchAsync(async (req, res, next) => {
let { email, password } = req.body;
if (!email || !password) {
return next(new AppError("Email && Password are required.", 404));
}
// Check if the username is already taken
const existingUser = await db.User.findOne({ email: email.toLowerCase() });
if (existingUser) {
return next(new AppError("EmailAddress already existed.", 404));
}
// Hash the password
req.body.password = await bcrypt.hash(password, 10);
req.body.email = email.toLowerCase();
const newUser = new db.User(req.body);
const data = await newUser.save();
const employeeInfo = new db.EmployeeInfo({
user_id: data._id,
created_by: req.user._id,
});
await employeeInfo.save();
const employmentInfo = new db.EmploymentInfo({ user_id: data._id });
await employmentInfo.save();
const system = new db.System({ user_id: data._id });
await system.save();
res.status(201).json({
data,
message: "Agent created successfully",
employeeInfo,
employmentInfo,
system,
});
});
_56c.d({
view,
profile,
basicProfile,
viewAll,
allUsersByRole,
edit,
deleteData,
createAgent,
});
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists