2024-03-21 10:56:27 +00:00
|
|
|
|
const {User} = require('../models/user.model');
|
2024-03-07 13:01:44 +00:00
|
|
|
|
const constant = require('../util/constant')
|
|
|
|
|
const bcrypt = require('bcryptjs');
|
|
|
|
|
const objectConverter = require('../util/objectConverter');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-03-21 10:56:27 +00:00
|
|
|
|
// const createAdmin = async (req, res) => {
|
2024-03-07 13:01:44 +00:00
|
|
|
|
|
2024-03-21 10:56:27 +00:00
|
|
|
|
// try {
|
|
|
|
|
// let user = await User.findOne({ userTypes: constant.userTypes.admin })
|
2024-03-07 13:01:44 +00:00
|
|
|
|
|
2024-03-21 10:56:27 +00:00
|
|
|
|
// if (user) {
|
|
|
|
|
// console.log('Admin Already Created', user);
|
2024-03-07 13:01:44 +00:00
|
|
|
|
|
2024-03-21 10:56:27 +00:00
|
|
|
|
// return res.status(201).send({
|
|
|
|
|
// message: 'Admin already created'
|
|
|
|
|
// });
|
|
|
|
|
// }
|
2024-03-07 13:01:44 +00:00
|
|
|
|
|
|
|
|
|
|
2024-03-21 10:56:27 +00:00
|
|
|
|
// let obj = {
|
|
|
|
|
// email: req.body.email ? req.body.email : undefined,
|
|
|
|
|
// password: req.body.password ? bcrypt.hashSync(req.body.password) : undefined,
|
|
|
|
|
// userName: 'Admin',
|
|
|
|
|
// userTypes: constant.userTypes.admin
|
2024-03-07 13:01:44 +00:00
|
|
|
|
|
2024-03-21 10:56:27 +00:00
|
|
|
|
// }
|
|
|
|
|
// if (req.file) {
|
|
|
|
|
// const { filename, path } = req.file;
|
|
|
|
|
// obj["image"] = {
|
|
|
|
|
// fileName: filename,
|
|
|
|
|
// fileAddress: path
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2024-03-07 13:01:44 +00:00
|
|
|
|
|
2024-03-21 10:56:27 +00:00
|
|
|
|
// console.log(obj);
|
|
|
|
|
// await User.create(obj);
|
|
|
|
|
// res.status(200).send({
|
|
|
|
|
// errorCode: 200,
|
|
|
|
|
// message: 'Admin Got Created'
|
|
|
|
|
// })
|
|
|
|
|
// console.log('Admin got created');
|
2024-03-07 13:01:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-03-21 10:56:27 +00:00
|
|
|
|
// } catch (err) {
|
|
|
|
|
// console.log(err);
|
|
|
|
|
// res.status(500).send({
|
|
|
|
|
// errorCode: 500,
|
|
|
|
|
// message: 'Internal Error while creating Admin'
|
|
|
|
|
// })
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2024-03-07 13:01:44 +00:00
|
|
|
|
const getList = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const page = parseInt(req.query.page) || 1;
|
2024-03-21 10:56:27 +00:00
|
|
|
|
const limit = parseInt(req.query.limit) || 5;
|
2024-03-07 13:01:44 +00:00
|
|
|
|
const skipIndex = (page - 1) * limit;
|
2024-03-21 10:56:27 +00:00
|
|
|
|
const searchQuery = req.query.search || '';
|
2024-03-07 13:01:44 +00:00
|
|
|
|
|
|
|
|
|
let obj = {
|
|
|
|
|
userTypes: constant.userTypes.customer
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (searchQuery) {
|
|
|
|
|
obj.$or = [
|
2024-03-21 10:56:27 +00:00
|
|
|
|
{ username: { $regex: searchQuery, $options: 'i' } },
|
|
|
|
|
{ email: { $regex: searchQuery, $options: 'i' } }
|
2024-03-07 13:01:44 +00:00
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const users = await User.find(obj)
|
|
|
|
|
.limit(limit)
|
2024-03-21 10:56:27 +00:00
|
|
|
|
.skip(skipIndex)
|
|
|
|
|
.lean(); // Convert to plain JavaScript objects for manipulation
|
|
|
|
|
|
|
|
|
|
const totalCount = await User.countDocuments(obj); // Get total count of matching documents
|
2024-03-07 13:01:44 +00:00
|
|
|
|
|
|
|
|
|
console.log('users', users)
|
|
|
|
|
return res.status(200).send({
|
|
|
|
|
status: 200,
|
|
|
|
|
message: 'Users retrieved successfully',
|
|
|
|
|
users: users,
|
2024-03-21 10:56:27 +00:00
|
|
|
|
total_users: totalCount, // Include total data count in the response
|
2024-03-07 13:01:44 +00:00
|
|
|
|
total_pages: Math.ceil(totalCount / limit),
|
|
|
|
|
current_page: page
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
return res.status(500).send({
|
|
|
|
|
status: 500,
|
|
|
|
|
message: 'Internal Server Error',
|
|
|
|
|
error: err.message
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2024-03-21 10:56:27 +00:00
|
|
|
|
|
|
|
|
|
|
2024-03-07 13:01:44 +00:00
|
|
|
|
const update = async (req, res) => {
|
|
|
|
|
try {
|
2024-03-11 12:29:54 +00:00
|
|
|
|
const adminId = req.userId;
|
|
|
|
|
const { firstName, lastName, address, mobileNo } = req.body;
|
|
|
|
|
console.log(req.body, "================================>")
|
|
|
|
|
const admin = await User.findOne({ _id: adminId });
|
|
|
|
|
|
|
|
|
|
if (!admin) {
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
error_code: 404,
|
|
|
|
|
message: 'Admin not found'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
const obj = {
|
|
|
|
|
firstName: firstName,
|
|
|
|
|
lastName: lastName,
|
|
|
|
|
address: address,
|
|
|
|
|
mobileNo: mobileNo
|
|
|
|
|
}
|
|
|
|
|
// console.log("🚀 ~ update ~ obj:", obj)
|
|
|
|
|
await User.findOneAndUpdate({ _id: adminId }, obj);
|
|
|
|
|
const updatedAdmin = await User.findOne({ _id: adminId });
|
|
|
|
|
// console.log("🚀 ~ update ~ updatedAdmin:", updatedAdmin)
|
2024-03-07 13:01:44 +00:00
|
|
|
|
|
2024-03-11 12:29:54 +00:00
|
|
|
|
return res.status(200).json({
|
|
|
|
|
error_code: 200,
|
|
|
|
|
message: 'Admin profile updated successfully',
|
|
|
|
|
admin: updatedAdmin
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error inside update admin:', error);
|
|
|
|
|
return res.status(500).json({
|
|
|
|
|
error_code: 500,
|
|
|
|
|
message: "Internal Server Error"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateProfile = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const adminId = req.userId;
|
|
|
|
|
console.log("🚀 ~ updateProfile ~ adminId:", adminId)
|
|
|
|
|
|
|
|
|
|
if (!adminId) {
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
error_code: 404,
|
|
|
|
|
message: 'Admin not found'
|
|
|
|
|
});
|
2024-03-07 13:01:44 +00:00
|
|
|
|
}
|
2024-03-11 12:29:54 +00:00
|
|
|
|
|
|
|
|
|
const baseUrl = `${req.protocol}://${req.get('host')}`;
|
|
|
|
|
const imagePath = `uploads/${req.file.filename}`;
|
|
|
|
|
const imageUrl = `${baseUrl}/${imagePath}`;
|
|
|
|
|
|
|
|
|
|
// Construct the updateFields object with the image details
|
|
|
|
|
const updateFields = {
|
|
|
|
|
image: {
|
|
|
|
|
fileName: req.file.filename,
|
|
|
|
|
fileAddress: imagePath
|
|
|
|
|
},
|
|
|
|
|
imageUrl:imageUrl
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Update the admin profile with the new image details
|
|
|
|
|
const updatedAdmin = await User.findOneAndUpdate({ _id: adminId }, updateFields, { new: true });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
|
error_code: 200,
|
|
|
|
|
message: 'Admin profile updated successfully',
|
|
|
|
|
admin: {
|
|
|
|
|
...updatedAdmin.toObject(),
|
|
|
|
|
imageUrl: imageUrl
|
2024-03-07 13:01:44 +00:00
|
|
|
|
}
|
2024-03-11 12:29:54 +00:00
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error inside updateProfile:', error);
|
|
|
|
|
return res.status(500).json({
|
|
|
|
|
error_code: 500,
|
|
|
|
|
message: "Internal Server Error"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const adminProfile =async (req,res)=>{
|
|
|
|
|
try {
|
|
|
|
|
//view admin profile by req userid
|
|
|
|
|
|
|
|
|
|
const adminId = req.userId;
|
|
|
|
|
console.log("<22><><EFBFBD> ~ adminProfile ~ adminId:", adminId)
|
|
|
|
|
|
|
|
|
|
if (!adminId) {
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
error_code: 404,
|
|
|
|
|
message: 'Admin not found'
|
|
|
|
|
});
|
2024-03-07 13:01:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-11 12:29:54 +00:00
|
|
|
|
const userData = await User.findOne({ _id: adminId });
|
|
|
|
|
if (!userData) {
|
|
|
|
|
return res.status(400).send({
|
|
|
|
|
error_code: 400,
|
|
|
|
|
message: 'User not found'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
console.log("<22><><EFBFBD> ~ adminProfile ~ userData:", userData)
|
|
|
|
|
return res.status(200).send({
|
2024-03-07 13:01:44 +00:00
|
|
|
|
error_code: 200,
|
2024-03-11 12:29:54 +00:00
|
|
|
|
message: 'Admin profile retrieved successfully',
|
|
|
|
|
admin: userData
|
|
|
|
|
});
|
2024-03-07 13:01:44 +00:00
|
|
|
|
|
2024-03-11 12:29:54 +00:00
|
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error inside adminProfile:', error);
|
|
|
|
|
return res.status(500).json({
|
2024-03-07 13:01:44 +00:00
|
|
|
|
error_code: 500,
|
|
|
|
|
message: "Internal Server Error"
|
2024-03-11 12:29:54 +00:00
|
|
|
|
});
|
|
|
|
|
|
2024-03-07 13:01:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-03-11 12:29:54 +00:00
|
|
|
|
|
|
|
|
|
|
2024-03-07 13:01:44 +00:00
|
|
|
|
const userStatus = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { id } = req.params;
|
|
|
|
|
|
|
|
|
|
const userData = await User.findById(id);
|
|
|
|
|
if (!userData) {
|
|
|
|
|
return res.status(400).send({
|
|
|
|
|
error_code: 400,
|
|
|
|
|
message: 'User not found'
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-03-11 12:29:54 +00:00
|
|
|
|
userData.status = userData.status === 'Active' ? 'Deactive' : 'Active';
|
2024-03-07 13:01:44 +00:00
|
|
|
|
|
|
|
|
|
await userData.save();
|
|
|
|
|
|
|
|
|
|
res.status(200).send({
|
|
|
|
|
message: `User status toggled successfully to ${userData.status}`,
|
|
|
|
|
user: userData
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Error inside update admin', err);
|
|
|
|
|
res.status(500).send({
|
|
|
|
|
error_code: 500,
|
|
|
|
|
message: 'Internal Server Error'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2024-03-11 12:29:54 +00:00
|
|
|
|
const changePassword = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const userId = req.userId;
|
|
|
|
|
console.log("🚀 ~ changePassword ~ userId:", userId)
|
|
|
|
|
|
|
|
|
|
const userData = await User.findOne({ _id: userId });
|
|
|
|
|
console.log("🚀 ~ changePassword ~ userData:", userData)
|
|
|
|
|
if (!userData) {
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
error_code: 404,
|
|
|
|
|
message: 'User not found'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
const { oldPassword, newPassword, confirmPassword } = req.body;
|
|
|
|
|
|
|
|
|
|
if (newPassword !== confirmPassword) {
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
error_code: 400,
|
|
|
|
|
message: 'New password and confirm password do not match'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
const isMatch = await bcrypt.compare(oldPassword, userData.password);
|
|
|
|
|
if (!isMatch) {
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
error_code: 400,
|
|
|
|
|
message: 'Old password is incorrect'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const hashedPassword = await bcrypt.hash(newPassword, 10);
|
|
|
|
|
|
|
|
|
|
userData.password = hashedPassword;
|
|
|
|
|
|
|
|
|
|
await userData.save();
|
|
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
|
message: 'Password changed successfully',
|
|
|
|
|
user: userData
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error inside changePassword controller', error);
|
|
|
|
|
return res.status(500).json({
|
|
|
|
|
error_code: 500,
|
|
|
|
|
message: 'Internal Server Error'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-03-07 13:01:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
2024-03-21 10:56:27 +00:00
|
|
|
|
// createAdmin,
|
2024-03-11 12:29:54 +00:00
|
|
|
|
updateProfile,
|
2024-03-07 13:01:44 +00:00
|
|
|
|
getList,
|
2024-03-11 12:29:54 +00:00
|
|
|
|
adminProfile,
|
|
|
|
|
update, userStatus,
|
|
|
|
|
changePassword
|
2024-03-07 13:01:44 +00:00
|
|
|
|
}
|