This commit is contained in:
ROSHAN GARG 2024-09-16 12:55:04 +05:30
commit 21b85364dc
8 changed files with 91 additions and 13 deletions

Binary file not shown.

View File

@ -35,13 +35,43 @@ export const addBrand = async (req, res) => {
// Get all Brands // Get all Brands
export const getBrands = async (req, res) => { export const getBrands = async (req, res) => {
try { try {
const brands = await BrandModel.find().sort({ createdAt: -1 }); const PAGE_SIZE = parseInt(req.query.show) || 10;
const page = parseInt(req.query.page) || 1;
const skip = (page - 1) * PAGE_SIZE;
let filter = {};
// Search by brandName if provided
if (req.query.brandName) {
filter.brandName = {
$regex: new RegExp(req.query.brandName, "i"), // Case-insensitive search
};
}
// Get total number of brands matching the filter
const total = await BrandModel.countDocuments(filter);
// Fetch brands with pagination and filtering
const brands = await BrandModel.find(filter)
.limit(PAGE_SIZE)
.skip(skip)
.sort({ createdAt: -1 })
.exec();
// If no brands are found, return 404 error
if (!brands.length) { if (!brands.length) {
return res.status(404).json({ message: "No brands found" }); return res.status(404).json({ message: "No brands found" });
} }
res.status(200).json({ success: true, brands }); // Return the paginated and filtered brands list
res.status(200).json({
success: true,
total_data: total,
total_pages: Math.ceil(total / PAGE_SIZE),
current_page: page,
brands,
});
} catch (error) { } catch (error) {
// Handle server error
res.status(500).json({ res.status(500).json({
success: false, success: false,
message: error.message || "Something went wrong", message: error.message || "Something went wrong",

View File

@ -35,14 +35,43 @@ export const addCategory = async (req, res) => {
// Get all Categories // Get all Categories
export const getCategories = async (req, res) => { export const getCategories = async (req, res) => {
try { try {
const categories = await CategoryModel.find().sort({ createdAt: -1 }); const PAGE_SIZE = parseInt(req.query.show) || 10;
const page = parseInt(req.query.page) || 1;
const skip = (page - 1) * PAGE_SIZE;
let filter = {};
// Handle filtering by categoryName
if (req.query.categoryName) {
filter.categoryName = {
$regex: new RegExp(req.query.categoryName, "i"),
};
}
// Count the total number of documents matching the filter
const total = await CategoryModel.countDocuments(filter);
// Fetch the categories with pagination
const categories = await CategoryModel.find(filter)
.limit(PAGE_SIZE)
.skip(skip)
.sort({ createdAt: -1 })
.exec();
// If no categories are found, return a 404 error
if (!categories.length) { if (!categories.length) {
return res.status(404).json({ message: "No categories found" }); return res.status(404).json({ message: "No categories found" });
} }
res.status(200).json({ success: true, categories }); // Return success response with total data and total pages
res.status(200).json({
success: true,
total_data: total,
total_pages: Math.ceil(total / PAGE_SIZE),
current_page: page,
categories,
});
} catch (error) { } catch (error) {
// Handle server error
res.status(500).json({ res.status(500).json({
success: false, success: false,
message: error.message || "Something went wrong", message: error.message || "Something went wrong",

View File

@ -140,6 +140,7 @@ export const AdmingetLeaveByUser = async (req, res) => {
success: true, success: true,
user: leaveDoc.userId, user: leaveDoc.userId,
leave: paginatedRecords, leave: paginatedRecords,
userType: leaveDoc.userType,
total_data: totalData, total_data: totalData,
}); });
} catch (error) { } catch (error) {

View File

@ -12,7 +12,7 @@ const leaveRecordSchema = new mongoose.Schema({
}, },
location: { location: {
type: String, type: String,
required: true, // required: true,
}, },
reason: { reason: {
type: String, type: String,
@ -21,7 +21,7 @@ const leaveRecordSchema = new mongoose.Schema({
leaveType: { leaveType: {
type: String, type: String,
required: true, required: true,
enum: ["Sick Leave", "Casual Leave"], enum: ["Sick Leave", "Privilege Leave","Personal Leave"],
}, },
}); });

View File

@ -430,10 +430,10 @@ export const getAllRetailDistributorApproved = async (req, res) => {
export const getRDId = async (req, res) => { export const getRDId = async (req, res) => {
try { try {
const { id } = req.params; const { id } = req.params;
console.log(id); // console.log(id);
// Fetch the KYC document from the database by ID // Fetch the KYC document from the database by ID
const RD = await RetailDistributor.findById(id) const RD = await RetailDistributor.findById(id)
.populate("principal_distributer", "name") .populate("principal_distributer", "name email phone")
.populate("addedBy") .populate("addedBy")
.populate("kyc") .populate("kyc")
.populate("mappedTM") .populate("mappedTM")
@ -443,7 +443,7 @@ export const getRDId = async (req, res) => {
if (!RD) { if (!RD) {
return res.status(404).json({ message: "No RetailDistributor found" }); return res.status(404).json({ message: "No RetailDistributor found" });
} }
// console.log(RD);
// Send the fetched KYC document as a response // Send the fetched KYC document as a response
res.status(200).json(RD); res.status(200).json(RD);
} catch (error) { } catch (error) {
@ -1150,7 +1150,7 @@ export const updateRDMapped = async (req, res) => {
try { try {
const { id } = req.params; const { id } = req.params;
const { principal_distributor, mappedTM, mappedSC } = req.body; const { principal_distributor, mappedTM, mappedSC } = req.body;
console.log(principal_distributor); // console.log(principal_distributor);
// Find the RetailDistributor document by ID // Find the RetailDistributor document by ID
const RD = await RetailDistributor.findById(id); const RD = await RetailDistributor.findById(id);

View File

@ -932,7 +932,7 @@ export const getUserOrderForAdmin = async (req, res) => {
}; };
// 8.update User password // 8.update User password
export const updatePassword = catchAsyncErrors(async (req, res, next) => { export const updatePassword = catchAsyncErrors(async (req, res, next) => {
const user = await User.findById(req.user.id).select("+password"); const user = await User.findById(req.user._id).select("+password");
const isPasswordMatched = await user.comparePassword(req.body.oldPassword); const isPasswordMatched = await user.comparePassword(req.body.oldPassword);

View File

@ -11,11 +11,29 @@ const userSchema = new mongoose.Schema(
uniqueId: { uniqueId: {
type: String, type: String,
unique: true, unique: true,
required: true, validate: {
validator: function (value) {
// Only require uniqueId if the role is not "admin"
if (this.role !== "admin" && !value) {
return false;
}
return true;
},
message: "Unique ID is required for users",
},
}, },
SBU: { SBU: {
type: String, type: String,
required: [true, "Please Enter Your SBU"], validate: {
validator: function (value) {
// Only require SBU if the role is not "admin"
if (this.role !== "admin" && !value) {
return false;
}
return true;
},
message: "SBU is required for users",
},
}, },
name: { name: {
type: String, type: String,